Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

constraints:

constraints are used to prevents invalid data


entery into our tables
orcale having following types of constrants
1.not null
2.unique
3.primary key
4.foreign key
5.check

in all databases all above constarints are


defined into two ways
1.columnlevel
2.tables level

column level:
in this we are defining constarints in
individual columns
syntax:
create table tablename(col1 datatype(size) constraint,
col2 datatype(size) constraint name.......
table level:
in this we are defining constraints on group
of columns

syntax:
create table tablename(col1 datatype(size),
col2 datatype(size),...constrainttype(col1,col2..);

not null:
in all databases not null doesnot support table
level
not null doesnot accepts null values but it
accepts duplicates

column level:
SQL> create table test(sno number(10) not null,name varchar2(10));

Table created.

SQL> insert into test values(1,'a');

1 row created.

SQL> insert into test values(null,'a');


insert into test values(null,'a')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."TEST"."SNO")

unique:
in all databases unique constraint defined both
column,table level
unique doesnot accepts duplicates and it accepts
null values

column level:

SQL> create table test(sno number(10) unique,name varchar2(10));


Table created.

SQL> insert into test values(1,'a');

1 row created.

SQL> insert into test values(1,'b');


insert into test values(1,'b')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C0012014) violated

tabel level:

SQL> create table test(sno number(10),name varchar2(10),unique(sno,name));

Table created.

SQL> insert into test values(1,'a');

1 row created.

SQL> select * from test;

SNO NAME
---------- ----------
1 a

SQL> insert into test values(2,'b');

1 row created.

SQL> select * from test;

SNO NAME
---------- ----------
1 a
2 b

SQL> insert into test values(1,'b');

1 row created.

SQL> select * from test;

SNO NAME
---------- ----------
1 a
2 b
1 b

SQL> insert into test values(2,'b');


insert into test values(2,'b')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C0012016) violated
primary key:
primary key uniquely identifying a record in a
table,in all databases there can be one primary
key in a table and also primary key doesnot
accepts duplicates,nulls

note:
when ever we are primary key then oracle server
automatically creates btree index

column level:
syntax:
create table tablename(col1 datatype(size) primary
key,col2 datatype(size),.......);

SQL> create table test(sno number(10) primary key,name varchar2(10));

Table created.

SQL> insert into test values(1,'a');

1 row created.

SQL> insert into test values(1,'b');


insert into test values(1,'b')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C0012017) violated

SQL> insert into test values(null,'b');


insert into test values(null,'b')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."TEST"."SNO")

table level:
syntax:
create table tablename(col1 datatype(size),col2
datatype(size),...primary key(col1,col2...);

SQL> create table test(sno number(10),name varchar2(10),primary key(sno,name));

Table created.

SQL> insert into test values(1,'a');

1 row created.

SQL> select * from test;

SNO NAME
---------- ----------
1 a

SQL> insert into test values(1,'b');


1 row created.

SQL> select * from test;

SNO NAME
---------- ----------
1 a
1 b

SQL> insert into test values(1,'b');


insert into test values(1,'b')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C0012018) violated

unique/not null:
not null-null values not allowing,duplicate allow
unique-dupl not allow,null values allow
primary key:
both null,duplcates not allow

You might also like