primary key 主键
- 需设置为不为空不重复&自增列
not null auto_increment primary key
- 一个表仅允许一个主键,但主键可由多列组成
create table tb1 (
id int not null auto_increment,
department_id int,
name char,
age int,
primary key(id,department_id)
)
foreign key 外键
- 外键由单列组成
constraint 外键名 foreign key(列名) references (表名,列名);
- 外键由多列组成(前提是被连接的主键是由多列组成)
create table tb1 (
id int not null auto_increment,
department_id int,
name char,
age int,
primary key(id,department_id)
)
create table tb2(
t_id int not null auto_increment primary key,
q_id int,
constarint fk_1 foreign key (t_id,q_id) references t1(id,department_id)
)