日期:2014-05-17 浏览次数:20895 次
-- Create table create table DEPT_TEST ( DEPTNO NUMBER(2) not null, DNAME VARCHAR2(14), LOC VARCHAR2(13) ); alter table DEPT_TEST add constraint PK_DEPT_TEST primary key (DEPTNO); -- Create table create table EMP_TEST ( EMPNO VARCHAR2(10), ENAME VARCHAR2(10), SEX VARCHAR2(9), DEPTNO NUMBER(2) ); alter table EMP_TEST add constraint FK_DEPTNO_TEST foreign key (DEPTNO) references DEPT_TEST (DEPTNO); SQL> select * from dept_test; DEPTNO DNAME LOC ------ -------------- ------------- SQL> --插入deptno空的时候可以正常插入 SQL> insert into EMP_TEST values('e005','kk','f',NULL); 1 row inserted SQL> COMMIT; Commit complete SQL> SELECT * FROM EMP_TEST; EMPNO ENAME SEX DEPTNO ---------- ---------- --------- ------ e005 kk f SQL> insert into EMP_TEST values('e005','kk','f',10); insert into EMP_TEST values('e005','kk','f',10) ORA-02291: integrity constraint (SCOTT_TEST.FK_DEPTNO_TEST) violated - parent key not found SQL> COMMIT; Commit complete SQL> SELECT * FROM EMP_TEST; EMPNO ENAME SEX DEPTNO ---------- ---------- --------- ------ e005 kk f