DBMS Report Dibya Deep Acharya
DBMS Report Dibya Deep Acharya
“DBMS”
Submitted by:
Dibya Deep Acharya
TU Registration no: 7-2-22-184-2019
Submitted to:
Dhadi Ram Ghimire
Tribhuvan University
Patan Multiple Campus
Year: 2019
1
Table of Contents
Acknowledgement…………………………………………………………………………………………………………………….. 2
Showing all the available databases in MySQL................................................................................3
SELECTing a database in MySQL......................................................................................................3
Showing all tables belonging to a Database....................................................................................3
DDL CREATE Command to create tables in MySQL..........................................................................4
DDL ALTER Command to Add CONSTRAINT FOREIGN KEY...............................................................4
DDL ALTER Command to Add, Delete and modify Column...............................................................4
DDL ALTER Command to SET DEFAULT VALUES...............................................................................5
MySQL CHECK CONSTRAINT...........................................................................................................5
Displaying the structure of Tables using DESC Commands...............................................................5
DML Command INSERT to INSERT data into tables..........................................................................5
Inserting multiple rows at the same time........................................................................................6
Selecting all the data from the table at once...................................................................................6
SELECT Query Example 1 (Supplier, parts, supplies)........................................................................7
SELECT Query Example 2 (Employee, Company, Works, Supervisor)..............................................10
Practical Examples of all the Sql syntax.……………………………………………………………………………….13
2
Acknowledgement
I would like to express my sincerest gratitude to Mr. Dhadi Ram Ghimire for
allowing me to have this opportunity to finish such a wonderful project report
on Sql and I would like to thank him for his guidance on all the things he taught
me on Sql, Databases, Servers, and Database management system as a whole.
His suggestions and guidance have served a major role in finishing this project.
This research boarded my knowledge in the study of DBMS and has served as a
great tool in refining my Sql skills not just theoretically but practically as well.
3
Showing all the available databases in MySQL
Database changed
4
DDL CREATE Command to create tables in MySQL
Supplier (supplier-id , supplier-name, city)
Supplies (supplier-id, part-id, quantity)
Parts (part-id, par-name, color, weight)
mysql> create table supplier(sid int NOT NULL primary key,sname varchar(20),city
varchar(25));
mysql> create table parts(pid int NOT NULL Primary key,pname varchar(20),color
varchar(10),wt int);
mysql> alter table supplies add CONSTRAINT fk2 FOREIGN KEY(sid) references
supplier(sid) on DELETE CASCADE;
5
DDL ALTER Command to SET DEFAULT VALUES
mysql> alter table supplies alter column quantity SET DEFAULT 1;
6
Query OK, 1 row affected (0.03 sec)
7
SELECT Query Example 1 (Supplier, parts, supplies)
8
(c) Find the name of all parts that are supplied in quantity greater than 75.
SELECT DISTINCT(pname) FROM parts p, supplies s
WHERE p.pid=s.pid and s.quantity>75;
e) Find the name of all suppliers who supply more than 5 parts.
SELECT sname,nos FROM(SELECT s.sid,sp.sname,COUNT(p.pid) as nos
FROM supplies s JOIN parts p on
(p.pid=s.pid) JOIN supplier sp on(sp.sid=s.sid) GROUP BY s.sid) as T
WHERE nos>5
9
Sample data from table
10
SELECT Query Example 2 (Employee, Company, Works, Supervisor)
emp (ss, name)
cmp (cname, address)
works (ss, cname)
super (s_ss, e_ss)
Write relational algebra and SQL queries for each of the following cases:
a) Find the names of all supervisors that work in companies whose address equals “Cuperito‟.
SELECT DISTINCT(e.name) FROM emp e, super s, works w, cmp c
WHERE c.address="Cuperito" AND e.ss=s.s_ss AND s.s_ss=w.ss AND w.cname=c.cname;
b) Find the name of all the companies who have more than 1 supervisors.
SELECT cname, COUNT(cname) as NOS
FROM (SELECT c.cname, COUNT(s.s_ss) AS nos FROM cmp c, super s, works w
WHERE c.cname=w.cname AND w.ss=s.s_ss GROUP BY s.s_ss) as T
GROUP BY cname
HAVING NOS>1;
c) Find the name of supervisor who has the largest number of employees.
SELECT name, MAX(noe)
FROM (SELECT e.name, s_ss, COUNT(s.e_ss) AS noe FROM emp e, super s
WHERE e.ss=s.s_ss GROUP BY s_ss) as T;
11
Works Table
Cmp Table
1. DDL.
DDL, commonly known as data definition language, is a type of
language which helps to create the structure of the database and
restructure of database objects.
12
Use the created database and create table using every
constraints.
2. DML.
DML, commonly known as data manipulation language, is a part of sql
that allows manipulation of data within object of a relational object.
13
Update (any 3 example).
Example no 1:
Example no 2:
Example no 3:
14
Example no 1:
Example no 2:
Example no 3:
3. DQL.
DQL, commonly known as data query language, is a part of sql that
allows the user to display the particular data. Basically, it is use for
smart search of required data.
The prominent DQL statement is SELECT.
15
Use of AND, OR, NOT operator.
16
Example of NOT operator along with OR:
Question: Display pid, pname, pbed whose fee is not 5000 and
gender is also not male.
17
Use of group by, having clause (any 2 example).
Question: Display gender and total fee and grouping them into
gender.
Question: Display gender and total fee and grouping them into
gender having total fee greater than 12000.
Example no 1:
Example no 2:
18
Example no 3:
Example no 4:
Question: Find pid, pname, fee in which pname has “I” at its
second place.
Example no 5:
Question: Find all records of patient whose name starts with “r”
and ends with “m”.
19
Example no 1: Sum function.
Example no 4:
Example no 5:
20
21