Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
19 views19 pages

DB Observation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views19 pages

DB Observation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

1.

Study of Database Concepts: Relational model – table – operations on


tables – index – table space – clusters – synonym – view – schema – data
dictionary – privilege – role transactions.

Aim:
To study and understand basic database concepts such as tables, operations on tables, indexes,
views, schemas, and privileges using SQL queries.

Procedure:
1. Create a table and insert data.
2. Perform operations like SELECT, INSERT, UPDATE, DELETE.
3. Create an index and view.
4. Check schema and data dictionary information.
5. Create a synonym and role.
6. Grant and revoke privileges.
7. Perform a simple transaction with COMMIT and ROLLBACK.

Queries & Output:

-- 1. Create a table
CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT
);

-- 2. Insert data
INSERT INTO student VALUES (1, 'Amit', 21);
INSERT INTO student VALUES (2, 'Sara', 22);

-- 3. Select data
SELECT * FROM student;

-- 4. Update data
UPDATE student SET age = 23 WHERE id = 2;
-- 5. Delete data
DELETE FROM student WHERE id = 1;

-- 6. Create an index
CREATE INDEX idx_age ON student(age);

-- 7. Create a view
CREATE VIEW student_view AS SELECT name, age FROM student;

-- 8. Check schema info (for example)


SELECT table_name FROM user_tables;

-- 9. Create synonym
CREATE SYNONYM stud FOR student;

-- 10. Create role and assign privileges


CREATE ROLE role_stud;
GRANT SELECT, INSERT ON student TO role_stud;

-- 11. Transaction example


BEGIN;
UPDATE student SET age = 25 WHERE id = 2;
ROLLBACK;

Result:
Basic database concepts like table creation, data manipulation, indexing, views, schema details,
synonym, roles, privileges, and transaction control were successfully executed and understood.
2. Study of SQL: Primitive Data Types – User Defined data Types –
Built-in Functions – Parts of Speech of create, alter, drop, select,
insert, delete, update, commit, rollback, save point, grant, Revoke.

Aim:
To study SQL data types, built-in functions, and SQL commands like CREATE, ALTER, DROP,
SELECT, INSERT, UPDATE, DELETE, COMMIT, ROLLBACK, SAVEPOINT, GRANT, and
REVOKE.

Procedure:
1. Create a table using primitive and user-defined data types.
2. Use SQL commands to insert, update, delete data.
3. Apply built-in functions on data.
4. Alter and drop table structure.
5. Use transaction control commands: COMMIT, ROLLBACK, SAVEPOINT.
6. Grant and revoke privileges.

Queries & Output:

-- 1. Create a table with data types


CREATE TABLE employee (
emp_id INT,
emp_name VARCHAR(30),
salary FLOAT,
doj DATE
);

-- 2. Insert data
INSERT INTO employee VALUES (1, 'John', 50000, '2021-06-01');
INSERT INTO employee VALUES (2, 'Rita', 60000, '2022-01-10');

-- 3. Use built-in function


SELECT emp_name, UPPER(emp_name), NOW() FROM employee;

-- 4. Update salary
UPDATE employee SET salary = 65000 WHERE emp_id = 2;
-- 5. Savepoint and rollback
SAVEPOINT sp1;
DELETE FROM employee WHERE emp_id = 1;
ROLLBACK TO sp1;

-- 6. Alter table to add column


ALTER TABLE employee ADD dept VARCHAR(20);

-- 7. Drop a column
ALTER TABLE employee DROP COLUMN dept;

-- 8. Grant select privilege


GRANT SELECT ON employee TO PUBLIC;

-- 9. Revoke select privilege


REVOKE SELECT ON employee FROM PUBLIC;

-- 10. Drop table


DROP TABLE employee;

Result:
The SQL data types, functions, and commands for table management, data manipulation, and
transaction control were successfully executed and understood.
3. Study of Query Types: Queries involving Union, Intersection,
Difference, Cartesian product, Divide Operations – Subqueries – Join
Queries – Nested Queries – Correlated, Queries – Recursive Queries.

Aim:
To understand and perform SQL queries involving union, intersection, difference, cartesian product,
division, subqueries, joins, nested, correlated, and recursive queries.

Procedure:
1. Create and insert data into multiple related tables.
2. Perform set operations like UNION, INTERSECT, and MINUS (difference).
3. Use joins, subqueries, nested and correlated queries.
4. Execute recursive queries for hierarchical data.

Queries & Output:

-- Create two tables


CREATE TABLE dept (
dept_id INT,
dept_name VARCHAR(20)
);

CREATE TABLE employee (


emp_id INT,
emp_name VARCHAR(20),
dept_id INT
);

