Вы находитесь на странице: 1из 6

CONSTRAINTS

1) What are constraints?


a) Constraint is a rule or restrictions which is imposed on the table data.
For a new table, constraints can be specified at the create table syntax.
For an existing table, constraints can be added by using alter table syntax.
It is used to enforce business rules on the data.

2) Why we use constraints?
a) if we want to enforce rules whenever a row is inserted, deleted or updated from that table i.e for
every operation on the table data to succeed, constraint must be satisfied first.

3) List of constraints ?

a) NOT NULL
b) UNIQUE
C) CHECK
d) PRIMARY KEY
e) FORIEGN KEY and
f) ON DELETE.

3) Different ways to add constraints?
a) Column Level
b) Table level and
c) Alter level.

Whatever the way we choose, constraints are always attached to columns not to a table.

4) Constraint name should be unique to the owner.
Format for the constraint name is
TABLENAME_CONSTRAINT TYPE_COLNAME.

5) While adding the constraint, we don't need to specify the name. Oracle will automatically generate
the name of the constraint.
If we want to mention the name of the constraint then we need to use CONSTRAINT clause along with
the constraint name.


Tables:
1) USER_CONSTRAINTS
select constraint_name,constraint_type,search_condition from user_constraints;
2) DBA_CONSTRAINTS
select constraint_name,constraint_type,search_condition from dba_constraints;
3) ALL_CONSTRAINTS
select constraint_name,constraint_type,search_condition from all_constraints;

NOT NULL Constraint:-
1) Is used to avoid null values for a specific column.
2) We can add NOT NULL only at column level.

Eg:-

CREATE TABLE my_details(std_name VARCHAR2(10) NOT NULL
,mobile_num NUMBER
,Qualification VARCHAR2(20)
)'

CREATE TABLE my_details(std_name VARCHAR2(20) CONSTRAINT mydetails_NN NOT NULL,
,mobile_num NUMBER
,Qualification VARCHAR2(20)
)'

CHECK Constraint:-
It is used to insert the values into the table based on specified condition for each record.
It can be add at three levels:-
1) Column level
2) Alter level and
3) Table level.



Column Level:-

a) Without name:-

CREATE TABLE my_details(std_name varchar2(100),city varchar2(10) CHECK (city='Hyderabad'),course
varchar2(10));
b) With name:-
CREATE TABLE my_details(std_name varchar2(100),city varchar2(10) CONSTRAINT std_ck CHECK
(city='Hyderabad'),course varchar2(10));
Table Level:-
a) Without name:

CREATE TABLE my_details(std_name varchar2(100),city varchar2(10),course varchar2(10),
CHECK (city='Hyderabad'));

b) With name:

CREATE TABLE my_details(std_name varchar2(100),city varchar2(10),course varchar2(10),
CONSTRAINT std_ch CHECK (city='Hyderabad'));

Alter Level:-

ALTER TABLE my_details ADD CONSTRAINT std_ch CHECK (city='Hyderabad')

To drop an constraint from the existing table

ALTER TABLE my_details DROP CONSTRAINT std_ch CHECK(city='Hyderabad')


UNIQUE Constraint:-
Is used to avoid the duplicate values in the column data but it allows null values.

1) Column Level:-

a) Without name:-

CREATE TABLE my_details(std_name varchar2(10) UNIQUE,city varchar2(10),course varchar2(20));

b) With name:-

CREATE TABLE my_details(std_name varchar2(10) CONSTRAINT std_un UNIQUE,city
varchar2(10),course varchar2(20));


2) Table Level:-

a) Without name:-

CREATE TABLE my_details(std_name varchar2(10),city varchar2(10),course varchar2(20),UNIQUE
(std_name));

b) With name:-

CREATE TABLE my_details(std_name varchar2(10),city varchar2(10),course varchar2(20), CONSTRAINT
std_un UNIQUE (std_name));

3)Alter Level:-

ALTER TABLE my_details ADD CONSTRAINT std_un UNIQUE (std_name);

To drop a constraint

ALTER TABLE my_details DROP CONSTRAINTS std_un UNIQUE(std_name);



PRIMARY KEY Constraint:-

=> It is used to avoid the duplicates and null values. It will work as a combination of both
UNIQUE and NOT NULL.

=> Only one primary key is allowed.

=> In the parent-child relationship, primary key is always attached to Parent table only.

=> If we want more than one column to work as a primary key then it is called Composite
primary key.

=> In a composite primary key we can have maximum 32 columns.

1) Column level creation:-

CREATE TABLE tata_motors(model_name varchar2(20) PRIMARY KEY,color varchar2(10),price
number);

CREATE TABLE tata_motors(model_name varchar2(20) CONSTRAINT motor_pk PRIMARY
KEY,color varchar2(10),price number);

2) Table level creation:-

CREATE TABLE tata_motors(model_name varchar2(20),color varchar2(10),price
number,PRIMARY KEY (model_name));

CREATE TABLE tata_motors(model_name varchar2(20),color varchar2(10),price
number,CONSTRAINT motor_pk PRIMARY KEY (model_name));


3) Alter level creation:-

ALTER TABLE tata_motors ADD PRIMARY KEY(model_name));

ALTER TABLE tata_motors ADD CONSTRAINT motor_pk PRIMARY KEY (model_name));


To drop a constraint
ALTER TABLE tata_motors DROP CONSTRAINT motor_pk PRIMARY KEY(model_name);


FOREIGN KEY Constraint:-

Foreign keys are used to refer the parent table primary key column which does not
allow duplicates.
The Foreign key constraint provides referential integrity rules (either within a table or
between tables). i.e. We can only place a value in TABLE B if the values exist as a
primary key in TABLE A.
For parent child relationships across tables, foreign key is always attached to the child
table.
We use foreign key if we want to ensure that for every child table record there is a
reference in parent table.

We can add this constraint in all three levels.
Column Level
Table Level
Alter Level

Вам также может понравиться