mysql相关约束
约束的种类:主键,外键,非空,唯一
定义主键自动增长auto_increment
create table myclass (
id INT(11) primary key auto_increment,//主键自增长
name varchar(20) unique,//不能重复unique
hiredate timestamp;//timestamp表示时间戳,它可用于自动记录insert,update操作的时间
);
-- insert into myclass(name) values('yekai');
-- insert into myclass(name) values('fuhongxue');
-- insert into myclass(id,name) values(5,'luxiaojia');
-- insert into myclass(name) values('lixunhuan');
-- select *from myclass;//id显示的是1,2,5,6
create table student (
id INT(11) primary key auto_increment,
name varchar(20) unique,
passwd varchar(15) not null,
classid INT(11),
constraint stu_classid_FK foreign key(classid) references myclass(id)//外键
);
-- insert into student(name,passwd,classid) values('xiaoming','123',1);
-- insert into student(name,passwd,classid) values('xiaohong','123',9);//这个插不进去外键约束只能插1,2,5,6班的