注意

  • 进行任何操作前,首先选择使用该数据库 use test;
  • 查看要使用的数据库 select database();

修改表,添加字段

- alter table [表名] add [列名] [类型][约束];
alter table test add department varchar(5);
idnameagehighgendercls_idbrithdepartment
1zhangsan18168.00boy2NULLNULL
2wuangwu19166.00boy2NULLNULL
4lisi19166.00boy22012-02-04NULL
5lisiNULLNULLboyNULL2002-01-01NULL
6lisi19166.00boy2NULLNULL

数据的插入

全列插入

insert into 表名 values([插入的数据]);
insert into test values(0,"zhaoliu",22,178,"boy",1,"1999-01-02","a set");
# 注意插入的数据类型,而且必须全部插入
idnameagehighgendercls_idbrithdepartment
1zhangsan18168.00boy2NULLNULL
2wuangwu19166.00boy2NULLNULL
4lisi19166.00boy22012-02-04NULL
5lisiNULLNULLboyNULL2002-01-01NULL
6lisi19166.00boy2NULLNULL
7zhaoliu22178.00boy11999-01-02a set

部分插入

insert into 表名([列名],[列名], ...) values([数据],[数据], ...);
insert into test(name,department) values("gg","Two groups");
idnameagehighgendercls_idbrithdepartment
1zhangsan18168.00boy2NULLNULL
2wuangwu19166.00boy2NULLNULL
4lisi19166.00boy22012-02-04NULL
5lisiNULLNULLboyNULL2002-01-01NULL
6lisi19166.00boy2NULLNULL
7zhaoliu22178.00boy11999-01-02a set
8ggNULLNULLNULLNULL2002-01-01Two groups
  • 可以看出没有约束默认值的为空[NULL] ,有默认值而没有插入数据的字段为默认值

多行插入

insert into test ([列名],[列名], ...) values ([数据],[数据], ...),([数据],[数据], ...);
insert into test (name,cls_id) values ("jj",3),("am",4);
idnameagehighgendercls_idbrithdepartment
1zhangsan18168.00boy2NULLNULL
2wuangwu19166.00boy2NULLNULL
4lisi19166.00boy22012-02-04NULL
5lisiNULLNULLboyNULL2002-01-01NULL
6lisi19166.00boy2NULLNULL
7zhaoliu22178.00boy11999-01-02a set
8ggNULLNULLNULLNULL2002-01-01Two groups
9jjNULLNULLNULL32002-01-01NULL
10amNULLNULLNULL42002-01-01NULL

数据的删除

物理删除

- 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;
idnameagehighgendercls_idbrithdepartment
1zhangsan18168.00boy2NULLNULL
2wuangwu19166.00boy2NULLNULL
4lisi19166.00boy22012-02-04NULL
5lisiNULLNULLboyNULL2002-01-01NULL
6lisi19166.00boy2NULLNULL
7zhaoliu22178.00boy11999-01-02a set
8ggNULLNULLgirlNULL2002-01-01a set
9jjNULLNULLgirl32002-01-01a set
10amNULLNULLgirl42002-01-01a set
  • 注意!!没有加上 where 条件;会默认全部记录

数据的查询

查询所有列

- select * from [表名];
select * from test;
idnameagehighgendercls_idbrithdepartment
1zhangsan18168.00boy2NULLNULL
2wuangwu19166.00boy2NULLNULL
4lisi19166.00boy22012-02-04NULL
5lisiNULLNULLboyNULL2002-01-01NULL
6lisi19166.00boy2NULLNULL
7zhaoliu22178.00boy11999-01-02a set
8ggNULLNULLgirlNULL2002-01-01a set
9jjNULLNULLgirl32002-01-01a set
10amNULLNULLgirl42002-01-01a set

指定条件查询

- select * from [表名] where [条件];
select * from test where name="lisi";
# 查询name为 lisi 的所有列
idnameagehighgendercls_idbrithdepartment
4lisi19166.00boy22012-02-04NULL
5lisiNULLNULLboyNULL2002-01-01NULL
6lisi19166.00boy2NULLNULL
select * from test where id=8;
# 查询id为8的列
idnameagehighgendercls_idbrithdepartment
8ggNULLNULLgirlNULL2002-01-01a set
select * from test where id>8;
# 查询id大于8的列
idnameagehighgendercls_idbrithdepartment
9jjNULLNULLgirl32002-01-01a set
10amNULLNULLgirl42002-01-01a set

查询指定列

- select [列名1],[列名2], ... from [表名];
select name,age from test;
nameage
zhangsan18
wuangwu19
lisi19
lisiNULL
lisi19
zhaoliu22
ggNULL
jjNULL
amNULL

使用as为列或表指定别名

select name as "名字",age as "年龄" from test;
名字年龄
zhangsan18
wuangwu19
lisi19
lisiNULL
lisi19
zhaoliu22
ggNULL
jjNULL
amNULL
  • 指定字段的顺序
select age as "年龄",name as "名字" from test;
年龄名字
18zhangsan
19wuangwu
19lisi
NULLlisi
19lisi
22zhaoliu
NULLgg
NULLjj
NULLam