1.外码是另外一个表的主码;
2.插入数据是必须先给所关联外码的那个表插入数据;
3.报错的原因是插入的某个属性在所关联外码表中找不到。
外码的意义是:保证两个表之间的数据的一致性,例如:职工表中的部门号,必须在部门表中存在。

create table 部门(
部门号 char(20) primary key not null,
名称 char(20) constraint UK_dName unique not null ,
经理名 char(20),
地址 varchar(20),
电话号码 char(20)
)
drop table 部门

create table 职工(
职工号 char(20) primary key,
姓名 char(20) not null,
年龄 smallint check (年龄>=18 and 年龄<=60),
职务 char(20),
工资 int not null check(工资>=800),
部门号 char(20)
FOREIGN KEY (部门号) REFERENCES  部门(部门号)--直接建立外码
)
drop table 职工

--新增关系表属性
--新增表的外码
--方法二:
alter table 职工
add constraint S_worker
foreign key(部门号)
references 部门(部门号)