SQL Integrity Constraints
Integrity Constraints are used to apply business rules for the
database tables.
The constraints available in SQL are Foreign Key, Not Null, Unique, Check.
Constraints can be defined in two ways
1) The constraints can be specified immediately after the column definition. This is called column-level definition.
2) The constraints can be specified after all the columns are defined. This is called table-level definition.
1) The constraints can be specified immediately after the column definition. This is called column-level definition.
2) The constraints can be specified after all the columns are defined. This is called table-level definition.
1)
SQL Primary key:
This constraint defines a column or combination of columns which
uniquely identifies each row in the table.
Syntax
to define a Primary key at column level:
column name datatype [CONSTRAINT
constraint_name] PRIMARY KEY
Syntax
to define a Primary key at table level:
[CONSTRAINT
constraint_name] PRIMARY KEY (column_name1, column_name2,..)
·
column_name1,
column_name2 are the names of the columns which define the
primary Key.
·
The syntax within the bracket i.e. [CONSTRAINT
constraint_name] is optional.
For
Example: To create an employee table with Primary Key
constraint, the query would be like.
Primary Key at column level:
CREATE TABLE
EMPLOYEE
( ID NUMBER(5) PRIMARY
KEY,
NAME CHAR(20),
DEPT CHAR(10),
AGE NUMBER(2),
SALARY NUMBER(10),
LOCATION CHAR(10)
);
or
CREATE
TABLE EMPLOYEE
(
ID
NUMBER(5) CONSTRAINT EMP_ID_PK PRIMARY KEY,
NAME
CHAR(20),
DEPT
CHAR(10),
AGE
NUMBER(2),
SALARY
NUMBER(10),
LOCATION
CHAR(10)
);
Primary Key at column level:
CREATE
TABLE EMPLOYEE
(
ID NUMBER(5),
NAME
CHAR(20),
DEPT
CHAR(10),
AGE
NUMBER(2),
SALARY
NUMBER(10),
LOCATION
CHAR(10),
CONSTRAINT
EMP_ID_PK PRIMARY KEY (ID)
);
Primary Key at table level:
CREATE
TABLE EMPLOYEE
(
ID NUMBER(5), NOT NULL,
NAME
CHAR(20),
DEPT
CHAR(10),
AGE
NUMBER(2),
SALARY
NUMBER(10),
LOCATION
CHAR(10),
ALTER
TABLE EMPLOYEE ADD CONSTRAINT PK_EMPLOYEE_ID PRIMARY KEY (ID)
2)
SQL Foreign key or Referential Integrity:
This constraint identifies any column referencing the PRIMARY
KEY in another table. It establishes a relationship between two columns in the
same table or between different tables. For a column to be defined as a Foreign
Key, it should be a defined as a Primary Key in the table which it is
referring. One or more columns can be defined as Foreign key.
Syntax
to define a Foreign key at column level:
[CONSTRAINT
constraint_name] REFERENCES Referenced_Table_name(column_name)
Syntax to define a Foreign key at table level:
[CONSTRAINT
constraint_name] FOREIGN KEY(column_name) REFERENCES
referenced_table_name(column_name);
For
Example:
1) Lets use the "product" table and
"order_items".
Foreign Key at column level:
Foreign Key at column level:
CREATE TABLE PRODUCT
(
PRODUCT_ID NUMBER(5) CONSTRAINT
PD_ID_PK PRIMARY KEY,
PRODUCT_NAME CHAR(20),
SUPPLIER_NAME CHAR(20),
UNIT_PRICE NUMBER(10)
);
CREATE TABLE ORDER_ITEMS
(
ORDER_ID NUMBER(5) CONSTRAINT OD_ID_PK
PRIMARY KEY,
PRODUCT_ID NUMBER(5) CONSTRAINT
PD_ID_FK REFERENCES, PRODUCT(PRODUCT_ID),
PRODUCT_NAME CHAR(20),
SUPPLIER_NAME CHAR(20),
UNIT_PRICE NUMBER(10)
);
Foreign Key at table level:
CREATE TABLE ORDER_ITEMS
(
ORDER_ID NUMBER(5) ,
PRODUCT_ID NUMBER(5),
PRODUCT_NAME CHAR(20),
SUPPLIER_NAME CHAR(20),
UNIT_PRICE NUMBER(10)
CONSTRAINT OD_ID_PK PRIMARY
KEY(ORDER_ID),
CONSTRAINT PD_ID_FK FOREIGN
KEY(PRODUCT_ID) REFERENCES PRODUCT(PRODUCT_ID)
);
2) If the employee table has a 'mgr_id' i.e, manager id as a
foreign key which references primary key 'id' within the same table, the query
would be like,
CREATE TABLE EMPLOYEE
(
ID NUMBER(5) PRIMARY KEY,
NAME CHAR(20),
DEPT CHAR(10),
AGE NUMBER(2),
MGR_ID NUMBER(5) REFERENCES
EMPLOYEE(ID),
SALARY NUMBER(10),
LOCATION CHAR(10)
);
3) SQL
Not Null Constraint:
This constraint ensures all rows in the table contain a definite
value for the column which is specified as not null. Which means a null value
is not allowed.
Syntax
to define a Not Null constraint:
[CONSTRAINT
constraint name] NOT NULL
For
Example: To create a employee table with Null value, the
query would be like
CREATE
TABLE EMPLOYEE
(
ID
NUMBER(5),
NAME
CHAR(20) CONSTRAINT NM_NN NOT NULL,
DEPT
CHAR(10),
AGE
NUMBER(2),
SALARY
NUMBER(10),
LOCATION
CHAR(10)
);
4) SQL Unique Key:
This constraint ensures that a column or a group of columns in each row have a distinct value. A column(s) can have a null value but the values cannot be duplicated.
Syntax
to define a Unique key at column level:
[CONSTRAINT
constraint_name] UNIQUE
Syntax
to define a Unique key at table level:
[CONSTRAINT
constraint_name] UNIQUE(column_name)
For Example: To create an employee
table with Unique key, the query would be like,
Unique Key at column level:
CREATE
TABLE EMPLOYEE
(
ID
NUMBER(5) PRIMARY KEY,
NAME
CHAR(20),
DEPT
CHAR(10),
AGE
NUMBER(2),
SALARY
NUMBER(10),
LOCATION
CHAR(10) UNIQUE
);
OR
CREATE
TABLE EMPLOYEE
(
ID
NUMBER(5) PRIMARY KEY,
NAME
CHAR(20),
DEPT
CHAR(10),
AGE
NUMBER(2),
SALARY
NUMBER(10),
LOCATION
CHAR(10) CONSTRAINT LOC_UN UNIQUE
);
Unique Key at table level:
CREATE
TABLE EMPLOYEE
(
ID
NUMBER(5) PRIMARY KEY,
NAME
CHAR(20),
DEPT
CHAR(10),
AGE
NUMBER(2),
SALARY
NUMBER(10),
LOCATION
CHAR(10),
CONSTRAINT
LOC_UN UNIQUE(LOCATION)
);
5) SQL Check Constraint :
This constraint defines a business rule on a column. All the
rows must satisfy this rule. The constraint can be applied for a single column
or a group of columns.
Syntax
to define a Check constraint:
[CONSTRAINT
constraint_name] CHECK (condition)
For Example: In the employee table to
select the gender of a person, the query would be like
Check Constraint at column
level:
CREATE
TABLE EMPLOYEE
(
ID
NUMBER(5) PRIMARY KEY,
NAME
CHAR(20),
DEPT
CHAR(10),
AGE
NUMBER(2),
GENDER
CHAR(1) CHECK (GENDER IN ('M','F')),
SALARY
NUMBER(10),
LOCATION
CHAR(10)
);
Check Constraint at table
level:
CREATE
TABLE EMPLOYEE
(
ID
NUMBER(5) PRIMARY KEY,
NAME
CHAR(20),
DEPT
CHAR(10),
AGE
NUMBER(2),
GENDER
CHAR(1),
SALARY
NUMBER(10),
LOCATION
CHAR(10),
CONSTRAINT
GENDER_CK CHECK (GENDER IN ('M','F'))
);
No comments:
Post a Comment
Thank you :
- kareem