Dbms Printing Content
Dbms Printing Content
EX. No.:1
DATA DEFINITION COMMANDS, DATA MANIPULATION COMMANDS FOR INSERTING,
DELETING, UPDATING AND RETRIEVING TABLES AND TRANSACTION CONTROL
STATEMENTS
COMMANDS
SQL> create table stud (sname varchar2(30), sid varchar2(10), sage number(2), sarea
varchar2(20));
Table created. SQL> desc stud;
Name Null? Type
----------------------------------------------------- -------- --------------------------------
SNAME VARCHAR2(30)
SID VARCHAR2(10)
SAGE NUMBER(2)
SAREA VARCHAR2(20)
SQL>alter table stud modify ( sage number(10)); Table altered.
SQL> alter table stud add ( sdept varchar2(20)); Table altered.
SQL> desc stud;
Name Null? Type
----------------------------------------------------- -------- --------------------------------
SNAME VARCHAR2(30)
SID VARCHAR2(10)
SAGE NUMBER(10)
SAREA VARCHAR2(20)
SDEPT VARCHAR2(20)
SQL> alter table stud drop ( sdept varchar2(20)); Table altered.
SQL> desc studs;
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
SNAME VARCHAR2(30)
SID VARCHAR2(10)
SAGE NUMBER(10)
SAREA VARCHAR2(20)
SQL> truncate table studs; Table truncated.
SQL> desc studs;
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
SNAME VARCHAR2(30)
SID VARCHAR2(10)
SAGE NUMBER(10)
SAREA VARCHAR2(20)
SDEPT VARCHAR2(20)
Department of Computer Science and Engineering Page No.
K. Ramakrishnan College of Engineering, Trichy
RNO NAME
--------------------------------------------------------
2 kavi
3 prami
SQL> insert into tb43 values(7,‟balu‟);
1 row created.
SQL> select * from tb43;
RNO NAME
------------------------------------------------------
2 kavi
3 prami
7 balu
SQL> rollback to s1;
Rollback complete;
SQL> commit;
Committed.
EX.NO-2
CONCATENATION OPERATOR
SQL> select sname || ' is a ' || sdept || ' engineer. ' AS "PROFESSION" from studs;
PROFESSION
-------------------------------------------------------------------
ashwin is a aeronautical engineer.
BETWEEN OPERATOR
SQL> select sname,sarea, sid from studs where sid between 102 and 104;
SNAME SAREA SID
------------------------------ -------------------- ----------
bhavesh nungambakkam 102
pruthvik anna nagar 103
charith kilpauk 104
IN PREDICATE
SQL> select sname,sarea , sid from studs where sid in(102,104);
SNAME SAREA SID
------------------------------ -------------------- ----------
bhavesh nungambakkam 102
charith kilpauk 104
PATTERN MATCHING
SQL> select sname, sarea from studs where sarea like '%g%';
SNAME SAREA
------------------------------ --------------------
ashwin anna nagar
bhavesh nungambakkam
pruthvik anna nagar
LOGICAL OR OPERATOR
SQL> select sname ,sid from studs where sid>102 or sarea='anna nagar';
SNAME SID
------------------------------ ----------
ashwin 101
pruthvik 103
charith 104
NOT IN PREDICATE
SQL> select sname, sid from studs where sid not in(102,104);
SNAME SID
------------------------------ ----------
ashwin 101
pruthvik 103
---------
400
SQL> select min(spocket) result from studs; RESULT
--------------------
100
SQL> select count(spocket) result from studs;
---------
4
SQL> select count(*) result from studs; RESULT
---------
4
SQL> select count(spocket) result from studs where sarea='anna nagar'; RESULT
---------
2
SQL> select max(spocket) result from studs; RESULT
--------------------
750
SQL> select sum(spocket) result from studs;
RESULT
---------
1600
NUMERIC FUNCTIONS
SQL> select abs(-20) result from dual;
---------
20
SQL> select power (2,10) result from dual;
---------
1024
SQL> select round(15.359,2) result from dual;
---------
15.36
SQL> select sqrt (36) result from dual; RESULT
---------
6
STRING FUNCTIONS
SQL> select lower('ORACLE') result from dual; RESULT
------
oracle
SQL> select upper('oracle') result from dual; RESULT
------
ORACLE
DATE FUNCTIONS
SQL> select sysdate from dual;
SYSDATE
---------
16-JUL-08
SQL> select sysdate,add_months(sysdate,4) result from dual; SYSDATE RESULT
--------- ---------
16-JUL-08 16-NOV-08
SQL> select sysdate, last_day(sysdate) result from dual; SYSDATE RESULT
--------- ---------
16-JUL-08 31-JUL-08
SQL> select sysdate, next_day(sysdate,'sunday') result from dual; SYSDATE
--------- ---------
16-JUL-08 20-JUL-08
SQL> select months_between('09-aug-91','11-mar-90') result from dual;
RESULT
--------- 16.935484
GROUP BY CLAUSE
SQL> select sarea, sum(spocket) result from studs group by sarea;
SAREA RESULT
-------------------- ------------
anna nagar 1000
nungambakkam 500
kilpauk 100
HAVING CLAUSE
SQL> select sarea, sum(spocket) result from studs group by sarea having spocket<600;
SAREA RESULT
-------------------- ------------
nungambakkam 500
kilpauk 100
DELETION
SQL> delete from studs where sid=101;
1 row deleted.
SQL> select * from studs;
SNAME SID SAGE SAREA SDEPT SPOCKET
------------------------------ ---------- --------- -------------------- --------------------
table 10001
chair 10010
desk 10110
cot 11110
sofa 10010
tvstand 11010
TO CREATE SALE TABLE
SQL> create table sale(prodname varchar2(30),orderno number(10),prodno varchar2(10)); Table created.
ORDERNO PRODNO
------------------------------ --------- ----------
table 801 10001
chair 805 10010
desk 809 10110
cot 813 11110
Sofa 817 10010
SET OPERATIONS
SQL> select prodname from product where prodno=10010 union select prodname from sale where
prodno=10010;
PRODNAME
------------------------------
chair sofa
SQL> select prodname from product where prodno=11110 intersect select prodname from sale where
prodno=11110;
PRODNAME
------------------------------
cot
-------------------- ----------
anand it
prajan cse
ravi it
SQL> select sstud1.sname, dept from sstud1 left outer join sstud2 on ( sstud1.sname= sstud2.sname);
SNAME DEPT
-------------------- ----------
prajan cse
anand it
ravi it
kumar cse
SQL> select sstud1.sname, dept from sstud1 right outer join sstud2 on ( sstud1.sname= sstud2.sname)
SNAME DEPT
-------------------- ----------
prajan cse
anand it
ravi it
SQL> select sstud1.sname, dept from sstud1 full outer join sstud2 on ( sstud1.sname= sstud2.sname);
SNAME DEPT
-------------------- ----------
prajan cse
anand it
ravi it
kumar cse
NESTED QUERIES
SQL> select sname from sstud1 where sstud1.sname in ( select sstud2.sname from 2 sstud2 );
SNAME
--------------------
anand
prajan
ravi
SQL> select sname from sstud1 where sstud1.sname not in ( select sstud2.sname from sstud2);
SNAME
--------------------
kumar
SQL> select sname from sstud2 where marks > some(select marks from sstud2 2 where
dept='cse');
SNAME
--------------------
prajan
SQL> select sname from sstud2 where marks >= some (select marks from sstud2 2 where
dept='cse' );
SNAME
--------------------
prajan vasu
SQL> select sname from sstud2 where marks > any ( select marks from sstud2 where dept='cse' );
SNAME
--------------------
prajan
SQL> select sname from sstud2 where marks >= any ( select marks from sstud2 2 where
dept='cse' );
SNAME
--------------------
prajan vasu
SQL> select sname from sstud2 where marks > all ( select marks from sstud2 where dept='cse' );
no rows selected
SQL> select sname from sstud2 where marks < all ( select marks from sstud2 where dept='cse' );
SNAME
--------------------
anand ravi
SQL> select sname from sstud1 where exists ( select sstud2.sname from sstud2 2 where
sstud1.sname=sstud2.sname );
SNAME
--------------------
prajan anand ravi
SQL> select sname from sstud1 where not exists ( select sstud2.sname from 2 sstud2
where sstud1.sname=sstud2.sname );
SNAME
--------------------
kumar
EX.NO-3
VIEWS, SEQUENCES, SYNONYMS
COMMANDS:
SQL> create view kaviview as select name, rno, marks, attendance from kavi1, kavi2 where rollno=rno; view
created.
SQL> select * from kaviview;
INDEX
SQL> create table persons (first name varchar (20), last name varchar(10));
Table created;
SQL> create index plndex on persons (last name); Index created.
SQL> select * from persons; No rows selected.
SQL> drop plndex on persons;
Drop index plndex on persons
*
ERROR at line1:
ORA_00950: Invalid DROP option.
SEQUENCE
SQL> create table supplier2 (supplierid number (10), supplier name varchar (20));
Table created.
SQL>insert into supplier2 values (10,‟ganesh‟);
1 row created.
SQL> select * from supplier2;
SUPPLIER ID SUPPLIERNAME
--------------------------------------------------------------------------------
10 ganesh
SQL> create sequence supplier2_seq2
2 MINVALUE 1
3 STARTWITH 1
4 INCREMENT BY 1
5 CACHE 20
Department of Computer Science and Engineering Page No.
K. Ramakrishnan College of Engineering, Trichy
Sequence created.
SYNONYMS
create public synonym suppliers for app.suppliers;
Synonym created.
EX.NO-4
DATABASE PROGRAMMING: IMPLICIT AND EXPLICIT CURSORS
number (10),dnonumber(5));
Table created.
SQL> insert into ssempp values(1,'nala','lecturer',34000,11);
1 row created.
SQL> insert into ssempp values(2,'kala',' seniorlecturer',20000,12);
1 row created.
SQL> insert into ssempp values(5,'ajay','lecturer',30000,11);
1 row created.
SQL> insert into ssempp values(6,'vijay','lecturer',18000,11);
1 row created.
SQL> insert into ssempp values(3,'nila','professor',60000,12);
1 row created.
9 --close cem;
10 end;
11 /
PL/SQL procedure successfully completed.
TO WRITE A PL/SQL BLOCK TO DISPLAY THE EMPLOYEE ID AND EMPLOYEE NAME WHERE
DEPARTMENT NUMBER IS 11 USING EXPLICIT CURSORS
1 declare
2 cursor cenl is select eid,sal from ssempp where dno=11;
3 ecode ssempp.eid%type;
4 esal empp.sal%type;
5 begin
6 open cenl;
7 loop
8 fetch cenl into ecode,esal;
9 exit when cenl%notfound;
10 dbms_output.put_line(' Employee code and employee salary are' || ecode „and‟|| esal);
11 end loop;
12 close cenl;
13 * end;
14 SQL>/
PL/SQL procedure successfully completed.
TO WRITE A PL/SQL BLOCK TO UPDATE THE SALARY BY 5000 WHERE THE JOB IS LECTURER ,
TO CHECK IF UPDATES ARE MADE USING IMPLICIT CURSORS AND TO DISPLAY THE
UPDATED TABLE
SQL> declare
2 county number;
3 begin
5 county:= sql%rowcount;
6 if county > 0 then
7 dbms_output.put_line('The number of rows are '|| county);
8 end if;
EX.NO-5
PROCEDURES AND FUNCTIONS
COMMANDS:
SQL> create table ititems(itemid number(3), actualprice number(5), ordid number(4), prodid number(4));
Table created.
SQL> insert into ititems values(101, 2000, 500, 201);
1 row created.
SQL> insert into ititems values(102, 3000, 1600, 202);
1 row created.
SQL> insert into ititems values(103, 4000, 600, 202);
1 row created.
SQL> select * from ititems;
2 a number;
3 b number;
4 begin
5 zzz(101,b);
6 dbms_output.put_line('The value of b is '|| b);
7 end;
8/
The value of b is 100
PL/SQL procedure successfully completed.
PROCEDURE FOR ‘INOUT’ PARAMETER – CREATION, EXECUTION
SQL> create procedure itit ( a in out number) is
2 begin
3 a:=a+1;
4 end;
5/
Procedure created.
SQL> declare
2 a number:=7;
3 begin
4 itit(a);
5 dbms_output.put_line(„The updated value is „||a);
6 end;
7/
The updated value is 8
PL/SQL procedure successfully completed.
CREATE THE TABLE ‘ITTRAIN’ TO BE USED FOR FUNCTIONS
SQL>create table ittrain ( tno number(10), tfare number(10));
Table created.
SQL>insert into ittrain values (1001, 550);
1 row created.
SQL>insert into ittrain values (1002, 600);
1 row created.
SQL>select * from ittrain;
TNO TFARE
--------- ------------
1001 550
1002 600
PROGRAM FOR FUNCTION AND IT’S EXECUTION
SQL> create function aaa (trainnumber number) return number is
6 end;
7/
Function created.
EX.NO-6
TRIGGERS
COMMANDS:
SQL> create table itempls (ename varchar2(10), eid number(5), salary number(10));
Table created.
SQL> insert into itempls values('xxx',11,10000);
1 row created.
SQL> insert into itempls values('yyy',12,10500);
1 row created.
SQL> insert into itempls values('zzz',13,15500);
1 row created.
SQL> select * from itempls;
ENAME EID SALARY
---------- --------- ---------
xxx 11 10000
yyy 12 10500
zzz 13 15500
TO CREATE A SIMPLE TRIGGER THAT DOES NOT ALLOW INSERT UPDATE AND DELETE
OPERATIONS ON THE TABLE
SQL> create trigger ittrigg before insert or update or delete on itempls for each row
2 begin
3 raise_application_error(-20010,'You cannot do manipulation');
4 end;
5
6/
Trigger created.
EX.NO-7
EXCEPTION HANDLING
PROGRAM
1 declare
2 sellingprice number(10,2);
3 begin
4 select sellprice into sellingprice from sproductmasters where pno='p1';
5 if sellingprice < 4000
6 then
7 goto add_old_price;
8 else
9 dbms_output.put_line(' Current price is '|| sellingprice);
10 end if;
11 <<add_old_price>>
12 update sproductmasters set sellprice = 4000 where pno='p1';
13 insert into soldprices values('p1',sysdate,sellingprice);
14 dbms_output.put_line(' The new price of p1 is 4000 ');
15 end;
16 /
The new price of p1 is 4000
PL/SQL procedure successfully completed
Table created.
103 2 2000
4 begin
5 select actualprice into price from ssitems where id=103;
6 if price=0 or price is null then
7 raise zero_price;
8 end if;
9 exception
10 when zero_price then
11 dbms_output.put_line('Failed zero price');
12 end;
13 /
PL/SQL procedure successfully completed.
DISPLAYING THE UPDATED TABLE
SQL> select * from ssitems;
ID QUANTITY ACTUALPRICE
--------- --------- -----------
100 5 5000
101 6 9000
102 4 4000
103 2 2000
EX.NO-9
DATABASE CONNECTIVITY WITH FRONT END TOOLS
PROGRAM:
SQL>create table emp(eno number primary key,enamr varchar(20),age number,addr varchar(20),DOB date,phno
number(10));
Table created.
SQL>create table salary(eno number,edesig varchar(10),basic number,da number,hra number,pf
number,mc number,met number,foreign key(eno) references emp); Table created.
TRIGGER to calculate DA,HRA,PF,MC
Form3.Show
End Sub
PROGRAM FOR FORM 2
Private Sub add_Click()
Adodc1.Recordset.AddNew MsgBox "Record added"
End Sub Private
Sub clear_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Then Adodc1.Recordset.MovePrevious
End If
End
Sub Private Sub exit_Click()
Unload Me
End Sub
Private Sub main_Click()
Form1.Show
End Sub
Private Sub modify_Click()
Adodc1.Recordset.Update
End Sub
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
End Sub
Private Sub delte_Click()
Adodc1.Recordset.Delete MsgBox "Record Deleted"
If Adodc1.Recordset.EOF = True
Then Adodc1.Recordset.MovePrevious
End If
End Sub
Private Sub exit_Click()
Unload Me
End Sub
modify_Click()
Adodc1.Recordset.Update
End Sub
OUTPUT:
EX.NO-10
CASE STUDY USING REAL LIFE DATABASE APPLICATIONS
CREATE A TABLE IN ORACLE
SQL>create table account(cname varchar(20),accno number(10),balance number); Table Created
SQL> insert into account values('&cname',&accno,&balance);
Enter value for cname: Mathi
Enter value for accno: 1234
Enter value for balance: 10000
old 1: insert into account values('&cname',&accno,&balance)
new 1: insert into emp values('Mathi',1234,10000) 1 row created.
SOURCE CODE FOR FORM1
Private Sub ACCOUNT_Click()
Form2.Show
End Sub
Private Sub
EXIT_Click()
Unload Me
End Sub
Private Sub
TRANSACTION_Click()
Form3.Show
End Sub
SOURCE CODE FOR FORM 2
Private Sub CLEAR_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
Private Sub
DELETE_Click()
Adodc1.Recordset.DELETE MsgBox "record deleted"
Adodc1.Recordset.MoveNext If Adodc1.Recordset.EOF = True Then
Adodc1.Recordset.MovePrevious
End If
End Sub
Private Sub EXIT_Click()
Unload Me
End Sub
Private Sub
Department of Computer Science and Engineering Page No.
K. Ramakrishnan College of Engineering, Trichy
HOME_Click()
Form1.Show
End Sub
Private Sub
INSERT_Click() Adodc1.Recordset.AddNew
End Sub
Private Sub
TRANSACTION_Click()
Form3.Show
End Sub
Private Sub UPDATE_Click() Adodc1.Recordset.UPDATE MsgBox "record updated
successfully"
End Sub
SOURCE CODE FOR FORM 3
Private Sub ACCOUNT_Click()
Form2.Show
End Sub
Private Sub CLEAR_Click()
Text1.Text = ""
Text2.Text = ""
End Sub
Private Sub
DEPOSIT_Click()
Dim s As String s = InputBox("enter the amount to be deposited")
Text2.Text = Val(Text2.Text) + Val(s) A = Text2.Text MsgBox "CURRENT BALANCE IS Rs" + Str(A)
Adodc1.Recordset.Save Adodc1.Recordset.UPDATE
Private Sub
EXIT_Click()
Unload Me
End Sub
Private Sub
HOME_Click()
Form1.Show End
Sub Private Sub
WITHDRAW_Click()
Dim s As String s = InputBox("enter the amount to be deleted")
Text2.Text = Val(Text2.Text) - Val(s) A = Text2.Text MsgBox "current balance is Rs" +
Str(A)
Adodc1.Recordset.Save
Adodc1.Recordset.UPDATE
End Sub