0% found this document useful (0 votes)
68 views32 pages

SQL Constraints: - : Syntax

Constraints are used to limit the type of data that can be inserted into database tables. They can be defined at the column level or table level. Common constraint types include NOT NULL, UNIQUE, PRIMARY KEY. NOT NULL ensures columns cannot contain NULL values. UNIQUE prevents duplicate values across rows. PRIMARY KEY combines NOT NULL and UNIQUE such that columns cannot contain NULL and values must be unique. Constraints help maintain data integrity.

Uploaded by

Irfan Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
68 views32 pages

SQL Constraints: - : Syntax

Constraints are used to limit the type of data that can be inserted into database tables. They can be defined at the column level or table level. Common constraint types include NOT NULL, UNIQUE, PRIMARY KEY. NOT NULL ensures columns cannot contain NULL values. UNIQUE prevents duplicate values across rows. PRIMARY KEY combines NOT NULL and UNIQUE such that columns cannot contain NULL and values must be unique. Constraints help maintain data integrity.

Uploaded by

Irfan Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 32

SQL Constraints:

---------------

 Constraints are used to limit the type of data that can go into a table. This ensures the

accuracy and reliability of the data in the table.

 Constraints can be specified when the table is created with the CREATE TABLE statement, or
after the table is created with the ALTER TABLE statement.

 Constraints can be column level or table level. Column level constraints apply to a column,

and table level constraints apply to the whole table.

COLUMN LEVEL:

------------------------

In this method we are defining constraints in individual columns i.e whenever we are defining
column than only we are specifying constraints type.

Syntax:

---------

Create table Table_Name

col1 datatype(size) constraint_type,

col2 datatype(size) constraint_type

);

TABLE LEVEL:

-----------------

In this method we are defining constraints after declaring all column , In this method we are defining
constraints on group of column.

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


NOT NULL CONSTRAINT:

------------------------------------

 It won’t allow NULL value to a column.


 Not Null should be define only at column level.
 Not null constraint is supported for an attribute
 whose data type is LOB ,REF, VARRAY.
 In all database NOT NULL does not support table level.

SYNTAX:

create table <table_name>

column_name1 <data_type>(size) not null

column_name2 <data_type>(size)

);

or

create table <table_name>

column_name1 <data_type>(size) constraint constraint_name not null

column_name2 <data_type>(size)

);

SQL> drop table test_null;

Table dropped.

SQL> create table test_null

Sno number(10) not null

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


);

Table created.

SQL> insert into test_null

values(10);

1 row created.

SQL> insert into test_null

values(null);

values(null)

ERROR at line 2:

ORA-01400: cannot insert NULL into ("SCOTT"."TEST_NULL"."SNO")

SQL> insert into test_null

values(10);

1 row created.

**Duplicate Value can insert into Not null constraint.

Assign User defined names to constraints:

-------------------------------------------------------

In all DB whenever we are creating constraints then DB server automatically generates an

unique identification number in the format of sys_c this is called server define constraint

name, Instead of server define constraint name we can also create our own constraint name by

below syntax.

SYNTAX:

CONSTRAINT CONSTRAINT_NAME CONSTRAINT_TYPE ;

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


Creating NOT NULL constraint by giving Constraint Name.

Drop table test_null;

create table test_null

Sno number(10) constraint test_null_sno_nn not null

);

Create Constraint With Same Name.

------------------------------------------------------

create table test_null_dup

sno1 number(10) constraint test_null_sno_nn not null

);

o/p:ERROR at line 3:

ORA-02264: name already used by an existing constraint

*Can’t create same constraint name .

Create NOT NULL constraint with composite:

DROP TABLE test_null_dup;

Create table test_null_dup

sno1 number(10) ,

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


sno2 number(10) ,

sno3 number(10) ,

CONSTRAINT test_null_dup_SNO_UQ NOT NULL(SNO1,SNO2)

);

O/P:

ERROR at line 6:

ORA-00904: : invalid identifier

**Composite not null not allowed.

UNIQUE CONSTRAINT:

-----------------------------

 It won’t allow duplicate record .


 Unique Constraint can’t be implemented on LOB,LONG,LONG RAW.
 A composite UNIQUE KEY can’t have more than 32 columns.
 Same column or combination of columns can’t be designated as both primary key
and unique key.
 A composite UNIQUE key is always declared at the table level.

SQL>

Create table test_uq1

Sno number(10) unique

);

Table created.

SQL> insert into test_uq1

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


values(10);

1 row created.

SQL> insert into test_uq1

values(20);

1 row created.

SQL> insert into test_uq

values(20);

insert into test_uq

ERROR at line 1:

