Skip to content

Instantly share code, notes, and snippets.

@nulpatrol
Last active March 14, 2016 09:24
Show Gist options
  • Save nulpatrol/7287e0fe53f70bd527c1 to your computer and use it in GitHub Desktop.
Save nulpatrol/7287e0fe53f70bd527c1 to your computer and use it in GitHub Desktop.
AutoIncrement.sql
CREATE TABLE people (
id NUMBER NOT NULL,
name VARCHAR(56),
last_name VARCHAR(56),
dob DATE,
graduation_date DATE,
created_at TIMESTAMP,
updated_at TIMESTAMP)
ALTER TABLE people ADD CONSTRAINT people_pk PRIMARY KEY ( id )
CREATE SEQUENCE people_seq START WITH 1 INCREMENT BY 1
CREATE OR REPLACE TRIGGER people_trigger
BEFORE INSERT ON people REFERENCING
NEW AS new
OLD AS old
FOR EACH ROW
BEGIN
SELECT COALESCE(:new.id, people_seq.nextval) INTO :new.id FROM dual;
END;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment