Oracle Constraints

 Primary Key Constraint:

CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
employee_name VARCHAR2(50));

Foreign Key Constraint:

CREATE TABLE orders (
order_id NUMBER PRIMARY KEY,
customer_id NUMBER,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id));

Check Constraint:

CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
employee_name VARCHAR2(50),
salary NUMBER,
CONSTRAINT age CHECK (age > 18));

Not Null Constraint:

CREATE TABLE customers (
customer_id NUMBER PRIMARY KEY,
customer_name VARCHAR2(50) NOT NULL);

Composite Constraints:

CREATE TABLE sales (
order_id NUMBER,
product_id NUMBER,
quantity NUMBER,
PRIMARY KEY (order_id, product_id),
FOREIGN KEY (order_id) REFERENCES orders(order_id),
FOREIGN KEY (product_id) REFERENCES products(product_id));

Unique Constraint:

CREATE TABLE students (
student_id NUMBER UNIQUE,
student_name VARCHAR2(50));

Constraints Views:

select constraint_name, constraint_type,table_name from user_constraints;

Comments