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 Creation Syntax: - 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 Table Syntax: - 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 user Syntax: - 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: Suman Enter value for f_name: Kalyan Enter value for city: Asansol Enter value for sex: M Enter value for salary: 40000 Enter value for date_of_birth: 12-Nov-88 old 1: insert into employee values('&Name','&F_Name','&City','&Gender',&Salary,'&Date_of_B irth') 1
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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)…….);
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;
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
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
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
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)
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
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;
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)
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)
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'
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;
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
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.
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;
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;
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;
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
DipanjanSarkar B_ID B_NAME COLOR ---------- --------------- ---------- 101 interlake blue 102 interlake red 103 clipper green 104 marine red 21-09-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;