主键约束(primary key):具有唯一且非空
外键约束(foreign key)
非空约束(not null):不可为空
唯一性约束(unique [key|index]):唯一,可为空,但空值只允许出现一次
默认值约束(default):create(address varchar(50) default ‘南京’)
自增约束(auto_increment):随着记录增加,基于最新的记录的id进行+1的自增长
外键的定义:如果同一个属性字段x在表一中是主键,而在表二中不是主键,则字段称为表二的外键。
create table test01 (hobid int(4),hobname varchar(50));

create table test02 (id int(4) primary key auto_increment,name varchar(10),age int(3),hobid int(4));

alter table test01 add constraint PK_hobid primary key(hobid);

alter table test02 add constraint FK_hobid foreign key(hobid) references test01(hobid);
references:引用

show create table test02;

insert into test01 values(1,‘runing’);
insert into test02 values(1,‘zhangsan’,20,1);


delete from test01;

delete from test02;

删除数据表也是一样,先删从表再删主表

show create table test02;

alter table (表名) drop foreign key (约束值)
alter table test02 drop foreign key FK_hobid;
desc test02;

show create table test02;
