Вы находитесь на странице: 1из 60

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, 12Nov-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: 400 00 Enter value for date_of_birth: 12-Nov-88 old 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 database


Syntax: - select * from tab;
TNAME TABTYPE CLUSTERID ------------------------------------------------------------------------------EMPLOYEE TABLE

To see a table structure


Syntax: - desc <table name>; Example:- desc employee;

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 table


Syntax:- select * from <table name>; Example:- select * from employee; The output of the employee table will be as NAME FATHER_NAME CITY SEX SALARY DOB ------------------------------------------------------------------------------------------------------Samrat Mrinal Kolkata M 50000 16-DEC-88 Romit Ashok Kolkata M 45000 12-JAN-85 Anurupa Swapnil Delhi F 50000 16-MAY-88 Tamal Raj Durgapur M 45000 02-APR-89 Manish Abhay Raniganj F 20000 01-AUG-81 Suman Kalyan Asansol M 40000 12-NOV-88

To save the values in the database


Syntax:- commit; To save the values automatically the command is, Syntax:- set autocommit on;

To open a new editor


Syntax:- ed <editor name>; Example:- ed xyz; To run the editor in the database the command is Syntax:- @ <editor name>; Example:- @ xyz;

To copy a table from another table


Syntax:- 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 table Syntax:- 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 table


Syntax:- create table <new table name> (<new attributes name>) as select <old attribute name> from <old table name>;

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 table


Syntax:- 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 database


Syntax:- 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 bin


Syntax:- Show recycle bin;

To restore the deleted table from the recycle bin


Syntax:- flashback table <old table name> to before drop; Example:- flashback table employee4 to before drop;

Dipanjan Sarkar

2010

Questions for Assignment 2 ( Customer ) :

1. Create and insert given data in table customer 2. Add column stay_from_year 3. Set value of stay_from_year as 2001 for Italy\America and 2003 otherwise 4. Display credit limit attribute for America 5. Delete the record corresponding to Meg Sen 6. Show credit limit attribute for all America 7. Show all attributes for Italy 8. If territory is India and status is Single set value of credit to 7000 9. Rename cust_fname to first_name 10. Rename cust_lname to last_name 11. 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 cust1 14. Rename cust1 to cust_one 15. Insert values into cust2 table from customer table 16. 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 cust4 20. Drop table cust3 and then bring it back

Dipanjan Sarkar
SQL> ed file1.txt

