Sei sulla pagina 1di 3

INTEGRITY CONSTRAINTS

AIM
To implement INTEGRITY CONSTRAINTS using SQL.

I. DOMAIN INTEGRITY CONSTRAINTS

1. NOT NULL CONSTRAINT

SYNTAX
create table <table name> (column 1 datatype 1 not null,
……., column n datatype n);

EXPLANATION

By default ORACLE allows null values in all columns. When a not-null


constraint is enforced on a column or a group of columns in a table,
ORACLE will not allow null values in the columns.

EXAMPLE

SQL> create table ace (VT number not null, name varchar (10));

2. CHECK CONSTRAINT

SYNTAX:
alter table <table name> add constraint constraint_name check<condition>;

EXPLANATION

In ORACLE, we can create constraint for columns with check conditions.

EXAMPLE

SQL> alter table ace add constraint RX check (VT between 4100 and 4200);
II. ENTITY INTEGRITY CONSTRAINTS

1. UNIQUE CONSTRAINT

SYNTAX
alter table <table name> add/modify constraint constraint_name
unique (column name);

EXPLANATION

Unique key constraint is used to prevent duplication of values within the


row of a specified column or a group of table.

EXAMPLE

SQL> alter table ace add constraint ZXY unique (VT);

2. PRIMARY KEY CONSTRAINT

SYNTAX
alter table <table name> add/modify constraint constraint_name
primary key (column name);

EXPLANATION

A primary key is one or more column in a table which identifies each row
in the table uniquely. The constraint does not allow null values and
duplicate values in columns.

EXAMPLE

SQL> alter table ace add constraint accue primary key (VT);
III. REFERENTIAL INTEGRITY CONSTRAINTS

1. REFERENCE COMMAND

SYNTAX
create table <table name> (column 1 datatype 1 references
<table name> (column name),……., column n datatype n);

EXPLANATION

Referential integrity constraints are used to establish a parent child


relationship between two tables having a common column.

EXAMPLE

SQL> create table semnr (VT number references ace (VT), subject varchar (10));

2. FOREIGN KEY

SYNTAX
create table <table name> (column 1 datatype 1,……., column n datatype n
foreign key (column name) references <table name> (column name));

EXPLANATION

To implement this integrity, the column should be defined as primary key


in the parent table and as a foreign key in the child table.

EXAMPLE

SQL> create table wise (roll number, iq int, foreign key (roll) references ace(VT));

Potrebbero piacerti anche