数据库操作

创建数据库

1
create database $DBName;

查看数据库

1
show databases;

进入数据库

1
use $DBName;

删除数据库

1
drop database $DBName;

表操作

创建表

进入数据库

1
2
use $DBName;
create table $TableName (name char(20),id int);

不进入数据库

1
create table $DBName.$TableName (name char(20),id int);

常用数据类型

字符类型

  • char —最大 255
  • varchar — 比 char 大

数值类型

  • int

浮点型

  • float

枚举型

  • enum 单选
  • set 多选
1
2
3
4
5
create table students (
name char(15),
sex enum("boy","girl"),
like set("book","ball","tea","game")
);

其他类型

约束条件

不允许为空 not null

1
2
3
4
create table $TableName (
name char(10) not null,
id int not null
);

设置默认值 default

1
2
3
4
create table $TableName (
name char(10),
sex enum("boy","girl") default "boy"
);

查看表

1
show tables;

查看表结构

1
desc $TableName;

删除表

1
drop table $TableName;

插入表数据

1
2
3
4
5
6
insert into $TableName values (value1,value2,value3,···);

insert into $TableName values
(A1,A2,A3),
(B1,B2,B3),
(C1,C2,C3);

查看表内容

1
select * from $TableName;

查看某一列

1
2
3
select name from $TableName;

select * from $TableName where id = 2;

多表查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
mysql> select * from student;
+----+-------+------+------+
| id | name | sex | home |
+----+-------+------+------+
| 1 | alice | gril | zh |
| 2 | bob | boy | zh |
| 3 | tom | boy | zh |
| 4 | mask | boy | zh |
| 5 | aila | gril | zh |
+----+-------+------+------+
5 rows in set (0.00 sec)

mysql> select * from class;
+----+------+
| id | cj |
+----+------+
| 1 | 89 |
| 2 | 80 |
| 3 | 60 |
| 4 | 77 |
| 5 | 97 |
+----+------+
5 rows in set (0.00 sec)

mysql> select name,sex,home,cj from student,class where student.id=class.id;
+-------+------+------+------+
| name | sex | home | cj |
+-------+------+------+------+
| alice | gril | zh | 89 |
| bob | boy | zh | 80 |
| tom | boy | zh | 60 |
| mask | boy | zh | 77 |
| aila | gril | zh | 97 |
+-------+------+------+------+
5 rows in set (0.00 sec)

mysql> select name,sex,home,cj from student join class on student.id=class.id;
+-------+------+------+------+
| name | sex | home | cj |
+-------+------+------+------+
| alice | gril | zh | 89 |
| bob | boy | zh | 80 |
| tom | boy | zh | 60 |
| mask | boy | zh | 77 |
| aila | gril | zh | 97 |
+-------+------+------+------+
5 rows in set (0.00 sec)

更新表数据

1
2
3
4
update $TableName set $字段名=值,$字段名=值 where $字段名=值;

# 不加 where 会修改所有的数据
update test set Tel="1111",home="zh" where name="alice";

删除表数据

1
2
3
4
5
# 删除指定行
delete from $TableName where $字段名=值;

# 删除所有
delete from $TableName;

修改表结构

  • 添加字段
1
2
3
4
5
alter table $Table add name char(20); #默认在最后

alter table $Table add name char(20) after id;

alter table $Table add name char(20) first;
  • 删除字段
1
alter table $Table drop class;
  • 修改字段类型
1
2
3
4
alter table $Table modify $字段 $新类型;

# 新字段类型不能和已有数据相悖
alter table $Table modify class char(20);
  • 修改字段名
1
2
3
4
5
alter table $Table change $老字段 $新字段 $字段类型 [after $字段 | first]

alter table $Table tel TEL char(20);
alter table $Table tel TEL char(20) after home;
alter table $Table tel TEL char(20) first;

主键

创建主键

创建表时创建主键

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
create table $tableName(
id char(10) not null primary key auto_increment,
name char(10) not null
);

# auto_increment 自增属性 , 只有数值类型才能设置

create table $tableName(
id char(10) not null,
name char(10),
primary key(id)
);

# 可以多个字段一起做主键
primary key(id,name)

在已有表中创建主键

1
alter table $tableName add primary key(id);

删除主键

1
2
3
4
alter table $tableName drop primary key;

# 有自增属性需要先去掉
alter table $tableName modify id int;

外键

创建外键

插入表记录时,字段值在另一个表字段值范围内选择

  • 表存储引擎必须时 innodb ###mariadb 默认存储引擎
  • 字段类型要一致
  • 被参照字段必须要是索引类型的一种(primary key)
1
2
3
4
5
6
create table $tableName(
字段列表,
foreign key (字段名) references 表名(字段名)
on update cascade # 同步更新
on delete cascade # 同步删除
)engine=innodb;

向已有表添加外键

1
2
3
alter table $tableName add 
foreign key (字段名) references 表名(字段名)
on update cascade on delete cascade;

**创建好外键之后,在插入数据时,外键字段的值只能为主键字段中包含的值 **

查看外键

就是查看表创建时的命令,相当于表的属性

1
show create table $tableName \G

૮(˶ᵔ ᵕ ᵔ˶)ა