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

Ex.

No: 1 Date:

ANALYSIS OF DDL COMMANDS

AIM: To perform data definition language operations on a set of tables. 1. Create a table called student with column student name, reg. no. , dept & date of birth. SYNTAX Create table <table name>(column name1 datatype (size), column name2 datatype (size),......); QUERY SQL> Create table student (name varchar2(15),dept varchar2(3),reg_no number(10),dob date); OUTPUT Table created. 2. Give the logical structure of the table. SYNTAX desc <tablename>; QUERY SQL>desc student; OUTPUT Name NAME REG_NO DEPT DOB Null? Type VARCHAR2(15) NUMBER(3) VARCHAR2(10) DATE

3. Add a column name mark1 and mark2. SYNTAX alter table <table name> add(column name datatype(size));

QUERY SQL>alter table student add(mark1 number(4),mark2 number(4)); OUTPUT Table altered 4. Modify the size of column reg_no. SYNTAX alter table<table name> modify(column name datatype(new size)); QUERY SQL>alter table student modify(reg_no number(5)); OUTPUT Table altered 5.Add a column named mark3 in student table and update the size of field dept. SYNTAX alter table <table name> add(column name datatype(size))modify(column name datatype(new size)); QUERY SQL>alter table student add(mark3 number(4)) modify(dept varchar2(8)); OUTPUT Table altered 6. Delete the column named DOB. SYNTAX alter table <table name> drop(column name); QUERY SQL>alter table student drop(dob); OUTPUT Table altered 7. Create a table named books with fields name, author, no of copies.

SYNTAX create table <table name>(column name1 datatype (size), column name2 datatype (size),......); QUERY SQL>create table books(name varchar2(15),author varchar2(15),no_of_copies number(5)); OUTPUT Table created 8. Insert records for both tables. SYNTAX insert into <table name> values(columnvalue1,columnvalue2.....); QUERY SQL>insert into student values(mohit,142,CSE,90,92,98); SQL>insert into student values(manu,134,CSE,95,92,93); SQL>insert into books values(DBMS,Ullmaan,2); SQL>insert into books values(Alchemist, Paulo Coehlo,5); OUTPUT Row created 9. View the contents of both tables. SYNTAX select * from <tablename>; QUERY SQL>select * from student; select * from books; OUTPUT Name Mohit Manu Reg_no 142 134 CSE CSE Dept 90 95 Mark1 92 92 Mark2 98 93 Mark3

Name DBMS Alchemist

Author Ullmaan Paulo Coehlo

No_of_copies 2 5

10. Truncate the table named books SYNTAX truncate table<tablename>; QUERY SQL>truncate table books; OUTPUT Table truncated 11. Create a table and drop the table. SYNTAX create table <table name>(column name1 datatype (size), column name2 datatype (size),......); drop table <tablename>; QUERY SQL>create table marks(mark1 number(4),mark2 number(4)); SQL>drop table marks; SQL>select * from marks; OUTPUT Table created Table dropped.

RESULT: The programs were run successfully and all operations using data definition language were explored.

Ex. No.: 2 Date:

DATA MANIPULATION LANGUAGE COMMANDS

AIM: To perform Data Definition Language operations on a set of tables. 1. Create a table employee, with the following fields: Name, Employee id, Designation, Department, Date of Joining, Basic Pay SYNTAX: create table <table name> (column name1 datatype(<size>), column name2 datatype(<size>),. column name datatype(<size>)); QUERY: SQL>create table emp(name varchar2(20), empid number(10), designation varchar(10), dept varchar(10), DOJ date, basicpay number(8,2)); OUTPUT: Table created.

2. Describe the table. SYNTAX: desc <tablename>; QUERY: SQL>desc emp; OUTPUT: NAME EMPID DESIGNATION DEPT DOJ BASICPAY varchar(20) NUMBER(10) VARCHAR(10) VARCHAR(10) DATE NUMBER(8,2)

3. Insert records into the above table

SYNTAX: Insert into <tablename> values (&columnname1, &columnname2, &columnname3, .. &columnnamen);

QUERY: SQL>Insert into emp values (&name,&empid,&designation,&dept,&doj,&basicpay);

OUTPUT: Enter the value for name: John Enter the value for empid: 10001 Enter the value for designation: HOD Enter the value for dept: CSE Enter the value for DOJ: 03-MAR-2011 Enter the value for basicpay: 50000.00 SQL> / Enter the value for name: Peter Enter the value for empid: 10002 Enter the value for designation: Senior Prof Enter the value for dept: Mech Enter the value for DOJ: 01-JUN-2000 Enter the value for basicpay: 45000.00 SQL> / Enter the value for name: Elizabeth Enter the value for empid: 10003 Enter the value for designation: Assistant Prof Enter the value for dept: CHEM Enter the value for DOJ: 20-AUG-1999 Enter the value for basicpay: 40000.00 SQL> / Enter the value for name: Jack

Enter the value for empid: 10004 Enter the value for designation: HOD Enter the value for dept: Mech Enter the value for DOJ: 05-JUN-2003 Enter the value for basicpay: 60000.00 SQL> / 4. Insert only the value of employee id into the table

SYNTAX: Insert into <tablename>(column name) values (&columnname); QUERY: SQL>Insert into emp(empid) values(&empid); OUTPUT: Enter the value for empid: 10005 5. Insert a value into employee leaving only dept attribute. SYNTAX: Insert into <tablename> values (&columnname1, &columnname2, &columnname3, .. &column name); QUERY: SQL> Insert into emp values (Harry,10007,Assistant prof,NULL,11-JAN2010,25000.00); OUTPUT: 1 row created. 6. View the records of employee table SYNTAX: Select <column names> from <tablename>; QUERY: SQL> Select * from emp; OUTPUT:

Name John Peter Elizabh Jack Harry

Empid 10001 10002 10003 10004 10005 10007

Dept cse mech chem mech

DOJ 03-MAR-2011 01-JUN-2000 20-AUG-1999 05-JUN-2003 11-JAN-2010

Basicpay 50000 45000 40000 60000 25000

7. Display only name and empid from table employee SYNTAX: Select <column names> from <tablename>; QUERY: SQL> Select name,empid from emp; OUTPUT: NAME -------------John Peter Elizabeth Jack Harry EMPID -------------------10001 10002 10003 10004 10005 10007

8. Display all the records where dept is CHEM. SYNTAX: Select <column names> from <tablename> where condition; QUERY: SQL> Select * from emp where dept=Chem; OUTPUT: NAME EMPID DESIGNATION DEPT DOJ BASICPAY --------------------------------------------------------------------------------------------------Elizabeth 10003 Assistant Prof Chem 20-AUG-1999 40000.00 9. Delete the table books. SYNTAX: delete from table <tablename>;

