You are on page 1of 4

Assignment-2

1) Creating customer table:

create table customer(

customer_id number constraint c_id primary key,

customer_name varchar2(20) constraint c_name not null,

customer_contactnumber char(10) constraint c_contactnumber unique );

2) Creating Order table:

create table Order_table(

order_id number constraint ord_id primary key;

order_details varchar2(50),

order_date varchar2(10),

customer_id number);

Adding foreign key to Order_table

alter table Order_table

add constraint customer_order_rel foreign key(customer_id)

references customer(customer_id);

3) Adding values to tables:

insert into customer values(1,'Ramya','7658935465');

insert into customer values(2,'chandana','7658935455');

insert into customer values(3,'sonu','7658935423');

insert into customer values(4,'suresh','7758935423');

insert into customer values(5,'ramesh','7658935413');

insert into Order_table values(1,'Veg Birayani','20/07/2021',5);

insert into Order_table values(2,'Chicken Birayani','20/07/2021',2);

insert into Order_table values(3,'Noodles','20/08/2021',3);


insert into Order_table values(4,'munchurian','20/09/2021',1);

insert into Order_table values(5,'Curd Rice','20/10/2021',4);

Voilating constraint condition:

insert into Order_table values(1,'Chicken Birayani','20/07/2021',2);

 Primary key constraint condition is voilated for order_id.

insert into customer values(1,'ramesh','678945367');

 Primary key constraint condition is voilated for customer_id.

insert into customer values(1,'ramesh','7658935465');

 Unique key constraint condition is voidated for customer_contactnumber.

4) Creating parent table:

create table parent_table(

parent_id number constraint parent_id primary key,

parent_name varchar2(20) constraint parent_name not null,

parent_contactnumber varchar2(20) constraint parent_contactnumber not null,

parents_noofchildrens number );

Creating children table:

create table children_table(

children_id number constraint children_id primary key,

children_name varchar2(20) constraint children_name not null,

parent_id number );

Adding foreign key to children_table:

alter table children_table

add constraint parent_child_rel foreign key(parent_id)

references parent_table(parent_id);

Adding values to tables:

insert into parent_table values(1,'Ramya','7658935465',2);

insert into parent_table values(2,'chandana','7658935455',1);

insert into parent_table values(3,'sonu','7658935423',0);


insert into parent_table values(4,'suresh','7758935423',1);

insert into parent_table values(5,'ramesh','7658935413',1);

insert into children_table values(1,'lakshmi',4);

insert into children_table values(2,'pavan',1);

insert into children_table values(3,'sonalika',5);

insert into children_table values(4,'tanish',2);

insert into children_table values(5,'bhavya',1);

Adding foreign key column to children_table with delete cascade,update cascade,delete set null
conditions:

When a delete occurs on the row from the parent table, it automatically delete the matching rows in
the child table.

alter table children_table

add constraint child_parent_id

foreign key(parent_id)

references parent _table (parent_id)

on delete cascade;

When a update occurs on the row from the parent table, it automatically update the matching rows
in the child table.

alter table children_table

add constraint child_parent_id

foreign key(parent_id)

references parent _table (parent_id)

on update cascade;

The foreign key column from the parent table will be set null when the children record gets deleted.

alter table children_table

add constraint child_parent_id

foreign key(parent_id)

references parent _table (parent_id)

on delete set null;


5) Truncate:

truncate table Order_table;


 The truncate table command deletes the data inside a order table, but not the table itself.
 TRUNCATE is a DDL(Data Definition Language)  command and is used to delete all the rows or tuples
from a table. Unlike the DELETE command, TRUNCATE command does not contain a WHERE clause. In
the TRUNCATE command, the transaction log for each deleted data page is recorded. Unlike the DELETE
command, the TRUNCATE command is fast. Like DELETE, we can rollback the data even after using the
TRUNCATE command. 

Delete:

delete from Order_table where order_id =1;


delete from Order_table;
 The DELETE command is used to delete existing records in a table.
 DELETE is a DML(Data Manipulation Language)  command and is used when we specify the row(tuple)
that we want to remove or delete from the table or relation. The DELETE command can contain a
WHERE clause. If WHERE clause is used with DELETE command then it remove or delete only those
rows(tuple) that satisfy the condition otherwise by default it removes all the tuples(rows) from the
table. 

6)a) select constraint_name,constraint_type from user_constraints;

CONSTRAINT_NAM
E CONSTRAINT_TYPE

C_CONTACTNUMBE
U
R
C_ID P
C_NAME C
ORD_ID P

b)select constraint_name,constraint_type from user_constraints

where table_name = 'customer';

CONSTRAINT_NAME CONSTRAINT_TYPE

C_NAME C
C_ID P
C_CONTACTNUMBER U

You might also like