ORA-00001: unique constraint (SCOTT.SYS_C0015367) violated

SQL> insert into test_uq

values(30);

1 row created.

SQL> insert into test_uq

values(null);

1 row created.

** Null can insert in unique constraint.

SQL> insert into test_uq

values(null);

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


1 row created.

** Multiple null can insert in unique constraint.

Creating UNIQUE constraint by giving Constraint Name.

--------------------------------------------------------------------------

DROP TABLE TEST_UQ;

Create table test_uq

sno1 number(10) unique,

sno2 number(10) ,

sno3 number(10) unique,

sno4 number(10),

sno5 number(10) unique

);

insert into test_uq

values(1,2,3,4,5);

o/p: inserted

insert into test_uq

values(8 ,2,3,6,7);

op:

ERROR at line 1:

ORA-00001: unique constraint (SCOTT.SYS_C0017083) violated

insert into test_uq

values(1,2,4);

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


**From above error message we can’t recognise which constraint is throwing error.

because oracle internally assigned a constraint name.

DROP TABLE TEST_UQ;

Create table test_uq

sno1 number(10) constraint test_uq_sno1_uq unique ,

sno2 number(10) ,

sno3 number(10) constraint test_uq_sno3_uq unique

);

insert into test_uq

values(1,2,3);

o/p: inserted

insert into test_uq

values(1,2,4);

o/p:ERROR at line 1:

ORA-00001: unique constraint (SCOTT.TEST_UQ_SNO3N) violated

TABLE LEVEL SYNTAX:

DROP TABLE TEST_UQ;

Create table test_uq

sno1 number(10),

sno2 number(10),

sno3 number(10),

CONSTRAINT TEST_UQ_SNO_UQ unique(SNO1)

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


);

COMPOSITE UNIQUE CONSTRAINT:

-----------------------------

DROP TABLE TEST_UQ;

Create table test_uq

sno1 number(10),

sno2 number(10),

sno3 number(10),

CONSTRAINT TEST_UQ_SNO_UQ unique(SNO1,SNO2 )

);

insert into test_uq

values(1,2,3);

insert into test_uq

values(1,3,3)

insert into test_uq

values(2,3,3);

CREATE TABLE STUD_INFO

ROLL_NUM NUMBER(10) UNIQUE ,

PASSOUT_YEAR NUMBER(5),

NAME VARCHAR2(10)

INSERT INTO STUD_INFO

VALUES(101, 2020 , 'KING' ) ;

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


INSERT INTO STUD_INFO

VALUES(102, 2020 , 'SCOTT' ) ;

INSERT INTO STUD_INFO

VALUES(103, 2020 , 'MILLER' ) ;

INSERT INTO STUD_INFO

VALUES(104, 2020 , 'RAJ' ) ;

INSERT INTO STUD_INFO

VALUES(105, 2020 , 'JONES' ) ;

INSERT INTO STUD_INFO

VALUES(106, 2020 , 'KUMAR' ) ;

INSERT INTO STUD_INFO

VALUES(107, 2020 , 'VENKI' ) ;

INSERT INTO STUD_INFO

VALUES(108, 2020 , 'RAJ' ) ;

INSERT INTO STUD_INFO

VALUES(109, 2020 , 'VIRAT' ) ;

INITIALLY DEFERRED DEFERRABLE:

----------------------------------------------------

If we declared the constraints as "deferred", they are only checked at the commit point.

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


DROP TABLE TEST_DEFFER;

Create table TEST_DEFFER

sno1 number(10) UNIQUE INITIALLY DEFERRED DEFERRABLE ,

sno2 number(10),

sno3 number(10)

);

insert into TEST_DEFFER

values(1,2,3);

insert into TEST_DEFFER

values(1,3,3) ;

insert into TEST_DEFFER

values(2,3,3) ;

insert into TEST_DEFFER

values(2,3,3) ;

insert into TEST_DEFFER

values(2,2,3);

insert into TEST_DEFFER

values(3,2,3);

insert into TEST_DEFFER

values(4,2,3);

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


Create table test_uq

sno1 number(10) ,

sno2 number(10),

sno3 number(10),

CONSTRAINT TEST_UQ_SNO_UQ unique(SNO1,SNO2 )

);

PRIMARY KEY CONSTRAINT:

------------------------------------

 Primary key won’t allow NULL and duplicate to my table. So this is combination of NOT NULL
and UNIQUE.

 A table or view can have only one Primary key.

 Primary key can’t be implement on columns having LOB,LONG,LONG RAW,BFILE,


VARRY,NESTED TABLE.

 A composite pk cannot have more than 32 columns.

 The same column or combination columns cannot design both as primary key and unique
