Constraints in SQL - Gvs
Constraints in SQL - Gvs
column,...
[CONSTRAINT constraint_name] constraint_type
(column, ...),
Defining Constraints
v Column-level constraint:
CREATE TABLE employees(
employee_id NUMBER(6)
CONSTRAINT emp_emp_id_pk PRIMARY KEY,
first_name VARCHAR2(20),
...); 1
v Table-level constraint:
CREATE TABLE employees(
employee_id NUMBER(6),
first_name VARCHAR2(20),
...
job_id VARCHAR2(10) NOT NULL,
CONSTRAINT emp_emp_id_pk 2
PRIMARY KEY (EMPLOYEE_ID));
NOT NULL Constraint
Ø Ensures that null values are not permitted for the column:
…
INSERT INTO
Allowed
Not allowed:
already exists
UNIQUE Constraint
…
Not allowed INSERT INTO
(null value)
Not allowed
(50 already exists)
KEY Constraint
FOREIGN DEPARTMENTS
PRIMARY
KEY
…
EMPLOYEE
S FOREIGN
KEY
…
INSERT INTO Not allowed
(9 does not
exist)
Allowed
FOREIGN KEY Constraint
UPDATE employees
SET department_id = 55
WHERE department_id = 110;
UPDATE employees
*
ERROR at line 1:
ORA-02291: integrity constraint (HR.EMP_DEPT_FK)
violated - parent key not found