0% found this document useful (0 votes)
38 views8 pages

Oracle11am 15 17 Feb

Uploaded by

pavan raut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views8 pages

Oracle11am 15 17 Feb

Uploaded by

pavan raut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

CONSTRAINTS: (15-02-2024)

========================
- are used to restricted / enforce the unwanted data(i.e invalid) into a
table.
- oracle supports the following six types of constraints are,
> UNIQUE
> NOT NULL
> CHECK
> PRIMARY KEY
> FOREIGN KEY
> DEFAULT
- we can define a constraint at two levels:

i) column level:
==============
- we are applying constraints on each column wise.

syntax:
=======
create table <table name>(<column name1> <datatype>[size] <constraint type>,
<column name2> <datatype>[size] <constraint type>,..................);

ii) table level:


===========
- we are applying constraints after all columns are defined i.e the end of
the
table definition.

syntax:
=======
create table <table name>(<column name1> <datatype>[size],
<column name2> <datatype>[size],.............<constraint type>(colum name1,column
name2,......));

NOTE:
======
- table level constraints are also called as "composite constraints".

UNIQUE:
========
- to restricted duplicate values but allowed nulls.

EX:
column level:
===========
SQL> CREATE TABLE TEST1(SNO NUMBER(3) UNIQUE,
2 NAME VARCHAR2(10) UNIQUE);

TESTING:
SQL> INSERT INTO TEST1 VALUES(1,'A');----------------ALLOWED
SQL> INSERT INTO TEST1 VALUES(1,'A');----------------NOT ALLOWED
SQL> INSERT INTO TEST1 VALUES(NULL,NULL);-----ALLOWED

table level:
=========
SQL> CREATE TABLE TEST2
2 (
3 SNO NUMBER(3),
4 NAME VARCHAR2(10),
5 UNIQUE(SNO,NAME)
6 );

TESTING:
=========
SQL> INSERT INTO TEST2 VALUES(1,'A');-----ALLOWED
SQL> INSERT INTO TEST2 VALUES(1,'A');-----NOT ALLOWED
SQL> INSERT INTO TEST2 VALUES(1,'B');-----ALLOWED
SQL> INSERT INTO TEST2 VALUES(NULL,NULL);----ALLOWED

NOT NULL:
==========
- to restricted nulls but allowed duplicate values.
- it can defined at column level only.
EX:
column level:
===========
SQL> CREATE TABLE TEST3(SNO NUMBER(2) NOT NULL,
2 NAME VARCHAR2(10) NOT NULL);

TESTING:
SQL> INSERT INTO TEST3 VALUES(1,'A');-------------ALLOWED
SQL> INSERT INTO TEST3 VALUES(1,'A');------------ ALLOWED
SQL> INSERT INTO TEST3 VALUES(NULL,NULL);---NOT ALLOWED

CHECK:
=======
- to check a value with user defined condition before accepting into a
column.

EX:
column level:
===========
SQL> CREATE TABLE STUDENT
2 (
3 STID NUMBER(3) UNIQUE NOT NULL,
4 SNAME VARCHAR2(10) NOT NULL,
5 SFEE NUMBER(8,2) CHECK(SFEE>=5000) NOT NULL,
6 AGE NUMBER(3) CHECK(AGE BETWEEN 18 AND 30) NOT NULL,
7 LOC VARCHAR2(10) CHECK(LOC IN('HYD','PUNE')) NOT NULL
8 );

TESTING:
SQL> INSERT INTO STUDENT VALUES(1,'JONES',4500,17,'HYDERABAD');-----NOT ALLOWED
SQL> INSERT INTO STUDENT VALUES(1,'JONES',5500,21,'HYD');----ALLOWED

table level:
==========
SQL> CREATE TABLE TEST4(ENAME VARCHAR2(10),SAL NUMBER(8,2),
CHECK(ENAME=LOWER(ENAME) AND SAL>15000));

TESTING:
SQL> INSERT INTO TEST4 VALUES('SMITH',15000);-----NOT ALLOWED
SQL> INSERT INTO TEST4 VALUES('smith',16000);-----ALLOWED

PRIMARY KEY:
=============
- it is combination of unique and not null constraint.
- by using primary key we can restrict duplicate and nulls.
- a table is having only one primary key.

