根据oracle中父表中的列大小删除子表记录

时间:2014-06-04 06:04:39

标签: sql oracle plsql

我有这样的场景: 我有三张桌子:

CREATE TABLE DEPT(DEPT_PK NUMBER NOT NULL,DEPT_NAME VARCHAR(10),DESCRIPTION VARCHAR(50));
CREATE TABLE EMPLOYEE(EMPLOYEE_PK NUMBER(4) NOT NULL PRIMARY KEY,DEPT_PK NUMBER(4),EMP_NAME VARCHAR2(50),ADDRESS VARCHAR2(200));
CREATE TABLE SALARY(SALARY_PK NUMBER NOT NULL,EMP_PK NUMBER(4),net_salary number(5,2));

现在我想删除dept表中的记录,其dept_pk大小为4,并将表的dept_pk大小修改为3.

请注意,emp表将dept_pk作为外键,而salary表将emp_pk作为外键。

我需要为此编写一个pl / sql过程或sql查询。

提前致谢。

1 个答案:

答案 0 :(得分:0)

所以你有第一个从SALARY中删除:

delete from SALARY where EMP_PK in
(
  select EMPLOYEE_PK
  from EMPLOYEE
  where DEPT_PK in
  (
    select DEPT_PK from DEPT where length(DEPT_PK) = 4
  );
);

然后从EMPLOYEE

delete from EMPLOYEE
where DEPT_PK in
(
  select DEPT_PK from DEPT where length(DEPT_PK) = 4
);

现在来自DEPT

delete from DEPT where length(DEPT_PK) = 4;

编辑:如果你真的想要使用PL / SQL,你只需要将以下代码包含在beginend中,以便执行一次:

begin
delete from SALARY where EMP_PK in
(
  select EMPLOYEE_PK
  from EMPLOYEE
  where DEPT_PK in
  (
    select DEPT_PK from DEPT where length(DEPT_PK) = 4
  );
);

delete from EMPLOYEE
where DEPT_PK in
(
  select DEPT_PK from DEPT where length(DEPT_PK) = 4
);

delete from DEPT where length(DEPT_PK) = 4;
end;
/

或创建可重复使用的程序:在create or replace procedure proc_name is之前添加begin