key.

drop table test_pk;

create table test_pk

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


snonumber(10) primary key

);

Table created.

insert into test_pk

values(10);

insert into test_pk

values(20);

insert into test_pk

values(20);

insert into test_pk

ERROR at line 1:

ORA-00001: unique constraint (SCOTT.SYS_C0015368) violated

SQL> insert into test_pk

values(null);

values(null)

ERROR at line 2:

ORA-01400: cannot insert NULL into ("SCOTT"."TEST_PK"."SNO")

**PRIMARY KEY AND UNIQUE WE CAN DECLARE IN TABLE LEVEL BUT NOT NULL WE CAN’T DECLARE

TABLE LEVEL.

SQL> drop table test_pk;

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


create table test_pk

snonumber(10),

name varchar2(10),

primary key(sno)

);

Table created.

SQL> drop table test_uq;

create table test_uq

Sno number(10),

name varchar2(10),

unique(sno)

);

Table created.

SQL> drop table test_null;

create table test_null

snonumber(10),

name varchar2(10),

not null(sno)

);

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


not null(sno)

ERROR at line 5:

ORA-00904: : invalid identifier

SQL> drop table test_null;

drop table test_null

ERROR at line 1:

ORA-00942: table or view does not exist

SQL> create table pk_test

id number(10) constraint pk_test_id_pk primary key ,

name varchar2(10)

);

Adding constraint to existing table :


----------------------------------------------

create table test

id number(10) ,

name varchar2(10)

);

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


Alter table test

Add constraint test_id_uq unique(id) ;

Op: Table created.

It is possible to add a NOT NULL constraint to an existing table by using


the ALTER TABLE statement?

NO

Alter table test


Add constraint test_name_uq not null(name) ;
**We cant add not null constraint , We can modify not null constraint.
Alter table test
modify (name not null);
FOREIGEN KEY:

-----------------------

 It is called as Integrity constraint.


 The table or view contain Foreign key is called as child object.
 A fk columns can’t be applied on lob,long, long raw, varray,bfile.
 A child and parent tables must be on the same data base.
 To delete parent record or to insert child record oracle provides

On delete set null and on delete cascade.

SQL> create table fk_test

id number(10) ,

name varchar2(10),

constraint fk_test_id_fk foreign key (id) references pk_test(id)

);

Table created.

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


SQL> create table Test1_PK

Id Number(20) Constraint Test1_PK_Id Primary Key,

Name Varchar2(20)

);

Table created.

Insert into PK_test

Values(10,'raj');

Insert into PK_test

Values(20,'king');

Insert into PK_test

Values(30,'scott');

SQL> select * from pk_test;

ID NAME

---------- ----------

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


10 raj

20 king

30 scott

SQL> commit;

Commit complete.

SQL> Insert into FK_test Values(10,'raj');

1 row created.

SQL> Insert into FK_test Values(40,'rajesh');

Insert into FK_test Values(40,'rajesh')

ERROR at line 1:

ORA-02291: integrity constraint (SCOTT.FK_TEST_ID_FK) violated - parent key not

found

SQL> Insert into pK_test

Values(40,'rajesh');

1 row created.

SQL> Insert into fK_test

Values(40,'rajesh');

1 row created.

SQL> select * from pk_test;

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


ID NAME

---------- ----------

10 raj

20 king

30 scott

40 rajesh

SQL> select * from fk_test;

ID NAME

---------- ----------

10 raj

40 rajesh

SQL> delete from pk_test

where id=10;

delete from pk_tes

ERROR at line 1:

ORA-00942: table or view does not exist

SQL> delete from pk_test

where id=10;

delete from pk_test

ERROR at line 1:

ORA-02292: integrity constraint (SCOTT.FK_TEST_ID_FK) violated - child record

found

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


SQL> delete from pk_test

where id=20;

ERROR at line 1:

ORA-02292: integrity constraint (SCOTT.FK_TEST_ID_FK) violated - child record

found

SQL> delete from fk_test

where id=10;

1 row deleted.

SQL> delete from pk_test

where id=10;

1 row deleted.

SQL> select * from pk_test;

ID NAME

---------- ----------

30 scott

40 rajesh

SQL> select * from fk_test;

ID NAME

---------- ----------

40 rajesh

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


SQL> set linesize120;

SQL> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

---------- ---------- --------- ---------- --------- ---------- ---------- ----------

7369 SMITH CLERK 7902 17-DEC-80 800 20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7566 JONES MANAGER 7839 02-APR-81 2975 20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7698 BLAKE MANAGER 7839 01-MAY-81 2850 30

