Oracle11am 15 17 Feb
Oracle11am 15 17 Feb
========================
- 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>,..................);
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
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.
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));
17-02-2024:
==========
- Once we establish a relationship between tables there are two rules are
come into picture.
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));
TESTING:
SQL> DELETE FROM DEPT2 WHERE DNO=1;------ALLOWED
Ex:
SQL> CREATE TABLE DEPT23(DNO NUMBER(4) PRIMARY KEY,DNAME VARCHAR2(10));
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));
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
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
EX:
SQL> ALTER TABLE PARENT MODIFY ENAME CONSTRAINT NN_ENAME NOT NULL;
EX:
SQL> CREATE TABLE CHILD(DNAME VARCHAR2(10),EID NUMBER(4));