DBMS Lab Record 2017-18 2.0
DBMS Lab Record 2017-18 2.0
DDL
Create table
Alter table
o Add/Drop attribute
o Modify type and size of an attribute
o Rename attribute
Truncate
Drop
Student(rollno,name)
Add branch
Modify the width of Attribute branch
Modify the datatype of Attribute branch
Rename the attribute
Remove the attribute
1
mysql> desc students;
+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| rollno | int(11) | YES | | NULL | |
| name | char(20) | YES | | NULL | |
| branch | char(10) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
2
2. DML
Insert
Update
Delete
3
mysql> select * from students;
+--------+---------+--------+
| rollno | name | branch |
+--------+---------+--------+
| 1 | saketh | cse |
| 2 | venkat | it |
| 3 | vinod | ece |
| 4 | sathish | it |
| 4 | sridhar | eee |
+--------+---------+--------+
5 rows in set (0.00 sec)
mysql> update students set name='vamshi' where rollno=4;
Query OK, 2 rows affected (0.48 sec)
Rows matched: 2 Changed: 2 Warnings: 0
+--------+--------+--------+
| rollno | name | branch |
+--------+--------+--------+
| 1 | saketh | cse |
| 2 | venkat | it |
| 3 | vinod | ece |
| 4 | vamshi | it |
| 4 | vamshi | eee |
+--------+--------+--------+
5 rows in set (0.00 sec)
mysql> delete from students where rollno=1;
Query OK, 1 row affected (0.34 sec)
4
3. Objective to understand the commands used to impose constraints on a table such as
Not null
unique
check
primary key
foreign key
Database used:
Branch()
Account()
Loan()
Customer()
Depositor()
Borrower()
5
create table Borrower
(customer_name varchar(15) ,
loan_number varchar(15) ,
primary key(customer_name, loan_number),
foreign key(customer_name) references Customer(customer_name),
foreign key(loan_number) references Loan(loan_number));
6
4.Simple to Complex Queries
Select
o distinct
o order by
o group by
Comparison operators
o Like, Not like
o > ,<.>=,<=,==,<>
o Between and, not between and
o In, not in
o Any, all
Aggregate functions
o Count, Sum, Average, Max, Min
Set Operations
o Union, Except, Intersect
Nested Queries
o Correlated
Database used:
emp()
dept()
Depositor()
Borrower()
+-----------+
| job |
+-----------+
| CLERK |
| SALESMAN |
| MANAGER |
| ANALYST |
| PRESIDENT |
+-----------+
5 rows in set (0.05 sec)
+-------+
| ename |
+-------+
| JAMES |
| JONES |
+-------+
2 rows in set (0.03 sec)
mysql> select ename from emp where ename not like 'J%';
+--------+
| ename |
+--------+
| ADAMS |
| ALLEN |
| BLAKE |
| CLARK |
7
| FORD |
| KING |
| MARTIN |
| MILLER |
| SCOTT |
| SMITH |
| TURNER |
| WARD |
+--------+
12 rows in set (0.00 sec)
8
mysql> select ename from emp where ename like 'S%H';
+-------+
| ename |
+-------+
| SMITH |
+-------+
1 row in set (0.00 sec)
mysql> select empno,ename from emp where sal between 1500 and 2000;
+-------+--------+
| empno | ename |
+-------+--------+
| 7499 | ALLEN |
| 7844 | TURNER |
+-------+--------+
2 rows in set (0.00 sec)
mysql> select empno,ename from emp where sal not between 1500 and 2000;
+-------+--------+
| empno | ename |
+-------+--------+
| 7876 | ADAMS |
| 7698 | BLAKE |
| 7782 | CLARK |
| 7902 | FORD |
| 7900 | JAMES |
| 7566 | JONES |
| 7839 | KING |
| 7654 | MARTIN |
| 7934 | MILLER |
| 7788 | SCOTT |
| 7369 | SMITH |
| 7521 | WARD |
+-------+--------+
12 rows in set (0.00 sec)
11
mysql> select ename from emp where sal=(select max(sal) from emp);
+-------+
| ename |
+-------+
| KING |
+-------+
1 row in set (0.02 sec)
mysql> SELECT e1.empno, e1.sal FROM emp e1 WHERE e1.sal in (SELECT e2.sal FROM emp e2
WHERE e2.deptno = 10);
+-------+------+
| empno | sal |
+-------+------+
| 7782 | 2450 |
| 7839 | 5000 |
| 7934 | 1300 |
+-------+------+
3 rows in set (0.02 sec)
mysql> SELECT e1.empno, e1.sal FROM emp e1 WHERE e1.sal > ALL (SELECT e2.sal FROM emp e2
WHERE e2.deptno = 10);
mysql> SELECT e1.empno, e1.sal FROM emp e1 WHERE e1.sal > ANY (SELECT e2.sal FROM emp e2
WHERE e2.deptno=10);
+-------+------+
| empno | sal |
+-------+------+
| 7499 | 1600 |
| 7698 | 2850 |
| 7782 | 2450 |
| 7902 | 3000 |
| 7566 | 2975 |
| 7839 | 5000 |
| 7788 | 3000 |
| 7844 | 1500 |
+-------+------+
8 rows in set (0.00 sec)
12
mysql> select ename from emp where exists (select * from emp);
+--------+
| ename |
+--------+
| ADAMS |
| ALLEN |
| BLAKE |
| CLARK |
| FORD |
| JAMES |
| JONES |
| KING |
| MARTIN |
| MILLER |
| SCOTT |
| SMITH |
| TURNER |
| WARD |
+--------+
14 rows in set (0.02 sec)
mysql> SELECT e1.empno, e1.sal FROM emp e1 WHERE EXISTS (SELECT e2.sal FROM emp e2
WHERE e2.deptno = 10 AND e1.sal > e2.sal);
+-------+------+
| empno | sal |
+-------+------+
| 7499 | 1600 |
| 7698 | 2850 |
| 7782 | 2450 |
| 7902 | 3000 |
| 7566 | 2975 |
| 7839 | 5000 |
| 7788 | 3000 |
| 7844 | 1500 |
+-------+------+
8 rows in set (0.02 sec)
+-------------+-------------+--------+
| loan_number | branch_name | amount |
+-------------+-------------+--------+
| L-11 | Round Hill | 900 |
| L-14 | Downtown | 1500 |
| L-15 | Perryridge | 1500 |
| L-16 | Perryridge | 1300 |
| L-17 | Downtown | 1000 |
| L-20 | North Town | 7500 |
| L-21 | Central | 570 |
| L-23 | Redwood | 2000 |
| L-93 | Mianus | 500 |
+-------------+-------------+--------+
9 rows in set (0.00 sec)
mysql> select *from customer;
+---------------+-----------------+---------------+
| customer_name | customer_street | customer_city |
+---------------+-----------------+---------------+
| Adams | Spring | Pittsfield |
| Brooks | Senator | Brooklyn |
| Curry | North | Rye |
| Glenn | Sand Hill | Woodside |
| Green | Walnut | Stamford |
| Hayes | Main | Harrison |
| Jackson | University | Salt Lake |
| Johnson | Alma | Palo Alto |
| Jones | Main | Harrison |
| Lindsay | Park | Pittsfield |
| Majeris | First | Rye |
| McBride | Safety | Rye |
| Smith | Main | Rye |
| Turner | Putnam | Stamford |
| Williams | Nassau | Princeton |
+---------------+-----------------+---------------+
15 rows in set (0.00 sec)
mysql> select * from borrower;
+---------------+-------------+
| customer_name | loan_number |
+---------------+-------------+
| Smith | L-11 |
| Jackson | L-14 |
| Adams | L-16 |
| Jones | L-17 |
| Williams | L-17 |
| McBride | L-20 |
| Smith | L-21 |
| Curry | L-93 |
15
+---------------+----------------+
| Hayes | A-101 |
| Johnson | A-101 |
| Hayes | A-102 |
| Johnson | A-201 |
| Smith | A-215 |
| Jones | A-217 |
| Lindsay | A-222 |
| Turner | A-305 |
| Majeris | A-333 |
| Smith | A-444 |
+---------------+----------------+
10 rows in set (0.03 sec)
mysql> select customer_name,borrower.loan_number
-> from borrower,loan
-> where borrower.loan_number=loan.loan_number and branch_name='Perryridge';
+---------------+-------------+
| customer_name | loan_number |
+---------------+-------------+
| Adams | L-16 |
+---------------+-------------+
1 row in set (0.00 sec)
mysql> select distinct t.branch_name
-> from branch t,branch s
-> where t.assets>s.assets and s.branch_city='Brooklyn';
+-------------+
| branch_name |
+-------------+
| Brighton |
| North Town |
| Perryridge |
| Redwood |
| Round Hill |
+-------------+
5 rows in set (0.00 sec)
mysql> select customer_name from customer where customer_street like '%Main%';
+---------------+
| customer_name |
+---------------+
| Hayes |
| Jones |
| Smith |
+---------------+
3 rows in set (0.00 sec)
mysql> select distinct customer_name
-> from borrower,loan
-> where borrower.loan_number = loan.loan_number and branch_name='Perryridge';
+---------------+
| customer_name |
+---------------+
| Adams |
+---------------+
1 row in set (0.00 sec)
mysql> select *
-> from loan
-> order by amount desc, loan_number asc;
+-------------+-------------+--------+
| loan_number | branch_name | amount |
+-------------+-------------+--------+
| L-20 | North Town | 7500 |
16
| L-23 | Redwood | 2000 |
| L-14 | Downtown | 1500 |
| L-15 | Perryridge | 1500 |
| L-16 | Perryridge | 1300 |
| L-17 | Downtown | 1000 |
| L-11 | Round Hill | 900 |
| L-21 | Central | 570 |
| L-93 | Mianus | 500 |
+-------------+-------------+--------+
9 rows in set (0.00 sec)
mysql> select avg (balance) from account where branch_name='Perryridge';
+---------------+
| avg (balance) |
+---------------+
| 650.0000 |
+---------------+
1 row in set (0.00 sec)
mysql> select branch_name,avg(balance)from account group by branch_name;
+-------------+--------------+
| branch_name | avg(balance) |
+-------------+--------------+
| Brighton | 750.0000 |
| Central | 850.0000 |
| Downtown | 500.0000 |
| Mianus | 700.0000 |
| North Town | 625.0000 |
Vnktrmnb/DBMS LAB/Queries on Banking Schema
| Perryridge | 650.0000 |
| Redwood | 700.0000 |
| Round Hill | 350.0000 |
+-------------+--------------+
8 rows in set (0.00 sec)
mysql> select branch_name, count(distinct customer_name) from depositor, account
-> where depositor.account_number = account.account_number
-> group by branch_name;
+-------------+-------------------------------+
| branch_name | count(distinct customer_name) |
+-------------+-------------------------------+
| Brighton | 1 |
| Central | 1 |
| Downtown | 2 |
| Mianus | 1 |
| North Town | 1 |
| Perryridge | 2 |
| Redwood | 1 |
| Round Hill | 1 |
+-------------+-------------------------------+
8 rows in set (0.04 sec)
mysql>
mysql> select branch_name, avg (balance)
-> from account
-> group by branch_name
-> having avg (balance) > 1200;
Empty set (0.00 sec)
mysql>
mysql> select avg (balance)from account;
+---------------+
| avg (balance) |
+---------------+
| 641.6667 |
17
+---------------+
1 row in set (0.00 sec)
mysql> select count(*) from customer;
+----------+
| count(*) |
+----------+
| 15 |
+----------+
1 row in set (0.00 sec)
mysql> select d.customer_name, avg(a.balance)
-> from depositor d , account a, customer c
-> where d.account_number = a.account_number and d.customer_name = c.customer_name ;
+---------------+----------------+
| customer_name | avg(a.balance) |
+---------------+----------------+
| Hayes | 627.5000 |
+---------------+----------------+
1 row in set (0.00 sec)
mysql> select d.customer_name, avg(a.balance)
-> from depositor d , account a, customer c
-> where d.account_number = a.account_number and d.customer_name = c.customer_name and
c.customer_city='Harrison';
+---------------+----------------+
| customer_name | avg(a.balance) |
+---------------+----------------+
| Hayes | 550.0000 |
+---------------+----------------+
1 row in set (0.00 sec)
mysql> select d.customer_name, avg(a.balance)
-> from depositor d , account a, customer c
-> where d.account_number = a.account_number and d.customer_name = c.customer_name and
c.customer_city='Harrison' group by d.customer_name;
+---------------+----------------+
| customer_name | avg(a.balance) |
+---------------+----------------+
| Hayes | 450.0000 |
| Jones | 750.0000 |
+---------------+----------------+
2 rows in set (0.00 sec)
mysql> select d.customer_name, avg(a.balance)
-> from depositor d , account a, customer c
-> where d.account_number = a.account_number and d.customer_name = c.customer_name and
c.customer_city='Harrison'
group by d.customer_name having count(distinct d.account_number)>3;
Empty set (0.00 sec)
mysql> (select customer_name
-> from Depositor)
-> union
-> (select customer_name
-> from Borrower);
+---------------+
| customer_name |
+---------------+
| Hayes |
| Johnson |
| Smith |
| Jones |
| Lindsay |
| Turner |
| Majeris |
18
| Jackson |
| Adams |
| Williams |
Vnktrmnb/DBMS LAB/Queries on Banking Schema
| McBride |
| Curry |
+---------------+
12 rows in set (0.00 sec)
mysql> select customer_name from depositor where customer_name in(select customer_name from
borrower);
+---------------+
| customer_name |
+---------------+
| Smith |
| Jones |
| Smith |
+---------------+
3 rows in set (0.00 sec)
mysql> select customer_name from depositor where customer_name not in(select customer_name from
borrower);
+---------------+
| customer_name |
+---------------+
| Hayes |
| Johnson |
| Hayes |
| Johnson |
| Lindsay |
| Turner |
| Majeris |
+---------------+
7 rows in set (0.00 sec)
19
5. JOINS
5. Objective To understand the working of various kinds of join operations in MySQL such as
Cartesian Product
Inner Join
o Self Join
o Equi join / Natural join
o Non equi join
Outer Join
o Left
o Right
Database used: emp(empno :int, ename : varchar(20), job : varchar(20), mgr : int, hiredate : date,
Sal : decimal(10,2), comm : decimal (10,2), deptno int)
Dept(deptno : int, dname : varcahar(20), location : varchar(20))
Salgrade( grade : int, losal : int, hisal : int)
Cartesian Product
Equi -Join
Another way
mysql> select e.ename "employee name" ,d.loc "location" from emp e,dept d where e.deptno=d.deptno;
+---------------+----------+
| employee name | location |
+---------------+----------+
| CLARK | NEW YORK |
| KING | NEW YORK |
| MILLER | NEW YORK |
| SMITH | DALLAS |
| JONES | DALLAS |
| SCOTT | DALLAS |
| ADAMS | DALLAS |
21
| FORD | DALLAS |
| ALLEN | CHICAGO |
| WARD | CHICAGO |
| MARTIN | CHICAGO |
| BLAKE | CHICAGO |
| TURNER | CHICAGO |
| JAMES | CHICAGO |
+---------------+----------+
14 rows in set (0.00 sec)
Non-Equijoin
mysql> select e.empno,e.ename,s.grade from emp e,salgrade s where e.sal between s.losal and s.hisal;
+-------+--------+-------+
| empno | ename | grade |
+-------+--------+-------+
| 7369 | SMITH | 1.00 |
| 7499 | ALLEN | 3.00 |
| 7521 | WARD | 2.00 |
| 7566 | JONES | 4.00 |
| 7654 | MARTIN | 2.00 |
| 7698 | BLAKE | 4.00 |
| 7782 | CLARK | 4.00 |
| 7788 | SCOTT | 4.00 |
| 7839 | KING | 5.00 |
| 7844 | TURNER | 3.00 |
| 7876 | ADAMS | 1.00 |
| 7900 | JAMES | 1.00 |
| 7902 | FORD | 4.00 |
| 7934 | MILLER | 2.00 |
+-------+--------+-------+
14 rows in set (0.00 sec)
Note :
mysql> select e.ename,d.loc from emp e natural join dept d on e.deptno=d.deptno;
22
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'on e.deptno=d.deptno' at line 5
Self join
mysql> select e1.ename "employee name",e2.ename "Manager name"
-> from emp e1,emp e2
-> where e1.mgr=e2.empno;
+---------------+--------------+
| employee name | Manager name |
+---------------+--------------+
| SMITH | FORD |
| ALLEN | BLAKE |
| WARD | BLAKE |
| JONES | KING |
| MARTIN | BLAKE |
| BLAKE | KING |
| CLARK | KING |
| SCOTT | JONES |
| TURNER | BLAKE |
| ADAMS | SCOTT |
| JAMES | BLAKE |
| FORD | JONES |
| MILLER | CLARK |
| MILLER | CLARK |
| JAMES | CLARK |
+---------------+--------------+
Outer Joins
Left Join
mysql> select e.ename,d.loc from emp e left join dept d on e.deptno=d.deptno;
+--------+----------+
| ename | loc |
+--------+----------+
| CLARK | NEW YORK |
| KING | NEW YORK |
| MILLER | NEW YORK |
| SMITH | DALLAS |
| JONES | DALLAS |
| SCOTT | DALLAS |
| ADAMS | DALLAS |
| FORD | DALLAS |
| ALLEN | CHICAGO |
| WARD | CHICAGO |
| MARTIN | CHICAGO |
| BLAKE | CHICAGO |
| TURNER | CHICAGO |
| JAMES | CHICAGO |
+--------+----------+
14 rows in set (0.00 sec)
23
Right join
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'full join
dept d
on e.deptno=d.deptno' at line 3
Full join
6. DCL
24
6. Objective To understand the syntax of DCL commands such as
Grant
Revoke
25
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| test |
+--------------------+
3 rows in set (0.00 sec)
26
7. Views
Database used:Books,Customer,Loan,Branch
27
+------+-------+------+
| isbn | title | cost |
+------+-------+------+
| 101 | C | 200 |
| 102 | CPP | 300 |
| 103 | CPP | 400 |
| 104 | JAVA | 400 |
| 107 | VB | 350 |
+------+-------+------+
5 rows in set (0.00 sec)
28
mysql> select * from mybooks;
+------+-------+------+
| isbn | title | cost |
+------+-------+------+
| 102 | CPP | 300 |
| 103 | CPP | 400 |
| 104 | JAVA | 400 |
| 107 | RDBMS | 350 |
+------+-------+------+
4 rows in set (0.00 sec)
31
8. Built-in Functions
Numeric Functions
String Functions
Date and Time Functions
+---------+--------+--------+
| abs(-5) | abs(0) | abs(5) |
+---------+--------+--------+
| 5| 0| 5|
+---------+--------+--------+
1 row in set (0.03 sec)
33
1 row in set (0.00 sec)
34
35
9. MySQL Table Locking
In this experiment, you will learn how to use MySQL locking for cooperating table access between sessions.
UNLOCK TABLES;
After that, to acquire a lock, you use the LOCK TABLE statement.
So the once READ lock is acquired, you cannot write data into the table within the same session. Let’s check the READ lock
from a different session.
36
Next, insert a new row into the books table from the second session.
The insert operation from the second session is in the waiting state because a READ lock already acquired on the books table
by the first session and it has not released yet.
You can see the detailed information from the SHOW PROCESSLIST statement.
After that, go back to the first session and release the lock by using the UNLOCK TABLES statement.
After you release the READ lock from the first session, the INSERT operation in the second session executed.
Finally, check it the data of the books table to see if the INSERT operation from the second session really executed.
1. Only session that holds the lock of a table can read and write data from the table.
2. Other sessions cannot read and write from the table until the WRITE lock is released.
It works.
Next, read data from the books table.
It is fine.
After that, from the second session, try to write and read data:
37
mysql> INSERT INTO books VALUES(112255,'C PROGRAMMING','Ditel');
mysql> SELECT * FROM books;
MySQL puts those operations into a waiting state. You can check it using the SHOW PROCESSLIST statement.
mysql> SHOW PROCESSLIST;
38
PLSQL
39
Procedures
Functions
Database : EMP(empno :int, ename : varchar(20), job : varchar(20), mgr : int, hiredate : date,
Sal : decimal(10,2), comm : decimal (10,2), deptno int)
CASE x
WHEN 0 THEN SET result = 'Zero';
WHEN 1 THEN SET result = 'One';
WHEN 2 THEN SET result = 'Two';
WHEN 3 THEN SET result = 'Three';
WHEN 4 THEN SET result = 'Four';
WHEN 5 THEN SET result = 'Five';
WHEN 6 THEN SET result = 'Six';
WHEN 7 THEN SET result = 'Seven';
WHEN 8 THEN SET result = 'Eight';
WHEN 9 THEN SET result = 'Nine';
ELSE SET result = 'Not a digit';
END CASE;
END;
//
CALL digitName(0) //
CALL digitName(4) //
CALL digitName(100) //
+-------+------+
| Digit | Name |
+-------+------+
| 0 | Zero |
+-------+------+
1 row in set (0.00 sec)
+-------+------+
| Digit | Name |
+-------+------+
| 4 | Four |
+-------+------+
1 row in set (0.00 sec)
40
+-------+-------------+
| Digit | Name |
+-------+-------------+
| 100 | Not a digit |
+-------+-------------+
1 row in set (0.00 sec)
CALL mySign(2) //
CALL mySign(-5) //
CALL mySign(0) //
+--------+------+
| Number | Sign |
+--------+------+
| 2|+ |
+--------+------+
1 row in set (0.00 sec)
+--------+------+
| Number | Sign |
+--------+------+
| -5 | - |
+--------+------+
1 row in set (0.00 sec)
+--------+------+
| Number | Sign |
+--------+------+
| 0 | Zero |
+--------+------+
1 row in set (0.00 sec)
41
9.3. Procedure to compute factorial of given number
REPEAT
SET result = result * i;
SET i = i + 1;
UNTIL i > x
END REPEAT;
END;
//
CALL fact(1) //
CALL fact(2) //
CALL fact(4) //
CALL fact(0) //
+--------+-----------+
| Number | Factorial |
+--------+-----------+
| 1| 1|
+--------+-----------+
1 row in set (0.00 sec)
+--------+-----------+
| Number | Factorial |
+--------+-----------+
| 2| 2|
+--------+-----------+
1 row in set (0.00 sec)
+--------+-----------+
| Number | Factorial |
+--------+-----------+
| 4| 24 |
+--------+-----------+
1 row in set (0.00 sec)
+--------+-----------+
| Number | Factorial |
+--------+-----------+
| 0| 1|
+--------+-----------+
1 row in set (0.00 sec)
42
9.4. Function to compute simple interest
mysql> DELIMITER //
mysql> CREATE FUNCTION ptr( p float,t int,r float )
-> RETURNS float
-> BEGIN
-> DECLARE interest float;
-> SET interest =(p*t*r)/100;
-> RETURN interest ;
-> END; //
Query OK, 0 rows affected (0.01 sec)
mysql> DELIMITER ;
mysql> select ptr(4000,5,0.05);
+------------------+
| ptr(4000,5,0.05) |
+------------------+
| 10 |
+------------------+
1 row in set (0.00 sec)
mysql> DELIMITER //
mysql> CREATE FUNCTION reverseNumber( n int )
-> RETURNS int
-> BEGIN
-> DECLARE r int;
-> DECLARE s int;
-> SET s = 0;
-> WHILE n>0 DO
-> SET r =mod(n,10);
-> SET s =s*10+r;
-> SET n =truncate(n/10,0);
-> END WHILE ;
-> RETURN s;
-> END; //
Query OK, 0 rows affected (0.00 sec)
+--------------------+
| reverseNumber(123) |
+--------------------+
| 321 |
+--------------------+
1 row in set (0.00 sec)
43
9.6. Function to getname of employee of the given empno;
mysql> DELIMITER //
mysql> CREATE FUNCTION getName( eid int )
-> RETURNS varchar(20)
-> BEGIN
-> DECLARE name varchar(20);
-> select ename into name from emp where empno=eid;
-> RETURN name;
-> END; //
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> select getName(1);
+------------+
| getName(1) |
+------------+
| saketh |
+------------+
1 row in set (0.00 sec)
11. CURSORS
44
10.1. Find the names and their jobs who are working in the particular department.
mysql> DELIMITER //
mysql> CREATE PROCEDURE get_empdetails_using_cursor(
-> IN deptid INT
-> )
-> BEGIN
-> DECLARE record_not_found INTEGER DEFAULT 0;
-> DECLARE namee VARCHAR(20) DEFAULT "";
-> DECLARE jobe VARCHAR(10) DEFAULT "";
-> DECLARE c CURSOR FOR
-> SELECT ename,job FROM emp
-> WHERE deptno = deptid;
-> DECLARE CONTINUE HANDLER FOR NOT FOUND SET record_not_found = 1;
-> OPEN c;
-> edetails_loop: LOOP
-> FETCH c INTO namee,jobe;
-> IF record_not_found THEN
-> LEAVE edetails_loop;
-> END IF;
-> SELECT namee,jobe;
-> END LOOP edetails_loop;
-> CLOSE c;
-> END //
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> call get_empdetails_using_cursor(10);
+-------+---------+
| namee | jobe |
+-------+---------+
| CLARK | MANAGER |
+-------+---------+
1 row in set (0.01 sec)
+-------+-----------+
| namee | jobe |
+-------+-----------+
| KING | PRESIDENT |
+-------+-----------+
1 row in set (0.01 sec)
+--------+-------+
| namee | jobe |
+--------+-------+
| MILLER | CLERK |
+--------+-------+
1 row in set (0.01 sec)
+--------+-------+
| namee | jobe |
+--------+-------+
| MILLER | CLERK |
+--------+-------+
1 row in set (0.02 sec)
Query OK, 0 rows affected (0.02 sec)
12. TRIGGER
mysql> delimiter //
mysql> create trigger trig45
after insert on t4
for each row
begin
if new.a>25 then
insert into t5 values(new.b,new.a);
end if;
end;
//
Query OK, 0 rows affected (0.08 sec)
+------+------+
|a |b |
+------+------+
| 27 | x |
+------+------+
1 row in set (0.00 sec)
46
mysql> select * from t5;
+------+------+
|c |d |
+------+------+
| x | 27 |
+------+------+
1 row in set (0.00 sec)
mysql> create or replace trigger trg_emp_sal_check before update on t6 for each row
begin
if new.sal<=old.sal then
set new.sal=old.sal;
end if;
end
//
Query OK, 0 rows affected (0.08 sec)
47
Objective: To understand the syntax of TCL commands such as
Commit
Savepoint
Rollback
mysql> savepoint p;
Query OK, 0 rows affected (0.00 sec)
mysql> rollback to p;
Query OK, 0 rows affected (0.02 sec)
MysqlCon.java
48
18. java program to connect to mysql database and printing the details of all employees present in EMP table
import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Statement stmt=con.createStatement();
con.close();
}
}
49
Database Application
Tables used in this note: Sailors(sid: integer, sname: string, rating: integer, age: real);
Boats(bid: integer, bname: string, color: string);
Reserves(sid: integer, bid: integer, day: date).
51
mysql> insert into reserves (sid, bid, day)
+-----+-----------+-------+
| bid | bname | color |
+-----+-----------+-------+
| 101 | Interlake | blue |
| 102 | Interlake | red |
| 103 | Clipper | green |
| 104 | Marine | red |
+-----+-----------+-------+
4 rows in set (0.00 sec)
52
1. Display sailors name and age
mysql> SELECT sname, age FROM sailors;
+---------+------+
| sname | age |
+---------+------+
| Dustin | 45 |
| Brutus | 33 |
| Lubber | 55.5 |
| Andy | 25.5 |
| Rusty | 35 |
| Horatio | 35 |
| Zorba | 16 |
| Horatio | 35 |
| Art | 25.5 |
| Bob | 63.5 |
+---------+------+
10 rows in set (0.00 sec)
2. Find all information of sailors who have reserved boat number 101.
mysql> SELECT S.* FROM Sailors S, Reserves R WHERE S.sid = R.sid AND R.bid = 103;
+-----+---------+--------+------+
| sid | sname | rating | age |
+-----+---------+--------+------+
| 22 | Dustin | 7 | 45 |
| 31 | Lubber | 8 | 55.5 |
| 74 | Horatio | 9 | 35 |
+-----+---------+--------+------+
3 rows in set (0.03 sec)
3. Find the names of sailors who have reserved a red boat, and list in the order of age.
mysql> SELECT S.sname, S.age FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid AND R.bid = B.bid AND B.color ='red' ORDER BY S.age;
+---------+------+
| sname | age |
+---------+------+
| Horatio | 35 |
| Dustin | 45 |
| Dustin | 45 |
| Lubber | 55.5 |
| Lubber | 55.5 |
+---------+------+
5 rows in set (0.00 sec)
53
4. Find the names of sailors who have reserved at least one boat
mysql> SELECT sname FROM Sailors S, Reserves R WHERE S.sid = R.sid;
+---------+
| sname |
+---------+
| Dustin |
| Dustin |
| Dustin |
| Dustin |
| Lubber |
| Lubber |
| Lubber |
| Horatio |
| Horatio |
| Horatio |
+---------+
10 rows in set (0.00 sec)
5. Find the ids and names of sailors who have reserved two different boats on the same day.
mysql> SELECT DISTINCT S.sid, S.sname FROM Sailors S, Reserves R1, Reserves R2
WHERE S.sid = R1.sid AND S.sid = R2.sid AND R1.day = R2.day AND R1.bid <> R2.bid;
+-----+--------+
| sid | sname |
+-----+--------+
| 22 | Dustin |
+-----+--------+
1 row in set (0.01 sec)
Set operations
7. Find the ids of sailors who have reserved a red boat or a green boat.
(SELECT R.sid FROM Boats B, Reserves R WHERE R.bid = B.bid AND B.color = 'RED');
UNION
(SELECT R2.sid FROM Boats B2, Reserves R2 WHERE R2.bid = B2.bid AND B2.color = 'GREEN');
+-----+
| sid |
+-----+
| 22 |
| 31 |
| 64 |
| 74 |
+-----+
4 rows in set (0.01 sec)
54
8. Find the names of sailors who have reserved boat 103.
mysql> SELECT S.sname FROM Sailors S WHERE S.sid IN ( SELECT R.sid FROM Reserves R WHERE R.bid = 103 );
+---------+
| sname |
+---------+
| Dustin |
| Lubber |
| Horatio |
+---------+
3 rows in set (0.04 sec)
10. Find the names and ratings of sailor whose rating is better than some sailor called Horatio.
mysql> SELECT S.sname, S.rating FROM Sailors S
WHERE S.rating > ANY ( SELECT S2.rating FROM Sailors S2 WHERE S2.sname = 'Horatio');
+---------+--------+
| sname | rating |
+---------+--------+
| Lubber | 8|
| Andy | 8|
| Rusty | 10 |
| Zorba | 10 |
| Horatio | 9|
+---------+--------+
5 rows in set (0.00 sec)
11. Find the names of sailors who have reserved all boats.
mysql> SELECT S.sname FROM Sailors S
WHERE NOT EXISTS ( SELECT B.bid FROM Boats B
WHERE NOT EXISTS ( SELECT R.bid FROM Reserves R WHERE R.bid = B.bid AND R.sid = S.sid ) );
+--------+
| sname |
+--------+
| Dustin |
+--------+
1 row in set (0.00 sec)
Aggregate Functions
14: Find the name and the age of the youngest sailor
mysql> SELECT S.sname, S.age FROM Sailors S WHERE S.age = (SELECT MIN(S2.age) FROM Sailors S2 );
+-------+------+
| sname | age |
+-------+------+
| Zorba | 16 |
+-------+------+
1 row in set (0.00 sec)
15. Find the average age of sailors for each rating level.
mysql> SELECT S.rating, AVG(S.age) AS avg_age FROM Sailors S GROUP BY S.rating;
+--------+---------+
| rating | avg_age |
+--------+---------+
| 1| 33 |
| 3 | 44.5 |
| 7| 40 |
| 8 | 40.5 |
| 9| 35 |
| 10 | 25.5 |
+--------+---------+
6 rows in set (0.04 sec)
16. Find the average age of sailors for each rating level that has at least two sailors.
mysql> SELECT S.rating, AVG(S.age) AS avg_age FROM Sailors S GROUP BY S.rating HAVING COUNT(*) > 1;
+--------+---------+
| rating | avg_age |
+--------+---------+
| 3 | 44.5 |
| 7| 40 |
| 8 | 40.5 |
| 10 | 25.5 |
+--------+---------+
4 rows in set (0.00 sec)
56
17. An example shows difference between WHERE and HAVING
mysql> SELECT S.rating, AVG(S.age) as avg_age FROM Sailors S WHERE S.age >=40 GROUP BY S.rating;
+--------+---------+
| rating | avg_age |
+--------+---------+
| 3 | 63.5 |
| 7| 45 |
| 8 | 55.5 |
+--------+---------+
3 rows in set (0.00 sec)
mysql> SELECT S.rating, AVG(S.age) as avg_age FROM Sailors S GROUP BY S.rating HAVING AVG(S.age) >= 40;
+--------+---------+
| rating | avg_age |
+--------+---------+
| 3 | 44.5 |
| 7| 40 |
| 8 | 40.5 |
+--------+---------+
3 rows in set (0.00 sec)
Join
57