oracle级联删除 触发器

CREATE TABLE STUDENT( --创建学生表
  ID NUMBER(10) PRIMARY KEY,   --主键ID
  SNAME VARCHAR2(20),
  CLASSNAME VARCHAR2(20) --班级ID

);

INSERT INTO STUDENT VALUES(1,'Tom',1);
INSERT INTO STUDENT VALUES(2,'Jack',1);
INSERT INTO STUDENT VALUES(3,'Bay',2);
INSERT INTO STUDENT VALUES(4,'John',3);

CREATE TABLE CLASSTAB(  --创建班级表
  CLASSID NUMBER(2) PRIMARY KEY,  --班级ID
  CNAME VARCHAR2(20)
);

INSERT INTO CLASSTAB VALUES(1,'3G');
INSERT INTO CLASSTAB VALUES(2,'SVSE');
INSERT INTO CLASSTAB VALUES(3,'GIS');
INSERT INTO CLASSTAB VALUES(4,'EM');

--创建触发器  删除班级时  将该班级的所有学生信息也删除
CREATE OR REPLACE TRIGGER MYTRIGGER
  AFTER DELETE 
  ON CLASSTAB
  FOR EACH ROW
BEGIN
  DELETE FROM STUDENT WHERE CLASSNAME = :old.CLASSID;
END;

DELETE FROM CLASSTAB WHERE CLASSID = 2;  删除班级表中CLASSID为2的班级  学生表的第三条记录也会被删除

你可能感兴趣的:(oracle)