-- Insert data
INSERT INTO dept VALUES (1, 'HR'), (2, 'IT'), (3, 'Finance');
INSERT INTO employee VALUES (101, 'Alice', 1), (102, 'Bob', 2), (103, 'Eve', 2);

-- 1. UNION
SELECT dept_id FROM dept
UNION
SELECT dept_id FROM employee;
-- 2. INTERSECT (may not work in all DBMS, like MySQL)
SELECT dept_id FROM dept
INTERSECT
SELECT dept_id FROM employee;

-- 3. DIFFERENCE / MINUS
SELECT dept_id FROM dept
MINUS
SELECT dept_id FROM employee;

-- 4. CARTESIAN PRODUCT
SELECT * FROM employee, dept;

-- 5. DIVISION operation (employees who work in all departments — logic


simulated using NOT EXISTS)
-- Skipped in basic example due to complexity

-- 6. INNER JOIN
SELECT emp_name, dept_name
FROM employee e
JOIN dept d ON e.dept_id = d.dept_id;

-- 7. SUBQUERY
SELECT emp_name FROM employee
WHERE dept_id = (SELECT dept_id FROM dept WHERE dept_name = 'IT');

-- 8. NESTED QUERY
SELECT * FROM employee
WHERE dept_id IN (SELECT dept_id FROM dept WHERE dept_name LIKE 'I%');

-- 9. CORRELATED QUERY
SELECT emp_name FROM employee e
WHERE EXISTS (SELECT * FROM dept d WHERE e.dept_id = d.dept_id AND d.dept_name =
'HR');

-- 10. RECURSIVE QUERY (Example in PostgreSQL / Oracle using WITH)


-- Find numbers from 1 to 5
WITH RECURSIVE nums(n) AS (
SELECT 1
UNION ALL
SELECT n + 1 FROM nums WHERE n < 5
)
SELECT * FROM nums;

Result:
Various types of SQL queries including set operations, joins, subqueries, and recursive queries were
successfully executed and understood.
4. Study of Procedural Query Language: Blocks, Exception Handling, Functions,
Procedures, Cursors,Triggers, Packages.

Aim:
To understand and use PL/SQL features such as blocks, exception handling, functions, procedures,
cursors, triggers, and packages.

Procedure:
1. Write a PL/SQL block to print a message.
2. Add exception handling for division by zero.
3. Create a function to calculate bonus based on salary.
4. Write a procedure to update employee salary.
5. Use a cursor to display employee details.
6. Create a trigger to log salary changes.
7. Create a package that includes both a function and a procedure.

Queries & Output:

-- 1. Simple PL/SQL Block


BEGIN
DBMS_OUTPUT.PUT_LINE('Welcome to PL/SQL');
END;

-- 2. Exception Handling
DECLARE
a NUMBER := 10;
b NUMBER := 0;
c NUMBER;

BEGIN
c := a / b;

EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Cannot divide by zero');
END;
-- 3. Function
CREATE OR REPLACE FUNCTION calc_bonus(salary NUMBER)
RETURN NUMBER IS
BEGIN
RETURN salary * 0.10;
END;

-- 4. Procedure
CREATE OR REPLACE PROCEDURE raise_salary(empid IN NUMBER, inc IN NUMBER) IS
BEGIN
UPDATE employee SET salary = salary + inc WHERE emp_id = empid;
END;

-- 5. Cursor
DECLARE
CURSOR c IS SELECT emp_name FROM employee;
name employee.emp_name%TYPE;
BEGIN
OPEN c;
LOOP
FETCH c INTO name;
EXIT WHEN c%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(name);
END LOOP;
CLOSE c;
END;

-- 6. Trigger
CREATE OR REPLACE TRIGGER log_salary_change
AFTER UPDATE OF salary ON employee
FOR EACH ROW
BEGIN
INSERT INTO salary_log(emp_id, old_sal, new_sal, change_date)
VALUES (:OLD.emp_id, :OLD.salary, :NEW.salary, SYSDATE);
END;

-- 7. Package
CREATE OR REPLACE PACKAGE emp_pkg IS
FUNCTION calc_bonus(salary NUMBER) RETURN NUMBER;
PROCEDURE raise_salary(empid NUMBER, inc NUMBER);
END;

CREATE OR REPLACE PACKAGE BODY emp_pkg IS


FUNCTION calc_bonus(salary NUMBER) RETURN NUMBER IS
BEGIN
RETURN salary * 0.10;
END;

PROCEDURE raise_salary(empid NUMBER, inc NUMBER) IS


BEGIN
UPDATE employee SET salary = salary + inc WHERE emp_id = empid;
END;
END;
Result:
PL/SQL concepts like blocks, exceptions, functions, procedures, cursors, triggers, and packages
were successfully practiced and understood.

Application: Design and develop the following:


(a) Students’ Information System (b)Hotel Management System