7782 CLARK MANAGER 7839 09-JUN-81 2650 10

7788 SCOTT ANALYST 7566 19-APR-87 3000 20

7839 KING PRESIDENT 17-NOV-81 5200 10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7876 ADAMS CLERK 7788 23-MAY-87 1100 20

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

---------- ---------- --------- ---------- --------- ---------- ---------- ----------

7900 JAMES CLERK 7698 03-DEC-81 950 30

7902 FORD ANALYST 7566 03-DEC-81 3000 20

7934 MILLER CLERK 7782 23-JAN-82 1500 10

7938 SURESH CLERK 7698 10-SEP-19 4000 30

7977 SURESH CLERK 7698 10-SEP-19 2000 20

16 rows selected.

SQL> select * from dept;

DEPTNO DNAME LOC

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


---------- -------------- -------------

60 SALES BBSR

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON

50 SHIPPING KOLKATA

6 rows selected.

SQL> drop table sampleself01;

create table sampleself01

sampidnumber(10)

constraint sampleself01_sampid_pk primary key,

sampname varchar2(10),

sampidfknumber(10)

constraint sampleself01_sampidfk_fk references sampleself01(sampid)

);

Table created.

SQL> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

---------- ---------- --------- ---------- --------- ---------- ---------- ----------

7369 SMITH CLERK 7902 17-DEC-80 800 20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7566 JONES MANAGER 7839 02-APR-81 2975 20

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7698 BLAKE MANAGER 7839 01-MAY-81 2850 30

7782 CLARK MANAGER 7839 09-JUN-81 2650 10

7788 SCOTT ANALYST 7566 19-APR-87 3000 20

7839 KING PRESIDENT 17-NOV-81 5200 10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7876 ADAMS CLERK 7788 23-MAY-87 1100 20

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

---------- ---------- --------- ---------- --------- ---------- ---------- ----------

7900 JAMES CLERK 7698 03-DEC-81 950 30

7902 FORD ANALYST 7566 03-DEC-81 3000 20

7934 MILLER CLERK 7782 23-JAN-82 1500 10

7938 SURESH CLERK 7698 10-SEP-19 4000 30

7977 SURESH CLERK 7698 10-SEP-19 2000 20

16 rows selected.

SQL> insert into emp

2 values(1234,'mukesh','clerk',2345,sysdate,60000,900,80);

insert into emp

ERROR at line 1:

ORA-02291: integrity constraint (SCOTT.FK_DEPTNO) violated - parent key not found

SQL> insert into emp

2 values(1234,'mukesh','clerk',2345,sysdate,60000,900,30);

1 row created.

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


SQL> alter table emp

2 add constraint emp_mgr foreign key (mgr) references emp(empno);

add constraint emp_mgr foreign key (mgr) references emp(empno)

ERROR at line 2:

ORA-02298: cannot validate (SCOTT.EMP_MGR) - parent keys not found

SQL> rollback;

Rollback complete.

SQL> alter table emp

2 add constraint emp_mgr foreign key (mgr) references emp(empno);

add constraint emp_mgr foreign key (mgr) references emp(empno)

ERROR at line 2:

ORA-02298: cannot validate (SCOTT.EMP_MGR) - parent keys not found

SQL> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

---------- ---------- --------- ---------- --------- ---------- ---------- ----------

1234 mukesh clerk 2345 29-OCT-19 60000 900 30

7369 SMITH CLERK 7902 17-DEC-80 800 20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7566 JONES MANAGER 7839 02-APR-81 2975 20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7698 BLAKE MANAGER 7839 01-MAY-81 2850 30

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


7782 CLARK MANAGER 7839 09-JUN-81 2650 10

7788 SCOTT ANALYST 7566 19-APR-87 3000 20

7839 KING PRESIDENT 17-NOV-81 5200 10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

---------- ---------- --------- ---------- --------- ---------- ---------- ----------

7876 ADAMS CLERK 7788 23-MAY-87 1100 20

7900 JAMES CLERK 7698 03-DEC-81 950 30

7902 FORD ANALYST 7566 03-DEC-81 3000 20

7934 MILLER CLERK 7782 23-JAN-82 1500 10

7938 SURESH CLERK 7698 10-SEP-19 4000 30

7977 SURESH CLERK 7698 10-SEP-19 2000 20

17 rows selected.

SQL>delete from emp

2 whereempno=1234;

1 row deleted.

SQL> alter table emp

2 add constraint emp_mgr foreign key (mgr) references emp(empno);

Table altered.

SQL> insert into emp

2 values(1234,'mukesh','clerk',2345,sysdate,60000,900,30);

insert into emp