QUERY: SQL> delete from table books; OUTPUT: Table deleted. 10. Add the following fields like Hra,Da,Pf, Netsalary to the employee table SYNTAX: Alter table <tablename> add (<column names> datatype(<size>)); QUERY: SQL> alter table emp add (HRA number(7), DA number(7), PF number(7), netsal number(7)); OUTPUT: Table altered. 11. Update the value of HRA, DA, PF and Net Salary SYNTAX: Update <tablename> set <column name>=values; QUERY: SQL> update emp set HRA=Basicpay/2, DA=150, PF=(20*Basicpay)/100, netsalary=Basicpay+DA+HRA-PF; OUTPUT: 7 rows updated. 12. View the records of employee table. SYNTAX: Select <column name> from <tablename>; QUERY: SQL> select * from emp; OUTPUT: Name Empid Dept DOJ Basicpay John 10001 cse 03-MAR-2011 50000 Peter 10002 mech 01-JUN-2000 45000 Elizabh10003 chem 20-AUG-1999 40000 Jack 10004 mech 05-JUN-2003 60000 10005 Harry 10007 11-JAN-2010 25000

HRA 25000 27500 20000 30000

DA 150 150 150 150

PF 10000 9000 8000 12000 5000

Netsal 65150 63650 68150 78150 42650

12500 150

13. Display the details of employee belonging to the cse dept and having salary more than 10000. SYNTAX: Select <column name> from <table name> where condition1 AND Condition2; QUERY: SQL> select * from emp where dept=cse and netsalary>10000; OUTPUT: Name Empid Dept John 10001 cse

DOJ Basicpay 03-MAR-2011 50000

HRA DA 25000 150

PF Netsal 10000 65150

14. Display details of emp NOT belonging to IT Dept SYNTAX: Select <column name> from <table name> where conditions; QUERY: SQL> select * from emp where NOT dept =IT; OUTPUT: Name Empid John 10001 Peter 10002 Elizabh10003 Jack 10004 10005 Harry 10007

Dept cse mech chem mech

DOJ Basicpay 03-MAR-2011 50000 01-JUN-2000 45000 20-AUG-1999 40000 05-JUN-2003 60000 11-JAN-2010 25000

HRA 25000 27500 20000 30000

DA 150 150 150 150

PF 10000 9000 8000 12000 5000

Netsal 65150 63650 68150 78150 42650

12500 150

15. Display the details of employee where salary is between 60000 and 70000 SYNTAX: Select <column name> from <table name> where conditions; QUERY: SQL> select * from emp where netsalary between 60000 and 70000; OUTPUT: Name Empid John 10001 Peter 10002 Elizabh10003

Dept cse mech chem

DOJ Basicpay 03-MAR-2011 50000 01-JUN-2000 45000 20-AUG-1999 40000

HRA 25000 27500 20000

DA 150 150 150

PF 10000 9000 8000

Netsal 65150 63650 68150

16. Display the details of emp in ascending order SYNTAX: Select <column name> from <table name> order by column name asc; QUERY: SQL> select * from emp order by netsalary asc; OUTPUT: Name Empid Dept 10005 Harry 10007 Peter 10002 John 10001 Elizabh10003 Jack 10004 mech cse chem mech

DOJ

Basicpay

HRA DA 12500 150 27500 25000 20000 30000 150 150 150 150

PF 5000 9000 10000 8000 12000

Netsal 42650 63650 65150 68150 78150

11-JAN-2010 25000 01-JUN-2000 45000 03-MAR-2011 50000 20-AUG-1999 40000 05-JUN-2003 60000

17. Display the details of emp in descending order SYNTAX: Select <column name> from <table name> order by column name desc; QUERY: SQL> select * from emp order by netsalary desc; OUTPUT: Name Empid Dept DOJ Basicpay HRA DA PF Netsal Jack 10004 mech 05-JUN-2003 60000 30000 150 12000 78150 Elizabh10003 chem 20-AUG-1999 40000 20000 150 8000 68150 John 10001 cse 03-MAR-2011 50000 25000 150 10000 65150 Peter 10002 mech 01-JUN-2000 45000 27500 150 9000 63650 Harry 10007 11-JAN-2010 25000 12500 150 5000 42650 10005 18. Display the details of emp, where the second letter of name is o. SYNTAX: Select <column name> from <table name> where column name like condition; QUERY: SQL> select * from emp where name LIKE _o%; OUTPUT: Name Empid Dept DOJ Basicpay HRA DA PF Netsal John 10001 cse 03-MAR-2011 50000 25000 150 10000 65150

RESULT: The programs were run successfully and all operations using data manipulation language were explored.

Ex. No: 3 Date:

BUILT IN FUNCTIONS

AIM: To perform built in functions operations on a set of tables. 1. Create Student table with following column Name, Regno, DOB, Department, Mark1, Mark2, Mark3. SYNTAX: Create table <tablename>(<Columnname1> <Datatype.....>); QUERY: create table student(name varchar2(20),dept varchar2(20),regno number(10),dob date,mark1 number(5),mark2 number(5),mark3 number(10)); OUTPUT: Table Created. 2. Insert more than 5 records to student table. SYNTAX: Insert into <tablename> values ('&columnname 1','&columnname n'); QUERY: SQL> insert into marks values('&name','&dept',&regno,'&dob',&mark1,&mark2,&mark3); OUTPUT: Enter value for name: Niru Enter value for dept: cse Enter value for regno: 61 Enter value for dob: 25-feb-1990 Enter value for mark1: 85 Enter value for mark2: 95 Enter value for mark3: 85 old 1: insert into marks values('&name','&dept',&regno,'&dob',&mark1,&mark2,&mark3) new 1: insert into marks values('niru','cse',61,'25-feb-1990',85,95,85)

1 row created.

SQL> / SQL> insert into marks values('&name','&dept',&regno,'&dob',&mark1,&mark2,&mark3); Enter value for name: Ranka Enter value for dept: cse Enter value for regno: 62 Enter value for dob: 26-mar-1991 Enter value for mark1: 80 Enter value for mark2: 90 Enter value for mark3: 89 old 1: insert into marks values('&name','&dept',&regno,'&dob',&mark1,&mark2,&mark3) new 1: insert into marks values('Ranka','cse',62,'26-mar-1991',80,90,89) 1 row created. SQL> / Enter value for name: Arvind Enter value for dept: cse Enter value for regno: 63 Enter value for dob: 28-apr-1989 Enter value for mark1: 89 Enter value for mark2: 87 Enter value for mark3: 89 old 1: insert into marks values('&name','&dept',&regno,'&dob',&mark1,&mark2,&mark3) new 1: insert into marks values('Arvind','cse',63,'28-apr-1989',89,87,99)

1 row created. SQL> /

Enter value for name: Ishak Enter value for dept: cse Enter value for regno: 64 Enter value for dob: 21-dec-1990 Enter value for mark1: 90 Enter value for mark2: 89 Enter value for mark3: 89 old 1: insert into marks values('&name','&dept',&regno,'&dob',&mark1,&mark2,&mark3) new 1: insert into marks values('ishak','cse',64,'21-dec-1990',99,99,99) 1 row created. SQL> / Enter value for name: nagu Enter value for dept: cse Enter value for regno: 65 Enter value for dob: 9-nov-1991 Enter value for mark1: 90 Enter value for mark2: 90 Enter value for mark3: 88 old 1: insert into marks values('&name','&dept',&regno,'&dob',&mark1,&mark2,&mark3) new 1: insert into marks values('nagu','cse',65,'9-nov-1991',84,89,96) 1 row created. SQL> set linesize 200; SQL> select *from marks;

