CS2258 DBMS Manual
CS2258 DBMS Manual
CS2258 DBMS Manual
AIM:
CREATE objects like CREATE TABLE, CREATE FUNCTION, CREATE
SYNONYM, CREATE VIEW. Etc.
DROP Objects like DROP TABLE, DROP USER, DROP TABLESPACE, and
DROP FUNCTION. Etc.
To create tables: -
Syntax:
Example:
create table emp (empno number(5) ,
name varchar2(20),
sal number(10,2),
job varchar2(20),
comm number(10,2));
Output:-
Table Created.
1
To create view: -
Purpose: Views can be considered as virtual tables. A table has a set of definition, and it
physically stores the data. A view also has a set of definitions, which is build on top of table(s)
or other view(s),and it does not physically store the data.
Syntax:
Example:
Purpose: A primary key is used to identify each row in a table. When describe that table,
it shows NOT NULL value in NULL column.
Syntax :
Example:
2
Name Null? Type
---------------------------------------------------------------
NAME VARCHAR2(20)
SAL NUMBER(10,2)
JOB VARCHAR2(20)
COMM NUMBER(10,2)
Purpose:
A foreign key is a field (or fields) that points to the primary key of another table.
Syntax :
Example:
Output:
Table created.
3
SQL> desc dept;
----------------------------------------------------------------
DEPT_NAME VARCHAR2(20)
EMPLOYEENO NUMBER(5)
CREATE DEFAULT : -
Purpose:
Syntax :
Example:
Output:
1 row created.
1 row created.
1 row created.
4
SQL> select * from dept_defalt;
DEPTNO DEPT_NAME
----------------------------------
11 IT
12 IT
13 MCA
ALTER:-
Purpose:
Once a table is created in the database ,there are many occasions where one may wish
to change the structure of the table. Typical cases include the following:
Add a column
Modify the datatype for a column
Purpose:
Syntax :
Example:
Output:
Table altered.
5
Name Null? Type
---------------------------------------------------------------
NAME VARCHAR2(20)
SAL NUMBER(10,2)
JOB VARCHAR2(20)
COMM NUMBER(10,2)
ADDRESS VARCHAR2(20)
DROP:
Purpose:
To delete a table ( the table structure, attributes and indexes will also be deleted).
Syntax :
Example:
Output:
Table dropped.
Result :
Thus the DDL program has been successfully executed and verified.
6
2 . INSERT, SELECT COMMANDS, UPDATE & DELETE COMMANDS
AIM:
The Data Manipulation Language (DML) is used to retrieve, insert and modify
database information.
a.INSERT :
Purpose:
Example:
Insert into dept values(11,’IT’,1101);
Output:
1 row created.
1 row created.
b.SELECT :
Purpose:
The SELECT allows database users to retrieve the specific information from the
table.
Syntax:
To retrieve all attributes from the particular table.
select * from <table name>;
14 IT 1101
7
15 IT 1102
DEPTNO DEPT_NAME
------------------------------------
16 IT
17 IT
Syntax:
Example:
Output:
1 row created.
--------------------------------------------------------------
18 IT 1101
19 IT 1102
20 1103
d.UPDATE :
Purpose:
8
Syntax:
Example:
Output:
1 row updated.
----------------------------------------------------------------
13 CSE 1101
14 IT 1102
15 1103
e.DELETE:
Purpose:
The DELETE command with a WHERE clause can be used to remove his record from
the personal_info table.
Syntax:
Example:
Output:
9
1 row deleted.
----------------------------------------------------------------
14 IT 1102
15 1103
Syntax:
select <column name> from <table name> [ where <condition>];
Example:
select name from dept where Dept_name=’IT’;
Output:
SQL> select * from dept where Dept_name=’IT’;
----------------------------------------------------------------
14 IT 1102
15 CSE 1103
16 IT 1104
Purpose:
10
Syntax:
Example:
Output:
DEPTNO DEPT_NAME
-----------------------------------
15 CSE
14 IT
16 IT
DEPTNO DEPT_NAME
-----------------------------------
15 CSE
14 IT
16 IT
DEPTNO DEPT_NAME
-----------------------------------
11
14 IT
16 IT
15 CSE
Purpose:
% (percentage) and _ (underscore) have a special meaning. These two wild card
characters are used with LIKE operator.
% allows matching any string of any length (including zero length)
_ allows matching a single character
Syntax:
DEPTNO DEPT_NAME
-----------------------------------
15 CSE
Result:
Thus the DML program has been successfully executed and verified.
12
3 . NESTED QUERIES & JOIN QUERIES
AIM:
Nested Query:
SQL nested query is SELECT Query that is nested inside a Select, Update, Insert or
Delete query.
SQL> select * from itemfile where qty>all(select qty from itemfile1 where
max=40);
SQL> select * from itemfile where qty=(select qty from itemfile1 where
max=40) or itemamt in(select itemamt from itemfile);
13
---------- ---------- ----------
100 200 300
50 90 70
40 34 56
Join Query :
A join is a query that combines rows from two or more tables,
views, or materialized views.
Oracle performs a join whenever multiple tables appear in the
query's FROM clause.
The query's select list can select any columns from any of these tables.
If any two of these tables have a column name in common, you must qualify
all references to these columns throughout the query with table names to avoid
ambiguity.
Join Conditions :
Most join queries contain WHERE clause conditions that compare two columns, each
from a different table.Such a condition is called a join condition.
To execute a join, Oracle combines pairs of rows, each containing one row from each
table, for which the join condition evaluates to TRUE. The columns in the join conditions
need not also appear in the select list.
Output:
14
300 200 300
300 200 300
40 34 56
Equi Join :
An equijoin is a join with a join condition containing an
equality operator ( = ). An equijoin combines rows that have
equivalent values for the specified columns.
Output:
QTY MAX
---------- ----------
34 40
Non-Equi join:
Non equi joins is used to return result from two or more tables
where exact join is not possible.
Output:
QTY MAX
---------- ----------
90 300
34 300
90 300
34 300
90 40
Outer join:
An outer join extends the result of a simple join. An outer join returns all
rows that satisfy the join condition and also returns some or all of those rows from one table
for which no rows from the other satisfy the join condition.
15
To write a query that performs an outer join of tables A and B and returns all rows from
A (a left outer join), use the ANSI LEFT[OUTER] JOIN syntax, or apply the outer
join operator (+) to all columns of B in the join condition. For all rows in A that have
no matching rows in B, Oracle returns null for any select list expressions containing
columns of B.
To write a query that performs an outer join of tables A and B and returns all rows from
B (a right outer join), use the ANSI RIGHT[OUTER] syntax, or apply the outer join
operator (+) to all columns of A in the join condition. For all rows in B that have no
matching rows in A, Oracle returns null for any select list expressions containing
columns of A.
To write a query that performs an outer join and and returns all rows from A and B,
extended with nulls if they do not satisfy the join condition (a full outer join), use the
ANSI FULL [OUTER] JOIN syntax.
Output:
QTY MAX
---------- ----------
200
34 40
90
Union:
Output:
Union All:
SQL> select * from itemfile union all select * from itemfile1;
16
Output:
6 rows selected.
Intersection:
Output:
Minus:
Output:
Result:
Thus the Nested and Join Queries has been successfully executed and verified.
17
4.VIEWS
AIM:
Purpose:
Views can be considered as virtual tables. A table has a set of definition, and it
physically stores the data. A view also has a set of definitions, which is build on top of table(s)
or other view(s),and it does not physically store the data.
Syntax:
Example:
Output:
View created.
Example:
18
Output:
View created.
EMPNO
------------
11
22
Example:
Output:
View created.
Result:
Thus the view creation program has been successfully executed and verified.
19
5 . HIGH LEVEL PROGRAMMING LANGUAGE EXTENSIONS
AIM :
To write programs a High Level Language extensions with stored procedures and
Procedures:
The procedure is a program that performs an action and does not return a value (outside of IN
OUT and OUT parameters).
Functions:
The function is a program that might perform an action and does return a value.
20
Procedures are traditionally the workhorse of the coding world and functions are traditionally
the smaller, more specific pieces of code. In general, if you need to update the chart of
accounts, you would write a procedure. If you need to retrieve the organization code for a
particular GL account, you would write a function.
21
SQL> create or replace procedure addacc(acc_id in
varchar2,name in varchar2,balance in number)
2 as
3 begin
4 insert into accoun111 values(acc_id,name,balance);
5 dbms_output.put_line('value added');
6 end addacc;
7 /
Output:
Procedure created.
22
11 end if;
12 exception when no_data_found then
13 dbms_output.put_line('No such data found');
14 end;
15 /
Output:
Procedure created.
SQL> select * from account111;
ACC_ID NAME BALANCE
---------- ---------- ---------
a0001 maha 3000
a0002 praveen 1000
a0003 anand 4000
Function Syntax :
IS | AS
<variable declarations>
23
BEGIN
<code_here>;
END <function_name>;
/
Function Example:
24
Output:
Function created.
SQL> declare
2 no account11.acc_id%type;
3 name account11.name%type;
4 begin
5 no:='&account_id';
6 name:=f1(no);
7 dbms_output.put_line('account number:'||no);
8 dbms_output.put_line('customer name:'||name);
9 exception when no_data_found then
10 dbms_output.put_line('no such account id exists');
11 end;
12 /
Output:
Enter value for account_id: a0001
old 5: no:='&account_id';
new 5: no:='a0001';
account number:a0001
customer name:rani
25
Result:
Thus the High Level Programming Language Extension has been successfully executed
and verified.
AIM:
To develop an employee payroll system using Oracle as a back end (database) and
Microsoft Visual Basic6.0 as a front end.
PROCEDURE:
2. Design the corresponding form with labels,text boxes and command buttons.
Form1Employee Payroll
3. Create the back with the front end using DAO method by creating a DSN as
follows,
a.Select administrative tools option from control panel.
Then click on Data Source(ODBC),which displays a dialog box named
ODBC DATASOURCES ADMINISTRATOR in which click Add button.
b.In Create New Data Source dialog box,select “Microsoft ODBC for
ORACLE” and click finish button
.
c.Give a suitable data source name,user name and server name.
26
5. Calculate the net salary of the employee.
TABLE DESIGN:
Table Name :Employee
NAME TYPE
EMPNO NUMBER(6)
EMPNAME VARCHAR2(30)
DESIGNATION VARCHAR2(20)
DA NUMBER(10)
HRA NUMBER(10)
ADDRESS VARCHAR2(20)
TA NUMBER(10)
PF NUMBER(10)
BASICPAY NUMBER(8,2)
LOAN.Text=adodc1.Recordset.Fields(9)
ADV.Text= adodc1.Recordset.Fields(10)
TD.Text=adodc1.Recordset.Fields(11)
GP.Text=adodc1.Recordset.Fields(12)
NP.Text=adodc1.Recordset.Fields(13)
End Sub
PROGRAM TO VIEW:
27
While Not Adodc1. Recordset.EOF
If Adodc1.Recordset.Fields(0)=a Then
fillt 1
Exit Sub
End If
Adodc1.Recordset.MOVENEXT
Wend
err:
MsgBox(“record doesnot exist”)
End Sub
Private sub MOVENEXT_Click()
Adodc1.Recordset.MOVENEXT
End Sub
PROGRAM TO SAVE:
Fill function
28
Public sub fillt()
NO.Text=adodc1.Recordset.Fields(0)
NAME.Text= adodc1.Recordset.Fields(1)
DESIG.Text=adodc1.Recordset.Fields(2)
MONYH.Text=adodc1.Recordset.Fields(14)
B1.Text=adodc1.Recordset.Fields(3)
DA.Text=adodc1.Recordset.Fields(4)
HRA.Text= adodc1.Recordset.Fields(5)
OA.Text=adodc1.Recordset.Fields(6)
TA.Text=adodc1.Recordset.Fields(7)
PF.Text=adodc1.Recordset.Fields(8)
LOAN.Text=adodc1.Recordset.Fields(9)
ADV.Text= adodc1.Recordset.Fields(10)
PROGRAM TO ADD:
29
Private sub B1_LostFocus()
If Val(B1.Text)<5000 Then
DA.Text=Val(B1)*(20/100)
HRA.Text= Val(B1)*(15/100)
OA.Text= Val(B1)*(10/100)
TA.Text=Val(B1)+Val(HRA)+Val(OA)
PF.Text=Val(B1)*(5/100)
Else
DA.Text=Val(B1)*(25/100)
HRA.Text= Val(B1)*(20/100)
OA.Text= Val(B1)*(15/100)
TA.Text=Val(DA)+Val(HRA)+Val(OA)
PF.Text=Val(B1)*(5/100)
End If
End Sub
Dim a As Integer
a=InputBox(“enter the empno”)
Adodc1.Recordset.MOVEFIRST
While Not Adodc1.Recordset.EOF
If Adodc1.Recordset.Fields(0)=a Then
Fillt
Exit Sub
End If
Adodc1.Recordset.MOVENEXT
Wend
MsgBox”record modify successfully”
add.Enabled=False
del.Enabled=False
BASIC. Enabled=False
edit. Enabled=False
End Sub
30
LOAN.Enabled= False
ADV.Enabled= False
TD.Enabled= False
GP.Enabled= False
PF.Enabled= False
NP.Enabled= False
save. Enabled=True
End Sub
PROGRAM TO DELETE:
Adodc1.Recordset.MOVENEXT
Wend
MsgBox”record deleted”
err:
MsgBox”record does not exists”
End Sub
Private sub edit_Click()
Form1.Caption=”modify operation”
Adodc1.Recordset.Update
NAME1.Enabled=True
NO.Enabled= True
DESIG.Enabled= True
B1.Enabled= True
HRA.Enabled= True
DA.Enabled= True
OA.Enabled= True
31
TA.Enabled= True
LOAN.Enabled= True
ADV.Enabled= True
TD.Enabled= True
GP.Enabled= True
PF.Enabled= True
NP.Enabled= True
save. Enabled=True
NO.SetFocus
OUTPUT:
RESULT:
32
Thus the employee payroll processing system has been successfully developed and
verified.
7.TRIGGERS
AIM:
a. Triggering Event
b. Triggering constraint ( Optional )
c. Triggering Action
Triggering Action:
Before Option : Fires the trigger only once,before executing the triggering statement.
After Option: Fires the trigger only once ,after the execution of the triggering
statement.
Instead of Option: Fires the trigger only once, to do something else instead of
performing the action that executed the trigger.
Syntax:
33
1 . Create trigger to display the message after the data insert / delete/
update operation
Output:
Trigger created.
1 row created.
2 . Create trigger to display the message after the data insert / delete operation
34
6 dbms_output.put_line('today is holiday');
7 else
8 dbms_output.put_line('welcome back');
9 end if;
10 end;
11 /
Output:
Trigger created.
1 row updated.
3. Trigger to create error message when balance becomes less than 2000
Output:
Trigger created.
4. Create trigger with referencing new as newrow values swapped in another table
35
2 for each row when(newrow.a>10)
3 begin
4 insert into t5 values(:newrow.b,:newrow.a);
5 end trig4;
6 /
Output:
Trigger created.
1 row created.
A B
---------- ---------
2 4
1 row created.
C D
---------- ---------
16 12
A B
---------- ---------
2 4
12 16
36
SQL> create table custdet(c_id varchar2(10) primary key,name varchar2(20),street_name
varchar2(20),c
ity varchar2(20),ph_no number(12));
Table created.
1 row created.
1 row created.
Output:
Trigger created.
1 row deleted.
37
---------- -------------------- -------------------- -------------------- ---------
c001 dhana k.k.nagar ch-24 243563
c003 hema nehru street ch-93 295123
1 row deleted.
Table created.
Table created.
Program:
38
Output:
Trigger created.
1 row created.
1 row deleted.
39
Result :
8. REPORTS
AIM:
To generate the REPORT using SQL commands.
Commands :
Remark :
Tells SQL plus that the cords to follow are to be treated as comments & not instructions.
SETHEADSEP :
The Heading separator identifies the single character that tells sql plus to split into one or
more lines.
TTITLE :
Sets the top title for each page of a report.
BTITLE :
Sets the bottom title for each page of a report.
COLUMN :
Gives sql plus a variety of instructions on the heading format and treatment of a column.
BREAKON :
Tells the sql plus how the transactions for each kind of job are grouped
together under any field.
COMPUTESUM :
Makes sql plus ,calculate sub tools
SETLINESIZE :
Set the maximum number of lines per page.
SETNEWPAGE :
Set the number of blank lines between pages.
40
SPOOL :
Moves a report you would normally displayed on the screen into a file.
PROGRAM:
41
Output:
42
RESULT :
9. MINI PROJECT(1)
AIM:
To develop a banking system using Oracle as s back end(data base) and Microsoft
Visual basic6.0 as a front end.
PROCEDURE :
2. Design the corresponding form with labels,text boxes and command buttons.
Form1customer details
Form2withdraw
Form3deposit
3. Create the back with the front end using DAO method by creating a dsn as follows
a.Select administrative tools option from control panel.
Then click on data source(ODBC),which displays a dialog box named
ODBCDATASOURCESADMINISTRATOR in which click Add button.
b.In Create New Data Source dialog box,select “Microsoft ODBC for
ORACLE” and click finish button
c.Give a suitable data source name,user name and server name.
43
5. Execute the project.
TABLE DESIGN:
NAME TYPE
ACNO NUMBER(6)
NAME VARCHAR2(30)
ADDRESS VARCHAR2(20)
BALANCE NUMBER(8,2)
FORM 1:
Dim DB As Database
Dim RS As recordset
44
MsgBox”the record is deleted”
RS.MoveNext
IfRS.EOF Then
MsgBox”no more records”
Else
Text1.text=RS(0)
Text2.text=RS(1)
Text3.text=RS(2)
Text4.text=RS(3)
End If
End Sub
45
Else
Text1.text=RS(0)
Text2.text=RS(1)
Text3.text=RS(2)
Text4.text=RS(3)
EndIf
End Sub
Private Sub PREVIOUS_Click()
RS.Move Previous
If RS.EOF Then
MsgBox”no more recfords”
Else
Text1.text=RS(0)
Text2.text=RS(1)
Text3.text=RS(2)
Text4.text=RS(3)
EndIf
End Sub
46
Form3.Show
End Sub
FORM 2:
End
FORM 3:
47
WITHDRAW FORM:
48
DEPOSIT FORM:
49
RESULT:
Thus the banking system has been successfully developed in Visual Basic.
AIM:
50
To develop a library information system using Oracle as a back end (data
base)and Microsoft Visual Basic as a front end.
PROCEDURE:
1.Create a book and issue database with the following fields in Oracle namely
Bid,Bname,Author,Price,noofcopiesavailable and Bid,Bname,Author,card
number,stuname,dateofreturn respectively and insert some record into the database.
2.Design the corresponding form with labels,text boxes and command buttons.
Form1 bookdetails
Form2 issuedetails
3.Create the back with the front end using DAO method by creating a dsn as follows
A . Select administrative tools option from control panel.
Then click on data source(ODBC),which displays a dialog box named
ODBC DATASOURCES ADMINISTRATOR in which click Add
button.
TABLE DESIGN:
51
BID NUMBER(6)
BNAME VARCHAR2(30)
AUTHOR VARCHAR2(20)
CARDNUMBER VARCHAR2(8,2)
STUNAME VARCHAR2(8,2)
DATEOFRETURN NUMBER(6)
Table Name 2 : Issue
NAME TYPE
BID NIMBER(6)
BNAME VARCHAR2(30)
AUTHOR VARCHAR2(20)
PRICE NUMBER(8,2)
NO OF COPIES AVAILABLE NUMBER(3)
52
ISSUE DETAILS FORM:
53
FORM 1:
Dim DB As Database
Dim RS As recordset
54
Text4.text=””
Text5.text=””
End Sub
55
Else
RS.MOVEFIRST
Text1.text=RS(0)
Text2.text=RS(1)
Text3.text=RS(2)
Text4.text=RS(3)
Text5.text=RS(4)
End Sub
Private Sub ISSUE_Click()
If Val(Text5.Text)>=1 Then
Form2.Show
Else
MsgBox”NO BOOKS AVAILABLE”
End If
End Sub
FORM2:
Dim DB As Database
Dim RS As recordset
56
End Sub
RESULT :
Thus the Library Information System has been successfully developed in VB.
57