表的约束:
1.主键约束 分为:单字段主键、多字段主键 约束条件:primary key
(1)单字段主键
示例:创建一个数据表student01,并设置id作为主键
答:create table student01(id int primary key,name varchar(20),grade float);
解析:在student01表中创建了id,name,grade,三个字段,其中id字段是主键
(2)多字段主键 约束条件:
示例:创建一个数据表student02,在表中将stu_id,course_id两个字段共同作为主键
答:create table student02(stu_id int, course_id int, greade float,primary key(stu_id,course_id));
注意:每个数据表中最多只能有一个主键约束,被定义的字段不能有重复值且不能为NULL值
2.非空约束 约束条件:not null
示例:创建一个数据表student03,将表中的name字段设置为非空约束
答:create table student03(id int primary key,name varchar(20) not null,grade float);
id字段为主键、name字段为非空字段
注意:在同一个数据表中可以定义多个非空字段
3.唯一约束 约束条件:unique
示例:创建一个数据表student04,将表中的stu_id设置为唯一约束
答:create table student04(id int