键值约束: 约束表中指定字段的数据必须符合某种规则
种类:
非空约束: NOT NULL -- 约束指定字段的数据不能为NULL
唯一约束: UNIQUE -- 约束指定字段的数据不能出现重复
主键约束: primary key -- 数据非空且唯一, 一张表只能有一个主键
外键约束: foreign key -- 表中指定字段的数据受父表数据约束
默认值: DEFAULT -- 为指定字段设置默认值
自增属性: AUTO_INCREMENT -- 整形字段数据自动+1
create table if not exists class(
id int primary key auto_increment,
name varchar(32)
);
create table if not exists student(
id int primary key auto_increment,
sn int not null unique,
name varchar(32),
class_id int,
sex varchar(1) default(‘男‘),
foreign key (class_id) references class(id)
);
原文:https://www.cnblogs.com/enjoyC/p/15116168.html