主表:
create table 主表名(字段名1 数据类型1 primary key,字段名2 数据类型2);
如:create table sb (sid int primary key,age int);
从表
create table 从表名(字段名1 数据类型1 primary key,字段名2 数据类型2 ,字段名3 数据类型3,foreign key(sid)references sb (字段名3) references 主表名(字段名) ;
如:create table cb(cid int primary key,age int,sid int,fooreign key(sid))references sb(sid);
使用外键约束:
!!! 主表以及从表的概念:
1)主表:对于两个具有关联关系的表而言,相联字段中的主键所在表就是主表。
2)从表:对于两个具有关联关系的表而言,相联字段中的外键所在表就是从表。
foreign key(外键字段名)references 主表名(主表主键);
如:create table tableA(tid int primay key,age int);
create table tableB(tbid int primay key,tid int,age int,foreign key(tid)references.tableA(tid));
分空约束:Not null;
*!!写在想要写的数据类型的后面,打空格,不打分号 *
唯一约束:unique;