NAME

DEPT

REGNO DOB

MARK1

MARK2

MARK3

-------------------- -------------------- ---------- --------- ---------- ---------- ---------Nirajan Ranka Arvind ishak nagu cse cse cse cse cse 61 25-FEB-90 62 26-MAR-91 63 28-APR-89 64 21-DEC-90 65 09-NOV-91 85 80 89 90 90 95 90 87 89 90 85 89 89 89 88

3. Find the value for sum of the 3 marks for each student. SYNTAX: Alter Table<tablename> add(columnname datatype); Update <tablename> set <columnname>='value'; Select<column name>From<tablename>; QUERY: Alter Table student add(total number(3)); Update Student set total=mark1+mark2+mark3; Select name,regno.total from Student; OUTPUT: NAME REG.NO TOTAL

------------------- -------------------- ---------------Nirajan ranka arvind ishak nagu 61 62 63 64 65 465 469 465 468 468

4. From Student Table (i)Display Maximum of reg.no and minimum of mark1 SYNTAX: Select Max(Columnname),min(columnname) from <tablename>; QUERY: SQL> select max(regno),min(mark3) from student; OUTPUT: MAX(REGNO) ---------65 MIN(MARK3) ---------85

(ii) Display Sum and Average of Mark3. SYNTAX: Select sum(Columnname),avg(Columnname) from <tablename>; QUERY: SQL> select sum(mark3),avg(mark3) from Student; OUTPUT: SUM(MARK3) ------440 AVG(MARK3) ---------88

(iii) Display the total number of records. SYNTAX: Select count(*) from <Tablename>; QUERY: SQL> select count(*) from Student;

OUTPUT: COUNT(*) ---------5

