考虑我有两个表Employee
和Department
下面给出的字段:
员工:
Fname、Minit、Lname、Ssn
Bdate、地址、性别、薪水、Super_ssn、Dno
部门:
Dname
, Dnumber
, Mgr_ssn, Mgr_start_date
Italics and code block
表示表的主键。
在上述情况下,Super_Ssn引用SSN员工,而DNO引用该部的Dnumber。为它们创建 sql 非常简单,首先添加列,然后在形成两个表时更改它们。但是,在插入如下所示的数据时:
insert into employee values('James','E','Borg','888665555','1937-11-10','asda','M',55000,NULL,1);
ERROR: insert or update on table "employee" violates foreign key constraint "dno_ref_dnum_dpt"
DETAIL: Key (dno)=(1) is not present in table "department".
Run Code Online (Sandbox Code Playgroud)
和
insert into sp0090.department values ('Headquarter',1,'888665555','1981-06-19');
ERROR: insert or update on table "department" violates foreign key constraint "department_mgr_ssn_fkey"
DETAIL: Key (mgr_ssn)=(888665555) is not …
Run Code Online (Sandbox Code Playgroud)