1 主键约束
方式1:(推荐:简单、明了、建表时创建)
create table scott.sex (
sex_code varchar2(2) constraint pk_sex_sex_code primary key,
description varchar2(10)
);
-- 复合主键
create table scott.sex2 (
sex_code varchar2(2),
description varchar2(10),
constraint pk_sex primary key(sex_code, description)
);
方式2:(表创建后再添加)
-- drop table scott.sex; -- 如果存在
create table scott.sex (
sex_code varchar2(2),
description varchar2(10)
);
alter table scott.sex add constraint fk_sex_sex_code primary key(sex_code);
2 外键约束
方式1:(推荐:简单、明了、建表时创建)
create table scott.stu_info (
sno number(3),
sname varchar2(30),
sex_code varchar2(2),
constraint fk_stu_info_sex_code foreign key(sex_code) references scott.sex(sex_code)
);
方式2:(表创建后再添加)
-- drop table scott.stu_info; -- 如果存在
create table scott.stu_info (
sno number(3),
sname varchar2(30),
sex_code varchar2(2)
);
alter table scott.stu_info add constraint fk_stu_info_sex_code foreign key(sex_code) references scott.sex(sex_code);