ERROR at line 1:

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


ORA-02291: integrity constraint (SCOTT.EMP_MGR) violated - parent key not found

SQL> insert into emp

2 values(2345,'mukesh','clerk',7977,sysdate,60000,900,30);

1 row created.

SQL> insert into emp

2 values(3456,'rakeshh','clerk',2345,sysdate,60000,900,30);

1 row created.

SQL> rollback;

Rollback complete.

DEFERRABLE CONSTRAINT:

--------- --------------------------------

Create table mymasterDF1

mastid number(10)

constraint mastidpk primary key

INITIALLY DEFERRED DEFERRABLE,

mastname varchar2(10)

constraint mastnamech check(mastname=upper(mastname) )

INITIALLY DEFERRED DEFERRABLE,

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


mastdate date

constraint mastdatenn not null

INITIALLY DEFERRED DEFERRABLE

);

insert into mymasterDF1

(mastid,mastname,mastdate)

values(11,'MASTER11','11-oct-07');

insert into mymasterDF1

(mastid,mastname,mastdate)

values(12,'MASTER12','12-oct-07');

insert into mymasterDF1

(mastid,mastname,mastdate)

values(13,'MASTER13','13-oct-07');

insert into mymasterDF1

(mastid,mastname,mastdate)

values(11,'MASTER11','11-oct-07');

Creating FK on UNIQUE

--------------------

Drop Table test_uq;

Create table test_uq

snonumber(10) constraint sno_uq unique

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


);

drop table test_fk;

Create table test_fk

snonumber(10) constraint test_fk_sno_fk references test_uq(sno)

);

Adding Constraint:

A Constraint can be added to a table at any time.

Drop Table test_uq;

Create table test_uq

snonumber(10)

);

Alter table test_uq

add constraint sno_uq unique(sno);

Dropping Constraint:

Alter table test_uq

drop constraint sno_uq ;

Dropping a constraint having Fk:

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


Alter table test_uq

drop constraint sno_uq cascade;

on delete set null vs on delete cascade:

----------------------------------------

create table samplepk01

sampidnumber(2)

constraint samplepk01_sampid_pk primary key,

sampname varchar2(15),

sampdate date

);

create table samplefk01

sampidnumber(2)

constraint samplefk01_sampid_pk primary key,

sampname varchar2(15),

sampdate date,

sampidfknumber(2)

constraint sampidfk_fk references samplepk01(sampid)

on delete set null

);

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


create table samplefk02

sampidnumber(2)

constraint samplefk02_sampid_pk primary key,

sampname varchar2(15),

sampdate date,

sampidfknumber(2)

constraint samplefk02_sampidfk_fk references samplepk01(sampid)

on delete cascade

);

begin

insert into samplepk01

(sampid,sampname,sampdate)

values(1,'sample1',sysdate);

insert into samplepk01

(sampid,sampname,sampdate)

values(2,'sample2',sysdate);

insert into samplepk01

(sampid,sampname,sampdate)

values(3,'sample3',sysdate);

insert into samplepk01

values(4,'sample04',sysdate);

insert into samplepk01

(sampid,sampname,sampdate)

values(5,'sample5',sysdate);

insert into samplepk01

(sampid,sampname,sampdate)

values(6,'sample6',sysdate);

commit;

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


end;

select * from samplepk01;

begin

insert into samplefk01

(sampid,sampname,sampdate,sampidfk)

values(11,'sample7',sysdate,3 );

insert into samplefk01

(sampid,sampname,sampdate,sampidfk)

values(12,'sample8',sysdate,3 );

insert into samplefk01

(sampid,sampname,sampdate,sampidfk)

values(13,'sample9',sysdate,2);

insert into samplefk01

(sampid,sampname,sampdate,sampidfk)

values(14,'sample9',sysdate,2 );

commit;

end;

begin

insert into samplefk02

(sampid,sampname,sampdate,sampidfk)

values(21,'sample10',sysdate,4 );

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060


insert into samplefk02

(sampid,sampname,sampdate,sampidfk)

values(22,'sample11',sysdate,4 );

insert into samplefk02

(sampid,sampname,sampdate,sampidfk)

values(23,'sample12',sysdate,5);

insert into samplefk02

(sampid,sampname,sampdate,sampidfk)

values(24,'sample13',sysdate,5);

insert into samplefk02

(sampid,sampname,sampdate,sampidfk)

values(25,'sample14',sysdate,6);

commit;

end;

delete from samplepk01

where sampid=2;

delete from samplepk01

where sampid=3;

BIBHU PRASAD TRIPATHY CONTACT NO: 9348993144, 7624815060

You might also like