EX:
column level:
============
SQL> CREATE TABLE TEST5(STID NUMBER(4) PRIMARY KEY,
2 MAILID VARCHAR2(20)UNIQUE NOT NULL);

TESTING:
SQL> INSERT INTO TEST5 VALUES(1,'[email protected]');------ALLOWED
SQL> INSERT INTO TEST5 VALUES(1,'[email protected]');-----NOT ALLOWED
SQL> INSERT INTO TEST5 VALUES(NULL,NULL);-------NOT ALLOWED
SQL> INSERT INTO TEST5 VALUES(2,'[email protected]');------ALLOWED

16-02-2024:
==========
table level / composite primary key :
==============================
- In composite primary key each individual columns are allowed duplicate
values but the combination of columns are not allowed duplicate values.

EX:
SQL> CREATE TABLE TEST6(SNO NUMBER(4),
2 NAME VARCHAR2(10),PRIMARY KEY(SNO,NAME));

TESTING:
SQL> INSERT INTO TEST6 VALUES(1,'A');-------ALLOWED
SQL> INSERT INTO TEST6 VALUES(1,'A');------NOT ALLOWED
SQL> INSERT INTO TEST6 VALUES(1,'B');-----ALLOWED

FOREIGN KEY (references) :


========================
- it is used to establish relationship between tables for taking some
referencial data (i.e identity) from one table to another table.

BASIC RULES:
=============
1. to maintain a common column in both tables with same datatypes.
2. one table should have primary key and another table should have foreign key.
here primary key & foreign key columns must be common column only.
3. a primary key table is called as parent table and a foreign key table is called
as
child table in the relationship.
4. by default a foreign key column is allowed duplicate and nulls.
5. a foreign key column is allowed the values which was existing in primary key
column
of parent table.

Ex: (parent) (child)


test1 test2
==== =====
Sno number(3)(pk) Sno number(4)(fk)
============ ============
1 1
2 2
3 3
4 --------- error
1
2
3
null

SYNTAX:
========
<common column of child table> <datatype>[size]
references <parent table name>(common column of parent table)

Ex:
SQL> CREATE TABLE DEPT1(DNO NUMBER(4) PRIMARY KEY,DNAME VARCHAR2(10));

SQL> INSERT INTO DEPT1 VALUES(1,'SAP');


SQL> INSERT INTO DEPT1 VALUES(2,'DBA');
SQL> COMMIT;

SQL> CREATE TABLE EMP1(EID NUMBER(4),ENAME VARCHAR2(10),


DNO NUMBER(4) REFERENCES DEPT1(DNO));

SQL> INSERT INTO EMP1 VALUES(11,'SMITH',1);


SQL> INSERT INTO EMP1 VALUES(12,'ALLEN',1);
SQL> INSERT INTO EMP1 VALUES(13,'JONES',2);
SQL> INSERT INTO EMP1 VALUES(14,'WARD',NULL);
SQL> COMMIT;

17-02-2024:
==========
- Once we establish a relationship between tables there are two rules are
come into picture.

Rule-1: Insertion rule:


==================
- we cannot insert values into a child table those values are not existing
in primary key column of parent table.
NO PARENT = NO CHILD
EX:
SQL> INSERT INTO EMP1 VALUES(15,'ADAMS',3);
ERROR at line 1:
ORA-02291: integrity constraint (MYDB11AM.SYS_C008807) violated - parent key not
found

Rule-2 : Deletion rule:


===================
- we cannot delete a row from the parent table those parent rows are having
the corresponding child rows in child table without addressing to child.

EX:
SQL> DELETE FROM DEPT1 WHERE DNO=1;

ERROR at line 1:
ORA-02292: integrity constraint (MYDB11AM.SYS_C008807) violated - child record
found

- when we want to delete a row from parent table then we should address
to child table by using "CASCADE RULES".
i) ON DELETE CASCADE
ii) ON DELETE SET NULL

i) ON DELETE CASCADE:
====================
- When we delete a row from parent table then the corresponding child rows
also deleted from child table automatically.

Ex:
SQL> CREATE TABLE DEPT2(DNO NUMBER(4) PRIMARY KEY,DNAME VARCHAR2(10));

SQL> INSERT INTO DEPT2 VALUES(1,'SAP');


SQL> INSERT INTO DEPT2 VALUES(2,'DBA');
SQL> COMMIT;

