database management system
TRANSCRIPT
Dipanjan Sarkar 2010
Database Management System: A Database is a collection of inter-related data & a set of programs to access those data. The system which manages the database is called database management system.
Table CreationSyntax: - create table <table name> (attribute1 name data type(size), attribute2 name data type(size), attribute3 name data type(size)…….);
Example:- create table employee (name varchar2(20),father_name varchar2(20),city varchar2(15),sex char(1),salary number(5,2),dob date);
Value insertion in TableSyntax: - insert into <table name> values (values of attribute1, values of attribute2,values of attribute3,……..);
Example:- insert into employee values(‘Suman’, ‘Kalyan’, ‘Asansol’, ‘M’, 400000, ‘12-Nov-88’);
By simultaneous usage of this syntax, we can insert as many values we like.
Value insertion directly from the userSyntax: - insert into <table name> values (‘& attribute1 name’,‘& attribute2 name’….);Example:- insert into employee values (‘&name’,‘&father_name’,‘&city’, ‘&sex’, &salary,’&dob’);Enter value for name: SumanEnter value for f_name: KalyanEnter value for city: AsansolEnter value for sex: MEnter value for salary: 40000Enter value for date_of_birth: 12-Nov-88old 1: insert into employee values('&Name','&F_Name','&City','&Gender',&Salary,'&Date_of_Birth')new 1: insert into employees values(' Suman,' Kalyan,' Asansol ','M', 40000,'12-Nov-88')
To see all the tables in the databaseSyntax: - select * from tab;
TNAME TABTYPE CLUSTERID-------------------------------------------------------------------------------EMPLOYEE TABLE
To see a table structureSyntax: - desc <table name>;Example:- desc employee;
1
Dipanjan Sarkar 2010
Name Null? Type ----------------------------------------- -------- ---------------------------- NAME VARCHAR2(20) FATHER_NAME VARCHAR2(20) CITY VARCHAR2(15) SEX CHAR(1) SALARY NUMBER(5,2) DOB DATE
To see all values of a tableSyntax:- select * from <table name>;Example:- select * from employee;The output of the employee table will be asNAME FATHER_NAME CITY SEX SALARY DOB-------------------------------------------------------------------------------------------------------Samrat Mrinal Kolkata M 50000 16-DEC-88Romit Ashok Kolkata M 45000 12-JAN-85Anurupa Swapnil Delhi F 50000 16-MAY-88Tamal Raj Durgapur M 45000 02-APR-89Manish Abhay Raniganj F 20000 01-AUG-81Suman Kalyan Asansol M 40000 12-NOV-88
To save the values in the databaseSyntax:- commit;To save the values automatically the command is,Syntax:- set autocommit on;
To open a new editorSyntax:- ed <editor name>;Example:- ed xyz;To run the editor in the database the command isSyntax:- @ <editor name>;Example:- @ xyz;
To copy a table from another tableSyntax:- create table < new table name > as select * from < old table name >;Example:- create table employee1 as select * from employee;To copy selected contents from a tableSyntax:- create table <new table name> as select attribute1, attribute2,....from <old table name>;Example:- create table employee2 as select Name, Father_Name, Salary from employee;
Renaming the attributes in the new tableSyntax:- create table <new table name> (<new attributes name>) as select <old attribute name> from <old table name>;
2
Dipanjan Sarkar 2010
Example:- create table employee3 (emp_name, emp_father_name, emp_city) as select name,father_name,city from employee;
To delete the contents of the tableSyntax:- delete from <table name>; or truncate table <table name>;Here all rows are deleted. But the structure of the table remains in the database. To delete a particular row a particular condition is given.As for example: - delete from employee where name= ‘Samrat’;
To delete a table from the databaseSyntax:- drop table <table name>;Example: - drop table employee1;Here the table resides in the recycle bin. To completely remove the table from the system the Syntax is:- drop table <table name> purge;
To see the contents of the recycle binSyntax:- Show recycle bin;
To restore the deleted table from the recycle binSyntax:- flashback table <old table name> to before drop;Example:- flashback table employee4 to before drop;
3
Dipanjan Sarkar 2010
Questions for Assignment 2 ( Customer ) :
1. Create and insert given data in table customer2. Add column stay_from_year3. Set value of stay_from_year as 2001 for Italy\America and
2003 otherwise4. Display credit limit attribute for America5. Delete the record corresponding to Meg Sen6. Show credit limit attribute for all America7. Show all attributes for Italy8. If territory is India and status is Single set value of credit
to 70009. Rename cust_fname to first_name10. Rename cust_lname to last_name11. Create table cust1 with attributes id, f_name,
l_name, dob, income and insert into the table values from the old table customer
12. Create tables cust2,cust3 and cust4 without values of cust1
13. Drop column income from cust114. Rename cust1 to cust_one15. Insert values into cust2 table from customer table16. Insert values into cust3 table with attributes id,
f_name, l_name from customer table where income > 50000
17. In cust4 change cust id to varchar(6) and income to number(5)
18. Add new attribute mngr_name to cust4 and insert 5 records
19. Add attribute territory to cust420. Drop table cust3 and then bring it back
4
Dipanjan Sarkar 2010
SQL> ed file1.txt
SQL> create table customer (cust_id number(3),cust_fname varchar2(15),cust_lname varchar2(15),territory varchar2(10),cred_lmt number(9,2),mngr_id number(3),dob date,marital_status varchar2(10),sex char(1),income number(10,2));
Table created.
SQL> desc customer; Name Null? Type ----------------------------------------- -------- ---------------------------- CUST_ID NUMBER(3) CUST_FNAME VARCHAR2(15) CUST_LNAME VARCHAR2(15) TERRITORY VARCHAR2(10) CRED_LMT NUMBER(9,2) MNGR_ID NUMBER(3) DOB DATE MARITAL_STATUS VARCHAR2(10) GEN CHAR(1) INCOME NUMBER(10,2)
SQL> insert into customer values (&cust_id,'&cust_fname','&cust_lname','&territory',&cred_lmt, &mngr_id,'&dob', '&marital_status','&sex',&income)
Enter value for cust_id: 147Enter value for f_name: AishwaryaEnter value for l_name: RobertsEnter value for terr: AMERICAEnter value for c_lmt: 600.34Enter value for mgr_id: 147Enter value for dob: 01-MAR-44Enter value for m_status: singleEnter value for gen: FEnter value for income: 130000old 1: insert into customer values(&cust_id,'&f_name','&l_name','&terr',&c_lmt,&mgr_id,'&dob','&m_new 1: insert into customer values(147,'Aishwarya','Roberts','AMERICA',600.34,147,'01-MAR-44','sin
1 row created.
SQL> /Enter value for cust_id: 148Enter value for f_name: GustavEnter value for l_name: SteelEnter value for terr: AMERICAEnter value for c_lmt: 600Enter value for mgr_id: 148Enter value for dob: 10-APR-50Enter value for m_status: marriedEnter value for gen: MEnter value for income: 110000old 1: insert into customer values(&cust_id,'&f_name','&l_name','&terr',&c_lmt,&mgr_id,'&dob','&m_new 1: insert into customer values(148,'Gustav','Steel','AMERICA',600,148,'10-APR-50','married','M
5
Dipanjan Sarkar 2010
1 row created.
SQL> /Enter value for cust_id: 352Enter value for f_name: KenEnter value for l_name: ReidEnter value for terr: ITALYEnter value for c_lmt: 3600.65Enter value for mgr_id: 147Enter value for dob: 13-JULY-71Enter value for m_status: singleEnter value for gen: MEnter value for income: 30000old 1: insert into customer values(&cust_id,'&f_name','&l_name','&terr',&c_lmt,&mgr_id,'&dob','&m_new 1: insert into customer values(352,'Ken','Reid','ITALY',3600.65,147,'13-JULY-71','single','M',
1 row created.
SQL> /Enter value for cust_id: 360Enter value for f_name: HelmetEnter value for l_name: CapshawEnter value for terr: CHINAEnter value for c_lmt: 3600.65Enter value for mgr_id: 148Enter value for dob: 01-AUG-77Enter value for m_status: marriedEnter value for gen: MEnter value for income: 190000old 1: insert into customer values(&cust_id,'&f_name','&l_name','&terr',&c_lmt,&mgr_id,'&dob','&m_new 1: insert into customer values(360,'Helmet','Capshaw','CHINA',3600.65,148,'01-AUG-77','married
1 row created.
SQL> /Enter value for cust_id: 363Enter value for f_name: CathyEnter value for l_name: LambertEnter value for terr: ITALYEnter value for c_lmt: 2400Enter value for mgr_id: 147Enter value for dob: 31-AUG-56Enter value for m_status: marriedEnter value for gen: MEnter value for income: 50000old 1: insert into customer values(&cust_id,'&f_name','&l_name','&terr',&c_lmt,&mgr_id,'&dob','&m_new 1: insert into customer values(363,'Cathy','Lambert','ITALY',2400,147,'31-AUG-56','married','M
1 row created.
SQL> /Enter value for cust_id: 378Enter value for f_name: MegEnter value for l_name: SenEnter value for terr: THAILAND
6
Dipanjan Sarkar 2010
Enter value for c_lmt: 3700.78Enter value for mgr_id: 159Enter value for dob: 30-SEP-55Enter value for m_status: marriedEnter value for gen: MEnter value for income: 50000old 1: insert into customer values(&cust_id,'&f_name','&l_name','&terr',&c_lmt,&mgr_id,'&dob','&m_new 1: insert into customer values(378,'Meg','Sen','THAILAND',3700.78,159,'30-SEP-55','married','M
1 row created.
SQL> /Enter value for cust_id: 380Enter value for f_name: MerylEnter value for l_name: HoldenEnter value for terr: INDIAEnter value for c_lmt: 3700Enter value for mgr_id: 148Enter value for dob: 10-OCT-61Enter value for m_status: singleEnter value for gen: FEnter value for income: 160000old 1: insert into customer values(&cust_id,'&f_name','&l_name','&terr',&c_lmt,&mgr_id,'&dob','&m_new 1: insert into customer values(380,'Meryl','Holden','INDIA',3700,148,'10-OCT-61','single','F',
1 row created.
SQL> /Enter value for cust_id: 447Enter value for f_name: RichardEnter value for l_name: CappolaEnter value for terr: ITALYEnter value for c_lmt: 500.76Enter value for mgr_id: 147Enter value for dob: 30-OCT-81Enter value for m_status: marriedEnter value for gen: FEnter value for income: 50000old 1: insert into customer values(&cust_id,'&f_name','&l_name','&terr',&c_lmt,&mgr_id,'&dob','&m_new 1: insert into customer values(447,'Richard','Cappola','ITALY',500.76,147,'30-OCT-81','married
1 row created.
SQL> /Enter value for cust_id: 449Enter value for f_name: RickEnter value for l_name: RomeroEnter value for terr: ITALYEnter value for c_lmt: 1500Enter value for mgr_id: 147Enter value for dob: 10-DEC-51Enter value for m_status: singleEnter value for gen: FEnter value for income: 30000
7
Dipanjan Sarkar 2010
old 1: insert into customer values(&cust_id,'&f_name','&l_name','&terr',&c_lmt,&mgr_id,'&dob','&m_new 1: insert into customer values(449,'Rick','Romero','ITALY',1500,147,'10-DEC-51','single','F',3
1 row created.
SQL> /Enter value for cust_id: 451Enter value for f_name: RidleyEnter value for l_name: HackmanEnter value for terr: ITALYEnter value for c_lmt: 700.59Enter value for mgr_id: 147Enter value for dob: 11-JAN-50Enter value for m_status: singleEnter value for gen: FEnter value for income: 150000old 1: insert into customer values(&cust_id,'&f_name','&l_name','&terr',&c_lmt,&mgr_id,'&dob','&m_new 1: insert into customer values(451,'Ridley','Hackman','ITALY',700.59,147,'11-JAN-50','single',
1 row created.
SQL> /Enter value for cust_id: 454Enter value for f_name: RobEnter value for l_name: RussellEnter value for terr: INDIAEnter value for c_lmt: 5000Enter value for mgr_id: 148Enter value for dob: 02-MAR-77Enter value for m_status: marriedEnter value for gen: MEnter value for income: 90000old 1: insert into customer values(&cust_id,'&f_name','&l_name','&terr',&c_lmt,&mgr_id,'&dob','&m_new 1: insert into customer values(454,'Rob','Russell','INDIA',5000,148,'02-MAR-77','married','M',
1 row created.
SQL> /Enter value for cust_id: 458Enter value for f_name: RobertEnter value for l_name: De niroEnter value for terr: INDIAEnter value for c_lmt: 3700Enter value for mgr_id: 148Enter value for dob: 12-MAR-86Enter value for m_status: singleEnter value for gen: FEnter value for income: 150000old 1: insert into customer values(&cust_id,'&f_name','&l_name','&terr',&c_lmt,&mgr_id,'&dob','&m_new 1: insert into customer values(458,'Robert','De niro','INDIA',3700,148,'12-MAR-86','single','F
1 row created.
8
Dipanjan Sarkar 2010
SQL> /Enter value for cust_id: 463Enter value for f_name: RobinEnter value for l_name: AdjaniEnter value for terr: INDIAEnter value for c_lmt: 1500Enter value for mgr_id: 148Enter value for dob: 01-MAR-48Enter value for m_status: marriedEnter value for gen: FEnter value for income: 50000old 1: insert into customer values(&cust_id,'&f_name','&l_name','&terr',&c_lmt,&mgr_id,'&dob','&m_new 1: insert into customer values(463,'Robin','Adjani','INDIA',1500,148,'01-MAR-48','married','F'
1 row created.
SQL> show pagesizepagesize 14SQL> set pagesize 25;SQL> select *from customer;
CUST_ID CUST_FNAME CUST_LNAME TERR C_LMT MGR_ID DOB M_STATUS G INC---------- --------------- --------------- ---------- ---------- ---------- --------- -------- - ---------- 147 Aishwarya Roberts AMERICA 600.34 147 01-MAR-44 single F 130000 148 Gustav Steel AMERICA 600 148 10-APR-50 married M 110000 352 Ken Reid ITALY 3600.65 147 13-JUL-71 single M 30000 360 Helmet Capshaw CHINA 3600.65 148 01-AUG-77 married M 190000 363 Cathy Lambert ITALY 2400 147 31-AUG-56 married M 50000 378 Meg Sen THAILAND 3700.78 159 30-SEP-55 married M 50000 380 Meryl Holden INDIA 3700 148 10-OCT-61 single F 160000 447 Richard Cappola ITALY 500.76 147 30-OCT-81 married F 50000 449 Rick Romero ITALY 1500 147 10-DEC-51 single F 30000 451 Ridley Hackman ITALY 700.59 147 11-JAN-50 single F 150000 454 Rob Russell INDIA 5000 148 02-MAR-77 married M 90000 458 Robert De niro INDIA 3700 148 12-MAR-86 single F 150000 463 Robin Adjani INDIA 1500 148 01-MAR-48 married F 50000
SQL> ALTER TABLE CUSTOMER ADD STAY_FROM_YEAR NUMBER(4);
Table altered.
SQL> update customer set stay_from_year = 2003 where terr = 'INDIA' or terr = 'CHINA' or terr = 'THAILAND';
9
Dipanjan Sarkar 2010
6 rows updated.
SQL> update customer set stay_from_year = 2001 where terr = 'AMERICA' or terr = 'ITALY';
7 rows updated.
SQL> select *from customer;
CUST_ID CUST_FNAME CUST_LNAME TERR C_LMT MGR_ID DOB M_STATUS G INC STAY_FROM_YEAR---------- --------------- --------------- ---------- ---------- ---------- --------- -------- - ---------- ------------ 147 Aishwarya Roberts AMERICA 600.34 147 01-MAR-44 single F 130000 2001 148 Gustav Steel AMERICA 600 148 10-APR-50 married M 110000 2001 352 Ken Reid ITALY 3600.65 147 13-JUL-71 single M 30000 2001 360 Helmet Capshaw CHINA 3600.65 148 01-AUG-77 married M 190000 2003 363 Cathy Lambert ITALY 2400 147 31-AUG-56 married M 50000 2001 378 Meg Sen THAILAND 3700.78 159 30-SEP-55 married M 50000 2003 380 Meryl Holden INDIA 3700 148 10-OCT-61 single F 160000 2003 447 Richard Cappola ITALY 500.76 147 30-OCT-81 married F 50000 2001 449 Rick Romero ITALY 1500 147 10-DEC-51 single F 30000 2001 451 Ridley Hackman ITALY 700.59 147 11-JAN-50 single F 150000 2001 454 Rob Russell INDIA 5000 148 02-MAR-77 married M 90000 2003 458 Robert De niro INDIA 3700 148 12-MAR-86 single F 150000 2003 463 Robin Adjani INDIA 1500 148 01-MAR-48 married F 50000 2003
SQL> commit;
Commit complete.
SQL> select c_lmt from customer where terr='AMERICA';
C_LMT---------- 600.34 600
SQL> select *from customer where terr='ITALY' 2 ;
10
Dipanjan Sarkar 2010
CUST_ID CUST_FNAME CUST_LNAME TERR C_LMT MGR_ID DOB M_STATUS G INC STAY_FROM_YEAR---------- --------------- --------------- ---------- ---------- ---------- --------- -------- - ---------- ------------ 352 Ken Reid ITALY 3600.65 147 13-JUL-71 single M 30000 2001 363 Cathy Lambert ITALY 2400 147 31-AUG-56 married M 50000 2001 447 Richard Cappola ITALY 500.76 147 30-OCT-81 married F 50000 2001 449 Rick Romero ITALY 1500 147 10-DEC-51 single F 30000 2001 451 Ridley Hackman ITALY 700.59 147 11-JAN-50 single F 150000 2001
SQL> delete from customer where cust_fname='Meg';
1 row deleted.
SQL> select *from customer;
CUST_ID CUST_FNAME CUST_LNAME TERR C_LMT MGR_ID DOB M_STATUS G INC STAY_FROM_YEAR---------- --------------- --------------- ---------- ---------- ---------- --------- -------- - ---------- ------------ 147 Aishwarya Roberts AMERICA 600.34 147 01-MAR-44 single F 130000 2001 148 Gustav Steel AMERICA 600 148 10-APR-50 married M 110000 2001 352 Ken Reid ITALY 3600.65 147 13-JUL-71 single M 30000 2001 360 Helmet Capshaw CHINA 3600.65 148 01-AUG-77 married M 190000 2003 363 Cathy Lambert ITALY 2400 147 31-AUG-56 married M 50000 2001 380 Meryl Holden INDIA 3700 148 10-OCT-61 single F 160000 2003 447 Richard Cappola ITALY 500.76 147 30-OCT-81 married F 50000 2001 449 Rick Romero ITALY 1500 147 10-DEC-51 single F 30000 2001 451 Ridley Hackman ITALY 700.59 147 11-JAN-50 single F 150000 2001 454 Rob Russell INDIA 5000 148 02-MAR-77 married M 90000 2003 458 Robert De niro INDIA 3700 148 12-MAR-86 single F 150000 2003 463 Robin Adjani INDIA 1500 148 01-MAR-48 married F 50000 2003
12 rows selected.
SQL> update customer set c_lmt=7000 where terr='INDIA' and m_status='single';
2 rows updated.
11
Dipanjan Sarkar 2010
CUST_ID CUST_FNAME CUST_LNAME TERR C_LMT MGR_ID DOB M_STATUS G INC STAY_FROM_YEAR---------- --------------- --------------- ---------- ---------- ---------- --------- -------- - ---------- ------------ 147 Aishwarya Roberts AMERICA 600.34 147 01-MAR-44 single F 130000 2001 148 Gustav Steel AMERICA 600 148 10-APR-50 married M 110000 2001 352 Ken Reid ITALY 3600.65 147 13-JUL-71 single M 30000 2001 360 Helmet Capshaw CHINA 3600.65 148 01-AUG-77 married M 190000 2003 363 Cathy Lambert ITALY 2400 147 31-AUG-56 married M 50000 2001 380 Meryl Holden INDIA 7000 148 10-OCT-61 single F 160000 2003 447 Richard Cappola ITALY 500.76 147 30-OCT-81 married F 50000 2001 449 Rick Romero ITALY 1500 147 10-DEC-51 single F 30000 2001 451 Ridley Hackman ITALY 700.59 147 11-JAN-50 single F 150000 2001 454 Rob Russell INDIA 5000 148 02-MAR-77 married M 90000 2003 458 Robert De niro INDIA 7000 148 12-MAR-86 single F 150000 2003 463 Robin Adjani INDIA 1500 148 01-MAR-48 married F 50000 2003
12 rows selected.
SQL> alter table customer rename column cust_fname to first_name;
Table altered.
SQL> select *from customer;
CUST_ID FIRST_NAME CUST_LNAME TERR C_LMT MGR_ID DOB M_STATUS G INC STAY_FROM_YEAR---------- --------------- --------------- ---------- ---------- ---------- --------- -------- - ---------- ------------ 147 Aishwarya Roberts AMERICA 600.34 147 01-MAR-44 single F 130000 2001 148 Gustav Steel AMERICA 600 148 10-APR-50 married M 110000 2001 352 Ken Reid ITALY 3600.65 147 13-JUL-71 single M 30000 2001 360 Helmet Capshaw CHINA 3600.65 148 01-AUG-77 married M 190000 2003 363 Cathy Lambert ITALY 2400 147 31-AUG-56 married M 50000 2001 380 Meryl Holden INDIA 7000 148 10-OCT-61 single F 160000 2003 447 Richard Cappola ITALY 500.76 147 30-OCT-81 married F 50000 2001 449 Rick Romero ITALY 1500 147 10-DEC-51 single F 30000 2001 451 Ridley Hackman ITALY 700.59 147 11-JAN-50 single F 150000 2001 454 Rob Russell INDIA 5000 148 02-MAR-77 married M 90000 2003
12
Dipanjan Sarkar 2010
458 Robert De niro INDIA 7000 148 12-MAR-86 single F 150000 2003 463 Robin Adjani INDIA 1500 148 01-MAR-48 married F 50000 2003
12 rows selected.
SQL> alter table customer rename column cust_lname to last_name;
Table altered.
SQL> select *from customer;
CUST_ID FIRST_NAME LAST_NAME TERR C_LMT MGR_ID DOB M_STATUS G INC STAY_FROM_YEAR---------- --------------- --------------- ---------- ---------- ---------- --------- -------- - ---------- ------------ 147 Aishwarya Roberts AMERICA 600.34 147 01-MAR-44 single F 130000 2001 148 Gustav Steel AMERICA 600 148 10-APR-50 married M 110000 2001 352 Ken Reid ITALY 3600.65 147 13-JUL-71 single M 30000 2001 360 Helmet Capshaw CHINA 3600.65 148 01-AUG-77 married M 190000 2003 363 Cathy Lambert ITALY 2400 147 31-AUG-56 married M 50000 2001 380 Meryl Holden INDIA 7000 148 10-OCT-61 single F 160000 2003 447 Richard Cappola ITALY 500.76 147 30-OCT-81 married F 50000 2001 449 Rick Romero ITALY 1500 147 10-DEC-51 single F 30000 2001 451 Ridley Hackman ITALY 700.59 147 11-JAN-50 single F 150000 2001 454 Rob Russell INDIA 5000 148 02-MAR-77 married M 90000 2003 458 Robert De niro INDIA 7000 148 12-MAR-86 single F 150000 2003 463 Robin Adjani INDIA 1500 148 01-MAR-48 married F 50000 2003
12 rows selected.
SQL> ttitle Dipanjan SarkarSQL> btitle 17-Aug-2010SQL> select *from customer;
DipanjanSarkar CUST_ID FIRST_NAME LAST_NAME TERR C_LMT MGR_ID DOB M_STATUS G INC STAY_FROM_YEAR ---------- --------------- --------------- ---------- ---------- ---------- --------- -------- - ---------- -------------- 147 Aishwarya Roberts AMERICA 600.34 147 01-MAR-44 single F 130000 2001 148 Gustav Steel AMERICA 600 148 10-APR-50 married M 110000 2001 352 Ken Reid ITALY 3600.65 147 13-JUL-71 single M 30000 2001
13
Dipanjan Sarkar 2010
360 Helmet Capshaw CHINA 3600.65 148 01-AUG-77 married M 190000 2003 363 Cathy Lambert ITALY 2400 147 31-AUG-56 married M 50000 2001 380 Meryl Holden INDIA 7000 148 10-OCT-61 single F 160000 2003 447 Richard Cappola ITALY 500.76 147 30-OCT-81 married F 50000 2001 449 Rick Romero ITALY 1500 147 10-DEC-51 single F 30000 2001 451 Ridley Hackman ITALY 700.59 147 11-JAN-50 single F 150000 2001 454 Rob Russell INDIA 5000 148 02-MAR-77 married M 90000 2003 458 Robert De niro INDIA 7000 148 12-MAR-86 single F 150000 2003 463 Robin Adjani INDIA 1500 148 01-MAR-48 married F 50000 2003
17-Aug-2010
12 rows selected.
SQL> create table cust1 (cust_id number(3),f_name varchar2(15),l_name varchar2(15),dob date,income number(10,2));
Table created.
SQL> insert into cust1 select cust_id,first_name,last_name,dob,inc from customer;
12 rows created.
SQL> set autocommit on;SQL> select *from cust1;
DipanjanSarkar CUST_ID F_NAME L_NAME DOB INCOME ---------- --------------- --------------- --------- ---------- 147 Aishwarya Roberts 01-MAR-44 130000 148 Gustav Steel 10-APR-50 110000 352 Ken Reid 13-JUL-71 30000 360 Helmet Capshaw 01-AUG-77 190000 363 Cathy Lambert 31-AUG-56 50000 380 Meryl Holden 10-OCT-61 160000 447 Richard Cappola 30-OCT-81 50000 449 Rick Romero 10-DEC-51 30000 451 Ridley Hackman 11-JAN-50 150000 454 Rob Russell 02-MAR-77 90000 458 Robert De niro 12-MAR-86 150000 463 Robin Adjani 01-MAR-48 50000
17-Aug-2010
12 rows selected.
14
Dipanjan Sarkar 2010
SQL> desc cust1; Name Null? Type ------------------------ -------- ----------------------- CUST_ID NUMBER(3) F_NAME VARCHAR2(15) L_NAME VARCHAR2(15) DOB DATE INCOME NUMBER(10,2)
SQL> create table cust2 (cust_id number(3),f_name varchar2(15),l_name varchar2(15),dob date,income number(10,2));
Table created.
SQL> create table cust3 (cust_id number(3),f_name varchar2(15),l_name varchar2(15),dob date,income number(10,2));
Table created.
SQL> create table cust4 (cust_id number(3),f_name varchar2(15),l_name varchar2(15),dob date,income number(10,2));
Table created.
SQL> alter table cust1 drop column income;
Table altered.
SQL> select *from cust1;
DipanjanSarkar CUST_ID F_NAME L_NAME DOB ---------- --------------- --------------- --------- 147 Aishwarya Roberts 01-MAR-44 148 Gustav Steel 10-APR-50 352 Ken Reid 13-JUL-71 360 Helmet Capshaw 01-AUG-77 363 Cathy Lambert 31-AUG-56 380 Meryl Holden 10-OCT-61 447 Richard Cappola 30-OCT-81 449 Rick Romero 10-DEC-51 451 Ridley Hackman 11-JAN-50 454 Rob Russell 02-MAR-77 458 Robert De niro 12-MAR-86 463 Robin Adjani 01-MAR-48
17-Aug-2010
12 rows selected.
SQL> alter table cust1 rename to cust_one;
Table altered.
SQL> select *from cust_one;
15
Dipanjan Sarkar 2010
DipanjanSarkar CUST_ID F_NAME L_NAME DOB ---------- --------------- --------------- --------- 147 Aishwarya Roberts 01-MAR-44 148 Gustav Steel 10-APR-50 352 Ken Reid 13-JUL-71 360 Helmet Capshaw 01-AUG-77 363 Cathy Lambert 31-AUG-56 380 Meryl Holden 10-OCT-61 447 Richard Cappola 30-OCT-81 449 Rick Romero 10-DEC-51 451 Ridley Hackman 11-JAN-50 454 Rob Russell 02-MAR-77 458 Robert De niro 12-MAR-86 463 Robin Adjani 01-MAR-48
17-Aug-2010
12 rows selected.
SQL> insert into cust2 select cust_id,first_name,last_name,dob,inc from customer;
12 rows created.
Commit complete.SQL> select *from cust2;
DipanjanSarkar CUST_ID F_NAME L_NAME DOB INCOME ---------- --------------- --------------- --------- ---------- 147 Aishwarya Roberts 01-MAR-44 130000 148 Gustav Steel 10-APR-50 110000 352 Ken Reid 13-JUL-71 30000 360 Helmet Capshaw 01-AUG-77 190000 363 Cathy Lambert 31-AUG-56 50000 380 Meryl Holden 10-OCT-61 160000 447 Richard Cappola 30-OCT-81 50000 449 Rick Romero 10-DEC-51 30000 451 Ridley Hackman 11-JAN-50 150000 454 Rob Russell 02-MAR-77 90000 458 Robert De niro 12-MAR-86 150000 463 Robin Adjani 01-MAR-48 50000
17-Aug-2010
12 rows selected.
SQL> insert into cust3 (cust_id,f_name,l_name) select cust_id,first_name,last_name from customer where inc>50000;
7 rows created.
Commit complete.SQL> select *from cust3;
16
Dipanjan Sarkar 2010
DipanjanSarkar CUST_ID F_NAME L_NAME DOB INCOME ---------- --------------- --------------- --------- ---------- 147 Aishwarya Roberts 148 Gustav Steel 360 Helmet Capshaw 380 Meryl Holden 451 Ridley Hackman 454 Rob Russell 458 Robert De niro
17-Aug-2010
7 rows selected.
SQL> ed file1
SQL> desc cust4; Name Null? Type ------------------- -------- ------------------------ CUST_ID NUMBER(3) F_NAME VARCHAR2(15) L_NAME VARCHAR2(15) DOB DATE INCOME NUMBER(10,2)
SQL> alter table cust4 modify (cust_id varchar2(10));
Table altered.
SQL> desc cust4; Name Null? Type ------------------- -------- ------------------------- CUST_ID VARCHAR2(10) F_NAME VARCHAR2(15) L_NAME VARCHAR2(15) DOB DATE INCOME NUMBER(10,2)
SQL> alter table cust4 modify (income number(5));
Table altered.
SQL> desc cust4; Name Null? Type ------------------- -------- ------------------------- CUST_ID VARCHAR2(10) F_NAME VARCHAR2(15) L_NAME VARCHAR2(15) DOB DATE INCOME NUMBER(5)
SQL> alter table cust4 add(mngr_name varchar2(15));
Table altered.
17
Dipanjan Sarkar 2010
SQL> desc cust4; Name Null? Type ------------------- -------- -------------------------- CUST_ID VARCHAR2(10) F_NAME VARCHAR2(15) L_NAME VARCHAR2(15) DOB DATE INCOME NUMBER(5) MNGR_NAME VARCHAR2(15)
SQL>insert into cust4 values ('&cust_id','&f_name','&l_name','&dob',&income,'&mngr_name');Enter value for cust_id: E001Enter value for f_name: RobertEnter value for l_name: BrownEnter value for dob: 12-MAR-88Enter value for income: 50000Enter value for mngr_name: Rahulold 1: insert into cust4 values ('&cust_id','&f_name','&l_name','&dob',&income,'&mngr_name')new 1: insert into cust4 values ('E001','Robert','Brown','12-MAR-88',50000,'Rahul')
1 row created.
Commit complete.SQL> /Enter value for cust_id: E005Enter value for f_name: KaranEnter value for l_name: SethEnter value for dob: 11-JUL-69Enter value for income: 18000Enter value for mngr_name: Deepakold 1: insert into cust4 values ('&cust_id','&f_name','&l_name','&dob',&income,'&mngr_name')new 1: insert into cust4 values ('E005','Karan','Seth','11-JUL-69',18000,'Deepak')
1 row created.
Commit complete.SQL> /Enter value for cust_id: E004Enter value for f_name: AnilEnter value for l_name: SharmaEnter value for dob: 23-AUG-79Enter value for income: 29000Enter value for mngr_name: Ajitold 1: insert into cust4 values ('&cust_id','&f_name','&l_name','&dob',&income,'&mngr_name')new 1: insert into cust4 values ('E004','Anil','Sharma','23-AUG-79',29000,'Ajit')
1 row created.
Commit complete.SQL> /Enter value for cust_id: E015Enter value for f_name: SumanEnter value for l_name: AdjaniEnter value for dob: 20-JAN-74
18
Dipanjan Sarkar 2010
Enter value for income: 85000Enter value for mngr_name: Tapanold 1: insert into cust4 values ('&cust_id','&f_name','&l_name','&dob',&income,'&mngr_name')new 1: insert into cust4 values ('E015','Suman','Adjani','20-JAN-74',85000,'Tapan')
1 row created.
Commit complete.SQL> /Enter value for cust_id: E011Enter value for f_name: AnirbanEnter value for l_name: SenEnter value for dob: 10-OCT-78Enter value for income: 55000Enter value for mngr_name: Kabirold 1: insert into cust4 values ('&cust_id','&f_name','&l_name','&dob',&income,'&mngr_name')new 1: insert into cust4 values ('E011','Anirban','Sen','10-OCT-78',55000,'Kabir')
1 row created.
Commit complete.SQL> select *from cust4;
DipanjanSarkar CUST_ID F_NAME L_NAME DOB INCOME MNGR_NAME ---------- --------------- --------------- --------- ---------- --------------- E001 Robert Brown 12-MAR-88 50000 Rahul E005 Karan Seth 11-JUL-69 18000 Deepak E004 Anil Sharma 23-AUG-79 29000 Ajit E015 Suman Adjani 20-JAN-74 85000 Tapan E011 Anirban Sen 10-OCT-78 55000 Kabir
17-Aug-2010
SQL> alter table cust4 add (territory varchar2(10));
Table altered.
SQL> desc cust4; Name Null? Type ----------------------- -------- -------------------------------- CUST_ID VARCHAR2(10) F_NAME VARCHAR2(15) L_NAME VARCHAR2(15) DOB DATE INCOME NUMBER(5) MNGR_NAME VARCHAR2(15) TERRITORY VARCHAR2(10)
SQL> update cust4 set territory='&territory' where cust_id='&cust_id';Enter value for territory: AMERICA Enter value for cust_id: E005old 1: update cust4 set territory='&territory' where cust_id='&cust_id'new 1: update cust4 set territory='AMERICA ' where cust_id='E005'
19
Dipanjan Sarkar 2010
1 row updated.
Commit complete.SQL> /Enter value for territory: INDIAEnter value for cust_id: 001old 1: update cust4 set territory='&territory' where cust_id='&cust_id'new 1: update cust4 set territory='INDIA' where cust_id='001'
0 rows updated.
Commit complete.SQL> /Enter value for territory: INDIAEnter value for cust_id: E001old 1: update cust4 set territory='&territory' where cust_id='&cust_id'new 1: update cust4 set territory='INDIA' where cust_id='E001'
1 row updated.
Commit complete.SQL> /Enter value for territory: CHINAEnter value for cust_id: E011old 1: update cust4 set territory='&territory' where cust_id='&cust_id'new 1: update cust4 set territory='CHINA' where cust_id='E011'
1 row updated.
Commit complete.SQL> /Enter value for territory: ITALYEnter value for cust_id: E004old 1: update cust4 set territory='&territory' where cust_id='&cust_id'new 1: update cust4 set territory='ITALY' where cust_id='E004'
1 row updated.
Commit complete.SQL> /Enter value for territory: THAILANDEnter value for cust_id: E015old 1: update cust4 set territory='&territory' where cust_id='&cust_id'new 1: update cust4 set territory='THAILAND' where cust_id='E015'
1 row updated.
Commit complete.SQL> select *from cust4;
DipanjanSarkar CUST_ID F_NAME L_NAME DOB INCOME MNGR_NAME TERRITORY ---------- --------------- --------------- --------- ---------- --------------- ---------- E001 Robert Brown 12-MAR-88 50000 Rahul INDIA E005 Karan Seth 11-JUL-69 18000 Deepak AMERICA
20
Dipanjan Sarkar 2010
E004 Anil Sharma 23-AUG-79 29000 Ajit ITALY E015 Suman Adjani 20-JAN-74 85000 Tapan THAILAND E011 Anirban Sen 10-OCT-78 55000 Kabir CHINA
17-Aug-2010
SQL> desc cust3; Name Null? Type -------------------------- -------- ---------------------- CUST_ID NUMBER(3) F_NAME VARCHAR2(15) L_NAME VARCHAR2(15) DOB DATE INCOME NUMBER(10,2)
SQL> select *from cust3;
DipanjanSarkar CUST_ID F_NAME L_NAME DOB INCOME ---------- --------------- --------------- --------- ---------- 147 Aishwarya Roberts 148 Gustav Steel 360 Helmet Capshaw 380 Meryl Holden 451 Ridley Hackman 454 Rob Russell 458 Robert De niro
17-Aug-2010
7 rows selected.
SQL> drop cust3;drop cust3 *ERROR at line 1:ORA-00950: invalid DROP option
SQL> drop table cust3;
Table dropped.
SQL> show recycle;ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME ---------------- ------------------------------ ------------ ------------------- CUST3 BIN$jJX7nhgoTsuHy0V9WImkVg==$0 TABLE 2010-08-17:15:31:17 SQL> flashback table cust3 to before drop;
Flashback complete.
SQL> drop table cust3;
Table dropped.
21
Dipanjan Sarkar 2010
SQL> select *from tab;
DipanjanSarkar TNAME TABTYPE CLUSTERID ------------------------------ ------- ---------- CUSTOMER TABLE BIN$/49N8kaYQ9ShCsq8/cTK3A==$0 TABLE CUST2 TABLE CUST4 TABLE CUST_ONE TABLE
17-Aug-2010
SQL> flashback table cust3 to before drop;
Flashback complete.
SQL> select *from tab;
DipanjanSarkar TNAME TABTYPE CLUSTERID ------------------------------ ------- ---------- CUSTOMER TABLE CUST3 TABLE CUST2 TABLE CUST4 TABLE CUST_ONE TABLE
17-Aug-2010
SQL> spool off;
22
Dipanjan Sarkar 2010
Questions for Assignment 3 ( Student ) :
1. Create table student with attributes st_name, st_address, ph_no, e_mail, %_marks and describe the table with its field names and datatypes
2. Update the table and change the values of e_mail and st_address corresponding to st_id s005
3. Alter the table and add new field called marks varchar2(6)
4. Alter the table to modify field name marks and change the style to varchar2(15)
5. Write SQL statement to view records of students who have more that 70% marks
6. Delete all records from the table where the students have got less than 40% marks
7. Create another table which will be having same structures and values of student. Drop the 2nd table and commit all changes
23
Dipanjan Sarkar 2010
SQL> create table student (st_id varchar2(5),st_name varchar2(10),st_address varchar2(15),ph_no number(8),e_mail varchar2(10),per_marks number(4,2));
Table created.
SQL> desc student; Name Null? Type ----------------------------- -------- ------------------------------ ST_ID VARCHAR2(5) ST_NAME VARCHAR2(10) ST_ADDRESS VARCHAR2(15) PH_NO NUMBER(8) E_MAIL VARCHAR2(10) PER_MARKS NUMBER(4,2)
SQL> insert into student values ('&st_id','&st_name','&st_address',&ph_no, '&e_mail',&per_marks);Enter value for st_id: s001Enter value for st_name: RahulEnter value for st_address: BandelEnter value for ph_no: 26112344Enter value for e_mail: [email protected] value for per_marks: 88.35old 1: insert into student values ('&st_id','&st_name','&st_address',&ph_no,'&e_mail',&per_marks)new 1: insert into student values ('s001','Rahul','Bandel',26112344,'[email protected]',88.35)
1 row created.
Commit complete.SQL> ed sd
SQL> @sd;
Table created.
SQL> select * from tab;
DipanjanSarkar TNAME TABTYPE CLUSTERID ------------------------------ ------- ---------- CUSTOMER TABLE CUST3 TABLE STUDENT TABLE STUD TABLE CUST2 TABLE CUST4 TABLE CUST_ONE TABLE
17-Aug-2010
7 rows selected.
24
Dipanjan Sarkar 2010
SQL> desc stud; Name Null? Type ------------------------------ -------- ---------------------- My Name VARCHAR2(10) % Marks NUMBER(4,2)
SQL> drop table stud purge;
Table dropped.
SQL> insert into student values ('&st_id','&st_name','&st_address',&ph_no, '&e_mail',&per_marks);Enter value for st_id: s003Enter value for st_name: AnshulEnter value for st_address: ChandannagarEnter value for ph_no: 23467543Enter value for e_mail: [email protected] value for per_marks: 39.75old 1: insert into student values ('&st_id','&st_name','&st_address',&ph_no,'&e_mail',&per_marks)new 1: insert into student values ('s003','Anshul','Chandannagar',23467543,'[email protected]',39.75)
1 row created.
Commit complete.SQL> /Enter value for st_id: s005Enter value for st_name: KaranEnter value for st_address: CalcuttaEnter value for ph_no: 26831234Enter value for e_mail: [email protected] value for per_marks: 75.35old 1: insert into student values ('&st_id','&st_name','&st_address',&ph_no,'&e_mail',&per_marks)new 1: insert into student values ('s005','Karan','Calcutta',26831234,'[email protected]',75.35)
1 row created.
Commit complete.SQL> /Enter value for st_id: s011Enter value for st_name: AnilEnter value for st_address: HowrahEnter value for ph_no: 26712380Enter value for e_mail: [email protected] value for per_marks: 30.58old 1: insert into student values ('&st_id','&st_name','&st_address',&ph_no,'&e_mail',&per_marks)new 1: insert into student values ('s011','Anil','Howrah',26712380,'[email protected]',30.58)
1 row created.
Commit complete.SQL> /Enter value for st_id: s025Enter value for st_name: DeepakEnter value for st_address: LiluahEnter value for ph_no: 26812005
25
Dipanjan Sarkar 2010
Enter value for e_mail: [email protected] value for per_marks: 85.40old 1: insert into student values ('&st_id','&st_name','&st_address',&ph_no,'&e_mail',&per_marks)new 1: insert into student values ('s025','Deepak','Liluah',26812005,'[email protected]',85.40)
1 row created.
Commit complete.SQL> select *from student;
DipanjanSarkar ST_ID ST_NAME ST_ADDRESS PH_NO E_MAIL PER_MARKS ----- ---------- --------------- ---------- ---------- ---------- s001 Rahul Bandel 26112344 [email protected] 88.35 s003 Anshul Chandannagar 23467543 [email protected] 39.75 s005 Karan Calcutta 26831234 [email protected] 75.35 s011 Anil Howrah 26712380 [email protected] 30.58 s025 Deepak Liluah 26812005 [email protected] 85.4
17-Aug-2010
SQL> set pagesize 10;SQL> select *from student;
DipanjanSarkar ST_ID ST_NAME ST_ADDRESS PH_NO E_MAIL PER_MARKS ----- ---------- --------------- ---------- ---------- ---------- s001 Rahul Bandel 26112344 [email protected] 88.35 s003 Anshul Chandannagar 23467543 [email protected] 39.75 s005 Karan Calcutta 26831234 [email protected] 75.35 s011 Anil Howrah 26712380 [email protected] 30.58 s025 Deepak Liluah 26812005 [email protected] 85.4
17-Aug-2010
SQL> alter table student rename column per_marks to "%_marks";
Table altered.
SQL> select *from student;
DipanjanSarkar ST_ID ST_NAME ST_ADDRESS PH_NO E_MAIL %_marks ----- ---------- --------------- ---------- ---------- ---------- s001 Rahul Bandel 26112344 [email protected] 88.35 s003 Anshul Chandannagar 23467543 [email protected] 39.75 s005 Karan Calcutta 26831234 [email protected] 75.35 s011 Anil Howrah 26712380 [email protected] 30.58 s025 Deepak Liluah 26812005 [email protected] 85.4
17-Aug-2010
SQL> update student set e_mail='&e_mail',st_address='&st_address' where st_id='s005';Enter value for e_mail: [email protected] value for st_address: Mumbaiold 1: update student set e_mail='&e_mail',st_address='&st_address' where st_id='s005'
26
Dipanjan Sarkar 2010
new 1: update student set e_mail='[email protected]',st_address='Mumbai' where st_id='s005'
1 row updated.
Commit complete.SQL> select *from student;
DipanjanSarkar ST_ID ST_NAME ST_ADDRESS PH_NO E_MAIL %_marks ----- ---------- --------------- ---------- ---------- ---------- s001 Rahul Bandel 26112344 [email protected] 88.35 s003 Anshul Chandannagar 23467543 [email protected] 39.75 s005 Karan Mumbai 26831234 [email protected] 75.35 s011 Anil Howrah 26712380 [email protected] 30.58 s025 Deepak Liluah 26812005 [email protected] 85.4
17-Aug-2010
SQL> alter table student add(marks varchar2(6));
Table altered.
SQL> select *from student;
DipanjanSarkar ST_ID ST_NAME ST_ADDRESS PH_NO E_MAIL %_marks MARKS ----- ---------- --------------- ---------- ---------- ---------- ------ s001 Rahul Bandel 26112344 [email protected] 88.35 s003 Anshul Chandannagar 23467543 [email protected] 39.75 s005 Karan Mumbai 26831234 [email protected] 75.35 s011 Anil Howrah 26712380 [email protected] 30.58 s025 Deepak Liluah 26812005 [email protected] 85.4
17-Aug-2010
SQL> desc student; Name Null? Type -------------------------------------- -------- ------------------ ST_ID VARCHAR2(5) ST_NAME VARCHAR2(10) ST_ADDRESS VARCHAR2(15) PH_NO NUMBER(8) E_MAIL VARCHAR2(10) %_marks NUMBER(4,2) MARKS VARCHAR2(6)
SQL> alter table student modify(marks varchar2(15));
Table altered.
SQL> desc student; Name Null? Type ---------------------------------------- -------- ----------------- ST_ID VARCHAR2(5) ST_NAME VARCHAR2(10) ST_ADDRESS VARCHAR2(15) PH_NO NUMBER(8) E_MAIL VARCHAR2(10) %_marks NUMBER(4,2) MARKS VARCHAR2(15)
27
Dipanjan Sarkar 2010
SQL> select *from student where "%_marks">70;
DipanjanSarkar ST_ID ST_NAME ST_ADDRESS PH_NO E_MAIL %_marks MARKS ----- ---------- --------------- ---------- ---------- ---------- -------- s001 Rahul Bandel 26112344 [email protected] 88.35 s005 Karan Mumbai 26831234 [email protected] 75.35 s025 Deepak Liluah 26812005 [email protected] 85.4
17-Aug-2010
SQL> select *from student;
DipanjanSarkar ST_ID ST_NAME ST_ADDRESS PH_NO E_MAIL %_marks MARKS ----- ---------- --------------- ---------- ---------- ---------- -------- s001 Rahul Bandel 26112344 [email protected] 88.35 s003 Anshul Chandannagar 23467543 [email protected] 39.75 s005 Karan Mumbai 26831234 [email protected] 75.35 s011 Anil Howrah 26712380 [email protected] 30.58 s025 Deepak Liluah 26812005 [email protected] 85.4 17-Aug-2010
SQL> delete from student where "%_marks"<40;
2 rows deleted.
Commit complete.SQL> select *from student;
DipanjanSarkar ST_ID ST_NAME ST_ADDRESS PH_NO E_MAIL %_marks MARKS ----- ---------- --------------- ---------- ---------- ---------- ------- s001 Rahul Bandel 26112344 [email protected] 88.35 s005 Karan Mumbai 26831234 [email protected] 75.35 s025 Deepak Liluah 26812005 [email protected] 85.4
17-Aug-2010
SQL> create table student2 as select *from student;
Table created.
SQL> desc student2; Name Null? Type -------------------------- -------- ------------------------ ST_ID VARCHAR2(5) ST_NAME VARCHAR2(10) ST_ADDRESS VARCHAR2(15) PH_NO NUMBER(8) E_MAIL VARCHAR2(10) %_marks NUMBER(4,2) MARKS VARCHAR2(15)
SQL> select *from student2;
28
Dipanjan Sarkar 2010
DipanjanSarkar ST_ID ST_NAME ST_ADDRESS PH_NO E_MAIL %_marks MARKS ----- ---------- --------------- ---------- ---------- ---------- --------- s001 Rahul Bandel 26112344 [email protected] 88.35 s005 Karan Mumbai 26831234 [email protected] 75.35 s025 Deepak Liluah 26812005 [email protected] 85.4
17-Aug-2010
SQL> drop table student2;
Table dropped.
SQL> select *from tab;
DipanjanSarkar TNAME TABTYPE CLUSTERID ------------------------------ ------- ---------- CUSTOMER TABLE CUST3 TABLE STUDENT TABLE BIN$v5cO9WTsTfqwndhY+35NYg==$0 TABLE CUST2 TABLE CUST4 TABLE CUST_ONE TABLE
17-Aug-2010
7 rows selected.
SQL> spool off;
29
Dipanjan Sarkar 2010
Questions for Assignment 4 ( Dept and Cust100 ) :
Create table dept with the following attributes :
Column name Datatype(size)Constraints
dept_id number(3)primary key
dept_name varchar2(15) ___
Insert 4 depts with names and id’s 90, 69, 100 and 110.
Create table cust_100 with the following attributes :
Column name Datatype(size) Constraints
emp_id number(3) Primary keyfirst_name varchar2(10) Initial letter capitallast_name varchar2(10) Initial letter capital
and not nulle_mail varchar2(20) All upper caseph_no varchar2(15) ____hire_date date Should be >
than 01-jan-1980
job_id varchar2(10) Must begin with
FI or AD or ITsalary number(8,2) ≥ 4000 & ≤ 25000mgr_id number(3) ____
30
Dipanjan Sarkar 2010
dept_id number(3) Foreign key, ref
table dept
1. Add 10 records to cust_1002. Drop column mrg_id3. Add column mgr_id and make it self referenced such that
first 4 id’s correspond to first emp_id, next 4 correspond to fifth emp_id and the last 2 correspond to the ninth emp_id.
31
Dipanjan Sarkar 2010
SQL> ttitle Dipanjan SarkarSQL> btitle 24-08-2010SQL> create table dept (dept_id number(3) primary key,dept_name varchar2(15));
Table created.
SQL> insert into dept values(&dept_id,'&dept_name');Enter value for dept_id: 90Enter value for dept_name: CSEold 1: insert into dept values(&dept_id,'&dept_name')new 1: insert into dept values(90,'CSE')
1 row created.
SQL> /Enter value for dept_id: 60Enter value for dept_name: ECEold 1: insert into dept values(&dept_id,'&dept_name')new 1: insert into dept values(60,'ECE')
1 row created.
SQL> /Enter value for dept_id: 100Enter value for dept_name: ITold 1: insert into dept values(&dept_id,'&dept_name')new 1: insert into dept values(100,'IT')
1 row created.
SQL> /Enter value for dept_id: 110Enter value for dept_name: EEold 1: insert into dept values(&dept_id,'&dept_name')new 1: insert into dept values(110,'EE')
1 row created.
SQL> desc dept; Name Null? Type ----------------------------------------- -------- ---------------------------- DEPT_ID NOT NULL NUMBER(3) DEPT_NAME VARCHAR2(15)
SQL> select *from dept;
DipanjanSarkar DEPT_ID DEPT_NAME ---------- --------------- 90 CSE 60 ECE 100 IT 110 EE
24-08-2010
SQL> ed cust;In the cust text file we have,
create table cust_100 (emp_id number(3) primary key,first_name varchar2(10) check(first_name=initcap(first_name)),last_name varchar2(10) check(last_name=initcap(last_name)) not null,e_mail varchar2(20) check(e_mail=upper(e_mail)),ph_no varchar2(15),hire_date date check(hire_date>'01-JAN-1980'),job_id varchar2(10) check(job_id like 'AD%' or job_id like 'FI%'or job_id like 'IT%'),salary number(8,2) check(salary >='4000' and salary <='25000'),mgr_id number(3),dept_id number(3) references dept(dept_id),unique(first_name,last_name));
SQL> @cust;
Table created.
SQL> desc cust_100;
32
Dipanjan Sarkar 2010
Name Null? Type ----------------------------------------- -------- ---------------- EMP_ID NOT NULL NUMBER(3) FIRST_NAME VARCHAR2(10) LAST_NAME NOT NULL VARCHAR2(10) E_MAIL VARCHAR2(20) PH_NO VARCHAR2(15) HIRE_DATE DATE JOB_ID VARCHAR2(10) SALARY NUMBER(8,2) MGR_ID NUMBER(3) DEPT_ID NUMBER(3)
SQL> set autocommit on;Again from cust notepad file we have,
insert into cust_100 values (&emp_id,'&first_name','&last_name','&e_mail','&ph_no','&hire_date','&job_id',&salary,&mgr_id,&dept_id);
SQL> @cust;Enter value for emp_id: 004Enter value for first_name: KaranEnter value for last_name: GroverEnter value for e_mail: [email protected] value for ph_no: 26781345Enter value for hire_date: 04-JUL-1982Enter value for job_id: FI005Enter value for salary: 12000Enter value for mgr_id: 147old 1: insert into cust_100 values (&emp_id,'&first_name','&last_name','&e_mail','&ph_no','&hire_date','&job_id',&salary,&mgr_id,new 1: insert into cust_100 values (004,'Karan','Grover','[email protected]','26781345','04-JUL-1982','FI005',12000,147,Enter value for dept_id: 100old 2: &dept_id)new 2: 100)
1 row created.
Commit complete.SQL> set pagesize 18;SQL> select *from cust_100;
DipanjanSarkar EMP_ID FIRST_NAME LAST_NAME E_MAIL PH_NO HIRE_DATE JOB_ID SALARY MGR_ID DEPT_ID ---------- ---------- ---------- -------------------- --------------- --------- ---------- ---------- ---------- ---------- 23 Manisha Das [email protected] 25647856 15-SEP-83 IT058 14000 151 110 4 Karan Grover [email protected] 26781345 04-JUL-82 FI005 12000 147 100 10 Deepta Naval [email protected] 26841209 02-MAR-85 AD013 20000 145 90 15 Anshul Mehta [email protected] 27653432 04-OCT-87 IT053 24750 151 110 34 Puja Dey [email protected] 26873450 23-MAY-83 AD101 13550 172 60 87 Deepa Roy [email protected] 26831009 25-SEP-88 IT110 16700 145 60 102 Sweta Sen [email protected] 23506780 21-JAN-86 AD035 17900 172 110 114 Anil Sharma [email protected] 26701890 18-AUG-83 FI198 12768 151 100 123 Rekha Singh [email protected] 26831670 22-NOV-89 AD125 19000 148 60 127 Suman Ghosh [email protected] 26571345 21-JUN-91 IT181 23000 145 60
24-08-2010
10 rows selected.
33
Dipanjan Sarkar 2010
SQL> ed cust;
SQL> desc cust_100;Name Null? Type ----------------------------------------- -------- -------------------- EMP_ID NOT NULL NUMBER(3) FIRST_NAME VARCHAR2(10) LAST_NAME NOT NULL VARCHAR2(10) E_MAIL VARCHAR2(20) PH_NO VARCHAR2(15) HIRE_DATE DATE JOB_ID VARCHAR2(10) SALARY NUMBER(8,2) MGR_ID NUMBER(3) DEPT_ID NUMBER(3)
SQL> alter table cust_100 drop column mgr_id;
Table altered.
SQL> desc cust_100; Name Null? Type --------------------------------------- -------- --------------------- EMP_ID NOT NULL NUMBER(3) FIRST_NAME VARCHAR2(10) LAST_NAME NOT NULL VARCHAR2(10) E_MAIL VARCHAR2(20) PH_NO VARCHAR2(15) HIRE_DATE DATE JOB_ID VARCHAR2(10) SALARY NUMBER(8,2) DEPT_ID NUMBER(3)SQL> select *from cust_100;
DipanjanSarkar EMP_ID FIRST_NAME LAST_NAME E_MAIL PH_NO HIRE_DATE JOB_ID SALARY DEPT_ID ---------- ---------- ---------- -------------------- --------------- --------- ---------- ---------- ---------- 23 Manisha Das [email protected] 25647856 15-SEP-83 IT058 14000 110 4 Karan Grover [email protected] 26781345 04-JUL-82 FI005 12000 100 10 Deepta Naval [email protected] 26841209 02-MAR-85 AD013 20000 90 15 Anshul Mehta [email protected] 27653432 04-OCT-87 IT053 24750 110 34 Puja Dey [email protected] 26873450 23-MAY-83 AD101 13550 60 87 Deepa Roy [email protected] 26831009 25-SEP-88 IT110 16700 60 102 Sweta Sen [email protected] 23506780 21-JAN-86 AD035 17900 110 114 Anil Sharma [email protected] 26701890 18-AUG-83 FI198 12768 100 123 Rekha Singh [email protected] 26831670 22-NOV-89 AD125 19000 60 127 Suman Ghosh [email protected] 26571345 21-JUN-91 IT181 23000 60
24-08-2010
10 rows selected.
SQL> alter table cust_100 add (mgr_id number(3));
Table altered.
DipanjanSarkar EMP_ID FIRST_NAME LAST_NAME E_MAIL PH_NO HIRE_DATE JOB_ID SALARY DEPT_ID MGR_ID ---------- ---------- ---------- -------------------- --------------- --------- ---------- ---------- ---------- ----------
34
Dipanjan Sarkar 2010
23 Manisha Das [email protected] 25647856 15-SEP-83 IT058 14000 110 4 Karan Grover [email protected] 26781345 04-JUL-82 FI005 12000 100 10 Deepta Naval [email protected] 26841209 02-MAR-85 AD013 20000 90 15 Anshul Mehta [email protected] 27653432 04-OCT-87 IT053 24750 110 34 Puja Dey [email protected] 26873450 23-MAY-83 AD101 13550 60 87 Deepa Roy [email protected] 26831009 25-SEP-88 IT110 16700 60 102 Sweta Sen [email protected] 23506780 21-JAN-86 AD035 17900 110 114 Anil Sharma [email protected] 26701890 18-AUG-83 FI198 12768 100 123 Rekha Singh [email protected] 26831670 22-NOV-89 AD125 19000 60 127 Suman Ghosh [email protected] 26571345 21-JUN-91 IT181 23000 60
24-08-2010
10 rows selected.
SQL> alter table cust_100 add foreign key(mgr_id) references cust_100(emp_id);
Table altered.
SQL> update cust_100 set mgr_id=&mgr_id where emp_id=&emp_id;
Enter value for mgr_id: 023Enter value for emp_id: 023old 1: update cust_100 set mgr_id=&mgr_id where emp_id=&emp_idnew 1: update cust_100 set mgr_id=023 where emp_id=023
1 row updated.
Commit complete.
SQL> select *from cust_100;
DipanjanSarkar EMP_ID FIRST_NAME LAST_NAME E_MAIL PH_NO HIRE_DATE JOB_ID SALARY DEPT_ID MGR_ID ---------- ---------- ---------- -------------------- --------------- --------- ---------- ---------- ---------- ---------- 23 Manisha Das [email protected] 25647856 15-SEP-83 IT058 14000 110 23 4 Karan Grover [email protected] 26781345 04-JUL-82 FI005 12000 100 23 10 Deepta Naval [email protected] 26841209 02-MAR-85 AD013 20000 90 23 15 Anshul Mehta [email protected] 27653432 04-OCT-87 IT053 24750 110 23 34 Puja Dey [email protected] 26873450 23-MAY-83 AD101 13550 60 34 87 Deepa Roy [email protected] 26831009 25-SEP-88 IT110 16700 60 34 102 Sweta Sen [email protected] 23506780 21-JAN-86 AD035 17900 110 34 114 Anil Sharma [email protected] 26701890 18-AUG-83 FI198 12768 100 34 123 Rekha Singh [email protected] 26831670 22-NOV-89 AD125 19000 60 123 127 Suman Ghosh [email protected] 26571345 21-JUN-91 IT181 23000 60 123
24-08-2010
10 rows selected.
SQL> spool off;
35
Dipanjan Sarkar 2010
Assignment 5:
Create the following tables with the datatypes and constraints
Sailor:
Attribute Datatype Constraintss_id varchar2(4) primary key & starts with small ss_name varchar2(15) initcapRating number(2)Age number(3,1)
Boat:
Attribute Data type Constraintsb_id number(3) primary keyb_name varchar2(10)color varchar2(6) comes from the set blue, red, green
Reserve:
Attribute Data type Constraintss_id varchar2(4) Foreign key reference sailorb_id number(3) Foreign key reference boatday date < 1-Jan-2000
Table Creation:
For table sailorSQL>Create table sailor(s_id varchar2(4) primary key check(s_id like ‘s%’), s_name varchar2(15) check(s_name=initcap(s_name)), rating number(2), age number(3,1));
Table Created
For table boatSQL>Create table boat(b_id number(3) primary key, b_name varchar2(10), color varchar(6) check(color in('red', 'green', 'blue'));
Table Created
For table reserveSQL>Create table reserve(s_id varchar2(4) references sailor, b_id number(3) references boat, day date check(day<’1-Jan-2000’) primary key(s_id,b_id));
Table Created
36
Dipanjan Sarkar 2010
Value insertion:
For table sailorSQL>Insert into sailor values(‘&s_id’, ‘&s_name’, ‘&raing’, ‘&age’);
Enter value for s_id: s500Enter value for s_name: DustainEnter value for rating: 7Enter value for age: 45old 1: Insert into sailor values('&s_id','&s_name','&rating','&age')new 1: Insert into sailor values('s500','Dustain','7','45')
1 row created.Commit complete.
Similarly we enter the other values of the table sailor consecutively.
For table boatSQL>Insert into boat values(‘&b_id’, ‘&b_name’, ‘&color’);
Enter value for b_id: 101Enter value for b_name: InterlakeEnter value for color: blueold 1: Insert into boat values('&b_id','&b_name','&color')new 1: Insert into boat values('101','Interlake','blue')
1 row created.Commit complete.
Similarly we enter other values for the table boat.
For table reserveSQL>Insert into reserve values(‘&s_id’, ‘&b_id’, ‘&day’);
Enter value for s_id: s500Enter value for b_id: 101Enter value for day: 10-oct-1998old 1: Insert into reserve values('&s_id','&b_id','&day')new 1: Insert into reserve values('s500','101','10-oct-1998')
1 row created.Commit complete.
Similarly we enter other values for the table reserve.
37
Dipanjan Sarkar 2010
SQL>Select * from sailor;
S_ID S_NAME RATING AGE ------ --------------- ------------------ ------------------ s500 Dustain 7 45 s501 Brutus 1 33 s502 Lusber 8 55.5 s503 Andy 8 25.5 s504 Rusty 10 35 s505 Horatio 7 35 s506 Zorpa 10 16 s507 Horatio 9 35.5 s508 Art 3 25.5 s509 Bud 4 51 s510 Robin 6 47.5
SQL>Select * from boat;
B_ID B_NAME COLOR ---------- ---------- ------ 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red
SQL> Select * from reserve;
S_ID B_ID DAY ---- ---------- --------- s500 101 10-OCT-98 s500 102 10-OCT-98 s500 103 10-NOV-98 s500 104 10-AUG-98 s502 102 11-OCT-98 s502 103 11-OCT-98 s502 104 11-DEC-98 s505 101 09-MAY-98 s505 102 09-AUG-98
38
Dipanjan Sarkar 2010
Display the s_name with left side padding with 3 asterisks
SQL> Select lpad (s_name, length(s_name)+3,'*') from sailor;
LPAD(S_NAME,LENGTH(S_NAME)+3,'*') ---------------------------------------------------------------------------------***Dustain ***Brutus ***Lusber ***Andy ***Rusty ***Horatio ***Zorpa ***Horatio ***Art ***Bud ***Robin
Display length of each s_name
SQL> Select s_name, length (s_name) length from sailor;
S_NAME LENGTH -------------- ---------------------------- Dustain 7 Robin 6 Lusber 6 Andy 4Rusty 5 Horatio 7 Zorpa 5 Horatio 7Art 3Bud 3 Brutus 5 Display the sailors name in upper case
SQL> Select s_name, upper(s_name) changed from sailor;
S_NAME CHANGED --------------- --------------- Dustain DUSTAIN Brutus BRUTUS Lusber LUSBER Andy ANDY Rusty RUSTY
39
Dipanjan Sarkar 2010
Horatio HORATIO Zorpa ZORPA Horatio HORATIO Art ART Bud BUD Robin ROBIN
Display the 4th and 7th letter of sailor name
SQL> select s_name, substr(s_name,4,1) fst, substr(s_name,7,1) snd from sailor;
S_NAME F S - ------------- -------------------- Dustain t nBrutus tLusbe bAndy yRusty tHoratio a oZorpa pHoratio a oArt BudRobin i
Display 4th to 7th letter of sailor name
SQL> Select s_name, substr(s_name,4,4) string from sailor;
S_NAME STRING - ------------- -------------------- Dustain tainBrutus tusLusbe beAndy yRusty tyHoratio atioZorpa paHoratio atioArt BudRobin in
Display concatenate s_id and s_name
SQL> select concat(s_id,s_name) from sailor;
40
Dipanjan Sarkar 2010
CONCAT(S_ID,S_NAME) ------------------- s500Dustain s501Brutus s502Lusber s503Andy s504Rusty s505Horatio s506Zorpa s507Horatio s508Art s509Bud s510Robin
Display square root of rating
SQL> select round(sqrt(rating),2) from sailor;
ROUND(SQRT(RATING),2) ---------------------
2.6512.832.833.162.653.1631.7322.45
Select ceiling values of all ages
SQL> select age, ceil(age) ceiling from sailor;
AGE CEILING ---------- ---------- 45 45 33 33 55.5 56 25.5 26 35 35
41
Dipanjan Sarkar 2010
35 35 16 16 35.5 36 25.5 26 51 51 47.5 48
Display list of all sailor name with first 2 letters off
SQL> select s_name, substr(s_name,3,10) trimmed from sailor;
S_NAME TRIMMED --------------- ---------- Dustain stain Brutus utus Lusber sber Andy dy Rusty sty Horatio ratio Zorpa rpa Horatio ratio Art t Bud d Robin bin
List months between today and reservation date
SQL> Select months_between(sysdate,day) from reserve;
MONTHS_BETWEEN(SYSDATE,DAY) --------------------------------- 142.548387 142.548387 141.548387 144.548387 142.516129 142.516129 140.516129 147.580645 144.580645
List days between today and reservation date
SQL> Select floor(sysdate-day) no_days from reserve;
42
Dipanjan Sarkar 2010
NO_DAYS ------------------ 4339 4339 4308 4400 4338 4338 4277 4493 4401
Shift all reservation date by 2months late and 3months earlier
SQL> Select day,add_months(day,2) later, add_months(day,-3) earlier from reserve;
DAY LATER EARLIER --------- --------- --------- --------------------------------------------------- 10-OCT-98 10-DEC-98 10-JUL-98 10-OCT-98 10-DEC-98 10-JUL-98 10-NOV-98 10-JAN-99 10-AUG-98 10-AUG-98 10-OCT-98 10-MAY-98 11-OCT-98 11-DEC-98 11-JUL-98 11-OCT-98 11-DEC-98 11-JUL-98 11-DEC-98 11-FEB-99 11-SEP-98 09-MAY-98 09-JUL-98 09-FEB-98 09-AUG-98 09-OCT-98 09-MAY-98
Suppose after selling they enjoyed the next Monday as holiday. Find the date
SQL> Select day,to_char(day,'day'),next_day(day,'Monday')from reserve;
DAY TO_CHAR(D NEXT_DAY( ---------------- ---------------- ----------------- 10-OCT-98 Saturday 12-OCT-98 10-OCT-98 Saturday 12-OCT-98 10-NOV-98 tuesday 16-NOV-98 10-AUG-98 Monday 17-AUG-98 11-OCT-98 sunday 12-OCT-98 11-OCT-98 Sunday 12-OCT-98 11-DEC-98 friday 14-DEC-98 09-MAY-98 Saturday 11-MAY-98 09-AUG-98 sunday 10-AUG-98
43
Dipanjan Sarkar 2010
Find the first and last three letters of s_name
SQL> Select s_name,substr(s_name,1,3) fst, substr(s_name,length(s_name)-2,3) lst from sailor;
S_NAME FST LST --------------- -------- ------------ Dustain Dus ain Brutus Bru tus Lusber Lus ber Andy And ndy Rusty Rus sty Horatio Hor tio Zorpa Zor rpa Horatio Hor tio Art Art Art Bud Bud Bud Robin Rob bin
Display three asterisks before and after sailor name
SQL> Select lpad(rpad(s_name,length(s_name)+3,'*'),length(s_name)+6,'*') from sailor;
LPAD(RPAD(S_NAME,LENGTH(S_NAME)+3,'*'),LENGTH(S_NAME)+6,'*') ------------------------------------------------------------------------------------------------------***Dustain*** ***Brutus*** ***Lusber*** ***Andy*** ***Rusty*** ***Horatio*** ***Zorpa*** ***Horatio*** ***Art*** ***Bud*** ***Robin***
Display the date 20 days after today
SQL> Select sysdate+20 from dual;
SYSDATE+20 --------------------- 16-SEP-10
27August2010
44
Dipanjan Sarkar 2010
Display all reservation date like 11th January TWO THOUSAND ONE
SQL> Select to_char(day,'DDth Month YEAR') from reserve;
TO_CHAR(DAY,'DDTHMONTHYEAR') --------------------------------------------------------- 10TH October NINETEEN NINETY-EIGHT 10TH October NINETEEN NINETY-EIGHT 10TH November NINETEEN NINETY-EIGHT 10TH August NINETEEN NINETY-EIGHT 11TH October NINETEEN NINETY-EIGHT 11TH October NINETEEN NINETY-EIGHT 11TH December NINETEEN NINETY-EIGHT 09TH May NINETEEN NINETY-EIGHT 09TH August NINETEEN NINETY-EIGHT
Display all reservation date like 11/10/98
SQL> select to_char(day,'DD/MM/YY') from reserve;
TO_CHAR( -------- 10/10/98 10/10/98 10/11/98 10/08/98 11/10/98 11/10/98 11/12/98 09/08/98 11/10/98
SQL> spool off;
45
Dipanjan Sarkar 2010
Assignment 6:
SQL> select *from sailor;
DipanjanSarkar S_ID S_NAME RATING AGE ---- --------------- ---------- ---------- s500 Dustain 7 45 s501 Brutus 1 33 s502 Lusber 8 55.5 s503 Andy 8 25.5 s504 Rusty 10 35 s505 Horatio 7 35 s506 Zorba 10 16 s507 Horatio 9 35.5 s508 Art 3 25.5 s509 Robin 6 47.5 21-09-2010
10 rows selected.
SQL> select *from boat;
DipanjanSarkar B_ID B_NAME COLOR ---------- --------------- ---------- 101 interlake blue 102 interlake red 103 clipper green 104 marine red 21-09-2010
SQL> select *from reserve;
DipanjanSarkar S_ID B_ID DAY ---- ---------- --------- s500 101 10-OCT-98 s500 102 10-OCT-98 s500 103 10-NOV-98 s500 104 10-AUG-98 s502 102 11-OCT-98 s502 103 11-OCT-98 s502 104 11-DEC-98 s505 102 09-AUG-98 s505 104 11-OCT-98
46
Dipanjan Sarkar 2010
1. Find name and age of all sailors:
SQL> select s_name,age from sailor;
DipanjanSarkar S_NAME AGE --------------- ---------- Dustain 45 Brutus 33 Lusber 55.5 Andy 25.5 Rusty 35 Horatio 35 Zorba 16 Horatio 35.5 Art 25.5 Robin 47.5 21-09-2010
2. Find name of sailors with rating >7:
SQL> select s_name from sailor where rating>7;
DipanjanSarkar S_NAME --------------- Lusber Andy Rusty Zorba Horatio
3. Name of sailors who have reserved boat 103:
SQL> select s_name from sailor,reserve where sailor.s_id=reserve.s_id and reserve.b_id=103;
DipanjanSarkar S_NAME --------------- Dustain Lusber
47
Dipanjan Sarkar 2010
4. Name of sailors who have reserved red boat:
SQL> select distinct s_name from sailor,boat,reserve where sailor.s_id=reserve.s_id and boat.b_id=reserve.b_id and boat.color='red';
DipanjanSarkar S_NAME --------------- Dustain Horatio Lusber
5. Find different sailor name under heading name of sailors:
SQL> select s_name as name_of_sailors from sailor;
DipanjanSarkar NAME_OF_SAILORS --------------- Dustain Brutus Lusber Andy Rusty Horatio Zorba Horatio Art Robin
6. Find color of boats reserved by Lusber:
SQL> select color from boat,reserve,sailor where boat.b_id=reserve.b_id and reserve.s_id=sailor.s_id and sailor.s_name='Lusber';
DipanjanSarkar COLOR ---------- red green red
48
Dipanjan Sarkar 2010
7. Find sailors whose age is between 20 and 45:
DipanjanSarkar S_NAME --------------- Dustain Brutus Andy Rusty Horatio Horatio Art
8. Find sailors not in age range 20-45:
SQL> select s_name from sailor where age not between 20 and 45;
DipanjanSarkar S_NAME --------------- Lusber Zorba Robin
9. Find names of sailors who have reserved at least one boat:
SQL> select distinct s_name from sailor,reserve where sailor.s_id=reserve.s_id;
DipanjanSarkar S_NAME --------------- Dustain Horatio Lusber
10. Find sailor name and rating who have sailed 2 different boats on same day:
SQL> select distinct s_name,rating from sailor s,reserve r1,reserve r2 where s.s_id=r1.s_id and s.s_id=r2.s_id and r1.day=r2.day and r1.b_id!=r2.b_id;
DipanjanSarkar S_NAME RATING --------------- ---------- Dustain 7 Lusber 8
49
Dipanjan Sarkar 2010
11. Find age of sailors whose name begins and end with ‘B’:
SQL> select age from sailor where s_name like 'B%b';
no rows selected
12. Find age of sailors whose name begins and end with ‘B’ and has 3 characters:
SQL> select age from sailor where s_name like 'B_b';
no rows selected
13. Find s_id and name of sailors in descending order:
SQL> select s_id,s_name from sailor order by s_name desc;
DipanjanSarkar S_ID S_NAME ---- --------------- s506 Zorba s504 Rusty s509 Robin s502 Lusber s505 Horatio s507 Horatio s500 Dustain s501 Brutus s508 Art s503 Andy
14. Find s_id of sailors who have reserved red boat but not green boat:
SQL> ( select s_id from sailor,reserve,boat where sailor.s_id=reserve.s_id and reserve.b_id=boat.b_id and boat.color='red')minus( select s_id from sailor,reserve,boat where sailor.s_id=reserve.s_id and reserve.b_id=boat.b_id and boat.color='green') ;
S_ID----s505
50
Dipanjan Sarkar 2010
PL\SQL programs:
1) Write a program to display all even numbers up to a number using simple loop
SQL> set autocommit on;SQL> ed p1
Contents of p1.sql is,
declaren number(5):=0;num number(5);
beginnum:=#loop
dbms_output.put_line(n);n:=n+2;exit when n>num;
end loop;end;/
SQL> set serveroutput on;SQL> @p1;Enter value for num: 20old 6: num:=#new 6: num:=20;0 2 4 6 8 10 12 14 16 18 20
PL/SQL procedure successfully completed.
Commit complete.
2) Write a program to display all multiples of 3 up to a number using while loop
SQL> ed p2;
51
Dipanjan Sarkar 2010
Contents of p2.sql are,
declaren number(5):=0;num number(5);
beginnum:=#while n<=numloop
dbms_output.put_line(n);n:=n+3;
end loop;end;/
SQL> @p2;Enter value for num: 25old 6: num:=#new 6: num:=25;0 3 6 9 12 15 18 21 24
PL/SQL procedure successfully completed.
Commit complete.
3) Write a program to display multiples of 5 using for loop
SQL> ed p3;Contents of p3.sql are,
declare
num number(5);x number(5);
beginnum:=#for x in 0..num
52
Dipanjan Sarkar 2010
loopif mod(x,5)=0 then
dbms_output.put_line(x);end if;
end loop;end;/
SQL> @p3;Enter value for num: 10old 7: num:=#new 7: num:=10;0 5 10 15 20 25 30 35 40 45 50
PL/SQL procedure successfully completed.
Commit complete.
4) Write a program that takes marks as input and displays grade using if-else ladder
SQL> ed p4;Contents of p4.sql are,
declaren number(3,1);
beginn:=&n;if(n>=90 and n<=100)then
dbms_output.put_line('GRADE = O');elsif(n>=80 and n<90)then
dbms_output.put_line('GRADE = E');elsif(n>=70 and n<80)then
dbms_output.put_line('GRADE = A');elsif(n>=60 and n<70)
53
Dipanjan Sarkar 2010
thendbms_output.put_line('GRADE = B');
elsif(n>=50 and n<60)then
dbms_output.put_line('GRADE = C');elsif(n>=40 and n<50)then
dbms_output.put_line('GRADE = D');else
dbms_output.put_line('GRADE = F');end if;
end;/
SQL> @p4;Enter value for n: 90old 4: n:=&n;new 4: n:=90;GRADE = O
PL/SQL procedure successfully completed.
Commit complete.SQL> @p4;Enter value for n: 40old 4: n:=&n;new 4: n:=40;GRADE = D
PL/SQL procedure successfully completed.
5) Display random numbers in a table random_num(rand_no number(20))
SQL> ed p5;Contents of p5.sql are,
declare i number(2):=0;x number(15);
beginwhile i<20loop
select dbms_random.random() into x from dual;insert into random_num values(x);dbms_output.put_line('val generated is: '||x);commit;i:=i+1;
54
Dipanjan Sarkar 2010
end loop;end;/
SQL> create table random_num(rand_no number(20));
Table created.
SQL> @p5;val generated is: 293315505 val generated is: 161987681 val generated is: 594817284 val generated is: 1146545030 val generated is: -1985936008 val generated is: 1905424908 val generated is: -2073800067 val generated is: -1489384802 val generated is: 950799112 val generated is: -791341352 val generated is: -93869842 val generated is: 933454456 val generated is: -1730170814 val generated is: 904025890 val generated is: -890530728 val generated is: 1882737245 val generated is: 1407370383 val generated is: -185434708 val generated is: -282062135 val generated is: 1071105009
PL/SQL procedure successfully completed.
Commit complete.SQL> set pagesize 22;SQL> select *from random_num;
DipanjanSarkar RAND_NO ----------------- 293315505 161987681 594817284 1146545030 -1.986E+09 1905424908 -2.074E+09 -1.489E+09
55
Dipanjan Sarkar 2010
950799112 -791341352 -93869842 933454456 -1.730E+09 904025890 -890530728 1882737245 1407370383 -185434708 -282062135 1071105009
6) Write a program to fill up the table sphere(rad number(2),area number(10,2), volume number(15,2)) with radius values from 1 – 20
SQL> create table sphere(rad number(2),area number(10,2),volume number(15,2));
Table created.
SQL> ed p6;Contents of p6.sql are,
declarea number(10,2);v number(15,2);pi constant number(4,3):=3.142;
beginfor r in 1..20loop
a:=4*pi*r*r;v:=(4*pi*r*r*r)/3;insert into sphere values(r,a,v);commit;
end loop;end;/
SQL> @p6;
PL/SQL procedure successfully completed.
Commit complete.SQL> set pagesize 25;SQL> select *from sphere;
56
Dipanjan Sarkar 2010
DipanjanSarkar RAD AREA VOLUME ---------- ---------- ---------- 1 12.57 4.19 2 50.27 33.51 3 113.11 113.11 4 201.09 268.12 5 314.2 523.67 6 452.45 904.9 7 615.83 1436.94 8 804.35 2144.94 9 1018.01 3054.02 10 1256.8 4189.33 11 1520.73 5576 12 1809.79 7239.17 13 2123.99 9203.97 14 2463.33 11495.53 15 2827.8 14139 16 3217.41 17159.51 17 3632.15 20582.19 18 4072.03 24432.19 19 4537.05 28734.64 20 5027.2 33514.67 05-10-2010
20 rows selected.
7) Write a program to find the factorial of a number
SQL> ed p7;Contents of p7.sql are,
declaren number(3);fact number(5):=1;
beginn:=&n;while n>1loop
fact:=fact*n;n:=n-1;
end loop;dbms_output.put_line('Factorial: '||fact);
end;/
57
Dipanjan Sarkar 2010
SQL> @p7; 14 /Enter value for n: 5old 6: n:=&n;new 6: n:=5;Factorial: 120
PL/SQL procedure successfully completed.
8) Write a program to display the Fibonacci series upto 20000
SQL> ed p8;Contents of p8.sql are,
declare f number(5):=0;s number(5):=1;t number(5);l number(5);
beginl:=&l;dbms_output.put_line(f);dbms_output.put_line(s);t:=f+s;while t<lloop
dbms_output.put_line(t);f:=s;s:=t;t:=f+s;
end loop;end;/
SQL> ed p8;
SQL> @p8Enter value for l: 20000old 8: l:=&l;new 8: l:=20000;0 1 1 2 3 5
58
Dipanjan Sarkar 2010
8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711
PL/SQL procedure successfully completed.
Commit complete.
9) Write a program to display name, age and rating from table sailor from user input s_id
SQL> select *from sailor;
DipanjanSarkar S_ID S_NAME RATING AGE ---- --------------- ---------- ---------- s500 Dustain 7 45 s501 Brutus 1 33 s502 Lusber 8 55.5 s503 Andy 8 25.5 s504 Rusty 10 35 s505 Horatio 7 35 s506 Zorba 10 16 s507 Horatio 9 35.5 s508 Art 3 25.5 s509 Robin 6 47.5
SQL> ed p9;Contents of p9.sql are,
declares sailor.s_id%type;n sailor.s_name%type;a sailor.age%type;
59
Dipanjan Sarkar 2010
r sailor.rating%type;begin
s:='&s';select s_name,rating,age into n,r,a from sailor where s_id=s;dbms_output.put_line('S_id: '||s||' S_name: '||n||' Age: '||a||' Rating: '||r);
end;/
SQL> @p9;Enter value for s: s507old 7: s:='&s';new 7: s:='s507';S_id: s507 S_name: Horatio Age: 35.5 Rating: 9
PL/SQL procedure successfully completed.
Commit complete.
10) Write a program to calculate the salary from basic pay
SQL> ed p10Contents of p10.sql are,
declare basic number(10);da number(15,3);hra number(15,3);interim number(15,3);total number(15,3);
beginDBMS_OUTPUT.PUT_LINE('Enter The Basic Pay');basic:=&basic;da:=0.6*basic;hra:=0.2*basic;interim:=0.35*(basic+da);total:=basic+da+hra+interim;DBMS_OUTPUT.PUT_LINE('DA= '||da);DBMS_OUTPUT.PUT_LINE('HRA= '||hra);DBMS_OUTPUT.PUT_LINE('Interim= '||interim);DBMS_OUTPUT.PUT_LINE('Total= '||total);
end;/
SQL> @p10Enter value for basic: 15000
60
Dipanjan Sarkar 2010
old 9: basic:=&basic;new 9: basic:=15000;Enter The Basic Pay DA= 9000 HRA= 3000 Interim= 8400 Total= 35400
PL/SQL procedure successfully completed.
SQL> spool off;
61