(iv) Display lowercase of name,uppercase of dept column. Synatx: Select Lower(Columnname),Upper(Columname) from <Tablename>; QUERY: SQL> select lower('ARVIND'),upper('nagu') from student; OUTPUT: LOWER UPPE ------ ---arvind NAGU arvind NAGU arvind NAGU arvind NAGU arvind NAGU (v) Find lastday of DOB. SYNTAX: Select last_day(columnname) from <tablename>; QUERY: SQL> select last_day(dob) from student; OUTPUT: LAST_DAY( --------28-FEB-90

31-MAR-91 30-APR-89 31-DEC-90 30-NOV-91 (vi)Find Next day of dob with Monday. SYNTAX: select next_day(<Columname>,<day>) from <tablename>; QUERY: 1* select next_day('26-mar-91','monday') from student SQL> / OUTPUT: NEXT_DAY( --------01-APR-91 01-APR-91 01-APR-91 01-APR-91 01-APR-91

vi) Display DOB and DOB added with 5 months. SYNTAX: Select dob,add_months(Columnname),(number) from <Tablename>; QUERY: SQL> select dob,add_months('26-Mar-91',5) from student;

OUTPUT: DOB ADD_MONTHS

--------- ------------25-FEB-90 26-AUG-91 26-MAR-91 26-AUG-91 28-APR-89 26-AUG-91 21-DEC-90 26-AUG-91 09-NOV-91 26-AUG-91 (viii) Select dob and Months between DOB with Current date. Syntax: Select Months_between((Column name),(System date)) from <Tablename>; QUERY: SQL> select months_between('29-apr-1993','29-jun-1992') from dual; OUTPUT: MONTHS_BETWEEN('29-APR-1993','29-JUN-1992') ------------------------------------------10 5. Find Absolute value for -110.100010. Syntax: Select abs(value) from <table name>; QUERY: Select abs(-110.10010) from dual; OUTPUT: ABS(-110.100010) ---------------110.10001

6.Find Ceil,floor,round,truncate function for the 45.12,45.68. Syntax: select ceil(values),floor(values),round(values),truc(values) from <tablename>; QUERY: select ceil(45.12), floor(45.12), round(45.12), trunc(45.12), ceil(45.68), floor(45.68), round(45.68), trunc(45.68) from student; OUTPUT: 46 45 45 45 46 45 46 45 7.Find sin,cos,tan,sinh,tanh,cosh for 90*; Syntax: Select sin(degree),cos(degree),tan(degree),sin(degree),tanh(degree),cosh(degree) from <tablename>; QUERY: SQL> select sin(90),cos(90),tan(90),sinh(90),cosh(90),tanh(90) from dual; SIN(90) COS(90) TAN(90) SINH(90) COSH(90) TANH(90) ---------- -------- -------- -------- ---------- --------------.893996664 -0.44807 -1.9952 6.1020 6.1020E+38 1

8.Find Mod and power value for 6,5. Syntax: Select mod(val1,val2),power(val1,val2) from <tablename>; QUERY: SQL> select mod(6,5),power(5,6) from dual;

OUTPUT:

MOD(5,6) POWER(5,6) --------- -----------1 7776 9. Convert the current date to 'Month' to 'year' format. SYNTAX: Select to_char(SYsdate,<format>) from dual; QUERY: SQL>select to_char(sysdate,'month dd year') from dual OUTPUT: TO_CHAR(SYSDATE,'MONTHDDYEAR') ----------------------------------------------------february 12 twenty thirteen 1* select to_char(dob,'month dd year') from marks SQL> / TO_CHAR(DOB,'MONTHDDYEAR') ------------------------------------------------------february 25 nineteen ninety march april 26 nineteen ninety-one 28 nineteen eighty-nine

december 21 nineteen ninety november 09 nineteen ninety-one

10.Display name and dept from student table use lpad and rpad for length 15 and character '*'. SYNTAX: Select lpad(<columnname number>,'character'),rpad(<columnname number,'character') from <tablename>; QUERY:

select rpad('nagu',15,'*'),rpad('cse',15,'*'),lpad('arvind',15,'*'),lpad('Cse',15,'*') from student;

RPAD('NAGU',15,'*') RPAD('CSE',15,'*') LPAD('arvind',15,'*')

LPAD('arvind',15,'*')

------------------- ------------------ ------------------------ --------------------nagu**************** ***************cse ******************arvind ******************cse nagu**************** ***************cse ******************arvind ******************cse nagu**************** ***************cse ******************arvind ******************cse nagu**************** ***************cse ******************arvind ******************cse nagu**************** ***************cse ******************arvind ******************cse 11.Display the number 17145 in dollar format. SYNTAX: Select to_char(value,'format') from <tablename>; QUERY: select to_char(17145,'$99999') from dual OUTPUT: TO_CHAR ------$17145

12.Write Queries using rtrim and ltrim. SYNTAX: Select replace('String1','String2','String3'),rtrim('String1','Substring'),ltrim('String','Substring') from <tablename>; QUERY:

SQL>select replace('Hello','el','**'),rtrim('India','ia'),ltrim('India','In') from dual; OUTPUT: REPLA RTR LTR ----- --- --H**lo Ind dia

RESULT: The PROGRAMs were run successfully and all operations using built in functions were explored.

Ex. No: 4 Date:

CONSTRAINTS

AIM : To analyze and perform the constraints commands of sql . 1.Create branch table with following attributes branch name as primary key, branchcity, assets. SYNTAX : Create table (columnname datatype(size),...); QUERY :

SQL> create table branch(branch_name varchar2(20)primary key,branch_city varchar2(20),assets varch OUTPUT : Table created. 2.Create account table with account number ,branch ,balance. SYNTAX : Create table <tablename>(columnname datatype(size),...); QUERY : SQL> create table account(account_no number(10),branch_name varchar2(10),balance number(10)); OUTPUT : Table created. SQL> desc account; Name Null? Type

----------------------------------------- -------- ---------------------------ACCOUNT_NO BRANCH_NAME BALANCE NUMBER(10) VARCHAR2(10) NUMBER(10)

3. Change the account_number as primary key and set branch_name as foreign key. SYNTAX : Alter table add primary key(column name); QUERY : SQL> Alter table account add primary key(account_no); OUTPUT : Table altered. SYNTAX : Alter table add foreign key(column_name)references (column_name); QUERY : SQL> Alter table account add foreign key(branch_name)references branch(branch_name); OUTPUT : Table altered.

Q4.Insert values to branch and account table and check the constraints. SYNTAX : insert into branch values('&column_name1','&column_name2',...); For table 'branch' : QUERY : SQL> insert into branch values('&branch_name','&branch_city',&assets); Enter value for branch_name: ktr Enter value for branch_city: chennai Enter value for assets: 12000 old 1: insert into branch values('&branch_name','&branch_city',&assets) new 1: insert into branch values('ktr','chennai',12000)

OUTPUT : 1 row created. SQL> / Enter value for branch_name: tambaram Enter value for branch_city: chennai Enter value for assets: 20000 old 1: insert into branch values('&branch_name','&branch_city',&assets) new 1: insert into branch values('tambaram','chennai',20000) OUTPUT : 1 row created. SQL> / Enter value for branch_name: egmore Enter value for branch_city: chennai Enter value for assets: 23000 old 1: insert into branch values('&branch_name','&branch_city',&assets) new 1: insert into branch values('egmore','chennai',23000) OUTPUT : 1 row created. For table account : QUERY : SQL> insert into account values(&account_no,'&branch_name',&balance); Enter value for account_no: 12300 Enter value for branch_name: ktr Enter value for balance: 11100 old 1: insert into account values(&account_no,'&branch_name',&balance) new 1: insert into account values(12300,'ktr',11100) OUTPUT : 1 row created. SQL> /

Enter value for account_no: 00456 Enter value for branch_name: ktr Enter value for balance: 22200 old 1: insert into account values(&account_no,'&branch_name',&balance) new 1: insert into account values(00456,'ktr',22200) OUTPUT : 1 row created. SQL> / Enter value for account_no: 90908 Enter value for branch_name: tambaram Enter value for balance: 11000 old 1: insert into account values(&account_no,'&branch_name',&balance) new 1: insert into account values(90908,'tambaram',11000) OUTPUT : 1 row created. SQL> select * from branch; BRANCH_NAME BRANCH_CITY ASSETS

-------------------- -------------------- ---------ktr tambaram egmore chennai chennai chennai 12000 20000 23000

SQL> select * from account; ACCOUNT_NO BRANCH_NAM ---------- ---------- ---------12300 456 90908 ktr ktr tambaram 11100 22200 11000 BALANCE

For Constraint Checking : SQL> insert into branch values('&branch_name','&branch_city',&assets); Enter value for branch_name: ktr Enter value for branch_city: chennai Enter value for assets: 11111 old 1: insert into branch values('&branch_name','&branch_city',&assets) new 1: insert into branch values('ktr','chennai',11111) insert into branch values('ktr','chennai',11111) OUTPUT : ERROR at line 1: ORA-00001: unique constraint (S1031010133.SYS_C004034) violated SQL> / Enter value for account_no: 90909 Enter value for branch_name: chengalpat Enter value for balance: 22222 old 1: insert into account values(&account_no,'&branch_name',&balance) new 1: insert into account values(90909,'chengalpat',22222) insert into account values(90909,'chengalpat',22222) OUTPUT : ERROR at line 1: ORA-02291: integrity constraint (S1031010133.SYS_C004055) violated - parent key not found

Q5.Delete any one record from branch table. SYNTAX :

delete from <tablename> where column_name = 'value'; QUERY : SQL> delete from branch where branch_name='ktr'; OUTPUT : 1 row deleted. SQL> select * from branch;

BRANCH_NAME

BRANCH_CITY

ASSETS

-------------------- -------------------- ---------tambaram egmore chennai chennai 20000 23000

Q6.Create customer table with customer name and number. SYNTAX : create table <tablename>(column_name datatypt(size),...); QUERY : SQL> create table customer(cust_name varchar2(10),cust_no number(10)); OUTPUT : Table created.

Q7.Change the number attribute of customer table as unique and create constraint name for uniqu SYNTAX : alter table <tablename> add unique (column_name); QUERY : SQL> alter table customer add constraint c1 unique(cust_name); OUTPUT : Table altered.

Q8.Create employee table with name ,emp_id,dept as not null. SYNTAX : create table <tablename>(column_name datatype(size) not null,...); QUERY : SQL> create table emp(name varchar2(10),emp_id number(10),dept varchar2(5) not null);

OUTPUT : Table created

Q9.Insert values to customer and emp table and check the constraints. SYNTAX : insert into <tablename> values('&column_name1','&column_name2'...); For Customer Table : QUERY : SQL> insert into customer values('&cust_name',&cust_no); Enter value for cust_name: mansi Enter value for cust_no: 8939008670 old 1: insert into customer values('&cust_name',&cust_no) new 1: insert into customer values('mansi',8939008670) OUTPUT : 1 row created. SQL> / Enter value for cust_name: prerna Enter value for cust_no: 123456789 old 1: insert into customer values('&cust_name',&cust_no) new 1: insert into customer values('prerna',123456789)

OUTPUT : 1 row created. Constraint Check : SQL> / Enter value for cust_name: mansi Enter value for cust_no: 2134567 old 1: insert into customer values('&cust_name',&cust_no) new 1: insert into customer values('mansi',2134567) insert into customer values('mansi',2134567) * ERROR at line 1: ORA-00001: unique constraint (S1031010133.C1) violated

For Emp Table : QUERY : SQL> insert into emp values('&name',&emp_id,'&dept'); Enter value for name: mansi Enter value for emp_id: 133 Enter value for dept: cse old 1: insert into emp values('&name',&emp_id,'&dept') new 1: insert into emp values('mansi',133,'cse') OUTPUT : 1 row created. SQL> / Enter value for name: manu Enter value for emp_id: 134

Enter value for dept: cse old 1: insert into emp values('&name',&emp_id,'&dept') new 1: insert into emp values('manu',134,'cse') OUTPUT : 1 row created. Constraint Checking : SQL> / Enter value for name: prerna Enter value for emp_id: 234 Enter value for dept: old 1: insert into emp values('&name',&emp_id,'&dept') new 1: insert into emp values('prerna',234,'') insert into emp values('prerna',234,'') * OUTPUT : ERROR at line 1: ORA-01400: cannot insert NULL into ("S1031010133"."EMP"."DEPT")

Q10.Drop the primary key of branch table. Delete the constraint of customer table. SQL> select constraint_name,constraint_type,table_name from user_constraints; CONSTRAINT_NAME C TABLE_NAME

------------------------------ - -----------------------------SYS_C004050 SYS_C004055 SYS_C004034 SYS_C004124 SYNTAX : P ACCOUNT R ACCOUNT P BRANCH C EMP

alter table <tablename> drop constraint constraint_name; QUERY : SQL> alter table account drop constraint SYS_C004055; OUTPUT : Table altered.

Q11.Create client table with client_no,name,address,city,state attribute with following constraint. a) Client no values should start with capital C . b) Value of name should be in upper case. c) City column should allow only Bombay,Madras,delhi,Calcutta. SYNTAX : create table<tablename>(column_name datatype(size) check constraint); QUERY : SQL> create table client(client_no varchar2(10) check(client_no like 'C%'),client_name varc har2(20)check(client_name=upper(client_name)),address varchar2(20),city varchar2(10) check(city in ('Bombay','Delhi','Madras','Calcutta'))) OUTPUT : Table created. SQL> insert into client values('&client_no','&client_name','&address','&city'); Enter value for client_no: mansi Enter value for client_name: mansi Enter value for address: 234,e-block Enter value for city: chennai old 1: insert into client values('&client_no','&client_name','&address','&city') new 1: insert into client values('mansi','mansi','234,e-block','chennai') insert into client values('mansi','mansi','234,e-block','chennai') OUTPUT: ERROR at line 1: ORA-02290: check constraint (S1031010133.SYS_C004173) violated

