Thursday, December 17, 2009

Violating Constraints

Query :
  • UPDATE emp SET department_id = 29 WHERE department_id = 30;
Error :
  • insert or update on table "emp" violates foreign key constraint "emp_ref_dept_fk"
DETAIL :
  • Key (deptno)=(29) is not present in table "dept"
  • Department 29 does not exist
You cannot delete a row that contains a primary key that is used as a foreign key in another table.

Query :
  • DELETE FROM departments WHERE department_id = 60;
Error :
  • update or delete on table "dept" violates foreign key constraint "emp_re f_dept_fk" on table "emp";
Detail :
  • Key (deptno)=(10) is still referenced from table "emp".

Wednesday, December 16, 2009

CHECK Constraint

CHECK Constraint :

Defines a condition that each row must satisfy
  • The following expressions are not allowed :
  • References to CURR_VAL, NEXT_VAL and CTID pseudocolumns
  • Queries that refer to other values in other rows
  • ..., salary numeric(2)CONSTRAINT emp_salary_minCHECK (salary > 0),...
CREATE TABLE : Example

Followers