0% found this document useful (0 votes)
50 views

SQL 16

Check constraints are used to define logical conditions that columns in a table must satisfy. They can be defined at the column level or table level. When defined at the column level, the check only applies to that column, while at the table level the check applies to the entire row. Check constraints prevent invalid data from being inserted or updated in the table by restricting the possible values.

Uploaded by

j shaik0626
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

SQL 16

Check constraints are used to define logical conditions that columns in a table must satisfy. They can be defined at the column level or table level. When defined at the column level, the check only applies to that column, while at the table level the check applies to the entire row. Check constraints prevent invalid data from being inserted or updated in the table by restricting the possible values.

Uploaded by

j shaik0626
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

check:

check constraint is used to defined logical condition


according to client request
in oracle check constraint doesnot work sysdate function

syntax:
create table tablename(col1 datatype(size) check(logical
cond) col2 datatype(size),...);

column level:
SQL> create table test(sal number(10) check(sal>2000));

Table created.

SQL> insert into test values(1500);


insert into test values(1500)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.SYS_C0012044) violated

SQL> insert into test values(20000);

1 row created.

SQL> insert into test values(2000);


insert into test values(2000)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.SYS_C0012044) violated

SQL> insert into test values(2001);

SQL> create table test(name varchar2(10) check(name=upper(name)));

Table created.

SQL> insert into test values('abc');


insert into test values('abc')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.SYS_C0012045) violated

SQL> insert into test values('ABC');

1 row created.

SQL> select * from test;

NAME
----------
ABC

1 row created.
table level:
syntax:
create table tablename(col1 datatype(size),col2 datatype(size)
check(cond1,cond2));

SQL> create table test(name varchar2(10),sal number(10),


2 check(sal>2000 and name=upper(name)));

Table created.

SQL> insert into test values('abc',20001);


insert into test values('abc',20001)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.SYS_C0012046) violated

SQL> insert into test values('ABC',20001);

1 row created.

SQL> insert into test values('ABC',200);


insert into test values('ABC',200)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.SYS_C0012046) violated

SQL> create table test(name varchar2(10),sal number(10),


2 check(sal>2000 or name=upper(name)));

Table created.

SQL> insert into test values('ABC',200);

1 row created.

SQL> insert into test values('abc',20001);

1 row created.

adding or dropping constraints:


using alter command we can also add or drop constraints
on existing table

SQL> select CONSTRAINT_NAME,CONSTRAINT_TYPE from user_constraints


2 where table_name='TEST';

CONSTRAINT_NAME C
------------------------------ -
SYS_C0012048 P

if you want to add new column with constraint

SQL> alter table test add name varchar2(10) unique;

Table altered.
SQL> select CONSTRAINT_NAME,CONSTRAINT_TYPE from user_constraints
2 where table_name='TEST';

CONSTRAINT_NAME C
------------------------------ -
SYS_C0012048 P
SYS_C0012049 U

desc user_constraints;

note:
if you want to add not null constraint on existing table
,existing column then we are using alter with modify

syntax:
alter table tabelname modify columnname not null;

SQL> alter table test modify sno not null;

Table altered.

dropping constrainsts:
syntax:
alter drop tablename drop constraint constraintname;
syntax:
alter table tablename drop primary key;

syntax:
alter table tablename drop unique(col1,col2.....)

SQL> alter table test drop unique(name);

Table altered.

SQL> desc mas;


Name Null? Type
----------------------------------------------------- --------
------------------------------------
SNO NOT NULL NUMBER(10)

SQL> desc child;


Name Null? Type
----------------------------------------------------- --------
------------------------------------
SNO NUMBER(10)

SQL> drop table mas;


drop table mas
*
ERROR at line 1:
ORA-02449: unique/primary keys in table referenced by foreign keys

note:
we cannot drop master table directly after dropping child
table only we can drop master table
SQL> drop table child;

Table dropped.

SQL> drop table mas;

Table dropped.

note:
if you want to drop primary key along with referenced foreign
key then we are using cascade along with alter drop

syntax:
alter table tablename drop primary key cascade;

SQL> create table master(sno number(10) primary key);

Table created.

SQL> create table child(sno number(10) references master);

Table created.

SQL> desc child;


Name Null? Type
----------------------------------------------------- --------
------------------------------------
SNO NUMBER(10)

SQL> desc master;


Name Null? Type
----------------------------------------------------- --------
------------------------------------
SNO NOT NULL NUMBER(10)

SQL> alter table master drop primary key;


alter table master drop primary key
*
ERROR at line 1:
ORA-02273: this unique/primary key is referenced by some foreign keys

SQL> alter table master drop primary key cascade;

Table altered.

SQL> desc master;


Name Null? Type
----------------------------------------------------- --------
------------------------------------
SNO NUMBER(10)

SQL> desc child;


Name Null? Type
----------------------------------------------------- --------
------------------------------------
SNO NUMBER(10)
SQL> drop table master;

Table dropped.

SQL> desc master;


ERROR:
ORA-04043: object master does not exist

SQL> desc child;


Name Null? Type
----------------------------------------------------- --------
------------------------------------
SNO NUMBER(10)

You might also like