Anda di halaman 1dari 17

Constraints

Constraint Type
NULL
NOT NULL
UNIQUE (column-name)
PRIMARY KEY (column-name)
FOREIGN KEY (column-name, ...)

REFERENCES table-name (column-name, ...)


ON DELETE CASCADE
CHECK (condition)
DEFAULT

Example : Create Table


CREATE TABLE EMP (
EMPNO NUMBER(4) NOT NULL,
NAME
VARCHAR2(30) NOT NULL,
DEG
VARCHAR2(10),
DEPTNO
NUMBER(2)
SALARY NUMBER(7,2)
);
Except for the columns EMPNO and NAME null values are
allowed

Example
CREATE TABLE EMP
( EMP_NO
NUMBER(4) PRIMARY KEY,
DEPT_NO
NUMBER(2) ,
EMP_NAME
VARCHAR2(25) CHECK
(EMP_NAME=UPPER(EMP_NAME)),
ADDRESS
VARCHAR2(45) NOT NULL,
ADHARNO
NUMBER(12) UNIQUE,
MANAGER NUMBER(4) REFERENCES
EMP(EMP_NO),
SALARY
NUMBER(8,2) DEFAULT 0,
CONSTRAINT EMP_DEPT_NO FORREIGN KEY
(DEPT_NO) REFERENCES DEPT (DEPT_NO));
DESCRIBE EMP;

Constraints
Constraints.

There are three main integrity constraints Key Constraints,


Domain Constraints, Referential Integrity constraints.
Key Constraints :
There must be at least one minimal subset of attributes in the relation,
which can identify a tuple uniquely. This minimal subset of attributes is
called primary key constraint for that relation. If there are more than one
such minimal subsets, these are called candidate keys.
The key constraints forces that in a relation with a key attribute, no two
tuples can have identical value for the key attributes and key attribute
can not have NULL values.
Key constraints are also referred to as Entity Constraints.
Based on a primary key, the database system ensures that no duplicates

appear in a table.

Referential Integrity Constraints :


Referential Integrity Constraints : The integrity constraints

works on the concept of Foreign Key. A key attribute of a


relation can be referred in other relation, where it is called
foreign key.

The definition of a table may include the specification of

integrity constraints.

Basically two types of constraints are provided :column

constraints are associated with a single column whereas table


constraints are typically associated with more than one column.

However, any column constraint can also be formulated as a

table constraint.

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,..)

Primary Key
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)

Foreign Key
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)
);
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)
);

Primary key foreign key in same table


If the employee table has a 'mgr_id as a foreign key

which references primary key 'id' within the same


table, the SQL would be like,
CREATE TABLE EMPLOYEE
( ID NUMBER(5) PRIMARY KEY,
ENAME
CHAR(20),
DEPT
HAR(10),
AGE NUMBER(2),
MGR_ID NUMBER(5) REFERENCES EMPLOYEE(ID),
SALARY NUMBER(10),
LOCATION CHAR(10)
);

Not Null Constraint


[Not] Null 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
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)
);

Unique Key constraint:


Unique Key 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 :[CONSTRAINT constraint_name] UNIQUE
[CONSTRAINT constraint_name] UNIQUE(column_name)

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
);
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)
);

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

Difference between UNIQUE and PRIMARY KEY constraints


Primary key
Primary key uniquely identify a record in the table.
Primary Key can't accept null values.
We can have only one Primary key in a table.
Unique key
Unique key also identify uniquely a record in the table.
It can accept null value once.
We can have more than one unique key in a table.

REFERENCE
By the SQL standard, a foreign key must

reference either the primary key or a


unique key of the parent table

Check Constraint

Check 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 :[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'))
);

DEFAULT Constraint
The DEFAULT constraint is used to insert a default value into a

column.

The default value will be added to all new records, if no other value

is specified.

CREATE TABLE PERSONS


(
P_ID
NUMBER NOT NULL,
LASTNAME
VARCHAR(25) NOT NULL,
FIRSTNAME
VARCHAR(25),
ADDRESS
VARCHAR(25),
CITY
VARCHAR(25) DEFAULT 'SANDNES
CREATE_DATE DATE DEFAULT SYSDATE;
)

Anda mungkin juga menyukai