Dbms Lab Manual
Dbms Lab Manual
AIM:
To design and implement a database in Oracle using Structured Query Language commands
SYNTAX:
CREATE:
Create table <table name> (column name1 datatype1 constraints, column2 datatype2 . . .);
PROBLEM STATEMENT:
1) Branch
2) Account
3) Loan
4) Customer
Create table branch(branch_name varchar2(30) primary key, branch_city varchar2(20), assets number);
Table created.
desc branch;
---------------------------------------------------------------------
BRANCH_CITY VARCHAR2(20)
ASSETS NUMBER
=============================================================================
Table created.
desc customer;
--------------------------------------------------------------------
CUSTOMER_NAME VARCHAR2(20)
CUSTOMER_STREET VARCHAR2(15)
CUSTOMER_CITY VARCHAR2(15)
=============================================================================
create table account(account_no varchar2(10) primary key, branch_name varchar2(30), balance number);
Table created.
desc account;
--------------------------------------------------------------------
BRANCH_NAME VARCHAR2(30)
BALANCE NUMBER
=============================================================================
create table loan(loan_no varchar2(20) primary key, branch_name varchar2(20), amount number);
Table created.
desc loan_ma;
--------------------------------------------------------------------
BRANCH_NAME VARCHAR2(20)
AMOUNT NUMBER
=============================================================================
RESULT:
AIM:
To implement and execute a query for manipulating & storing data items in a Oracle database using
Structured Query Language commands
DDL COMMANDS:
Create
Alter
Add
Modify
Drop
Rename
Drop
ALTER:
ADD:
Alter table <table name> add(column name1 datatype1);
MODIFY:
Alter table <table name> modify(column name1 datatype1);
DROP:
Alter table <table name> drop (column name);
RENAME:
Rename <old table name> to <new table name>;
DROP:
Drop table <table name>;
DML Commands:
Insert
Select
Update
Delete
INSERT:
Insert into <table name> values (‘attributes1’, ’attributes2’……);
SELECT:
Select <column name> from <table name>;
UPDATE:
Update <table name> set <column name>=’values’;
DELETE:
Delete from <table name>;
1. Alter the table branch by increasing the field width of branch city to 25.
Table altered.
desc branch;
--------------------------------------------------------------------
BRANCH_CITY VARCHAR2(25)
ASSETS NUMBER
=============================================================================
Table altered.
=============================================================================
Table altered.
Table altered.
=============================================================================
Table altered.
=============================================================================
Table renamed.
desc customer1
--------------------------------------------------------------------
CUSTOMER_NAME VARCHAR2(20)
CUSTOMER_STREET VARCHAR2(15)
CUSTOMER_CITY VARCHAR2(15)
=============================================================================
Table dropped.
============================================================================
8. INSERTING RECORDS IN ALL THE FOUR CREATED TABLES:
BRANCH_NAME
--------------------
guindy
adayar
tambaram
tambaram
adayar
saidapet
chrompet
7 rows selected.
=============================================================================
10. Find the names of all branches in loan relation eliminate duplicate.
BRANCH_NAME
--------------------
adayar
chrompet
guindy
saidapet
tambaram
=============================================================================
11.Display the loan relation with attributes amount multiplied by 100.
AMOUNT*100
--------------------
90000
150000
150000
130000
100000
200000
6 rows selected.
=============================================================================
12. Find all loan numbers for loan made at tambaram branch with loan amount greater than 1400.
LOAN_NO
--------------------
ln_103
=============================================================================
13. Find all loan numbers for loan’s with loan amount between 900 and 1500.
SQL> select LOAN_NO from loan where amount between 900 and 1500;
LOAN_NO
--------------------
ln_101
ln_102
ln_103
ln_104
ln_105
=============================================================================
14. Find the names of customer whose street name includes the character r in the third position.
CUSTOMER_NAME
--------------------
suresh
gopal
Krishnan
=============================================================================
15. Find the names of customer whose street name starts with substring ‘so’.
CUSTOMER_NAME
--------------------
selva
raja
mohammed
=============================================================================
16.Display the entire loan relation in descending order of amount.
COUNT(CUSTOMER_ID)
----------------------------------------------------
=============================================================================
18. Find all the loan number that appears in the loan relation with NULL values for amount.
LOAN_NO
--------------------
ln_108
=============================================================================
19. Update the branch city as Chennai-20 from Chennai-45 in the branch relation
1 row updated.
6 rows deleted.
no rows selected
SQL> rollback;
Rollback complete.
6 rows selected.
RESULT:
AIM:
To implement and execute view, synonyms, sequence, indexes, savepoint in Oracle using Structured
Query Language commands
SYNTAX:
VIEWS:
SYNONYMS
SEQUENCE
INDEXES
SAVE POINT
1) VIEWS:
Create a view using aggregate functions to calculate the age of the customer
View created.
9 rows selected.
2) SYNONYMS
Table created.
Product 6 45 1 31-DEC-08
6 rows selected.
Synonym created.
Synonym dropped.
3) SEQUENCE
create a sequence and design the student table with the given attributes.
SQL> create table student(student_id number, name varchar2(10),result varchar2(10));
SQL> desc student;
Name Null? Type
----------------------------------------- -------- -------------
STUDENT_ID NUMBER
NAME VARCHAR2(10)
RESULT VARCHAR2(10)
Sequence Creation
SQL> create sequence student_seq start with 100 minvalue 100 increment by 1;
Sequence created.
SQL> insert into student values(student_seq.nextval,'raja','pass');
1 row created.
SQL> insert into student values(student_seq.nextval,'ravi','pass');
1 row created.
SQL> select * from student;
STUDENT_ID NAME
---------- ---------- ----------
100 raja pass
101 ravi pass
4) INDEXES
Table created.
SQL> select * from Employee
Index created.
Index dropped.
5) SAVE POINT
DEPARTMENT_ID DEPARTMENT_NAME
------------- ---------------
101 it
102 cse
103 mech
104 chemical
105 biotech
106 eee
6 rows selected.
Savepoint created.
1 row created.
Savepoint created.
DEPARTMENT_ID DEPARTMENT_NAME
------------- ---------------
101 it
102 cse
103 mech
104 chemical
105 biotech
106 eee
107 ice
7 rows selected.
Rollback complete.
SQL> select * from employees;
DEPARTMENT_ID DEPARTMENT_NAME
------------- ---------------
101 it
102 cse
103 mech
104 chemical
105 biotech
106 eee
6 rows selected.
RESULT:
AIM:
To Creating a Student database to set various constraints in Oracle using Structured Query Language
commands
VARIOUS CONSTRAINTS
Constraint
Constraint Meaning
Type
CHECK C Specifies a certain condition for a column, or group of columns.
NOT NULL C Not null column
PRIMARY KEY P primary key
FOREIGN KEY R foreign key
UNIQUE U unique
CHECK Specifies that DML operations on a view must satisfy the sub
V
OPTION query.
READ ONLY O Specifies that a view may only be read from.
STUDENT DATABASE
Table Created
1) PRIMARY KEY
1 row created.
1 row created.
3) UNIQUE
1 row created.
CASE 2: (More than one null value can be accepted by the column)
1 row created.
1 row created.
1 row created.
Data
Column name Constraint
Type
COURSE_ID NUMBER PRIMARY KEY
STUDENT_NO NUMBER FOREIGN KEY
Table Created
5) FOREIGN KEY
1 row created.
6) READ ONLY
View created.
9 rows selected.
View created.
9 rows selected.
1 row created.
RESULT:
AIM:
To Creating relationship between the databases in Oracle using Structured Query Language
commands
RULES FOR CREATING RELATIONSHIP BETWEEN THE DATABASES
TERNARY
BINARY N:M
PROBLEM STATEMENT:
Create table branch(branch_name varchar2(30) primary key, branch_city varchar2(20), assets number);
Table created.
desc branch;
---------------------------------------------------------------------
BRANCH_CITY VARCHAR2(20)
ASSETS NUMBER
=============================================================================
Table name: Customer
create table customer(customer_id varchar2(10) primary key, customer_name varchar2(20), customer_Street
varchar2(15), customer_City varchar2(15));
Table created.
desc customer;
--------------------------------------------------------------------
CUSTOMER_NAME VARCHAR2(20)
CUSTOMER_STREET VARCHAR2(15)
CUSTOMER_CITY VARCHAR2(15)
=============================================================================
Table created.
desc account;
--------------------------------------------------------------------
BRANCH_NAME VARCHAR2(30)
BALANCE NUMBER
=============================================================================
Table created.
desc loan_ma;
--------------------------------------------------------------------
BRANCH_NAME VARCHAR2(20)
AMOUNT NUMBER
=============================================================================
CUSTOMER_ID VARCHAR2(11)
LOAN_NO VARCHAR2(4)
CUSTOMER_ID VARCHAR2(11)
ACCOUNT_NO VARCHAR2(11)
1. For all customer who have loan from the bank find their ID’s , loan number and loan amount.(Join)
2. For all customers who have loan at tambaram branch find their ID’s,loan ID,loan amount.(Join)
BRANCH_NAME NO_OF_DEPOSITOR
------------------------------ -----------------------------------------------
adayar 1
chrompet 2
guindy 1
saidapet 1
tnagar 1
RESULT:
AIM:
Declaration Section:
The Declaration section of a PL/SQL Block starts with the reserved keyword DECLARE. This
section is optional and is used to declare any placeholders like variables, constants, records and cursors,
which are used to manipulate data in the execution section. Placeholders may be any of Variables, Constants
and Records, which stores data temporarily. Cursors are also declared in this section.
Execution Section:
The Execution section of a PL/SQL Block starts with the reserved keyword BEGIN and ends with
END. This is a mandatory section and is the section where the program logic is written to perform any task.
The programmatic constructs like loops, conditional statement and SQL statements form the part of
executionsection.
Exception Section:
The Exception section of a PL/SQL Block starts with the reserved keyword EXCEPTION. This
section is optional. Any errors in the program can be handled in this section, so that the PL/SQL Blocks
terminates gracefully. If the PL/SQL Block contains exceptions that cannot be handled, the Block terminates
abruptly with errors. Every statement in the above three sections must end with a semicolon; PL/SQL
blocks can be nested within other PL/SQL blocks. Comments can be used to document code.
DECLARE
Variable declaration
BEGIN
Program Execution
EXCEPTION
Exception handling
END;
.RESULT:
AIM:
Write a PL/SQL block to satisfy some conditions by accepting input from the user using oracle
SYNTAX
DECLARE
Variable declaration
BEGIN
Program Execution
EXCEPTION
Exception handling
END;
FIND THE DEPARTMENT NAME FROM EMLOYEES TABLE USING PL/SQL BLOCK
1 declare
2 deptname varchar2(10);
3 begin
4 select department_name into deptname from employees where department_id=&departmentid;
5 dbms_output.put_line('The Department name is '||deptname);
6* end;
SQL> /
Enter value for departmentid: 101
old 4: select department_name into deptname from employees where
department_id=&departmentid;
new 4: select department_name into deptname from employees where department_id=101;
AIM:
To implement and execute PL/SQL Block that handles all types of exceptions in Oracle Database
using Procedural Language concepts.
EXCEPTIONS:
In PL/SQL, the user can catch certain runtime errors. Exceptions can be internally defined by Oracle
or the user. Exceptions are used to handle errors that occur in your PL/SQL code. A PL/SQL block contains
an EXCEPTION block to handle exception.
The predefined divide-by-zero exception has the following values for the attributes:
1. Name = ZERO_DIVIDE
2. Type = ORA (from the Oracle engine)
3. Exception Code = C01476
ZERO_DIVIDE EXCEPTION
SQL> BEGIN
2 DBMS_OUTPUT.PUT_LINE(1 / 0);
3 END;
4 /
BEGIN
*
ERROR at line 1:
------------------------------------------------------
BEGIN
2 DBMS_OUTPUT.PUT_LINE(1 / 0);
3 EXCEPTION
4 WHEN ZERO_DIVIDE THEN
5 DBMS_OUTPUT.PUT_LINE('Division by zero');
6 END;
7 /
Division by zero
PL/SQL procedure successfully completed.
INVALID_NUMBER EXCEPTION
1 BEGIN
3 EXCEPTION
6* end;
SQL> /
OTHERS EXCEPTION
1 BEGIN
2 DBMS_OUTPUT.PUT_LINE(1 / 0);
3 EXCEPTION
6* END;
7 /
An exception occurred
RESULT:
AIM:
To implement and execute Procedures in Oracle Database using Procedural Language concepts.
PROCEDURES:
1) Procedure is a sub program used to perform an action.
3 MODES:
1) IN – Means you must specify a value for the argument at the time execution of the procedure.
3) INOUT – When calling the procedure, yu must specify the value and that procedures passes value
back to the calling procedure.
SYNTAX:
Create or replace procedure <procedure_name> (argument {in, out, in out} data type) {is, as}
Variable declaration
Begin
Exception
End;
Write a procedure to include phone number for the customers in the customer table.
SQL> /
Procedure created.
9 rows selected.
9 rows selected.
RESULT:
AIM:
To implement and execute triggers and functions in Oracle Database using Procedural Language
concepts.
TRIGGERS:
1) Trigger is a special type of procedure that the oracle executes when an insert, modify or
delete operation is performed against a given table.
2) It is a stored sub program associated with a table.
3) It is used to keep an audit trial of a table, to prevent invalid transaction, enforce complex
security authorization, to generate data automatically.
SYNTAX:
FUNCTION:
1) A function is a sub program that accepts argument and returns a unique value to the caller.
FUNTION SYNTAX:
Create or replace function <function_name> (parameter{in, out, in out}) return <data type> is
Variable declaration
Begin
Pl/SQL Subprogram body.
Exception
Exception PL/SQL Block.
Return statement
End;
Create a trigger to calculate the total and average of a student in the student table.
Table Created
TRIGGER
SQL> /
Trigger created.
AVERAGE NUMBER
1 row created.
Write a Function to check the status of phone number in the customer table.
-------------------------------------------------------------------------------------------
SQL> /
Function created.
SQL> select check1('cus_105') as status from dual;
STATUS
---------------------------------------------------------------------------------
Enter phone number
STATUS
---------------------------------------------------------------------------------
Phone number already exists
RESULT:
PROBLEM STATEMENT
IDENTIFICATION OF ENTITY:
PASSENGER
FLIGHT_INFORMATION
FLEET_INFORMATION
TICKET_DETAILS
ATTRIBUTES:
PASSENGER
Name
P_id
Age
Sex
Address
Contact_no
FLIGHT_INFORMN
Flight_name
Flight_no
Class
FLEET_INFORMN
Source
Destination
Arrival_time
Departure_time
TICKET_DETAILS
Class_name
Ticket_no
Status
Fare
DESCRIPTION ABOUT ATTRIBUTES:
RELATIONSHIP:
BINARY
BOOKS
SCHEDULES
TRAVELS
HAS
ONE TO MANY(1:N):
BOOKS
MANY TO MANY(N:N):
TRAVELS
CARDINALITY ABOUT RELATIONSHIP:
ER DIAGRAM:
ER MODELS TO TABLES:
RELATIONSHIP RULE:
The primary key of the partial participant will become the foreign key of the total participant in
binary 1:1 relationship.
The primary key of the relation on the “1” side of the relationship becomes a foreign key in the
relation on the “N” side in binary 1:N relationship.
A new table is created to represent the relationship in binary M:N relationship.
Contains two foreign keys – one from each of the participants in the relationship in binary M:N
relationship.
The primary key of the new table is the combination of the two foreign keys in binary M:N
relationship.
The primary key field itself will become foreign key in the same table in unary 1:1 relationship.
The primary key field itself will become foreign key in the same table in unary 1:1 relationship.
There will be two resulting tables. One to represent the entity and another to represent the unary M:N
relationship.
Represented by a new table. The new table contains three foreign keys – one from each of the
participating Entities in ternary relationship. The primary key of the new table is the combination of
all three foreign keys.
UPDATED TABLE:
Primary Key
Unique
Not null
Null
Foreign key
TABLES:
COMMANDS:
CREATE
DELETE
ALTER
DDL QUERY:
FORM1:
CODE:
Private Sub Timer1_Timer()
Unload Me
Form7.Show
End Sub
FORM10:
CODE:
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub Command1_Click()
Form11.Show
Text1.Text = ""
Text2.Text = ""
End Sub
Private Sub Command2_Click()
Unload Me
Form7.Show
End Sub
Private Sub Form_Load()
conn.Open "Provider=OraOLEDB.Oracle.1;Password=tiger;Persist Security Info=True;User ID=scott"
End Sub
Private Sub Form_Unload(Cancel As Integer)
conn.Close
End Sub
FORM11:
CODE:
Text2.Text = rs1(0)
Text3.Text = rs1(1)
Text4.Text = rs(9)
Text5.Text = rs(2)
Text6.Text = rs(3)
Text7.Text = rs(4)
Text8.Text = rs(5)
Text9.Text = rs(6)
Text10.Text = rs(7)
Text11.Text = rs(8)
End Sub
FORM2:
CODE:
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Combo1.Text = ""
Unload Me
Form7.Show
End Sub
FORM3:
CODE:
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub Command1_Click()
If Combo1.Text = "" Or Combo2.Text = "" Or Combo3.Text = "" Then
MsgBox "SELECT VALUES"
Else
rs.Open "insert into flightinfo(flightname,flight_no,class)values('" & Trim(Combo1.Text) & "','" &
Trim(Combo2.Text) & "','" & Trim(Combo3.Text) & "')", conn, adOpenDynamic, adLockOptimistic
Form4.Show
Form4.Text1.Text = Combo2.Text
Me.Hide
End If
End Sub
Private Sub Command2_Click()
Unload Form2
Unload Me
Form7.Show
End Sub
Private Sub Command3_Click()
Form5.Show
End Sub
Private Sub Form_Load()
Set conn = Nothing
conn.Open "Provider=OraOLEDB.Oracle.1;Password=tiger;Persist Security Info=True;User ID=scott"
End Sub
FORM4:
CODE:
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub Command2_Click()
Form3.Show
Form3.Combo1.Text = ""
Form3.Combo2.Text = ""
Form3.Combo3.Text = ""
Unload Me
End Sub
Private Sub Command3_Click()
Form5.Show
End Sub
Private Sub Command4_Click()
Unload Form2
Unload Me
Form7.Show
End Sub
Private Sub Form_Load()
conn.Open "Provider=OraOLEDB.Oracle.1;Password=tiger;Persist Security Info=True;User ID=scott"
Set rs = Nothing
End Sub
Private Sub Form_Unload(Cancel As Integer)
conn.Close
End Sub
Private Sub Text1_Change()
rs.Open "select * from fleetinfo where flight_no='" & Trim(Text1.Text) & "'", conn, adOpenDynamic,
adLockOptimistic
Text2.Text = rs(1)
Text3.Text = rs(2)
Text4.Text = rs(3)
Text5.Text = rs(4)
End Sub
FORM5:
CODE:
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim rs1 As New ADODB.Recordset
Private Sub Command1_Click()
rs1.Open "select * from ticket where p_id=" & Val(Text1.Text) & " and flight_no='" & Trim(Text5.Text) &
"' ", conn, adOpenForwardOnly, adLockOptimistic
Text13.Text = rs1(0)
MsgBox "TICKET BOOKED SUCCESSFULLY "
Unload Me
Form9.Show
End Sub
Text2.Text = Form2.Text1.Text
Text3.Text = Form2.Text2.Text
Text4.Text = Form3.Combo1.Text
Text5.Text = Form3.Combo2.Text
Text6.Text = Form3.Combo3.Text
Text7.Text = Form4.Text2.Text
Text8.Text = Form4.Text3.Text
Text9.Text = Form4.Text4.Text
Text10.Text = Form4.Text5.Text
End Sub
CODE:
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim rs1 As New ADODB.Recordset
Private Sub Command1_Click()
rs1.Open "SELECT * FROM TICKET WHERE T_NO=" & Val(Text1.Text) & " ", conn, adOpenDynamic,
adLockOptimistic
If rs1(0).Value = Text1.Text And rs1(1).Value = Text2.Text Then
rs.Open "DELETE FROM TICKET WHERE T_NO=" & Val(Text1.Text) & " AND P_ID=" &
Val(Text2.Text) & "", conn, adOpenDynamic, adLockOptimistic
MsgBox " TICKET CANCELLED SUCCESSFULLY"
End If
If rs1.BOF = True Or rs1.EOF = True Then
MsgBox "TICKET NOT FOUND"
End If
Unload Me
Form8.Show
End Sub
Private Sub Command2_Click()
Unload Me
Form7.Show
End Sub
Private Sub Form_Load()
conn.Open "Provider=OraOLEDB.Oracle.1;Password=tiger;Persist Security Info=True;User ID=scott"
End Sub
Private Sub Form_Unload(Cancel As Integer)
conn.Close
End Sub
FORM7:
CODE:
Private Sub Command1_Click()
Unload Me
Form2.Show
End Sub
Private Sub Command2_Click()
Unload Me
Form6.Show
End Sub
Private Sub Command3_Click()
DataReport1.Show
End Sub
FORM8:
CODE:
Private Sub Form_Load()
Unload Form2
Unload Form3
Unload Form4
Unload Form5
Unload Form6
Unload Form7
End Sub
Private Sub Timer1_Timer()
Unload Form8
Form1.Show
End Sub
FORM9:
CODE:
Private Sub Timer1_Timer()
Unload Me
Form8.Show
End Sub
RESULT: