create table city
(
city_id number(4),
city_name varchar2(30)
);
select * from city;
insert into city values (10,'Chennai');
insert into city values (20,'Pune');
insert into city values (30,'Hyd');
insert into city values (40,'Delhi');
select * from city;
alter table city add primary key(city_id);
create table customer
(
cust_id number(6) primary key,
cust_name varchar2(30) not null,
mobile_no number(10) unique check (length(mobile_no)=10),
age number(3) check (age>=18),
city_id number(4) references city(city_id) ---on delete cascade
);
select * from customer;
ALTER TABLE customer
drop column city_id;
select * from customer;
select * from city;
ALTER TABLE customer
ADD CONSTRAINT city_fk
FOREIGN KEY (city_id)
REFERENCES city(city_id)
ON DELETE CASCADE;
select * from customer;
insert into customer values (100000,'Arun',9090909090,28,20);
insert into customer values (100001,'Arun',8080808080,31,30);
insert into customer values (100002,'Vijay',5050505050,31,10);
insert into customer values (100004,'Mahi',8080808089,33,30);
--OCIStmtExecute: ORA-02290: check constraint (6559f23c6ffa.SYS_C00109612) violated
--insert into customer values (100003,'Ajith',2894738243,13,30);
--OCIStmtExecute: ORA-02291: integrity constraint (6559f23c6ffa.SYS_C00109615) violated - parent key not found
--insert into customer values (100004,'Ramesh',2894738789,31,60);
select * from customer;
select * from all_constraints
where table_name='CUSTOMER';
select * from all_cons_columns where table_name='CUSTOMER';
select
a.owner,a.constraint_name,a.constraint_type,b.table_name,b.column_name
from all_constraints a , all_cons_columns b where a.constraint_name=b.constraint_name
and a.table_name='CUSTOMER';
select * from customer;
select * from city;
delete from city where city_id= '30';
ALTER TABLE customer
drop column city_id;
select * from customer;
select * from city;
ALTER TABLE customer
ADD CONSTRAINT city_fk
FOREIGN KEY (CITY_ID) REFERENCES city(city_id) ON DELETE CASCADE;
select * from customer;