有兩個表: t_emp 和 t_dept
t_emp(empid ,empno,empname,deptno)
t_dept(deptno,deptname,boss,)
create table t_emp(empid number,empno varchar2(15) not null,empname varchar2(30),deptno varchar2(15), constraint t_emp_id primary key(empid),constraint t_emp_u1 unique(empno))
create sequence t_emp19_id_s increment by 1;
create table t_dept(deptno varchar2(15),deptname varchar2(50), boss number, constraint t_dept_id primary key(deptno))
在表創建之後,我給它創建了外鍵:
alter table t_emp add constraint t_emp_f foreign key(deptno) references t_dept(deptno)
alter table t_dept add constraint t_dept_f foreign key(boss) references t_emp(empid)
我想再插入數據,就不行了,為甚麼?去除約束除了:
alter table t_emp disable constraint t_emp_f
alter table t_dept disable constraint t_dept_f
還有甚麼方式?
插入數據:
insert into t_emp values(t_emp_id_s.nextval,’6001’,’cory’,’001’)
insert into t_emp values(t_emp_id_s.nextval,’6002’,’jane’,’001’)
insert into t_emp values(t_emp_id_s.nextval,’6003’,’tom’,’002’)
insert into t_emp values(t_emp_id_s.nextval,’6004’,’jack’,’003’)
對於t_dept表的插入
(001,oracle,(6002’s id))
(002,sql,(6001’s id))
(003,mysql,(6003’s id))
我用的方法是:
insert into t_dept values('100','oracle',(select empid from t_emp where empno='6002'));
insert into t_dept values('100','sql',(select empid from t_emp where empno='6001'));
insert into t_dept values('100','mysql',(select empid from t_emp where empno='6003'));
在插入boss的值時是否有別的方法?
|