SQL>/ Enter value for client_no: C1234 Enter value for client_name: MANSI Enter value for address: 24,e-block Enter value for city: Madras old 1: insert into client values('&client_no','&client_name','&address','&city') new 1: insert into client values('C1234','MANSI','24,e-block','Madras') OUTPUT : 1 row created. RESULT : Constraints command are successfully executed displaying the output.

Ex. No.: 5 Date:

SET OPERATIONS

AIM: To perform set operations on a set of tables. 1. Create table student with the following fields name, register no, dob, course, branch, semester, percentage. SYNTAX: Create table <table name> (column name1 datatype(<size>), column name2 datatype(<size>),. column namen datatype(<size>)); QUERY: SQL> Create table student(name varchar2(20), regno number(10), dob date, course varchar(20), sem varchar(6), percentage number(3,2)); OUTPUT: Table created. 2. Create table employee with emp name, emp_no, dept, DOJ and BasicPay SYNTAX: Create table <table name> (column name1 datatype(<size>), column name2 datatype(<size>),. column namen datatype(<size>)); QUERY: SQL> Create table emp (emp_name varchar2(20), doj date, basicpay number(5,2)); OUTPUT: Table created. 3. Insert 5 records into employee and student tables. SYNTAX: Insert into <table name> values (&columnnames); QUERY: SQL> Insert into student values (&name,&regno,&dob,&course,&branch,&sem,&percentage); Insert into employee values (&emp_name,emp_no,&doj,&basicpay); OUTPUT: Enter the value for name: John Enter the value for regno:045 Enter the value for dob:03-DEC1992 Enter the value for course: B. Tech Enter the value for branch: ECE

Enter the value for sem: 6 Enter the value for percentage: 85 SQL> / Enter the value for name: Peter Enter the value for regno: 187 Enter the value for dob:05-OCT-199 Enter the value for course: B.E Enter the value for branch: Mechtronics Enter the value for sem:6 Enter the value for percentage:77 SQL> / Enter the value for name: Elizabeth Enter the value for regno: 031 Enter the value for dob: 16-AUG-1992 Enter the value for course: B. Tech Enter the value for branch: BioTech Enter the value for sem: 6 Enter the value for percentage: 85 SQL> / Enter the value for name: Mary Enter the value for regno: 155 Enter the value for dob: 17-AUG-1992 Enter the value for course: B. Tech Enter the value for branch: CSE Enter the value for sem: 6 Enter the value for percentage: 90 SQL> / Enter the value for name: Monica Enter the value for regno: 144 Enter the value for dob: 25-SEP-1992 Enter the value for course: B. E Enter the value for branch: Chem Enter the value for sem: 6 Enter the value for percentage: 89 SQL> / Enter the value for emp_name: Jack Enter the value for emp_no: 144 Enter the value for dept: cse Enter the value for doj: 10-JAN-1992 Enter the value for basicpay: 40000.00 SQL> / Enter the value for emp_name: Charles Enter the value for emp_no: 103

Enter the value for dept: mech Enter the value for doj: 03-JUL-2011 Enter the value for basicpay: 50000.00 SQL>/ Enter the value for emp_name: Sachin Enter the value for emp_no: 132 Enter the value for dept: Chem Enter the value for doj: 09-AUG-2010 Enter the value for basicpay: 45000.00 4. Display the reg no and emp no. from student and employee tables SYNTAX: Select <column name> from <table name1> UNION select <column name> from <table name2>; QUERY: SQL> Select regno from student UNION select empno from emp; OUTPUT: Regno 045 187 031 035 156 882 889 167 5. Display the dept common to both tables SYNTAX: select <column name> from <table name> INTERSECTION select <column name> from <table name>; QUERY: SQL> select branch from student INTERSECTION (select dept from employee); OUTPUT: Branch Ece Chem Mech

6. Display the dept available in student but not in employee SYNTAX: select <column names> from <table name> MINUS select <column name> from <tablename>; QUERY: SQL> Select branch from student minus select dept from employee; OUTPUT: Branch Cse Food RESULT: The programs were run successfully and all operations using set operations were explored.

Ex. No. : 6 Date:

GROUP BY AND JOIN OPERATIONS

AIM: To perform group by and join operations on a set of tables. 1. Create a client table with client number, name and city. SYNTAX: create table <TABLE NAME> (<COLUMN NAME1> <data type>, <COLUMN NAME2> <data type>, ); QUERY: SQL> create table cli(client_name varchar2(15), client_no number(10), city varchar2(15)); OUTPUT: Table created.

2. Create a product table with product number, description, price and quantity. SYNTAX: create table <TABLE NAME> (<COLUMN NAME1> <data type>, <COLUMN NAME2> <data type>, ); QUERY: SQL> create table product(product_no number(10), descrption varchar2(20), price number(10), quantity number(10)); OUTPUT: Table created.