SQL> CREATE TABLE EMP2(EID NUMBER(4),ENAME VARCHAR2(10),


DNO NUMBER(4) REFERENCES DEPT2(DNO) ON DELETE CASCADE);

SQL> INSERT INTO EMP2 VALUES(11,'SMITH',1);


SQL> INSERT INTO EMP2 VALUES(12,'ALLEN',2);

TESTING:
SQL> DELETE FROM DEPT2 WHERE DNO=1;------ALLOWED

ii) ON DELETE SET NULL:


====================
- When we delete a row from parent table then the corresponding child rows
of foreign key column values are converting into NULL in child table
automatically.

Ex:
SQL> CREATE TABLE DEPT23(DNO NUMBER(4) PRIMARY KEY,DNAME VARCHAR2(10));

SQL> INSERT INTO DEPT23 VALUES(1,'SAP');


SQL> INSERT INTO DEPT23 VALUES(2,'DBA');
SQL> COMMIT;

SQL> CREATE TABLE EMP23(EID NUMBER(4),ENAME VARCHAR2(10),


DNO NUMBER(4) REFERENCES DEPT23(DNO) ON DELETE SET NULL);

SQL> INSERT INTO EMP23 VALUES(11,'SMITH',1);


SQL> INSERT INTO EMP23 VALUES(12,'ALLEN',2);

TESTING:
SQL> DELETE FROM DEPT23 WHERE DNO=1;------ALLOWED
==========================================================================
How to add a constraint to an existing table:
======================================
syntax:
======
ALTER TABLE <TN> ADD <CONSTRAINT> <CONSTRAINT NAME> <TYPE OF THE CONSTRAINT>
(COLUMN NAME);

EX:
SQL> CREATE TABLE PARENT(EID NUMBER(4),ENAME VARCHAR2(10),SAL NUMBER(10));

i) Adding a primary key:


====================
SQL> ALTER TABLE PARENT ADD CONSTRAINT PK_EID PRIMARY KEY(EID);

- To view constraint details on columns of a particular table then we follow


the following pre-defined table (i.e datadictionary) is "user_cons_columns".

EX:
SQL> DESC USER_CONS_COLUMNS;
SQL> SELECT COLUMN_NAME,CONSTRAINT_NAME
2 FROM USER_CONS_COLUMNS WHERE TABLE_NAME='PARENT';

COLUMN_NAME CONSTRAINT_NAME
--------------------------------------------------- ------------------------
-----------------------------------------------------
EID PK_EID

ii) Adding a unique constraint:


=========================
SQL> ALTER TABLE PARENT ADD CONSTRAINT UQ_ENAME UNIQUE(ENAME);

iii) Adding a check constraint:


==========================
SQL> ALTER TABLE PARENT ADD CONSTRAINT CHK_SAL CHECK(SAL>=15000);

- To view constraint name and check constraint conditional value of a column


of a particular table then we use a datadictionary is "user_constraints".

EX:
SQL> DESC USER_CONSTRAINTS;
SQL> SELECT CONSTRAINT_NAME,SEARCH_CONDITION
2 FROM USER_CONSTRAINTS WHERE TABLE_NAME='PARENT';

CONSTRAINT_NAME SEARCH_CONDITION
--------------------------------- -----------------------------------------
------
CHK_SAL SAL>=15000

iv) Adding NOT NULL constraint:


============================
syntax:
======
ALTER TABLE <TN> MODIFY <COLUMN NAME> <CONSTRAINT> <CONSTRAINT NAME> NOT NULL;

EX:
SQL> ALTER TABLE PARENT MODIFY ENAME CONSTRAINT NN_ENAME NOT NULL;

v) Adding Foreign key reference:


===========================
syntax:
======
ALTER TABLE <TN> ADD <CONSTRAINT> <CONSTRAINT NAME>
FOREIGN KEY(COMMON COLUMN OF CHILD TABLE) REFERENCES
<PARENT TABLE NAME>(PRIMARY KEY COLUMN OF PARENT TABLE)
ON DELETE CASCADE / ON DELETE SET NULL;

EX:
SQL> CREATE TABLE CHILD(DNAME VARCHAR2(10),EID NUMBER(4));

SQL> ALTER TABLE CHILD ADD CONSTRAINT FK_EID


2 FOREIGN KEY(EID)REFERENCES PARENT(EID)
3 ON DELETE CASCADE;

You might also like