Database Assignment
Database Assignment
LAB REPORT
DBMS Laboratory
23CCE284
CCE
II year/IV Sem
DESC STUDENT_MASTER;
DESC STUDENT_MASTER;
DESC STUDENT_MASTER;
TRUNCATE TABLE STUDENT_MASTER;
RESULT:
Thus the Data Definition Language (DDL) commands were studied and executed.
2. DATA MANIPULATION LANGUAGE
AIM:
To Execute Data Manipulation Language (DML) commands available in RDBMS by using SQL.
PROCEDURE:
DML commands are the most frequently used SQL commands and is used to query and manipulate
the existing database objects. Some of the commands are o Insert o Select o Update o Delete
CREATE TABLE STU (
ID VARCHAR(5),
NAME VARCHAR(20),
CITY VARCHAR(20),
MARKS INT
);
UPDATE STU
SET CITY = 'Bangalore', MARKS = 95
WHERE ID = 'S1';
UPDATE STU
SET MARKS = 60;
UPDATE STU
SET MARKS = MARKS + 5
WHERE MARKS < 80;
SELECT * FROM STU;
Problem 1:
CREATE TABLE dept (
DNO INT PRIMARY KEY,
DNAME VARCHAR(50),
LOCATION VARCHAR(50)
);
RESULT: Thus the Data Manipulation Language (DML) commands were studied and executed.
3.experiment
CREATION OF A DATABASE AND WRITING SQL QUERIES TO RETRIEVE INFORMATION FROM
THE DATABASE
AIM: To Create a Database and Writing SQL Queries to Retrieve Information From The Database.
Sql:
CREATE TABLE emp (
EMPNO INT PRIMARY KEY,
ENAME VARCHAR(20),
JOB VARCHAR(20),
MGR INT,
HIREDATE DATE,
SAL INT,
COMM INT,
DEPTN INT
);
INSERT INTO emp VALUES (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, NULL, 20);
INSERT INTO emp VALUES (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30);
INSERT INTO emp VALUES (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30);
INSERT INTO emp VALUES (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, NULL, 20);
INSERT INTO emp VALUES (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 20);
INSERT INTO emp VALUES (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, NULL, 30);
INSERT INTO emp VALUES (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, NULL, 10);
INSERT INTO emp VALUES (7788, 'SCOTT', 'ANALYST', 7566, '1987-04-19', 3000, NULL, 20);
INSERT INTO emp VALUES (7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000, NULL, 10);
INSERT INTO emp VALUES (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30);
INSERT INTO emp VALUES (7876, 'ADAMS', 'CLERK', 7788, '1987-05-23', 1100, NULL, 20);
INSERT INTO emp VALUES (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, NULL, 30);
INSERT INTO emp VALUES (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, NULL, 20);
INSERT INTO emp VALUES (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, NULL, 10);
SELECT * FROM emp;
SELECT ENAME, JOB, SAL, DEPTN FROM emp WHERE SAL NOT BETWEEN 1500 AND 5000;
SELECT EMPNO, ENAME, SAL FROM emp WHERE SAL IN (800, 5000);
SELECT EMPNO, ENAME, SAL FROM emp WHERE SAL NOT IN (800, 1250, 3000, 5000);
SELECT EMPNO, ENAME, JOB, SAL FROM emp WHERE ENAME LIKE 'S%';
SELECT EMPNO, ENAME, JOB, SAL FROM emp WHERE ENAME NOT LIKE 'S%';
SELECT EMPNO, ENAME, JOB, SAL FROM emp WHERE SAL > 2500;
SELECT EMPNO, ENAME, JOB, SAL FROM emp WHERE SAL < 2500;
SELECT EMPNO, ENAME, JOB, SAL FROM emp ORDER BY SAL;
SELECT EMPNO, ENAME, JOB, SAL FROM emp ORDER BY SAL DESC;
Aggregate function:
SELECT COUNT(*) FROM EMP;
Grouping:
SELECT DEPTN, MAX(SAL) FROM EMP GROUP BY DEPTN;
SELECT SNAME
FROM S1
INNER JOIN S2 USING (SNAME);
SELECT SNAME
FROM S1
LEFT JOIN S2 USING (SNAME)
WHERE S2.SNAME IS NULL;
RESULT: Thus the Program Creation of a Database and Writing SQL Queries to Retrieve
Information from the Database is executed successfully.
Ex.No.4
BUILT IN FUNCTIONS
AIM:
To execute built in functions available in Oracle by using SQL
PROCEDURE:
The following are the built-in functions in Oracle. All of its operations were
discussed in this experiment.
CONVERSION FUNCTION
STRING FUNCTION
DATE FUNCTION
NUMBER FUNCTION
CONVERSION FUNCTIONS:
SELECT UPPER('dbms');
SELECT LENGTH('DATABASE');
SELECT SUBSTRING('ABCDEFGHIJ', 3, 4);
DATE FUNCTIONS
SELECT NOW();
SELECT LAST_DAY(NOW());
NUMBER FUNCTIONS
SELECT ROUND(12.36), ROUND(14.63);
SELECT SQRT(25);
Exp.No: 7
TCL COMMANDS
AIM:
To Execute Transaction Control Language commands available in oracle by using SQL
Code :
Table employee:
CREATE TABLE employee ( ENO INT(5), ENAME CHAR(4), DEPTNO INT(3), SAL
INT(5), DESIG CHAR(6), DESIGNO INT(4) );
Inserting values :
INSERT INTO employee (ENO, ENAME, DEPTNO, SAL, DESIG, DESIGNO)
-> VALUES
-> (10001, 'John', 101, 50000, 'Mng', 2001),
-> (10002, 'Jane', 102, 45000, 'Clerk', 2002),
-> (10003, 'Mike', 103, 48000, 'Anlst', 2003),
-> (10004, 'Anna', 104, 55000, 'Lead', 2004);
Rollback command :
Code : mysql> rollback;
Commit command :
Code : mysql> commit;
After changes :
Save point :
Code : mysql> SAVEPOINT sp2;
RESULT:
Thus the Program TCL Commands is executed successfully
Exp.No.8
CREATING RELATIONSHIP BETWEEN THE DATABASES
Aim : To Creating relationship between the databases in Oracle using Structured Query
Language commands
PROBLEM STATEMENT:
A branch contain many account holder
A branch provide more than one Loan
A Loan can be availed by more than customer.
A customer can get more than one Loan.
A customer can have more than one account
Inserting data :
-- Inserting data into Customer Table
INSERT INTO customer VALUES ('C-101', 'Arun', '1 st street', 'Chrompet');
INSERT INTO customer VALUES ('C-102', 'Sekar', 'Nehru street', 'Tambaram');
INSERT INTO customer VALUES ('C-103', 'Prabhu', 'Water tank', 'Pallavaram');
INSERT INTO customer VALUES ('C-104', 'Selvam', 'Gandhi street', 'Velachery');
INSERT INTO customer VALUES ('C-105', 'Pandi', 'Periyar street', 'T-Nagar');
INSERT INTO customer VALUES ('C-106', 'Kathir', 'Mgr street', 'Chrompet');
Problem 1: For all customer who have loan from the bank find their ID’s , loan number
and loan amount.(Join)
Code :
SELECT b.customer_id, b.loan_no, l.amount
FROM borrower b
JOIN loan l ON b.loan_no = l.loan_no;
Output :
Problem 2: For all customers who have loan at tnagar branch find their ID’s,loan ID,loan
amount.(Join)
Code :
SELECT b.customer_id, b.loan_no, l.amount
FROM borrower b
JOIN loan l ON b.loan_no = l.loan_no
WHERE l.branch_name = 'tnagar';
Output :
RESULT:
Thus the Program Creating Relationship between the Databases is executed
successfully
STUDY OF PL/SQL BLOCK
Ex.No.10
AIM:
THEORY:
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 execution section. Sequence of statements
can be executed on satisfying certain condition. If statements are being used and different
forms of if are:
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;
BRANCHING in PL/SQL:
Simple IF
ELSIF
SIMPLE IF:
Syntax:
END IF;
IF-THEN-ELSE STATEMENT:
Syntax:
ELSE statement2;
END IF;
ELSIF STATEMENTS:
Syntax:
ELSE statementn;
END IF;
ITERATIONS IN PL/SQL
Sequence of statements can be executed any number of times using loop construct. It is
broadly classified into:
Simple Loop
For Loop
While Loop
SIMPLE LOOP
Syntax:
LOOP
statement
1;
EXIT [ WHEN
Condition];
END LOOP;
WHILE LOOP
Syntax
WHILE condition
LOOP
statement1;
statement2;
END LOOP;
FOR LOOP
Syntax:
FOR counter IN
LowerBound..UpperBound
LOOP
statement1;
statement2;
END
LOOP;
Problem Statement:
Write a PL/SQL block that accepts a student's marks as input, determines the grade based
on the following criteria, and handles any exceptions:
If the input is invalid (negative or greater than 100), handle the exception.
DECLARE
marks NUMBER;
grade CHAR(1);
BEGIN
-- Accepting Marks
marks := &Enter_Marks;
grade := 'A';
grade := 'B';
grade := 'C';
grade := 'D';
ELSE
grade := 'F';
END IF;
EXCEPTION
END;
Sample Output:
Enter_Marks: 85
Student Grade: B
Enter_Marks: 105
RESULT:
Ex.No.11
Date:
AIM:
Write a PL/SQL block to satisfy some conditions by accepting input from the user
using oracle
PROGRAMS:
declare
startno number(4);
s number:=0;
i number:=0;
p number:=0;
begin
startno:=&no;
endno:=&no;
for i in startno..endno
loop
s:=s+i;
p:=p*i;
end loop;
dbms_output.put_line('sum='||s);
dbms_output.put_line('product='||p);
end;
OUTPUT:
SQL> @sum&pdt.sql;
old 8: startno:=&no;
new 8:
old 9: endno:=&no;
new 9: endno:=5;
declare
a number;
b number;
c number;
begin
a:=&a;
b:=&b;
c:=&c;
elsif
dbms_output.put_line(bll'is greatest no.) ;
else
end if;
end;
OUTPUT:
SQL> @big.sql;
old 6: a:=&a;
new 6:
old 7:
new 7:
old 8: c:=&c;
new 8: c:=12;
3. REVERSE A NUMBER
declare
a number;
rev number;
d number;
begin
a:=&a;
rev:=();
while a>()
loop
d:=mod(a,l());
(all 0);
end loop;
end;
OUTPUT:
SQL> @reverse.sql;
old 6: a:=&a;
new 6: a:=369;
declare
a number;
c number:=();
i number;
begin
a:=&a;
for i in l..a
loop
if mod(a,i)=() then
c:=C+1;
end if;
end loop;
else
end if;
end;
OUTPUT:
SQL> @prime.sql;
old 6: a:=&a;
new 6: a:=57;
declare
n number;
i number;
begin
p:=p*i;
end loop;
dbms_output.put_line(n II' !
= 'Ilp);
end;
OUTPUT:
SQL> @fact.sql;
old 6: n:=&n;
new 6: n:=5;
5! = 120
PL/SQL BLOCK
To execute this program need to create a table called department, which consist of the
After the table creation and values inserted the table become,
102 it I block
1 declare
2 deptname varchar2(10);
3 begin
OUTPUT:
SQL>@prog.sql;
department_id=&departmentid;
RESULT:
Thus the program PL/SQL block to satisfy some conditions by accepting input from the
user is
executed successfully
WRITE A PL/SQL BLOCK THAT HANDLES ALL TYPES OF
EXCEPTIONS
Exp.No.12
Date:
AIM:
To implement and execute PIJSQL Block that handles all types of exceptions in
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
3. User-defined errors
2. Raise an exception.
1. Name = ZERO_DIVIDE
ZERO_DIVIDE EXCEPTION
SQL> BEGIN
2 DBMS_OUTPUT.PUT_LINE(1 / 0);
3 END;
4/
BEGIN
ERROR at line 1:
ORA-06512: at line 2
SQL>BEGIN
3 EXCEPTION
5 by zero);
6 END;
7/
OUTPUT:
Division by zero
INVALID_NUMBER EXCEPTION
To Execute this exception need to create a table called department, which consist of
the following fields and corresponding data types are deptid number,dname
After the table creation and values inserted the table become,
102 it 1 block
3 EXCEPTION
6 end; /
OUTPUT:
NO_DATA_FOUND EXCEPTION
SQL>Declare
Deptname varchar2(15);
BEGIN
EXCEPTION
end; /
OUTPUT:
OTHERS EXCEPTION
SQL> BEGIN
2 DBMS_OUTPUT.PUT_LINE(1 / 0);
3 EXCEPTION
4 WHEN OTHERS THEN
6* END;
7/
OUTPUT:
An exception occurred
RESULT:
Thus the program PIJSQL Block that handles all types of Exceptions is executed
Successfully
Exp.No.13 CREATION OF PROCEDURES
Date:
AIM:
To implement and execute Procedures in Oracle Database using Procedural Language
concepts.
PROCEDURES:
3 MODES:
SYNTAX:
Variable declaration
Begin
End;
Example: In Procedure
Write a procedure to include a phone number for the customers in the customer table.
a varchar2(20);
b exception;
begin
raise
b; else
exception
when b then
end;
SQL> /
Procedure created.
APR-99
9 rows selected.
9 rows selected.
no such data
10 manager chennai
20 sales mumbai
30 executer trichy
40 product tuty
50 marketing hyderabad
Program:
create or replace procedure outpro(dno number, depname out varchar2, city out
varchar2) as
begin
select dname, loc into depname, city from dept131 where deptno =
dno; end;
Output:
SQL> @outpro.sql;
Procedure created.
Program:
declare
varchar2(20);
varchar2(30);
ch number;
begin
ch := &ch;
outpro(ch, x,
y);
y); end;
Output:
SQL> @ajay.sql
10 old 6: ch :=
&ch; new 6: ch :=
10; Manager
chennai
Problem:
Create a procedure in Oracle that updates the email address for a customer in the
customer table, using their customer_id. The procedure should perform the following:
Solution:
We will create a procedure named update_email. The procedure will take two
parameters:
SQL Code:
CREATE OR REPLACE PROCEDURE update_email(customer_id IN VARCHAR2,
new_email IN VARCHAR2) IS
v_customer_name VARCHAR2(50);
v_current_email VARCHAR2(100);
email_exists EXCEPTION;
BEGIN
-- Check if the customer exists
SELECT customer_name, email INTO v_customer_name, v_current_email
FROM customer
WHERE customer_id = customer_id;
UPDATE customer
SET email =
new_email
WHERE customer_id = customer_id;
EXCEPTION
-- If no data found, customer doesn't exist
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Customer not found.');
-- If the email is already
updated WHEN email_exists
THEN
DBMS_OUTPUT.PUT_LINE('Email is already updated for this
customer.'); END;
/
Explanation of Code:
Here is how you can execute the procedure to update the email address of a customer
with customer_id = 'cus_101':
Example Execution:
Before execution:
SELECT * FROM customer WHERE customer_id = 'cus_101';
Output:
CUSTOMER_ID CUSTOMER_NAME CUSTOMER_CIT CUSTOMER_DO EMAIL
Output:
Output:
Output:
Output:
AIM:
To implement and execute triggers and functions in Oracle Database using Procedural
Language concepts.
TRIGGERS:
SYNTAX:
{OLD AS OLD /NEW AS NEW} [FOR EACH STATEMENT /FOR EACH ROW
[WHEN <CONDITION>]]
DECLARE
Variable
declaration
Constant
declaration BEGIN
PL/SQL Sub-program
body. EXCEPTION
END;
FUNCTION:
A function is a sub-program that accepts an argument and returns a unique value to the
caller.
FUNCTION 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;
Example: Trigger for Insertion
Below program creates a trigger to calculate the total and average of a student for every
record insertion.
name VARCHAR2(20),
tamil NUMBER,
english NUMBER,
maths NUMBER,
science NUMBER,
social NUMBER,
total NUMBER,
average
NUMBER);
Table Created
TRIGGER PROGRAM
EACH ROW
DECLARE
BEGIN
END;
SQL> /
Trigger created.
NAME VARCHAR2(20)
TAMIL NUMBER
ENGLISH NUMBER
MATHS NUMBER
SCIENCE NUMBER
SOCIAL NUMBER
TOTAL NUMBER
AVERAGE NUMBER
SQL> INSERT INTO student(REGNO, NAME, TAMIL, ENGLISH, MATHS, SCIENCE, SOCIAL)
1 row created.
SQL> SELECT * FROM student;
ITEMID NUMBER(4)
NAME VARCHAR2(7)
QTY NUMBER(10)
1 row created.
10 Vimal 50
TRIGGER PROGRAM
CREATE OR REPLACE TRIGGER tv130 BEFORE DELETE ON stock131 FOR EACH ROW
DECLARE
BEGIN
END IF;
END;
Output:
SQL>
@tr130.sql
Trigger created.
ERROR at line 1:
ENO NUMBER(2)
ENAME VARCHAR2(10)
SALARY NUMBER(8)
20 FGF 4000
TRIGGER PROGRAM
CREATE OR REPLACE TRIGGER tr130 BEFORE UPDATE ON emp1301 FOR EACH ROW
DECLARE
BEGIN
END IF;
END;
Output:
SQL>
@tv130.sql
Trigger created.
ERROR at line 1:
BEGIN
IF n = 1 THEN
RETURN 1;
ELSE
END IF;
END;
Output:
SQL> @fact.sql
Function
created.
CALLING PROGRAM
DECLARE
n NUMBER;
BEGIN
n := &n;
END;
SQL> @main.sql
RESULT:
Thus, the program for the creation of database triggers and functions is executed
successfully.
Problem:
You are working with a database system where you need to enforce certain business
rules using triggers. You need to:
1. Calculate the total and average marks for a student whenever a new
record is inserted.
2. Prevent the deletion of a record from the stock131 table if the quantity is greater
than zero.
3. Prevent an update to an employee's salary unless the new salary is greater than
the old salary.
Solution:
DECLARE
BEGIN
END;
2. Example:
○ A new student Sachin with marks for various subjects is inserted. The
trigger calculates the total and average.
DECLARE
BEGIN
END IF;
END;
4. Example:
○ Attempting to delete an item Vimal from the stock when the quantity
is 50 raises an error and prevents the deletion.
5. Trigger for Update (Prevent Update If New Salary <= Old Salary):
DECLARE
BEGIN
END IF;
END;
6. Example:
insert Adodc1.Recordset.AddNew
End Sub
delete Adodc1.Recordset.Delete
Adodc1.Recordset.MovePrevious
End If
MsgBox("RECORD DELETED")
End Sub
update Adodc1.Recordset.Update
MsgBox("RECORD UPDATED")
End Sub
exit Unload Me
End Sub
RESULT:
Thus, the Student Database Application program is executed successfully.
Problem:
Solution:
The solution involves developing the student database application by setting up the
Oracle database connection in Visual Basic using ADO (ActiveX Data Objects) and
Oracle ODBC drivers. The necessary steps include:
○ Add the Microsoft ADO Data Control 6.0 to the form via the Toolbox.
○ Configure the ADO Data Control to use the Oracle DSN created
earlier, setting up the connection string, user credentials, and
record source to connect to the "Studentdb" table in Oracle.
By following these steps, the student database application allows users to manage
student records effectively. Upon executing the program, it successfully connects to the
Oracle database and performs the required CRUD operations.
Result:
The student database application was successfully developed and executed, providing an
interface for adding, updating, and deleting student records stored in the Oracle
database.
PROBLEM STATEMENT
Code :
);
college_id INT,
college_id INT,
captain_id INT,
);
);
ground_id INT,
);
team_id INT,
game_id INT,
rank INT,
PRIMARY KEY (team_id, game_id),
);
team_id INT,
game_id INT,
);
('Alice', 2);
INSERT INTO Team (team_name, college_id, captain_id) VALUES ('Team A', 1, 1);
INSERT INTO Team (team_name, college_id, captain_id) VALUES ('Team B', 2, 2);
INSERT INTO Ground (ground_name, location) VALUES ('Main Ground', 'ABC College');
INSERT INTO Ground (ground_name, location) VALUES ('Sports Arena', 'XYZ College');
INSERT INTO Game (game_name, ground_id) VALUES ('Football', 1);
FROM Awards
Er diagram :