MySQL基础操作

一:基础操作

1.数据库创建

create database jmdb;      创建一个名为 jmdb 的数据库
show databases;         显示当前数据库列表
show database jm;        查看数据库的创建方式 drop database jmdb;      删除名为jmdb的数据库
use jm;             选择数据库

 

2.远程连接

1.mysql> use mysql;
2.mysql> select user,host from user;

+---------------+-----------+
| user | host |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+

 3.mysql> update user set host='%' where user='root';  将host列修改成 '%'

 4.mysql> select user,host from user;   
+---------------+-----------+
| user | host |
+---------------+-----------+
| root | % |
| mysql.session | localhost |
| mysql.sys | localhost |
+---------------+-----------+

---------------------------------------分割线---------------------

远程连接需要关闭防火墙

[root@localhost ~]# systemctl stop firewalld      # 临时关闭

[root@localhost ~]# systemctl disable firewalld  # 禁止开机启动

Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

 

3.表创建

use jmdb;             选择一个名为jmdb的数据库

# 创建表

create table student(
id int primary key auto_increment,      # id,修饰成主键,自增
name varchar(20) unique,                # 表示该列数据不能有重复
age int                                 #int类型
)engine=InnoDB default charset=utf8;    # 指定数据库引擎Innodb,编码为utf8

约束: primary key (非空并且唯一):能够唯一区分当前记录的字段称为主键

    unique    : 唯一,就是数据不能有重复的,一旦有重复就会报错

    not null    : 该列数据不能为空,否则报错

    auto_increment: 主键字段必须是数字类型

    foreign key  :外键约束

-----------------------------------------------分割线-----------------------------

查看表信息:

  desc student  查看表结构

  show columns from student  查看表结构

  show tables    查看当前数据的所有表

  show create table student  查看表的创建语句

 

-----------------------------分割线-----------------------------------------------

修改表结构:

  1.增加字段(列):

    alter table student add [列名] 类型 [约束性条件] [first|after 列名]

    alter table student add abd int after name;  添加abd列,指定它在 name 列的后面

    alter table student add aba int first ;    添加ada列,指定该列在 最前面

    alter table student add abs int,           添加多列

                add acc varchar(20),

                add caa varchar(10);

 

-------------------------------分割线-------------------------------------

  2.修改一列的类型

    alter table student modify 列名 类型 [约束条件] [first|after 字段名]

    alter table student modify aba varchar(20) unique; 将 aba 列修饰成 唯一 字段

  3.修改列名

    alter table 表  change 列名 新列名 类型 [约束条件] [first|after 字段名]

    alter table student change aba aBa varchar(10);  将aba 修改成 aBa

  4.删除列

    alter table 表 drop [column] 列名

    alter table student drop aBa;

    alter table student drop column abd,drop column abs; 删除多列,必须要有column

  5.修改表名

    rename table 表名 to 新表名;

  6.修改字符集

    alter table student default CHARACTER SET utf8 COLLATE utf8_general_ci; 

  7.删除表:

    drop table 表名;

-----------------------------------分割线----------------------------------------

  8.添加主键

    alter table student add primary key(字段名...)

  9.删除主键

    alter table student modify id int;  先删除 auto_increment

    alter tabel student drop primary key; 才能删除主键

-----------------------------------分割线---------------------------------------

 索引:

  普通索引:alter table student add (index|key) [索引名](字段.....)

    1.alter table student add index (name)  #为name创建索引,索引名默认为字段名

    2.alter table student add index username(name)  #为name创建索引,索引名为username

    注意:key和index的使用方法一样

 

  唯一索引:alter table studnet add unique (index|key) [索引名](字段名....)

    1.alter table studnet add unique index username(name)  #为字段name创建唯一索引,索引名为username

  

  联合索引:

    1.alter table student add index user_age(name, age)

    2.alter table student add unique index user_age(name, age)

 

  删除索引:

    alter table student drop (index|key) 字段名

    如:alter table student drop index username  #删掉索引username

 4.主键

#单列字段主键
create
table student( id int primary key auto_increment, name varchar(20) unique, age int )engine=InnoDB default charset=utf8;
# 多字段联合主键
create table student( id int auto_increment, name varchar(20), age int, primary key (id,name) )engine=InnoDB default charset=utf8;
#一张表只能有一个主键
#主键类型不一定得是整型

 

posted @ 2018-07-16 21:52  lei-jia-ming  阅读(159)  评论(2编辑  收藏  举报