Table defination :
-- create table
CREATE TABLE departments (
ID NUMBER(10) NOT NULL,
DESCRIPTION VARCHAR2(50) NOT NULL);
-- alter table to add primary key
ALTER TABLE departments ADD (
CONSTRAINT dept_pk PRIMARY KEY (ID));
-- create sequence
CREATE SEQUENCE dept_seq;
Trigger defination :
-- create trigger to +1 at sequence number
CREATE OR REPLACE TRIGGER dept_bir
BEFORE INSERT ON departments
FOR EACH ROW
BEGIN
SELECT dept_seq.NEXTVAL
INTO :new.id
FROM dual;
END;
/
No comments:
Post a Comment