I have prepared an example Oracle "create table" script with those characteristics:
-
compact, using "inline constraints", you can write
more readable scripts. I preferred inline constraints instead
of using "ALTER TABLE xxx ADD (PRIMARY KEY /
CONSTRAINT...)" statements. Using GUI tools to create
those scripts, we can easily miss more readable scripts.
-
several constraints such as "multiple column PRIMARY KEY",
"REFERENCES (i.e. Foreign Key", "multiple column FOREIGN KEY", etc.
- Options such as "ON DELETE SET NULL"
- Several CHECK constraints
- EXCEPTIONS INTO
- and more...
DROP TABLE sa_dept CASCADE CONSTRAINTS;
CREATE TABLE sa_dept
employee_id
NUMBER(4)
PRIMARY KEY,
last_name
VARCHAR2(10),
email
VARCHAR2(100) NOT NULL UNIQUE,
-- example for foreignkey const. with
-- "different column names"
-- (i.e, sa_dept.job_id = sa_jobs.id)
-- of course, it is better to use the same
-- column names if possible (sa_dept.job_id = sa_jobs.job_id)
job_id
VARCHAR2(9)
CONSTRAINT fk_job
REFERENCES sa_jobs(id),
-- ref. int. constr. with ON DELETE SET NULL
manager_id
NUMBER(4)
CONSTRAINT fk_mgr
REFERENCES sa_employees ON DELETE SET NULL,
-- composite foreignkey constr. + EXCEPTIONS INTO example
hire_date
DATE
CONSTRAINT
FOREIGN KEY (employee_id, start_date)
REFERENCES sa_job_history(employee_id, start_date)
EXCEPTIONS INTO sa_wrong_emp,
-- sa_wrong_emp should exists!
-- CHECK constr. Any boolean expression in
-- parentheses after "CHECK" is valid
salary
NUMBER(7,2)
CONSTRAINT check_sal
CHECK(salary * commission_pct <= 5000),
-- CHECK constr. with BETWEEN. As stated above,
-- any boolean expression in parentheses' after "CHECK" is valid.
commission_pct
NUMBER(7,2)
CONSTRAINT check_divno
CHECK(
BETWEEN10 AND 99)
DISABLE,
-- if we use DISABLE, Oracle defines
-- the constr. but does not enable it.
-- ref. int. constr. with ON DELETE CASCADE
department_id
NUMBER(2)
CONSTRAINT fk_deptno
REFERENCES sa_departments(department_id)
ON DELETE CASCADE
);
Hiç yorum yok:
Yorum Gönder