-- 查询年龄大于18的总数selectcount(*)from student where age>18;-- 查询英语成绩总分selectsum(english) 总分 from student;-- 查询英语成绩最低分selectmin(english) 最低分 from student;-- 查询英语成绩最高分selectmax(english) 最高分 from student;-- 查询英语成绩平均分selectavg(english) 平均分 from student;
1.创建表时添加主键
字段名 字段类型 primarykey2.在已有表中添加主键
altertable 表名 addprimarykey(字段名);-- 创建学生表stu,包含字段(id,name,age)id作为主键createtable stu(
id intprimarykey,--id为主键
name varchar(20),
age int)desc stu;--删除主键altertable stu dropprimarykey;-- 添加主键altertable stu addprimarykey(id);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
修改自增长的默认起始值
默认自增长开始值是1,但是可以进行修改
创建表的起始值
createtable 表名(
列名 intprimarykeyauto_increment,)auto_increment=起始值;-- 指定起始值为100createtable stu1 (
id intprimarykeyauto_increment,
name varchar(20))auto_increment=100;insertinto stu1 values(null,'轩月');select*from stu1;
结果: id name
100 轩月
-- 删除自动增长altertable stu1 modify id int;-- 添加自动增长altertable stu1 modify id intauto_increment;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
唯一约束
unique,表中某一列不能出现重复值
字段名 字段类型 uniqe
1
1
创建学生表stu2,包含字段(id,name)name列设置唯一约束,即不能出现同名的学生
-- 创建表时,添加唯一约束createtable stu2 (
id int,
name varchar(20)unique);-- null 没有数据,不存在重复的问题 ,列值可以有多个null-- 删除唯一约束altertable stu2 dropindex name;--创建表后,添加唯一约束altertable stu2 modify name varchar(20)unique;
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
非空约束
某一列中不能为null
字段名 字段类型 notnull
1
1
创建学生表stu3,包含字段(id,name,age)name不能为空null
-- 创建表时添加非空约束createtable
stu3 (
id int,
name varchar(20)notnull,
age int);-- 创建表后,添加非空约束altertable stu3 modify name varchar(20)notnull;