20191ise0008 - Akshay - DBMS Lab Obs
20191ise0008 - Akshay - DBMS Lab Obs
20191ise0008 - Akshay - DBMS Lab Obs
EXPERIMENT-1
AKSHAY HARISH
20191ISE0008
4ISE1
COMMANDS
Enter password: **********
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| studentdb |
| sys |
| world |
+--------------------+
Database changed
+-------+-------------+------+-----+---------+-------+
+-------+-------------+------+-----+---------+-------+
+-------+-------------+------+-----+---------+-------+
+-------+-------------+------+-----+---------+-------+
+-------+-------------+------+-----+---------+-------+
+-------+-------------+------+-----+---------+-------+
+-------+-------------+------+-----+---------+-------+
+-------+-------------+------+-----+---------+-------+
+-------+-------------+------+-----+---------+-------+
+-------+-------------+------+-----+---------+-------+
+-------+-------------+------+-----+---------+-------+
+-------+-------------+------+-----+---------+-------+
AKSHAY HARISH
20191ISE0008
4ISE1
COMMANDS
AKSHAY HARISH
20191ISE0008
4ISE1
Experiment No.3: To Study and Implement SQL Constraints
i)Entity Integrity Constraint: Primary Key Value cannot be NULL and duplicate.
mysql> INSERT INTO STUDENT VALUES (NULL,'CHETHAN','CSE','2000-20-03',4);
ERROR 1048 (23000): Column 'SNUM' cannot be null.
mysql> INSERT INTO STUDENT(SNUM,SNAME,MAJOR,DOB,SEM) VALUES (1001,'CHETHAN','CSE','2000-05-
03',4);
ERROR 1062 (23000): Duplicate entry '1001' for key 'PRIMARY'.
AKSHAY HARISH
20191ISE0008
4ISE1
Experiment No. 4: To study and implement SQL data retrieval using
SELECT, FROM and WHERE clause.
1.Add the columns 'Fees' & 'Email' to the STUDENT table with default value '30000' &
'[email protected]'.
9. Display the details of the student whose student name starts with letter R.
10.Delete the first two records of a student table.
DBMS LAB
EXPERIMENT-5
AKSHAY HARISH
20191ISE0008
4ISE1
Experiment No.5: To study and implement different SQL single
row and multiple row functions.
AKSHAY HARISH
20191ISE0008
4ISE1
Experiment No.6: To study and implement aggregating Data using Group By
Clause, HAVING clause and sort data using Order By clause.
BRANCH TABLE
CREATE TABLE BRANCH (BR_NAME VARCHAR(20) PRIMARY KEY, BR_CITY VARCHAR(20), ASSETS REAL);
ACCOUNT TABLE
CREATE TABLE ACCOUNT (ACCNO INT PRIMARY KEY, BR_NAME VARCHAR(20), BALANCE REAL, FOREIGN
KEY (BR_NAME) REFERENCES BRANCH (BR_NAME) ON DELETE CASCADE);
CUSTOMER TABLE
CREATE TABLE CUSTOMER (CUST_NAME VARCHAR(20) PRIMARY KEY, CUST_STREET VARCHAR (20),
CUST_CITY VARCHAR (20));
DEPOSITOR TABLE
CREATE TABLE DEPOSITOR (CUST_NAME VARCHAR (20), ACCNO INT, PRIMARY KEY (CUST_NAME,
ACCNO), FOREIGN KEY (CUST_NAME) REFERENCES CUSTOMER (CUST_NAME) ON DELETE CASCADE,
FOREIGN KEY (ACCNO) REFERENCES ACCOUNT (ACCNO) ON DELETE CASCADE);
LOAN TABLE
CREATE TABLE LOAN (LOAN_NO INT PRIMARY KEY, BR_NAME VARCHAR (20), AMOUNT REAL, FOREIGN
KEY (BR_NAME) REFERENCES BRANCH (BR_NAME) ON DELETE CASCADE);
BORROWER TABLE
CREATE TABLE BORROWER (CUST_NAME VARCHAR (20), LOAN_NO INT, PRIMARY KEY (CUST_NAME,
LOAN_NO), FOREIGN KEY (CUST_NAME) REFERENCES CUSTOMER (CUST_NAME) ON DELETE CASCADE,
FOREIGN KEY (LOAN_NO) REFERENCES LOAN (LOAN_NO) ON DELETE CASCADE);
INSERT INTO TABLES
21. Find all the customers who have at least two accounts at the main branch.
22. Demonstrate how you delete all account tuples at every branch located in a specific
city.
23. Find all the customers who have an account at all the branches located in a specific
city.
24. Find all the customers with more than one loan
25. Find branches with assets greater than all branches in Bangalore
DBMS LAB
EXPERIMENT-7
AKSHAY HARISH
20191ISE0008
4ISE1
Experiment No.7: To Study and Implement different types of Set
Operation in SQL.
2. INTERSECTION OPERATION: Find the branch name of the branch table that currently
have loans
SELECT br_name FROM branch WHERE br_name IN(SELECT br_name FROM loan);
3.MINUS/DIFFERENCE OPEARATION: Find the number of branches that currently DONT
have loans
SELECT br_name FROM branch WHERE br_name NOT IN(SELECT br_name FROM loan);
4.CARTESIAN PRODUCT:
AKSHAY HARISH
20191ISE0008
4ISE1
Experiment No.8: To Study and Implement different types of Joins in SQL.
1. EQUI JOIN OPERATION: Display employee and their respective branch where employee
department number is same as department’s department id.
2. JOIN WITH NOT EQUALITY OPERATOR(NON EQUI JOIN): Display employee and their
respective Department where employee department number is not same as department’s
department id.
3. EQUI JOIN WITH SPECIFIED CONDITION: Display employee and their respective
Department where employee department number is not same as department’s department
id and Department name is MARKETING
D.DEPT_NAME='MARKETING';
OUTER JOIN:
1. LEFT OUTER JOIN OR LEFT JOIN: Join Employee and department tables with reference to
employee table
2. RIGHT OUTER JOIN OR RIGHT JOIN: Join Employee and department tables with reference
to department table
UNION
AKSHAY HARISH
20191ISE0008
4ISE1
Experiment No.9: To study and implement Sub queries/Nested queries,
Correlated nested queries in SQL.
1. Find the names of all Juniors (level = JR) who have enrolled in a Class taught by
Prof.Narasimha.
2. Find the names of all courses that either meet in room KG04 or have five or more
Students enrolled.
FROM COURSE C
FROM ENROLLED E
GROUP BY E.CNAME
3. Find the names of all students who have enrolled in two courses that meet at the same
time
FROM STUDENT S
WHERE S.SID IN
( SELECT E1.SID
FROM STUDENT S
FROM ENROLLED E
GROUP BY E.SID
FROM ENROLLED E2
GROUP BY E2.SID));
5. Find the names of faculty members for whom the combined enrolment of the courses
that they teach is less than five.
FROM FACULTY F
WHERE C.CNAME=E.CNAME
GROUP BY C.FID
HAVING COUNT(*)<5);
DBMS LAB
EXPERIMENT-10
AKSHAY HARISH
20191ISE0008
4ISE1
mysql> use studentdb;
Database changed
mysql> create view student_detail as select * from student;
Query OK, 0 rows affected (0.05 sec)
AKSHAY HARISH
20191ISE0008
4ISE1
Experiment No.11: To study and implement Functions and Procedures.
Step 1: Change the delimiter (i.e., terminating character) of SQL statement from semicolon
(;) to something else (e.g., //) So that you can distinguish between the semicolon of the SQL
statements in the procedure and the terminating character of the procedure definition.
Step 2: 1.Define a procedure called updateSalary which takes as input a department
number. 2.The body of the procedure is an SQL command to update the totalsalary column
of the deptsal table. 3.Terminate the procedure definition using the delimiter you had
defined in step 1 (//)
Step 3: Change the delimiter back to semicolon (;)
mysql> delimeter ;
Step 4: Call the procedure to update the totalsalary for each department
Step 5: Show the updated total salary in the deptsal table
Example using Cursors:
•The previous procedure updates one row in deptsal table based on input parameter
Example of Functions:
DBMS LAB
EXPERIMENT-12
AKSHAY HARISH
20191ISE0008
4ISE1
Experiment No.12: To study and implement SQL Triggers
1.Create a trigger to update the total salary of a department when a new employee is hired:
2. Create a trigger to update the total salary of a department when an employee tuple is
modified:
3. Create a trigger to update the total salary of a department when an employee tuple is
deleted: