Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
43 views2 pages

Select From Bank Select From Bank Order by Pin

The document creates two tables, bank and tbl_country. It defines constraints like primary keys and foreign keys on the tables. It inserts sample data and demonstrates integrity constraint violations when trying to delete parent records that have child records. It also shows an alternative way to define a composite primary key using multiple columns.

Uploaded by

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

Select From Bank Select From Bank Order by Pin

The document creates two tables, bank and tbl_country. It defines constraints like primary keys and foreign keys on the tables. It inserts sample data and demonstrates integrity constraint violations when trying to delete parent records that have child records. It also shows an alternative way to define a composite primary key using multiple columns.

Uploaded by

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

create table bank

(name varchar2(20), gender varchar2(1) check(gender in ('M','F','O')) , pin number(6,0));

select * from bank; select * from bank order by pin;


NAME GENDER PIN NAME GENDER PIN
ss F 12456 pts1 O 5296
ss1 F 12496 ss F 12456
ps1 M 12496 ss1 F 12496
pts1 O 52496 ps1 M 12496
pts1 O 5296 pts1 O 52496

select GENDER, count(GENDER) AS PERSON from bank group by gender;

GENDER PERSON

M 1

O 2

F 2

>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<

create table tbl_country

(c_id varchar2(10), c_name varchar2(40), c_std number (4,0));

alter table tbl_country add constraint pk_country primary key (c_id);


alter table tbl_country drop constraint pk_country;
drop table tbl_country;
create table tbl_country (c_id varchar2(10) primary key, c_name varchar2(40), c_std number
(4,0));
desc tbl_country;
Select * from user_constraints where TABLE_NAME=upper('tbl_country');
create table tbl_emp (id varchar2(10), name varchar2(40), address varchar2(40), pin
number(6,0), mobile number(10),country varchar2(10))
alter table tbl_emp add constraint pk_emp primary key(id);
ALTER TABLE tbl_emp ADD constraint fk_c FOREIGN KEY (country ) REFERENCES
tbl_country(c_id);
desc tbl_emp;
Select * from user_constraints where TABLE_NAME=upper('tbl_emp');
insert into tbl_emp (id,name,address, pin,mobile,country) values
('ea1','df','fsdf',123456,14005466,'IND');
insert into tbl_country (c_id,c_name,c_std) values ('IND','INDIA',033);

now again insert the 2nd before row


select * from tbl_country;
select * from tbl_emp;
delete from tbl_country where c_id='IND'

<see integrity constraint violation>

ALTER TABLE tbl_emp drop constraint fk_c;


ALTER TABLE tbl_emp ADD constraint fk_c FOREIGN KEY (country ) REFERENCES
tbl_country(c_id) on delete cascade;
delete from tbl_country where c_id='IND';

<see data deleted>

select * from tbl_emp;

another way for defining primary key

CREATE TABLE accounts (

acc_num INTEGER,

acc_type INTEGER,

acc_descr CHAR(20), constraint pk_acc

PRIMARY KEY (acc_num, acc_type));

You might also like