3. Create a sales order table with order number, client number, product number and quantity. SYNTAX: create table <TABLE NAME> (<COLUMN NAME1> <data type>, <COLUMN NAME2> <data type>, ); QUERY: SQL> create table sales(order_no number(10), client_no number(10), product_no number(10), quantitynumber(10)); OUTPUT: Table created.

4. Insert values into product, sales order and client tables. SYNTAX: insert into <TABLE NAME> values (&<COLUMN NAME1>, &<COLUMN NAME2>, ); QUERY: SQL> insert into cli values('&client_name', &client_no, '&city'); SQL> insert into product values(&product_no,'&description',&price,&quantity); SQL> insert into sales values(&order_no,&client_no,&product_no,&quantity); OUTPUT: Enter value for client_name: mansi Enter value for client_no: 133 Enter value for city: Chennai old 1: insert into cli values('&client_name',&client_no,'&city') new 1: insert into cli values('mansi',133,'chennai') 1 row created. Enter value for client_name: manya Enter value for client_no: 134 Enter value for city: delhi old 1: insert into cli values('&client_name',&client_no,'&city') new 1: insert into cli values('manya',134,'delhi') 1 row created. Enter value for product_no: 101 Enter value for description: ironbox Enter value for price: 1000 Enter value for quantity: 2 old 1: insert into product(&product_no,'&description',&price,&quantity) new 1: insert into product(101,'ironbox',1000,2) insert into product(101,'ironbox',1000,2) Enter value for order_no: 99 Enter value for client_no: 133 Enter value for product_no: 504 Enter value for quantity: 2 old 1: insert into sales values(&order_no,&client_no,&product_no,&quantity) new 1: insert into sales values(99,133,504,2) 1 row created.

Enter value for order_no: 55 Enter value for client_no: 135 Enter value for product_no: 101 Enter value for quantity: 2 old 1: insert into sales values(&order_no,&client_no,&product_no,&quantity) new 1: insert into sales values(55,135,101,2) 1 row created. Enter value for order_no: 23 Enter value for client_no: 135 Enter value for product_no: 101 Enter value for quantity: 5 old 1: insert into sales values(&order_no,&client_no,&product_no,&quantity) new 1: insert into sales values(23,135,101,5) 1 row created. Enter value for order_no: 667 Enter value for client_no: 133 Enter value for product_no: 504 Enter value for quantity: 5 old 1: insert into sales values(&order_no,&client_no,&product_no,&quantity) new 1: insert into sales values(667,133,504,5) 1 row created. Enter value for order_no: 78 Enter value for client_no: 133 Enter value for product_no: 101 Enter value for quantity: 2 old 1: insert into sales values(&order_no,&client_no,&product_no,&quantity) new 1: insert into sales values(78,133,101,2) 1 row created.

5. Display the product number and the total quantity ordered from the sales order table. SYNTAX: select <COLUMN NAME1>, <COLUMN NAME2>, .. from <TABLE NAME> groupby <COLUMN NAME>; QUERY: SQL> select product_no,sum(quantity) from sales groupby product_no; OUTPUT: PRODUCT_NO QUANTITY

---------- ---------504 2 101 2

6. Display product number and total quantity ordered for products 11, 13 from sales order table. SYNTAX: select <COLUMN NAME1>, <COLUMN NAME2>, .. from <TABLE NAME> group by <COLUMN NAME> having <COLUMN NAME1> in (11, 13); QUERY: SQL> select product_no,sum(quantity) from sales group by product_no having product_no in (11, 13); OUTPUT: PRODUCT_NO SUM(QUANTITY) ---------- ------------101 9 504 7

7. Using subqueries display all the orders placed by client AAAA from the sales order table. SYNTAX: select <TABLE NAME1.COLUMN NAME1>, <TABLE NAME1.COLUMN NAME2>, from <TABLE NAME1> where <TABLE NAME1.COLUMN NAMEx> = ( select <TABLE NAME2.COLUMN NAME1>, <TABLE NAME2.COLUMN NAME2>, from <TABLE NAME2> where <Condition> ); QUERY: SQL> select order_no from sales where client_no=(select client_no from cli where client_name=AAAA); OUTPUT: ORDER_NO ---------99 667 78

8. Select the total quantity of order placed for each product with product no and description. SYNTAX: select <TABLE NAME1.COLUMN NAME1>, <TABLE NAME2.COLUMN NAME1>, .., <TABLE NAME2.COLUMN NAME1>, <TABLE NAME2.COLUMN NAME2>, from <TABLE NAME1>, <TABLE NAME2> where <TABLE NAME1.COLUMN NAMEx> = <TABLE NAME2.COLUMN NAMEy>; QUERY: SQL> select sum(S.quantity), S.product_no, product.descrption from (select * from sales group by quantity) S, product where S.product_no = product.product_no; OUTPUT: QUANTITY PRODUCT_NO DESCRPTION ---------- ---------- -------------------9 101 ironbox 7 504 iphone

9. Select the orders placed by client 133. SYNTAX: select <TABLE NAME1.COLUMN NAME1>, <TABLE NAME2.COLUMN NAME1>, .., from <TABLE NAME1> where <Condition>; QUERY: SQL> select order_no from sales where client_no=133; OUTPUT: ORDER_NO ---------99 667 78

10. Select the order placed on product number 101. SYNTAX: select <COLUMN NAME> from <TABLE NAME> where <Condition>;

QUERY: SQL> select sales.order_no from sales, products where product_no=101; OUTPUT: ORDER_NO ---------55 23 78

11. Select the product whose quantity on hand is less than quantity ordered. SYNTAX: select <COLUMN NAME1>,<COLUMN NAME2>,.. from <TABLE NAME1>,<TABLE NAME2> where <Condition>; QUERY: SQL> select sales.order_no,product.product_no from sales,product where sales.product_no=product.product_no and product.quantity<sales.quantity; OUTPUT: ORDER_NO PRODUCT_NO 23 667 101 504

12. Create Employee table with emp_no, name and manager number. SYNTAX: create table <TABLE NAME> (<COLUMN NAME1> <data type>, <COLUMN NAME2> <data type>, ); QUERY: SQL> create table emp1(emp_no number(10),name varchar2(20),manager_no number(10)); OUTPUT: Table Created.

13. Create bank table with employee name, account number and balance.

SYNTAX: create table <TABLE NAME> (<COLUMN NAME1> <data type>, <COLUMN NAME2> <data type>, ); QUERY: SQL> create table bank(emp_name varchar2(20),account_no number(20),balance number(10));number(10)); OUTPUT: Table Created.