Aim:
To design and implement a Students’ Information System and Hotel Management System using
MySQL with basic database operations.

Procedure:
Part A: Students’ Information System
1. Create a student table with columns: ID, name, age, department.
2. Insert sample student records.

3. Perform SELECT, UPDATE, DELETE queries.

4. Use ALTER to add a new column.

5. Use constraints like PRIMARY KEY.


6. Drop the table at the end.

Part B: Hotel Management System


1. Create rooms and guests tables with appropriate keys.
2. Insert sample records into both tables.

3. Perform JOIN to fetch guest-room combined data.


4. Update room status based on booking.
5. Delete a guest record.
6. Display data using conditions (WHERE, ORDER BY).

Queries & Output:

-- Part A: Students’ Information System

-- 1. Create student table


CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
department VARCHAR(50));
-- 2. Insert records
INSERT INTO student VALUES (1, 'Ravi', 20, 'CSE');
INSERT INTO student VALUES (2, 'Anu', 21, 'ECE');

-- 3. Select all
SELECT * FROM student;

-- 4. Update age
UPDATE student SET age = 22 WHERE id = 2;

-- 5. Delete a record
DELETE FROM student WHERE id = 1;

-- 6. Alter table to add new column


ALTER TABLE student ADD grade CHAR(1);

-- 7. Drop student table


DROP TABLE student;

-- --------------------------------------------------------

-- Part B: Hotel Management System

-- 1. Create rooms table


CREATE TABLE rooms (
room_no INT PRIMARY KEY,
type VARCHAR(20),
status VARCHAR(10)
);

-- 2. Create guests table


CREATE TABLE guests (
guest_id INT PRIMARY KEY,
name VARCHAR(50),
room_no INT,
FOREIGN KEY (room_no) REFERENCES rooms(room_no)
);

-- 3. Insert data
INSERT INTO rooms VALUES (101, 'AC', 'Available'), (102, 'Non-AC', 'Booked');
INSERT INTO guests VALUES (1, 'John', 102);

-- 4. Join guest with room details


SELECT g.name, r.type FROM guests g JOIN rooms r ON g.room_no = r.room_no;

-- 5. Update room status


UPDATE rooms SET status = 'Booked' WHERE room_no = 101;

-- 6. Delete a guest
DELETE FROM guests WHERE guest_id = 1;

-- 7. Order room details


SELECT * FROM rooms ORDER BY type;
Result:
Both Students’ Information System and Hotel Management System were successfully created and
tested using basic SQL operations such as table creation, data insertion, updates, joins, and deletions
in MySQL.

Output:

For SELECT query :-

+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | Amit | 21 |
| 2 | Sara | 22 |
+----+------+-----+
Output:

For SELECT query (Use built-in function ) :-

+----------+-----------+---------------------+
| emp_name | upper | now() |
+----------+-----------+---------------------+
| John | JOHN | 2025-07-19 16:00:00 |
| Rita | RITA | 2025-07-19 16:00:00 |
+----------+-----------+---------------------+
Output :

For Union :-
+---------+
| dept_id |
+---------+
| 1 |
| 2 |
| 3 |
+---------+

For Intersection :-
+---------+
| dept_id |
+---------+
| 1 |
| 2 |
+---------+

For Cartesian Product :-


+--------+-----------+---------+---------+-----------+
| emp_id | dept_name | dept_id | dept_id | dept_name |
+--------+-----------+---------+---------+-----------+
| 103 | Eve | 2 | 1 | HR |
| 102 | Bob | 2 | 1 | HR |
| 101 | Alice | 1 | 1 | HR |
| 103 | Eve | 2 | 2 | IT |
| 102 | Bob | 2 | 2 | IT |
| 101 | Alice | 1 | 2 | IT |
| 103 | Eve | 2 | 3 | Finance |
| 102 | Bob | 2 | 3 | Finance |
| 101 | Alice | 1 | 3 | Finance |
+--------+-----------+---------+---------+-----------+

For Nested query :-


+--------+-----------+---------+
| emp_id | dept_name | dept_id |
+--------+-----------+---------+
| 102 | Bob | 2 |
| 103 | Eve | 2 |
+--------+-----------+---------+

For recursive query :-


+---+
| n |
+---+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+---+

Output :

For Simple PL/SQL Block :-

Welcome to PL/SQL

For Exception Handling :-

Cannot divide by zero


Output:

SELECT query for Students’ Information System :-

+------------------------------+
| id | name | age | department |
+----|------|-----|------------+
| 1 | Ravi | 20 | CSE |
| 2 | Anu | 21 | ECE |
+------------------------------+

JOIN query for Hotel Management System :-

+---------------+
| name | type |
+------+--------+
| John | Non-AC |
+---------------+

You might also like