注意
- 进行任何操作前,首先选择使用该数据库 use test;
- 查看要使用的数据库 select database();
修改表,添加字段
- alter table [表名] add [列名] [类型][约束];
alter table test add department varchar(5);
id | name | age | high | gender | cls_id | brith | department |
---|
1 | zhangsan | 18 | 168.00 | boy | 2 | NULL | NULL |
2 | wuangwu | 19 | 166.00 | boy | 2 | NULL | NULL |
4 | lisi | 19 | 166.00 | boy | 2 | 2012-02-04 | NULL |
5 | lisi | NULL | NULL | boy | NULL | 2002-01-01 | NULL |
6 | lisi | 19 | 166.00 | boy | 2 | NULL | NULL |
数据的插入
全列插入
insert into 表名 values([插入的数据]);
insert into test values(0,"zhaoliu",22,178,"boy",1,"1999-01-02","a set");
# 注意插入的数据类型,而且必须全部插入
id | name | age | high | gender | cls_id | brith | department |
---|
1 | zhangsan | 18 | 168.00 | boy | 2 | NULL | NULL |
2 | wuangwu | 19 | 166.00 | boy | 2 | NULL | NULL |
4 | lisi | 19 | 166.00 | boy | 2 | 2012-02-04 | NULL |
5 | lisi | NULL | NULL | boy | NULL | 2002-01-01 | NULL |
6 | lisi | 19 | 166.00 | boy | 2 | NULL | NULL |
7 | zhaoliu | 22 | 178.00 | boy | 1 | 1999-01-02 | a set |
部分插入
insert into 表名([列名],[列名], ...) values([数据],[数据], ...);
insert into test(name,department) values("gg","Two groups");
id | name | age | high | gender | cls_id | brith | department |
---|
1 | zhangsan | 18 | 168.00 | boy | 2 | NULL | NULL |
2 | wuangwu | 19 | 166.00 | boy | 2 | NULL | NULL |
4 | lisi | 19 | 166.00 | boy | 2 | 2012-02-04 | NULL |
5 | lisi | NULL | NULL | boy | NULL | 2002-01-01 | NULL |
6 | lisi | 19 | 166.00 | boy | 2 | NULL | NULL |
7 | zhaoliu | 22 | 178.00 | boy | 1 | 1999-01-02 | a set |
8 | gg | NULL | NULL | NULL | NULL | 2002-01-01 | Two groups |
- 可以看出没有约束默认值的为空[NULL] ,有默认值而没有插入数据的字段为默认值
多行插入
insert into test ([列名],[列名], ...) values ([数据],[数据], ...),([数据],[数据], ...);
insert into test (name,cls_id) values ("jj",3),("am",4);
id | name | age | high | gender | cls_id | brith | department |
---|
1 | zhangsan | 18 | 168.00 | boy | 2 | NULL | NULL |
2 | wuangwu | 19 | 166.00 | boy | 2 | NULL | NULL |
4 | lisi | 19 | 166.00 | boy | 2 | 2012-02-04 | NULL |
5 | lisi | NULL | NULL | boy | NULL | 2002-01-01 | NULL |
6 | lisi | 19 | 166.00 | boy | 2 | NULL | NULL |
7 | zhaoliu | 22 | 178.00 | boy | 1 | 1999-01-02 | a set |
8 | gg | NULL | NULL | NULL | NULL | 2002-01-01 | Two groups |
9 | jj | NULL | NULL | NULL | 3 | 2002-01-01 | NULL |
10 | am | NULL | NULL | NULL | 4 | 2002-01-01 | NULL |
数据的删除
物理删除
- delete from 表名 where 条件;
delete from test where id=5;
逻辑删除
- 用一个字段来表示 这条信息是否已经不能再使用了
- 给test表添加一个is_delete字段 bit 类型
- alter table test add is_delete bit default 0;
- update test set is_delete=1 where id=5
数据的修改
- update 表名 set [列名1=数据1],[列名2=数据2] ... where [条件];
update test set gender='girl',department="a set" where id>7;
id | name | age | high | gender | cls_id | brith | department |
---|
1 | zhangsan | 18 | 168.00 | boy | 2 | NULL | NULL |
2 | wuangwu | 19 | 166.00 | boy | 2 | NULL | NULL |
4 | lisi | 19 | 166.00 | boy | 2 | 2012-02-04 | NULL |
5 | lisi | NULL | NULL | boy | NULL | 2002-01-01 | NULL |
6 | lisi | 19 | 166.00 | boy | 2 | NULL | NULL |
7 | zhaoliu | 22 | 178.00 | boy | 1 | 1999-01-02 | a set |
8 | gg | NULL | NULL | girl | NULL | 2002-01-01 | a set |
9 | jj | NULL | NULL | girl | 3 | 2002-01-01 | a set |
10 | am | NULL | NULL | girl | 4 | 2002-01-01 | a set |
- 注意!!没有加上 where 条件;会默认全部记录
数据的查询
查询所有列
- select * from [表名];
select * from test;
id | name | age | high | gender | cls_id | brith | department |
---|
1 | zhangsan | 18 | 168.00 | boy | 2 | NULL | NULL |
2 | wuangwu | 19 | 166.00 | boy | 2 | NULL | NULL |
4 | lisi | 19 | 166.00 | boy | 2 | 2012-02-04 | NULL |
5 | lisi | NULL | NULL | boy | NULL | 2002-01-01 | NULL |
6 | lisi | 19 | 166.00 | boy | 2 | NULL | NULL |
7 | zhaoliu | 22 | 178.00 | boy | 1 | 1999-01-02 | a set |
8 | gg | NULL | NULL | girl | NULL | 2002-01-01 | a set |
9 | jj | NULL | NULL | girl | 3 | 2002-01-01 | a set |
10 | am | NULL | NULL | girl | 4 | 2002-01-01 | a set |
指定条件查询
- select * from [表名] where [条件];
select * from test where name="lisi";
# 查询name为 lisi 的所有列
id | name | age | high | gender | cls_id | brith | department |
---|
4 | lisi | 19 | 166.00 | boy | 2 | 2012-02-04 | NULL |
5 | lisi | NULL | NULL | boy | NULL | 2002-01-01 | NULL |
6 | lisi | 19 | 166.00 | boy | 2 | NULL | NULL |
select * from test where id=8;
# 查询id为8的列
id | name | age | high | gender | cls_id | brith | department |
---|
8 | gg | NULL | NULL | girl | NULL | 2002-01-01 | a set |
select * from test where id>8;
# 查询id大于8的列
id | name | age | high | gender | cls_id | brith | department |
---|
9 | jj | NULL | NULL | girl | 3 | 2002-01-01 | a set |
10 | am | NULL | NULL | girl | 4 | 2002-01-01 | a set |
查询指定列
- select [列名1],[列名2], ... from [表名];
select name,age from test;
name | age |
---|
zhangsan | 18 |
wuangwu | 19 |
lisi | 19 |
lisi | NULL |
lisi | 19 |
zhaoliu | 22 |
gg | NULL |
jj | NULL |
am | NULL |
使用as为列或表指定别名
select name as "名字",age as "年龄" from test;
名字 | 年龄 |
---|
zhangsan | 18 |
wuangwu | 19 |
lisi | 19 |
lisi | NULL |
lisi | 19 |
zhaoliu | 22 |
gg | NULL |
jj | NULL |
am | NULL |
select age as "年龄",name as "名字" from test;
年龄 | 名字 |
---|
18 | zhangsan |
19 | wuangwu |
19 | lisi |
NULL | lisi |
19 | lisi |
22 | zhaoliu |
NULL | gg |
NULL | jj |
NULL | am |