14. Insert values into both tables. SYNTAX: insert into <TABLE NAME> values (&<COLUMN NAME1>, &<COLUMN NAME2>, ); QUERY: SQL> insert into emp1 values(&emp_no,'&name',&manager_no); SQL> insert into bank values('&emp_name',&account_no,&balance); OUTPUT: Enter value for emp_no: 133 Enter value for name: mansi Enter value for manager_no: 99 old 1: insert into emp1 values(&emp_no,'&name',&manager_no) new 1: insert into emp1 values(133,'mansi',99) 1 row created. SQL> / Enter value for emp_no: 135 Enter value for name: manya Enter value for manager_no: 1000 old 1: insert into emp1 values(&emp_no,'&name',&manager_no) new 1: insert into emp1 values(135,'manya',1000) 1 row created. Enter value for emp_name: priya Enter value for account_no: 123456 Enter value for balance: 50000 old 1: insert into bank values('&emp_name',&account_no,&balance) new 1: insert into bank values('priya',123456,50000) 1 row created. SQL> / Enter value for emp_name: mansi Enter value for account_no: 9999

Enter value for balance: 10000 old 1: insert into bank values('&emp_name',&account_no,&balance) new 1: insert into bank values('mansi',9999,10000) 1 row created. SQL> / Enter value for emp_name: prerna Enter value for account_no: 006767 Enter value for balance: 40000 old 1: insert into bank values('&emp_name',&account_no,&balance) new 1: insert into bank values('prerna',006767,40000) 1 row created. 15. Retrieve names of employees and names of their respective managers from the employee table. SYNTAX: select <COLUMN NAME1>, <COLUMN NAME2> from <TABLE NAME> T1, <TABLE NAME> T2 where <condition>; QUERY: SQL> select E.name, M.name from employee E, employee M where E.manager_no = M.id; OUTPUT: E.name M.name Mansi aa Likhit bb Aman cc Manya dd 16. Perform outer join on table bank & employee. SYNTAX: select <COLUMN NAME1>, <COLUMN NAME2> FROM <TABLE NAME> from outer join <TABLE NAME> on TABLE NAME.COLUMN NAME = TABLE NAME.COLUMN NAME; QUERY: SQL> select employee.*, bank.* from employee from outer join bank on employee.name = bank.name; OUTPUT: Emp_No Name Manager_No Manager Name Acc. No. Bank 1 Manu 2 Manu 123 20000 2 Sam 3 3 Rohan 1 4 Aman 5 jayant 234 30000 RESULT: The programs were run successfully and all operations using group by and join were explored.

: Ex. No. : 7 Date: AIM: To perform operations on views and index. 1. Create a table with item code, item name, price. SYNTAX: create table <table name>(column name datatype(size),..); QUERY: SQL> create table item(itemcode number(10),itemname varchar(20),price number(10)); OUTPUT: Table Created. 2.Create a customer table with name,item code, status of delivery and quantity. SYNTAX: Create table <table name>(column name datatype(size),..); QUERY: SQL> create table customer(name varchar(20),itemcode number(10),sod varchar(10),qty number(10)); OUTPUT: Table Created. 3.Insert records into item and coustomer table SYNTAX: insert into <table name> values (value1,value2); QUERY: SQL> insert into item values('&itemcode','&itemname','&price'); Enter value for itemcode: 11 Enter value for itemname: bag Enter value for price: 200 VIEW AND INDEX

old 1: insert into item values('&itemcode','&itemname','&price') new 1: insert into item values('11','bag','200')

1 row created.

SQL> / Enter value for itemcode: 17 Enter value for itemname: books Enter value for price: 300 old 1: insert into item values('&itemcode','&itemname','&price') new 1: insert into item values('17','books','300')

1 row created.

SQL> / Enter value for itemcode: 35 Enter value for itemname: furniture Enter value for price: 700 old 1: insert into item values('&itemcode','&itemname','&price') new 1: insert into item values('35','furniture','700')

1 row created. SQL> insert into customer values('&name','&itemcode','&sod','&qty'); Enter value for name: pallavi Enter value for itemcode: 11 Enter value for sod: pending Enter value for qty: 4

old 1: insert into customer values('&name','&itemcode','&sod','&qty') new 1: insert into customer values('pallavi','11','pending','4')

1 row created.

SQL> / Enter value for name: pankhudri Enter value for itemcode: 35 Enter value for sod: pending Enter value for qty: 6 old 1: insert into customer values('&name','&itemcode','&sod','&qty') new 1: insert into customer values('pankhudri','35','pending','6')

1 row created.

SQL> / Enter value for name: tara Enter value for itemcode: 17 Enter value for sod: pending Enter value for qty: 7 old 1: insert into customer values('&name','&itemcode','&sod','&qty') new 1: insert into customer values('tara','17','pending','7')

1 row created.

4.Create view having item code, item name from item table. SYNTAX:

Create view <view table name> as select * from <table name>; QUERY: SQL>create view itemlist as select itemcode,itemname from item; OUTPUT: View created. 5.Create view from customer table having status of not delivered. SYNTAX: Create view <view table name> as select * from <table name> where condition; QUERY: SQL>create view status as select sod from customer where sod='pending'; OUTPUT: View created.

6.Create view having item code, total amount of quantity from customer table. SYNTAX: Create view <view table name> as select * from <table name> QUERY: SQL> create view amount as select itemcode,qty from customer; OUTPUT: View created.

7.Create view having item code, item name from item table and quantity & state from customer table. SYNTAX: Create view <view table name> as select * from <table name> QUERY: SQL>create view orderof as select a.itemcode, a.itemname, b.qty, b.sod from item a, customer b where a.itemcode=b.itemcode;

OUTPUT: View created. 8.Create view with read only option. SYNTAX: Create view <view table name> as select * from <table name> with read only. QUERY: SQL> create view v1 as select itemcode from item with read only; OUTPUT: View created.

9.Perform insert, delete and update operations on view SYNTAX: Insert into <viewname> values(value); Delete from <viewname> where columnname=value; Update <viewname> set columname=value QUERY: SQL> insert into amount values(10,4); 1 row created. SQL> delete from amount where itemcode='10'; 1 row deleted. SQL>update amount set qty=6 where itemcode='11 1 row updated. 10.Drop any view. SYNTAX: Drop view <viewname>; QUERY: SQL>drop view v1;

OUTPUT: View dropped. 11.Create unique index on employee table. SYNTAX: Create index <index name> on <tablename>(columnname); QUERY: SQL>create index i1 on employee(dept); OUTPUT: Index created. 12.Insert values to employee table and check. SYNTAX: Insert into <tablename> values(value); QUERY: SQL>insert into employee values(&emp_name,&emp_id,&dept,&dob,&bpay); Enter values for: Emp_name:pallavi Emp_id=171 Dept=cse Dob=20-sept-1992 Bpay=20,000 SQL>/ Enter values for: Emp_name:pankhudri Emp_id=171 Dept=cse Dob=20-dec-1992 Bpay=30,000

ERROR at line 1: ORA_0001:unique constraint CS1031010149.c2 violated. 13.Create composite index on customer table. SYNTAX: Create index <indexname> on <tablename>(columnnames); QUERY: Sql>create index i2 on customer(name,sod); OUTPUT: Index created. 14.Drop an index SYNTAX: Drop index <indexname>; QUERY: Sql>drop index i2; OUTPUT: Index dropped.