2010

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: 147 Enter value for f_name: Aishwarya Enter value for l_name: Roberts Enter value for terr: AMERICA Enter value for c_lmt: 600.34 Enter value for mgr_id: 147 Enter value for dob: 01 -MAR-44 Enter value for m_status: single Enter value for gen: F Enter value for income: 130000 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(147,'Aishwarya','Roberts','AMERICA',600.34,147,'01 -MAR-44','sin 1 row created. SQL> / Enter value for cust_id: 148 Enter value for f_name: Gustav Enter value for l_name: Steel Enter value for terr: AMERICA Enter value for c_lmt: 600 Enter value for mgr_id: 148 Enter value for dob: 10 -APR-50 Enter value for m_s tatus: married Enter value for gen: M Enter value for income: 110000 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(148,'Gustav','Steel','AMERICA',600,148,'10 -APR-50','married','M

Dipanjan Sarkar
1 row created.

2010

SQL> / Enter value for cust_id: 352 Enter value for f_name: Ken Enter value for l_name: Reid Enter value for terr: ITALY Enter value for c_lmt: 3600.65 Enter value for mgr_id: 147 Enter value for dob: 13 -JULY-71 Enter value for m_status: single Enter value for gen: M Enter value for income: 30000 old 1: insert into customer values(&cust_id,'&f_name','&l_name','&terr',&c_lmt,&mgr_id,'&dob','&m_ new 1: insert into cust omer values(352,'Ken','Reid','ITALY',3600.65,147,'13 -JULY-71','single','M', 1 row created. SQL> / Enter value for cust_id: 360 Enter value for f_name: Helmet Enter value for l_name: Capshaw Enter value for terr: CHINA Enter value for c_lmt: 3600.65 Enter value for mgr_id: 148 Enter value for dob: 01 -AUG-77 Enter value for m_status: married Enter value for gen: M Enter value for income: 190000 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(360,'Helmet','Capshaw','CHINA',3600.65,148,'01 -AUG-77','married 1 row created. SQL> / Enter value for cust_id: 363 Enter value for f_name: Cathy Enter value for l_name: Lambert Enter value for terr: ITALY Enter value for c_lmt : 2400 Enter value for mgr_id: 147 Enter value for dob: 31 -AUG-56 Enter value for m_status: married Enter value for gen: M Enter value for income: 50000 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(363,'Cathy','Lambert','ITALY',2400,147,'31 -AUG-56','married','M 1 row created. SQL> / Enter value Enter value Enter value Enter value for for for for cust_id: 378 f_name: Meg l_name: Sen terr: THAILAND

Dipanjan Sarkar

2010

Enter value for c_lmt: 3700.78 Enter value for mgr_id: 159 Enter value for dob: 30 -SEP-55 Enter value for m_status: married Enter value for gen: M Enter value for income: 50000 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(378,'Meg','Sen','THAILAND',3700.78,159,'30 -SEP-55','married','M 1 row created. SQL> / Enter value for cust_id: 380 Enter value for f_name: Meryl Enter value for l_name: Holden Enter value for terr: INDIA Enter value for c_lmt: 3700 Enter value for mgr_id: 148 Enter value for dob: 10 -OCT-61 Enter value for m_status: single Enter value for gen: F Enter value for income: 160000 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(380,'Meryl','Holden','INDIA',3700,148,'10 -OCT-61','single','F', 1 row created. SQL> / Enter value for cust_id: 447 Enter value for f_name: Richard Enter value for l_name: Cappola Enter value for terr: ITALY Enter value for c_lmt: 500.76 Enter value for mgr_id: 147 Enter value for dob: 30 -OCT-81 Enter value for m_status: married Enter value for gen: F Enter value for income: 50000 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(447,'Richard','Cappola','ITALY',500.76,147,'30 -OCT-81','married 1 row created. SQL> / Enter value Enter value Enter value Enter value Enter value Enter value Enter value Enter value Enter value Enter value for for for for for for for for for for cust_id: 449 f_name: Rick l_name: Romero terr: ITALY c_lmt: 1500 mgr_id: 147 dob: 10 -DEC-51 m_status: single gen: F income: 30000

Dipanjan Sarkar

2010

old 1: insert into customer values(&cust_id,'&f_name','&l_name','&te rr',&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: 451 Enter value for f_name: Ridley Enter value for l_name: Hackman Enter value for terr: ITALY Enter value for c_lmt: 700.59 Enter value for mgr_id: 147 Enter value for dob: 11 -JAN-50 Enter value for m_status: single Enter value for gen: F Enter value for income: 150000 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(451,'Ridley','Hackman','ITALY',700.59,147,'11 -JAN-50','single', 1 row created. SQL> / Enter value for cust_id: 454 Enter value for f_name: Rob Enter value for l_name: Russell Enter value for terr: INDIA Enter value for c_lmt: 5000 Enter value for mgr_id: 148 Enter value for dob: 02 -MAR-77 Enter value for m_status: married Enter value for gen: M Enter value for income: 90000 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(454,'Rob','Russell','INDIA',5000,148,'02 -MAR-77','married','M', 1 row created. SQL> / Enter value for cust_id: 458 Enter value for f_name: Robert Enter value for l_ name: De niro Enter value for terr: INDIA Enter value for c_lmt: 3700 Enter value for mgr_id: 148 Enter value for dob: 12 -MAR-86 Enter value for m_status: single Enter value for gen: F Enter value for income: 150000 old 1: insert into customer values(&cu st_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.

Dipanjan Sarkar

2010

SQL> / Enter value for cust_id: 463 Enter value for f_name: Robin Enter value for l_name: Adjani Enter value for terr: INDIA Enter value for c_lmt: 1500 Enter value for mgr_id: 148 Enter value for dob: 01 -MAR-48 Enter value for m_status: married Enter value for gen: F Enter value for income: 50000 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(463,'Robin','Adjani','INDIA',1500,148,'01 -MAR-48','married','F' 1 row created. SQL> show pagesize pagesize 14 SQL> 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 = 'CHINA' or terr = 'THAILAND'; = 'INDIA' or terr

Dipanjan Sarkar
6 rows updated.

2010

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_YE AR ---------- --------------- --------------- ---------- ---------- ------------------ -------- - ---------- -----------147 Aishwarya Roberts AMERICA 600.34 147 01-MAR-44 single F 130000 20 01 148 Gustav Steel AMERICA 600 148 10-APR-50 married M 110000 20 01 352 Ken Reid ITALY 3600.65 14 7 13-JUL-71 single M 30000 20 01 360 Helmet Capshaw CHINA 3600.65 148 01-AUG-77 married M 190000 20 03 363 Cathy Lambert ITALY 2400 147 31-AUG-56 married M 50000 20 01 378 Meg Sen THAILAND 3700.78 159 30-SEP-55 married M 50000 20 03 380 Meryl Holden INDIA 3700 148 10-OCT-61 single F 160000 20 03 447 Richard Cappola ITALY 500.76 147 30-OCT-81 married F 50000 20 01 449 Rick Romero ITALY 1500 147 10-DEC-51 single F 30 000 20 01 451 Ridley Hackman ITALY 700.59 147 11-JAN-50 single F 150000 20 01 454 Rob Russell INDIA 5000 148 02-MAR-77 married M 90000 20 03 458 Robert De niro INDIA 3700 148 12-MAR-86 single F 150000 20 03 463 Robin Adjani INDIA 1500 148 01-MAR-48 married F 50000 20 03 SQL> commit; Commit complete. SQL> select c_lmt from customer where terr='AMERICA'; C_LMT ---------600.34 600 SQL> selec t *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_YE AR ---------- --------------- --------------- ---------- ---------- ------------------ -------- - ---------- -----------352 Ken Reid ITALY 3600.65 147 13-JUL-71 single M 30000 20 01 363 Cathy Lambert ITALY 2400 147 31-AUG-56 married M 50000 20 01 447 Richard Cappola ITALY 500.76 147 30-OCT-81 married F 50000 20 01 449 Rick Romero ITALY 1500 147 10-DEC-51 single F 30000 20 01 451 Ridley Hackman ITALY 700.59 147 11-JAN-50 single F 150000 20 01 SQL> dele te 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_YE AR ---------- --------------- --------------- ---------- ---------- ------------------ -------- - ---------- -----------147 Aishwarya Roberts AMERICA 600.34 147 01-MAR-44 single F 130000 20 01 148 Gustav Steel AMERICA 600 148 10-APR-50 married M 110000 20 01 352 Ken Reid ITALY 3600.65 147 13-JUL-71 single M 30000 20 01 360 Helmet Capshaw CHINA 3600.65 148 01-AUG-77 married M 190000 20 03 363 Cathy Lambert ITALY 2400 147 31-AUG-56 married M 50000 20 01 380 Meryl Holden INDIA 3700 148 10-OCT-61 single F 160000 20 03 447 Richard Cappola ITALY 500.76 147 30-OCT-81 married F 50000 20 01 449 Rick Romero ITALY 1500 147 10-DEC-51 single F 30000 20 01 451 Ridley Hackman ITALY 700.59 147 11-JAN-50 single F 150000 20 01 454 Rob Russell INDIA 5000 148 02-MAR-77 married M 90000 20 03 458 Robert De niro INDIA 3700 148 12-MAR-86 single F 150000 20 03 463 Robin Adjani INDIA 1500 148 01-MAR-48 married F 500 00 20 03 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_YE AR ---------- --------------- --------------- ---------- ---------- ------------------ -------- - ---------- -----------147 Aishwarya Roberts AMERICA 600.34 147 01-MAR-44 single F 130000 20 01 148 Gustav Steel AMERICA 600 148 10-APR-50 married M 110000 20 01 352 Ken Reid ITALY 3600.65 147 13-JUL-71 single M 30000 20 01 360 Helmet Capshaw CHINA 3600.65 148 01-AUG-77 married M 190000 20 03 363 Cathy Lambert ITALY 2400 147 31-AUG-56 married M 50000 20 01 380 Meryl Holden INDIA 7000 148 10-OCT-61 single F 160000 20 03 447 Richard Cappola ITALY 500.76 147 30-OCT-81 married F 50000 20 01 449 Rick Romero ITALY 1500 147 10-DEC-51 single F 30000 20 01 451 Ridley Hackman ITALY 700.59 147 11-JAN-50 single F 150000 20 01 454 Rob Rus sell INDIA 5000 148 02-MAR-77 married M 90000 20 03 458 Robert De niro INDIA 7000 148 12-MAR-86 single F 150000 20 03 463 Robin Adjani INDIA 1500 148 01-MAR-48 married F 50000 20 03 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_YE AR ---------- --------------- --------------- ---------- ---------- ------------------ -------- - ---------- -----------147 Aishwarya Roberts AMERICA 600.34 147 01-MAR-44 single F 130000 20 01 148 Gustav Steel AMERICA 600 148 10-APR-50 married M 110000 20 01 352 Ken Reid ITALY 3600.65 147 13-JUL-71 single M 30000 20 01 360 Helmet Capshaw CHINA 3600.65 148 01-AUG-77 married M 190000 20 03 363 Cathy Lambe rt ITALY 2400 147 31-AUG-56 married M 50000 20 01 380 Meryl Holden INDIA 7000 148 10-OCT-61 single F 160000 20 03 447 Richard Cappola ITALY 500.76 147 30-OCT-81 married F 50000 20 01 449 Rick Romero ITALY 1500 147 10-DEC-51 single F 30000 20 01 451 Ridley Hackman ITALY 700.59 147 11-JAN-50 single F 150000 20 01 454 Rob Russell INDIA 5000 148 02-MAR-77 married M 90000 20 03

12

Dipanjan Sarkar
458 Robert 12-MAR-86 single 463 Robin 01-MAR-48 married 12 rows selected. SQL> alter table customer rename column cust_lname t o last_name; Table altered. SQL> select *from customer; De niro 150000 Adjani 50000 INDIA 20 03 INDIA 20 03 70 00 1500

2010
148 148

F F

CUST_ID FIRST_NAME LAST_NAME TERR C_LMT MGR_ID DOB M_STATUS G INC STAY_FROM_YE AR ---------- --------------- --------------- ---------- ---------- ------------------ -------- - ---------- -----------147 Aishwarya Roberts AMERICA 600.34 147 01-MAR-44 single F 130000 20 01 148 Gustav Steel AMERICA 600 148 10-APR-50 married M 110000 20 01 352 Ken Reid ITALY 3600.65 147 13-JUL-71 single M 30000 20 01 360 Helmet Capshaw CHINA 3600.65 148 01-AUG-77 married M 190000 20 03 363 Cathy Lambert ITALY 2400 147 31-AUG-56 married M 50000 20 01 380 Meryl Holden INDIA 7000 148 10-OCT-61 single F 160000 20 03 447 Richard Cappola ITALY 500.76 147 30-OCT-81 married F 50000 20 01 449 Rick Rom ero ITALY 1500 147 10-DEC-51 single F 30000 20 01 451 Ridley Hackman ITALY 700.59 147 11-JAN-50 single F 150000 20 01 454 Rob Russell INDIA 5000 148 02-MAR-77 married M 90000 20 03 458 Robert De niro INDIA 7000 148 12-MAR-86 single F 150000 20 03 463 Robin Adjani INDIA 1500 148 01-MAR-48 married F 50000 20 03 12 rows selected. SQL> ttitle Dipanjan Sarkar SQL> btitle 17 -Aug-2010 SQL> 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
360 Helmet 01-AUG-77 married 363 Cathy 31-AUG-56 married 380 Meryl 10-OCT-61 single 447 Richard 30-OCT-81 married 449 Rick 10-DEC-51 single 451 Ridley 11-JAN-50 single 454 Rob 02-MAR-77 married 458 Robert 12-MAR-86 single 463 Robin 01-MAR-48 married Capshaw 190000 Lambert 50000 Holden 1600 00 Cappola 50000 Romero 30000 Hackman 150000 Russell 90000 De niro 150000 Adjani 50000 CHINA 2003 ITALY 2001 INDIA 2003 ITALY 2001 ITALY 2001 ITALY 2001 INDIA 2003 INDIA 2003 INDIA 2003 17-Aug-2010 12 rows selected. SQL> create table cust1 (cust_id number (3),f_name varchar2(15),dob date,income number(10,2)); Table created. SQL> insert customer; into cust1 select 3600.65 2400 7000 500.76 1500 700.59 5000 7000 1500

2010
148 147 148 147 147 147 148 148 148

M M F F F F M F F

varchar2(15),l_name

cust_id,first_name,last_name,dob,inc

from

12 rows created. SQL> set autocommit on; SQL> select *from cust1; DipanjanSarka r CUST_ID F_NAME ---------- --------------147 Aishwarya 148 Gustav 352 Ken 360 Helmet 363 Cathy 380 Meryl 447 Richard 449 Rick 451 Ridley 454 Rob 458 Robert 463 Robin L_NAME DOB INCOME --------------- --------- ---------Roberts 01 -MAR-44 130000 Steel 10 -APR-50 110000 Reid 13 -JUL-71 30000 Capshaw 01 -AUG-77 19 0000 Lambert 31 -AUG-56 50000 Holden 10 -OCT-61 160000 Cappola 30 -OCT-81 50000 Romero 10 -DEC-51 30000 Hackman 11 -JAN-50 150000 Russell 02 -MAR-77 90000 De niro 12 -MAR-86 150000 Adjani 01 -MAR-48 50000

17-Aug-2010 12 rows selected.

14

Dipanjan Sarkar
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),dob date,income number(10,2)); Table created. SQL> create table cust3 (cust_id number(3),f_name varchar2(15),dob date,income number(10,2)); Table created. SQL> create table cust4 (cust_id number(3), f_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 ---------- --------------147 Aishwarya 148 Gustav 352 Ken 360 Helmet 363 Cathy 380 Meryl 447 Richard 449 Rick 451 Ridley 454 Rob 458 Robert 463 Robin L_NAME DOB --------------- --------Roberts 01 -MAR-44 Steel 10 -APR-50 Reid 13 -JUL-71 Capshaw 01 -AUG-77 Lambert 31 -AUG-56 Holden 10 -OCT-61 Cappola 30 -OCT-81 Romero 10 -DEC-51 Hackman 11 -JAN-50 Russell 02 -MAR-77 De niro 12 -MAR-86 Adjani 01 -MAR-48

2010

varchar2(15),l_name

varchar2(15),l_name

varchar2(15),l_name

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 ---------- --------------147 Aishwarya 148 Gustav 352 Ken 360 Helmet 363 Cathy 380 Meryl 447 Richard 449 Rick 451 Ridley 454 Rob 458 Robert 463 Robin

L_NAME DOB --------------- --------Roberts 01 -MAR-44 Steel 10 -APR-50 Reid 13 -JUL-71 Capshaw 01 -AUG-77 Lambert 31 -AUG-56 Holden 10 -OCT-61 Cappola 30 -OCT-81 Romero 10 -DEC-51 Hackman 11 -JAN-50 Russell 02 -MAR-77 De niro 12 -MAR-86 Adjani 01 -MAR-48

17-Aug-2010 12 rows selected. SQL> insert customer; into cust2 select cust_id,first_name,last_name,dob,inc from

12 rows created. Commit complete. SQL> select *from cust2; DipanjanSarkar CUST_ID F_NAME ---------- --------------147 Aishwarya 148 Gustav 352 Ken 360 Helmet 363 Cathy 380 Meryl 447 Richard 449 Rick 451 Ridley 454 Rob 458 Robert 463 Robin L_NAME DOB INCOME --------------- --------- ---------Roberts 01 -MAR-44 130000 Steel 10 -APR-50 11 0000 Reid 13 -JUL-71 30000 Capshaw 01 -AUG-77 190000 Lambert 31 -AUG-56 50000 Holden 10 -OCT-61 160000 Cappola 30 -OCT-81 50000 Romero 10 -DEC-51 30000 Hackman 11 -JAN-50 150000 Russell 02 -MAR-77 90000 De niro 12 -MAR-86 150000 Adjani 01 -MAR-48 50000

17-Aug-2010 12 rows selected. SQL> insert into cust3 (cust_id,f_name,l_name) cust_id,first_name,last_name from customer where inc>50000; 7 rows cr eated. Commit complete. SQL> select *from cust3; select

16

Dipanjan Sarkar

2010

DipanjanSarkar CUST_ID F_NAME ---------- --------------147 Aishwarya 148 Gustav 360 Helmet 380 Meryl 451 Ridley 454 Rob 458 Robert

L_NAME DOB INCOME --------------- --------- ---------Roberts Steel Capshaw Holden Hackman Russell 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
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)

2010

SQL>insert into cust4 values ('&cust_id', '&f_name','&l_name','&dob',&income,'&mngr_name'); Enter value for cust_id: E001 Enter value for f_name: Robert Enter value for l_name: Brown Enter value for dob: 12 -MAR-88 Enter value for income: 50000 Enter value for mngr_name: Rahul old 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 -MAR88',50000,'Rahul') 1 row created. Commit complete. SQL> / Enter value for cust_id: E005 Enter value for f_name: Karan Enter value for l_name: Seth Enter value for dob: 11 -JUL-69 Enter value for income: 18000 Enter value for mngr_name: Deepak old 1: insert into cust4 values ('&cust_id','&f_name','&l_name','&dob',&income,'&mngr_name') new 1: insert into cust4 valu es ('E005','Karan','Seth','11 -JUL69',18000,'Deepak') 1 row created. Commit complete. SQL> / Enter value for cust_id: E004 Enter value for f_name: Anil Enter value for l_name: Sharma Enter value for dob: 23 -AUG-79 Enter value for income: 29000 Enter valu e for mngr_name: Ajit old 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 -AUG79',29000,'Ajit') 1 row created. Commit complete. SQL> / Enter value for cust_id: E015 Enter value for f_name: Suman Enter value for l_name: Adjani Enter value for dob: 20 -JAN-74

18

Dipanjan Sarkar

2010

Enter value for income: 85000 Enter value for mngr_name: Tapan old 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 -JAN74',85000,'Tapan') 1 row created. Commit complete. SQL> / Enter value for cust_id: E011 Enter value for f_name: Anirban Enter value for l_name: Sen Enter value for dob: 10 -OCT-78 Enter value for income: 55000 Enter value for mngr_name: Kabir old 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 -OCT78',55000,'Kabir') 1 row created. Commit complete. SQL> select *from cust4; DipanjanSarkar CUST_ID F_NAME ---------- -----------------E001 Robert E005 Karan E004 Anil E015 Su man E011 Anirban L_NAME DOB INCOME MNGR_NAME --------------- --------- ---------- ----------Brown Seth Sharma Adjani Sen 12 11 23 20 10 -MAR-88 -JUL-69 -AUG-79 -JAN-74 -OCT-78 50000 1 8000 29000 85000 55000 Rahul Deepak Ajit Tapan Kabir 17Aug-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 D ATE 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: E005 old 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: INDIA Enter value for cust_id: 001 old 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 terri tory: INDIA Enter value for cust_id: E001 old 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: CHINA Enter value for cust_id: E011 old 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: ITALY Enter value f or cust_id: E004 old 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: THAILAND Enter value for cust_id : E015 old 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
E004 ITALY E015 THAILAND E011 CHINA Anil Suman An irban Sharma Adjani Sen 23 -AUG-79 20 -JAN-74 10 -OCT-78

2010

29000 Ajit 85000 Tapan 55000 Kabir

17Aug-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 ---------- --------------147 Aishwarya 148 Gustav 360 Helmet 380 Meryl 451 Ridley 454 Rob 458 Robert L_NAME DOB INCOME --------------- --------- ---------Roberts Steel Capshaw Holden Hackman Russell De niro 17-Aug-2010 7 rows selected. SQL> drop cust3; drop cust3 * ERROR at line 1: ORA-00950: invalid DROP option SQL> drop tab le cust3; Table dropped. SQL> show recycle; ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME ---------------- ------------------------------ ----------- - -----------------CUST3 BIN$jJX7nhgoTsuHy0V9WImkVg==$0 TABLE 2010 -0817:15:31:17 SQL> flashbac k table cust3 to before drop; Flashback complete. SQL> drop table cust3; Table dropped.

21

Dipanjan Sarkar

2010

SQL> select *from tab; DipanjanSarkar TNAME -----------------------------CUSTOMER BIN$/49N8kaYQ9ShCsq8/cTK3A==$0 CUST2 CUST4 CUST_ONE TABTYPE CLUSTERID ------- ---------TABLE TABLE TABLE TABLE TABLE 17-Aug-2010 SQL> flashback table cust3 to before drop; Flashback complete. SQL> select *from tab; DipanjanSarkar TNAME -----------------------------CUSTOMER CUST3 CUST2 CUST4 CUST_ONE TABTYPE CLU STERID ------- ---------TABLE TABLE TABLE TABLE 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
SQL> create table student (st_id varchar2(10),st_address varchar2(15),ph_no varchar2(10),per_marks number(4,2)); Table created.

2010

varchar2(5),st_name number(8),e_mail

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: s001 Enter value for st_name: Rahul Enter value for st_address: Bandel Enter value for ph_no: 26112344 Enter value for e_mail: r@yahoo.co Enter value for per_marks: 88.35 old 1: insert int o student values ('&st_id','&st_name','&st_address',&ph_no,'&e_mail',&per_marks) new 1: insert into student values ('s001','Rahul','Bandel',26112344,'r@yahoo.co',88.35) 1 row created. Commit complete. SQL> ed sd SQL> @sd; Table created. SQL> select * from tab; DipanjanSarkar TNAME -----------------------------CUSTOMER CUST3 STUDENT STUD CUST2 CUST4 CUST_ONE TABTYPE CLUSTERID ------- ---------TABLE TABLE TABLE TABLE TABLE TABLE TABLE

17-Aug-2010 7 rows selected.

24

Dipanjan Sarkar
SQL> desc stud; Name Null? Type ------------------------------ -------- ---------------------My Name VARCHAR2(10) % Marks NUMBER(4,2) SQL> drop table stud purge; Table dropped.

2010

SQL> insert into student values ('&st_id','&st_name','&st_address',&ph_no, '&e_mail',&per_marks); Enter value for st_id: s003 Enter value for st_name: Ansh ul Enter value for st_address: Chandannagar Enter value for ph_no: 23467543 Enter value for e_mail: an@msn.com Enter value for per_marks: 39.75 old 1: insert into student values ('&st_id','&st_name','&st_address',&ph_no,'&e_mail',&per_marks) new 1: ins ert into student values ('s003','Anshul','Chandannagar',23467543,'an@msn.com',39.75) 1 row created. Commit complete. SQL> / Enter value for st_id: s005 Enter value for st_name: Karan Enter value for st_address: Calcutta Enter value for ph_no: 26831234 Enter value for e_mail: k1@red.com Enter value for per_marks: 75.35 old 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,'k1@red.com',75.35) 1 row created. Commit complete. SQL> / Enter value for st_id: s011 Enter value for st_name: Anil Enter value for st_address: Howrah Enter value for ph_no: 26712380 Enter value for e_mail: An@msn.com Enter value for per_marks: 30.58 old 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,'An@msn.com',30.58) 1 row created. Commit complete. SQL> / Enter value for st_id: s025 Enter value for st_name: Deepak Enter value for st_address: Liluah Enter value for ph_no: 26812005

25

Dipanjan Sarkar
Enter value for e_mail: d@mail.com Enter value for per_marks: 85.40 old 1: insert into student values ('&st_id','&st_na me','&st_address',&ph_no,'&e_mail',&per_marks) new 1: insert into student values ('s025','Deepak','Liluah',26812005,'d@mail.com',85.40) 1 row created. Commit complete. SQL> select *from student; DipanjanSarkar ST_ID ST_NAME ----- ---------s001 Rahul s003 Anshul s005 Karan s011 Anil s025 Deepak ST_ADDRESS PH_NO E_MAIL PER_MARKS --------------- ---------- ---------- ---------Bandel 26112344 r@yahoo.co 88.35 Chandannagar 23467543 an@msn.com 39.75 Calcutta 26831234 k1@red.com 75.35 Howrah 26712380 An@msn.com 30.58 Liluah 26812005 d@mail.com 85.4 17-Aug-2010 SQL> set pagesize 10; SQL> select *from student; DipanjanSarkar ST_ID ST_NAME ----- ---------s001 Rahul s003 Anshul s005 Karan s011 Anil s025 Deepak ST_ADDRESS PH_NO E_MAIL PER_MARKS --------------- ---------- ---------- ---------Bandel 26 112344 r@yahoo.co 88.35 Chandannagar 23467543 an@msn.com 39.75 Calcutta 26831234 k1@red.com 75.35 Howrah 26712380 An@msn.com 30.58 Liluah 26812005 d@mail.com 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 ----- ---------s001 Rahul s003 Anshul s005 Karan s011 Anil s025 Deepak ST_ADDRESS PH_NO E_MAIL %_marks --------------- ---------- ---------- ---------Bandel 26112344 r@yahoo.co 88.35 Chandannagar 23467543 an@msn.com 3 9.75 Calcutta 26831234 k1@red.com 75.35 Howrah 26712380 An@msn.com 30.58 Liluah 26812005 d@mail.com 85.4 17-Aug-2010

2010

SQL> update student set e_mail='&e_mail',st_address='&st_address' where st_id='s005'; Enter value for e_mail: x@mail.com Enter value for st_address: Mumbai old 1: update student s et e_mail='&e_mail',st_address='&st_address' where st_id='s005'

26

Dipanjan Sarkar

2010

new 1: update student set e_mail='x@mail.com',st_address='Mumbai' where st_id='s005' 1 row updated. Commit complete. SQL> select *from student; DipanjanSarkar ST_ID ST_NAME ----- ---------s001 Rahul s003 Anshul s005 Karan s011 Anil s025 Deepak ST_ADDRESS PH_NO E_MAIL %_marks --------------- ---------- ---------- ---------Bandel 26112344 r@yahoo.co 88.35 Chandannagar 23467543 an@msn.com 39.75 Mumbai 26831234 x@mail.com 75.35 Howrah 26712380 An@msn.com 30.58 Liluah 26812005 d@mail.com 85.4 17-Aug-2010 SQL> alter table student add (marks varchar2(6)); Table altered. SQL> select *from student; DipanjanSarkar ST_ID ST_NAME ----- ---------s001 Rahul s003 Anshul s005 Karan s011 Anil s025 Deepak ST_ADDRESS P H_NO E_MAIL %_marks MARKS --------------- ---------- ---------- ---------- -----Bandel 26112344 r@yahoo.co 88.35 Chandannagar 23467543 an@msn.com 39.75 Mumbai 26831234 x@mail.com 75.35 Howrah 26712380 An@msn.com 30.58 Liluah 26812005 d@mail.com 85.4 17-Aug-2010 SQL> desc student; Name -------------------------------------ST_ID ST_NAME ST_ADDRESS PH_NO E_MAIL %_marks MARKS Null? Type -------- -----------------VARCHAR2(5) VARCHAR2(10) VARCHAR2(1 5) NUMBER(8) VARCHAR2(10) NUMBER(4,2) VARCHA R2(6)

SQL> alter table student modify(marks varchar2(15)); Table altered. SQL> desc student; Name ---------------------------------------ST_ID ST_NAME ST_ADDRESS PH_NO E_MAIL %_marks MARKS Null? Type -------- ----------------VARCHAR2(5) VARCHAR2(10) VARCHAR2(15) NUMBER (8) VARCHAR2(10) NUMBER(4,2) VARCHAR2(15)

27

Dipanjan Sarkar
SQL> select *from student where "%_marks">70; DipanjanSarkar ST_ID ST_NAME ----- ---------s001 Rahul s005 Karan s025 Deepak

2010

ST_ADDRESS PH_NO E_MAIL %_marks MARKS --------------- ---------- ---------- ---------- -------Bandel 26112344 r@yahoo.co 88.35 Mumbai 26831234 x@mail.com 75.35 Liluah 26812005 d@mail.com 85.4 17-Aug-2010

SQL> select *from student; DipanjanSarkar ST_ID ST_NAME ----- --------- s001 Rahul s003 Anshul s005 Karan s011 Anil s025 Deepak Aug-2010 SQL> delete from studen t where "%_marks"<40; 2 rows deleted. Commit complete. SQL> select *from student; DipanjanSarkar ST_ID ST_NAME ----- ---------s001 Rahul s005 Karan s025 Deepak ST_ ADDRESS PH_NO E_MAIL %_marks MARKS --------------- ---------- ---------- ---------- ------Bandel 26112344 r@yahoo.co 88.35 Mumbai 26831234 x@mail.com 75.35 Liluah 26812005 d@mail.com 85.4 17Aug-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; ST_ADDRESS PH_NO E_MAIL %_marks MARKS --------------- ---------- ---------- ---------- -------Bandel 26112344 r@yahoo.co 88.35 Chandannagar 23467543 an@msn.com 39.75 Mumbai 26831234 x@mail.com 75.35 Howrah 26712380 An@msn.com 30.58 Liluah 26812005 d@mail.com 85.4 17-

28

Dipanjan Sarkar
DipanjanSarkar ST_ID ST_NAME ----- ---------s001 Rahul s005 Karan s025 Deepak

2010

ST_ADDRESS PH_NO E_MAIL %_marks MARKS --------------- ---------- ---------- ---------- --------Bandel 26112344 r@yahoo.co 88.35 Mumbai 2683 1234 x@mail.com 75.35 Liluah 26812005 d@mail.com 85.4 17-Aug-2010

SQL> drop table student2; Table dropped. SQL> select *from tab; DipanjanSarkar TNAME -----------------------------CUSTOMER CUST3 STUDENT BIN$v5cO9WTsTfqwndhY+35NYg==$0 CUST2 CUST4 CUST_ONE TABTYPE CLUSTERID ------- ---------TABLE TABLE TABLE TABLE TABLE TABLE 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 dept_id dept_name Datatype(size) number(3) varchar2(15) Constraints primary key ___

Insert 4 depts with names and ids 90, 69, 100 and 110. Create table cust_100 with the following attributes : Column name emp_id first_name last_name e_mail ph_no hire_date job_id salary mgr_id dept_id Datatype(size) number(3) varchar2(10) varchar2(10) varchar2(20) varchar2(15) date varchar2(10) number(8,2) number(3) number(3) Constraints Primary key Initial letter capital Initial letter capital and not null All upper case ____ Should be > than 01-jan-1980 Must begin with FI or AD or IT 4000 & 25000 ____ Foreign key, ref table dept

1. Add 10 records to cust_100 2. Drop column mrg_id 3. Add column mgr_id and make it self referenced such that first 4 ids correspond to first emp_id, next 4 correspond to fifth emp_id and the last 2 correspond to the ninth emp_id.

30

Dipanjan Sarkar
SQL> ttitle Dipanjan Sarkar SQL> btitle 24-08-2010 SQL> 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: 90 Enter value for dept_name: CSE old 1: insert into dept values(&dept_id,'&dept_name') new 1: insert into dept values(90,'CSE') 1 row created. SQL> / Enter value for Enter value for old 1: insert new 1: insert 1 row created. SQL> / Enter value for Enter value for old 1: insert new 1: insert 1 row created. SQL> / Enter value for Enter value for old 1: insert new 1: insert 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 dept_id: 110 dept_name: EE into dept values(&dept_id,'&dept_name') into dept values(110,'EE') dept_id: 100 dept_name: IT into dept values(&dept_id,'&dept_name') into dept values(100,'IT') dept_id: 60 dept_name: ECE into dept values(&dept_id,'&dept_name' ) into dept values(60,'ECE')

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;

31

Dipanjan Sarkar
Name ----------------------------------------EMP_ID FIRST_NAME LAST_NAME E_MAIL PH_NO HIRE_DATE JOB_ID SALARY MGR_ID DEPT_ID SQL> set autocommit on; Again from cust notepad file we have, Null? Type -------- ---------------NOT NULL NUMBER(3) VARCHAR2(10) NOT NULL VARCHAR2(10) VARCHAR2(20) VARCHAR2(15) DATE VARCHAR2(10) NUMBER(8,2) NUMBER(3) NUMBER(3)

2010

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: 004 Enter value for first_name: Karan Enter value for last_name: Grover Enter value for e_mail: KARAN@MSN.COM Enter value for ph_no: 26781345 Enter value for hire_date: 04-JUL-1982 Enter value for job_id: FI005 Enter value for salary: 12000 Enter value for mgr_id: 147 old 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','KARAN@MSN.COM','26781345','04JUL1982','FI005',12000,147, Enter value for dept_id: 100 old 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 SALARY MGR_ID DEPT_ID ---------- ---------- ---------------- ---------- ---------23 Manisha Das 14000 151 110 4 Karan Grover 12000 147 100 10 Deepta Naval 20000 145 90 15 Anshul Mehta 24750 151 110 34 Puja Dey 13550 172 60 87 Deepa Roy 16700 145 60 102 Sweta Sen 17900 172 110 114 Anil Sharma 12768 151 100 123 Rekha Singh 19000 148 60 127 Suman Ghosh 23000 145 60 E_MAIL PH_NO HIRE_DATE JOB_ID

-------------------- --------------- --------- ---------- --MANI_D@MSN.COM KARAN@MSN.COM DN@YAHOO.COM ANS@HOTMAIL.COM PUJ15@MAIL.COM DR18@YAHOO.COM SWETA@MAIL.COM ANI13@MSN.COM REKS@YAHOO.COM SUM21@MSN.COM 25647 856 26781345 26841209 27653432 26873450 26831009 23506780 26701890 26831670 26571345 15-SEP-83 IT058 04 -JUL-82 FI005 02 -MAR-85 AD013 04 -OCT-87 IT053 23 -MAY-83 AD101 25 -SEP-88 IT110 21 -JAN-86 AD035 18 -AUG-83 FI198 22 -NOV-89 AD125 21 -JUN-91 IT181

24-08-2010 10 rows selected.

32

Dipanjan Sarkar
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 VARCH AR2(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 SALARY DEPT_ID ---------- ---------------- ---------23 Manisha 14000 110 4 Karan 12000 100 10 Deepta 20000 90 15 Anshul 24750 110 34 Puja 13550 60 87 Deepa 16700 60 102 Sweta 17900 110 114 Anil 12768 100 123 Rekha 19000 60 127 Suman 23000 60 LAST_NAME E_MAIL PH_NO

2010

HIRE_DATE JOB_ID

---------- -------------------- --------------- --------- ---------- --Das Grover Naval Mehta Dey Roy Sen Sharma Singh Ghosh MANI_D@MSN.COM KARAN@MSN.COM DN@YAHOO.COM ANS@HOTMAIL.COM PUJ15@MAIL.COM DR18@YAHOO.COM SWETA@MAIL.COM ANI13@MSN.COM REKS@YAHOO.COM SUM21@MSN.COM 25647856 26781 345 26841209 27653432 26873450 26831009 23506780 26701890 26831670 26571345 15 -SEP-83 IT058 04-JUL-82 FI005 02 -MAR-85 AD013 04 -OCT-87 IT053 23 -MAY-83 AD101 25 -SEP-88 IT110 21 -JAN-86 AD035 18 -AUG-83 FI198 22 -NOV-89 AD125 21 -JUN-91 IT181

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 ---------- ---------- ---------- -------------------- --------------- --------- ---------- --------- ---------- ----------

33

Dipanjan Sarkar
23 Manisha 110 4 Karan 100 10 Deepta 90 15 Anshul 110 34 Puja 60 87 Deepa 60 102 Sweta 110 114 Anil 100 123 Rekha 60 127 Suman 60 Das Grover Naval Mehta Dey Roy Sen Sharma Singh Ghosh MANI_D@MSN.COM KARAN@MSN.COM DN@YAHOO.COM ANS@HOTMAIL.COM PUJ15@MAIL.COM DR18@YAHOO.COM SWETA@MAIL.COM ANI13@MSN.COM REKS@YAHOO.COM SUM21@MSN.COM 25647856 26781345 26841209 27653432 26873 450 26831009 23506780 26701890 26831670 26571345

2010

15 -SEP-83 IT058 04 -JUL-82 FI005 02 -MAR-85 AD013 04 -OCT-87 IT053 23-MAY-83 AD101 25 -SEP-88 IT110 21 -JAN-86 AD035 18 -AUG-83 FI198 22 -NOV-89 AD125 21 -JUN-91 IT181

14000 12000 20000 24750 13550 16700 17900 12768 19000 23000

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 Enter old new value for value for 1: update 1: update mgr_id: 023 emp_id: 023 cust_100 set mgr_id=&mgr_id where emp_id=&emp_id 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 SALARY DEPT_ID MGR_ID ---------- ---------- ---------------- ---------- ---------23 Manisha Das 14000 110 23 4 Karan Grover 12000 100 23 10 Deepta Naval 20000 90 23 15 Anshul Mehta 24750 110 23 34 Puja Dey 13550 60 34 87 Deepa Roy 16700 60 34 102 Sweta Sen 17900 110 34 114 Anil Sharma 12768 100 34 123 Rekha Singh 19000 60 123 127 Suman Ghosh 23000 60 123 E_MAIL PH_NO HIRE_DATE JOB_ID

-------------------- --------------- --------- ---------- --MANI_D@MSN.COM KARAN@MSN.COM DN@YAHOO.COM ANS@HOTMAIL.COM PUJ15@MAIL.COM DR18@YAHOO.COM SWETA@MAIL.COM ANI13@MSN.COM REKS@YAHOO.COM SUM21@MSN.COM 24-08-2010 25647856 26781 345 26841209 27653432 26873450 26831009 23506780 26701890 26831670 26571345 15 -SEP-83 IT058 04-JUL-82 FI005 02 -MAR-85 AD013 04 -OCT-87 IT053 23 -MAY-83 AD101 25 -SEP-88 IT110 21 -JAN-86 AD035 18 -AUG-83 FI198 22 -NOV-89 AD125 21 -JUN-91 IT181

10 rows selected. SQL> spool off;

34

Dipanjan Sarkar

2010

Assignment 5:

Create the following tables with the datatypes and constraints Sailor: Attribute s_id s_name Rating Age Boat: Attribute b_id b_name color Reserve: Attribute s_id b_id day Data type varchar2(4) number(3) date Constraints Foreign key reference sailor Foreign key reference boat < 1-Jan-2000 Data type Constraints number(3) primary key varchar2(10) varchar2(6) comes from the set blue, red, green Datatype Constraints varchar2(4) primary key & starts with small s varchar2(15) initcap number(2) number(3,1)

Table Creation: For table sailor SQL>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 boat SQL>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 reserve SQL>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

35

Dipanjan Sarkar

2010

Value insertion: For table sailor SQL>Insert into sailor values(&s_id, &s_name, &raing, &age);
Enter value for s_id: s500 Enter value for s_name: Dustain Enter value for rating: 7 Enter value for age: 45 old 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 boat SQL>Insert into boat values(&b_id, &b_name, &color);
Enter value for b_id: 101 Enter value for b_name: Interlake Enter value for color: blue old 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 reserve SQL>Insert into reserve values(&s_id, &b_id, &day);
Enter value for s_id: s500 Enter value for b_id: 101 Enter value for day: 10-oct-1998 old 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.

36

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

37

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 4 Rusty 5 Horatio 7 Zorpa 5 Horatio 7 Art 3 Bud 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

38

Dipanjan Sarkar Horatio Zorpa Horatio Art Bud Robin HORATIO ZORPA HORATIO ART BUD ROBIN

2010

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 FS - ------------- -------------------Dustain tn Brutus t Lusbe b Andy y Rusty t Horatio ao Zorpa p Horatio ao Art Bud Robin 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 tain Brutus tus Lusbe be Andy y Rusty ty Horatio atio Zorpa pa Horatio atio Art Bud Robin in

Display concatenate s_id and s_name SQL> select concat(s_id,s_name) from sailor;

39

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.65 1 2.83 2.83 3.16 2.65 3.16 3 1.73 2 2.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

40

Dipanjan Sarkar 35 16 35.5 25.5 51 47.5 35 16 36 26 51 48

2010

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;

41

Dipanjan Sarkar NO_DAYS -----------------4339 4339 4308 4400 4338 4338 4277 4493 4401

2010

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 ---------------10-OCT-98 10-OCT-98 10-NOV-98 10-AUG-98 11-OCT-98 11-OCT-98 11-DEC-98 09-MAY-98 09-AUG-98 TO_CHAR(D NEXT_DAY( ---------------- ----------------Saturday 12-OCT-98 Saturday 12-OCT-98 tuesday 16-NOV-98 Monday 17-AUG-98 sunday 12-OCT-98 Sunday 12-OCT-98 friday 14-DEC-98 Saturday 11-MAY-98 sunday 10-AUG-98

42

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

43

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;

44

Dipanjan Sarkar

2010

Assignment 6:
SQL> select *from sailor; DipanjanSarkar S_ID S_NAME ---- --------------s500 Dustain s501 Brutus s502 Lusber s503 Andy s504 Rusty s505 Horatio s506 Zorba s507 Horatio s508 Art s509 Robin

RATING AGE ---------- ---------7 45 1 33 8 55.5 8 25.5 10 35 7 35 10 16 9 35.5 3 25.5 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 1 02 11-OCT-98 s502 103 11 -OCT-98 s502 104 11 -DEC-98 s505 102 09 -AUG-98 s505 104 11 -OCT-98

45

Dipanjan Sarkar

2010

1. Find name and age of all sailors : SQL> select s_name,age from sailor; DipanjanSarkar S_NAME --------------Dustain Brutus Lusber Andy Rusty Horatio Zorba Horatio Art Robin

AGE ---------45 33 55.5 25.5 35 35 16 35.5 25.5 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

46

Dipanjan Sarkar 4. Name of sailors who have reserved red boat:

2010

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

47

Dipanjan Sarkar 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 :

2010

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

48

Dipanjan Sarkar 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 sa ilor,reserve,boat where sailor.s_id=reserve.s_id and reserve.b_id=boat.b_id and boat.color='green') ; S_ID ---s505

2010

49

Dipanjan Sarkar 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, declare n number(5):=0; num number(5); begin num:=&num; 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: 20 old 6: num:=&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;

2010

50

Dipanjan Sarkar Contents of p2.sql are, declare n number(5):=0; num number(5); begin num:=&num; while n<=num loop dbms_output.put_line(n); n:=n+3; end loop; end; / SQL> @p2; Enter value for num: 25 old 6: num:=&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 u sing for loop SQL> ed p3; Contents of p3.sql are, declare num number(5); x number(5); begin num:=&num; for x in 0..num

2010

51

Dipanjan Sarkar loop if mod(x,5)=0 then dbms_output.put_line(x); end if; end loop; end; / SQL> @p3; Enter value for num: 10 old 7: num:=&num; new 7: num:=10; 0 5 10 15 20 25 30 35 40 45 50 PL/SQL procedure successfully completed. Commit complete.

2010

4) Write a program that takes marks as input and displays grade using if -else ladder SQL> ed p4; Contents of p4.sql are, declare n number(3,1); begin n:=&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)

52

Dipanjan Sarkar then dbms_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: 90 old 4: n:=&n; new 4: n:=90; GRADE = O PL/SQL procedure successfully completed. Commit complete. SQL> @p4; Enter value for n: 40 old 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); begin while i<20 loop 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;

2010

53

Dipanjan Sarkar end loop; end; / SQL> create table random_num(rand_no number(20)); Table created. SQL> @p5; val generated is: val generated is: val generated is: val generated is: val generated is: val generated is: val generated is: val generated is: val generated is: val generated is: val generated is: val generated is: val generated is: val generated is: val generated is: val generated is: val generated is: val generated is: val generated is: val generated is:

2010

293315505 161987681 594817284 1146545030 -1985936008 1905424908 -2073800067 -1489384802 950799112 -791341352 -93869842 933454456 -1730170814 904025890 -890530728 1882737245 1407370383 -185434708 -282062135 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

54

Dipanjan Sarkar 950799112 -791341352 -93869842 933454456 -1.730E+09 904025890 -890530728 1882737245 1407370383 -185434708 -282062135 1071105009

2010

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, declare a number(10,2); v number(15,2); pi constant number(4,3):=3.142; begin for r in 1..20 loop 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;

55

Dipanjan Sarkar 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 28 734.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, declare n number(3); fact number(5):=1; begin n:=&n; while n>1 loop fact:=fact*n; n:=n-1; end loop; dbms_output.put_line('Factorial: '||fact); end; /

2010

56

Dipanjan Sarkar SQL> @p7; 14 / Enter value for n: 5 old 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); begin l:=&l; dbms_output.put_line(f); dbms_output.put_line(s); t:=f+s; while t<l loop dbms_output.put_line(t); f:=s; s:=t; t:=f+s; end loop; end; / SQL> ed p8; SQL> @p8 Enter value for l: 20000 old 8: l:=&l; new 8: l:=20000; 0 1 1 2 3 5

2010

57

Dipanjan Sarkar 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 PL/SQL procedure successfully completed. Commit complete.

2010

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, declare s sailor.s_id%type; n sailor.s_name%type; a sailor.age%type;

58

Dipanjan Sarkar 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||' '||r); end; / SQL> @p9; Enter value for s: s507 old 7: s:='&s'; new 7: s:='s507'; S_id: s507 S_name: Horatio

2010

Age: '||a||'

Rating:

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 p10 Contents of p10.sql are, declare basic number(10); da number(15,3); hra number(15,3); interim number(15,3); total number(15,3); begin DBMS_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> @p10 Enter value for basic: 15000

59

Dipanjan Sarkar 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;

2010

60

Вам также может понравиться