RESULT: The program run successfully and operations are performed on a table using view and indexes.

Ex.. No. : 8a
Date:

PL/SQL SAMPLE PROGRAM

AIM: To write programs in PL/SQL to manipulate the database.

Pre-requisites: All the programs are performed on an existing employee table with columns: employee name, employee id, department and basic pay. All programs are written in a file named exercise. The employee table is initially populated with the following entries: Name Aa Bb Cc Dd Empid Dept 1 2 3 4 CSE ICE ECE BPay 15000 2350 7560

Civil 10600

1. Read employee id to access from the user for the employee table. Print the name & basic pay on the given input. Update the basic pay of the selected id according to: a. Branch CSE : +1500 b. Branch ECE : +1300 c. Branch ICE : +1250 PROGRAM: Sql> ed ex DECLARE ename employee.name % type; eid employee.emp.id % type; edept employee.dept % type; ebpay employee.dept % type; BEGIN DBMS_OUTPUT.PUT_LINE(Enter the employee id: );

Eid := &eid; Select name into ename, bpay into ebay from employee where emp_id = eid; DBMS_OUTPUT.PUT_LINE(name || bpay); Select dept into edept from employee where emp_id := eid; If edept = CSE then Update employee set bpay = bpay + 1500 where emp_id = eid; Else if dept = ECE then Update employee set bpay = bpay + 1300 where emp_id = eid; Else if dept = ICE then Update employee set bay = bay + 1250 where emp_id = eid; End if; END;

OUTPUT: SQL> set serverOUTPUT on; SQL> @exercise Enter the value of the employee id: 2 Cc 15000

2. Write a PL/SQL PROGRAM to reverse print the numbers 1-10. PROGRAM: BEGIN: FOR I IN reverse 1..10 LOOP DBMS_OUTPUT.PUT_LINE(i); END LOOP; END;

OUTPUT: SQL> set serverOUTPUT on; SQL> @exercise 10 9 8 7 6 5 4 3 2 1 Ex. No. : 8b CURSORS

AIM: Using an implicit cursor, display the name & department of the employee having some fixed employee id. PROGRAM: DECLARE Ename employee.name % type; Edept employee.dept % type; BEGIN Select name into ename, dept into edept from employee where emp_id = 4; DBMS_OUTPUT.PUT_LINE(Name: || ename || /nDept: || edept); END;

OUTPUT: SQL> set serverOUTPUT on; SQL> @exercise Name: dd Dept: Civil

4. Define an explicit cursor for retrieving the name & baic pay of all employee in the employee table.

PROGRAM: DECLARE P employee % rowtype Cursor employeeCursor IS select * from employee; BEGIN Open employeeCursor; LOOP Fetch employeeCursor into p; EXIT WHEN employeeCursor % NOTFOUND; DBMS_OUTPUT.PUT_LINE(Name: || p.na,e || \nBPay: || p.bpay); END LOOP; CLOSE employeeCusor; END;

OUTPUT: SQL> set serverOUTPUT on; SQL> @exercise Name: aa BPay: 15000

Name: bb Name: cc Name: dd

BPay: 2350 BPay: 7560 BPay: 10600

Ex. No. : 8c Date:

FUNCTIONS AND PROCEDURES

AIM: Write a procedure to insert values into the employee table. PROGRAM: CREATE OR REPLACE PROCEDURE insertIntoTable (name employee.name % type, eid employee.emp_id % type, dept employee.dept % type, pay employee.bpay % type) IS BEGIN Insert into employee values(name, eid, dept, pay); END;

OUTPUT: SQL> set serverOUTPUT on; SQL> @exercise SQL> insertIntoTable(ee, 5, CSE, 20000); Row Inserted. SQL> select * from employee; Name Empid Dept Aa Bb Cc Dd ee 1 2 3 4 5 CSE ICE ECE BPay 15000 2350 7560

Civil 10600 CSE 20000

6. Write a PL/SQL program to retrieve basic pay using functions. PROGRAM: CREATE OR REPLACE FUNCTION retrieve_bpay (id employee.emp_no % type) RETURN NUMBER is bpay numbe(10);

BEGIN Select basic_pay into bpay fom employee where emp_no = id; Return bpay; END;

OUTPUT: SQL> set serverOUTPUT on; SQL> @exercise SQL> select retrieve_bpay(4) from dual; 10600

Ex. No. 8d Date:

TRIGGERS

AIM: Write a PL/SQL PROGRAM using triggers. PROGRAM: Create or replace trigger sum alter Insert or update on empl; Declare dy varchar2(100); BEGIN: dy:=to_char(sysdate,dy); if dy=sum then RAISE_APPLICATION_ERROR(-20005,Today is SUNDAY TRANSACTION is not allowed Today); END if; END OUTPUT: SQL> @pt1: sql Trigger created.

Ex. No.: 9 Date:

STUDENT DETAILS

AIM: Create a program of ODBC between Oracle and Visual Basic. PROGRAM: dim cn as New ADODB.connection Dim rs as New ADODB.recordset. Private Sub Command1_Click() Text1.Text= Text2.Text= rs.AddNew End Sub Private Sub Command2_Click() rs.Fields(SNo)=Val(Text1.Text) rs.Fields(SName)=Text2.Text rs.Update End Sub Private Sub Command3_Click() End End Sub Private Sub Command4_Click() Rs.MovePrevious if rs.BOF then rs.MoveFirst msgBox first record End If Text1.Text=rs.Fields(SNo) Text2.Text=rs.Fields(SName) End Sub Private Sub Command5_Click() Rs.MoveNext If rs.EOF then rs.MoveLast msgBox Last Record Enf If

Text1.Text=rs.Fields(SNo) Text2.Text=rs.Fields(SName) End Sub Private Sub Form_Load() cn.open sample,sasi,sasi rs.Open Select * from stud,cn, adOpenDynamic, adLockOptimistic MsgBox Connect End Sub OUTPUT:

Name

Metoo

Number

121

New

Save

Exit

Select * from odbc Name No Metoo 121 RESULT: ODBC Connection is established between Visual Basic and Oracle.

Ex. No.: 10

JDBC CONNECTIVITY

AIM: To create a program for JDBC between Java and Oracle database PROGRAM: import java.sql.Connection; import java.sql.DriveManager; import java.sql.RESULTSet; import java.sql.Statement;

public class demo { public static void main(String args[]) throws Exception { String QUERY=Select * from abc; Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); Connection con=DriverManager.getConnection (jdbc:odbc:sent,sasi,sasi); Statement stmt=con.createStatement(); RESULTSet rs=stmt.executeQUERY(QUERY); while(rs.next()) { String name=rs.getString(1); Sting desc=rs.getString(description); int id=rs.getInt(1); float cost=rs.getFloat(cost); System.out.println(name+id); } con.close(); }}

OUTPUT: javac demo.java java demo

Name aa bb cc

No. 11 22 33

RESULT: JDBC Connectivity has been established between Oracle and Java.

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