You are on page 1of 43

NETAJI SUBHAS UNIVERSITY OF

TECHNOLOGY

BDFT. SEM-4
DATABSE MANAGEMENT
SYSTEM
LAB MANUAL

SUBMITTED TO- MS.


BUSHRA JAMAL
SUBMISSON FROM-
ELIE JAIN
(2020UFT9311)
S. No. Aim

1. Introduction to SQL
2. Basic database related operations on MYSQL
3. Design a garments store database with required tables and
constraints

4. Write SQL statement for implementing ALTER, DROP


5. Write SQL queries to populate tables of garments store database
and display the records

6. Write SQL query to create copy of a table (along with data), and
drop that copy

7. Write SQL query to implement all aggregate functions: MAX(),


MIN(), AVG(), COUNT(), SUM()

8. Create employee and department table and provide queries to


perform all basic DBMS operations as given in the task list
(16different queries in total).

9. Write SQL query to display the result of UNION operator


10. Write SQL query to create a view of employee table
11. Apply auto increment field on any table

INDEX

INTRODUCTION TO SQL
QUES. Introduction to SQL
a. About DDL,DML,DCL,TCL
b. SQL data types
c. SQL operators

DDL- Data Definition Language. DDL is used for specifying the database schema. It is
used for creating tables, schema, indexes, constraints etc. in database.

 To create the database instance – CREATE


 To alter the structure of database – ALTER
 To drop database instances – DROP
 To delete tables in a database instance – TRUNCATE
 To rename database instances – RENAME
 To drop objects from database such as tables – DROP
 To Comment – Comment

DML- Data Manipulation Language. DML is used for accessing and manipulating
data in a database.

 To read records from table(s) – SELECT


 To insert record(s) into the table(s) – INSERT
 Update the data in table(s) – UPDATE
 Delete all the records from the table – DELETE

DCL- Data Control language. DCL is used for granting and revoking user access on
a database.

 To grant access to user – GRANT


 To revoke access from user – REVOKE

TCL- Transaction Control Language. The changes in the database that we make
using DML commands are either performed or rollbacked using TCL.

 To persist the changes made by DML commands in database –


COMMIT
 To rollback the changes made to the database – ROLLBACK
SQL DATA TYPES- The data type of a column defines what value the column
can hold: integer, character, money, date and time, binary, and so on.

DATA TYPE DESCRIPTION

char A FIXED length string (can contain


letters, numbers, and special
characters). The size parameter
specifies the column length in
characters - can be from 0 to 255.
Default is 1
varchar A VARIABLE length string (can contain
letters, numbers, and special
characters). The size parameter
specifies the maximum column length
in characters - can be from 0 to 65535
binary Equal to CHAR(), but stores binary byte
strings. The size parameter specifies
the column length in bytes. Default is 1
varbinary Equal to VARCHAR(), but stores binary
byte strings. The size parameter
specifies the maximum column length
in bytes.
bit (size) A bit-value type. The number of bits
per value is specified in size.
The size parameter can hold a value
from 1 to 64. The default value
for size is 1.
tinyit (size) A very small integer. Signed range is
from -128 to 127. Unsigned range is
from 0 to 255. The size parameter
specifies the maximum display width
(which is 255)
bool Zero is considered as false, nonzero
values are considered as true.
boolean Equal to BOOL
smallint (size) A small integer. Signed range is from -
32768 to 32767. Unsigned range is
from 0 to 65535. The size parameter
specifies the maximum display width
(which is 255)
mediumint (size) A medium integer. Signed range is
from -8388608 to 8388607. Unsigned
range is from 0 to 16777215.
The size parameter specifies the
maximum display width (which is 255)
Int (size) A medium integer. Signed range is
from -2147483648 to 2147483647.
Unsigned range is from 0 to
4294967295. The size parameter
specifies the maximum display width
(which is 255)
integer (size) Equal to INT(size)
bigint (size) A large integer. Signed range is from -
9223372036854775808 to
9223372036854775807. Unsigned
range is from 0 to
18446744073709551615.
The size parameter specifies the
maximum display width (which is 255)
Float (size,d) A floating point number. The total
number of digits is specified in size.
The number of digits after the decimal
point is specified in the d parameter.
This syntax is deprecated in MySQL
8.0.17, and it will be removed in future
MySQL versions
decimal (size,d) An exact fixed-point number. The total
number of digits is specified in size.
The number of digits after the decimal
point is specified in the d parameter.
The maximum number for size is 65.
The maximum number for d is 30. The
default value for size is 10. The default
value for d is 0.
dec (size,d) Equal to DECIMAL(size,d)
date A date. Format: YYYY-MM-DD. The
supported range is from '1000-01-01'
to '9999-12-31'
datetime A date and time combination. Format:
YYYY-MM-DD hh:mm:ss. The
supported range is from '1000-01-01
00:00:00' to '9999-12-31 23:59:59'.
Adding DEFAULT and ON UPDATE in
the column definition to get automatic
initialization and updating to the
current date and time
timestamp A timestamp. TIMESTAMP values are
stored as the number of seconds since
the Unix epoch ('1970-01-01 00:00:00'
UTC). Format: YYYY-MM-DD hh:mm:ss.
time A time. Format: hh:mm:ss. The
supported range is from '-838:59:59'
to '838:59:59'
year A year in four-digit format. Values
allowed in four-digit format: 1901 to
2155, and 0000.
MySQL 8.0 does not support year in
two-digit format.

SQL OPERATORS-

ARITHMETIC OPEARTORS-

OPERATOR DESCRIPTION

+ Add

_ Subtract

* Multiply

/ Divide

% Modulo

BITWISE OPERATORS-
OPERATOR DESCRIPTION

& Bitwise AND

| Bitwise OR

^ Bitwise Exclusive OR

COMPARISION OPERATORS-

OPERATOR DESCRIPTION

= Equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

<> Not equal to

COMPOUND OPERATORS-

OPERATOR DESCRIPTION

+= Add equals

-= Subtract equals

*= Multiply equals

/= Divide equals

%= Modulo equals

&= Bitwise AND equals

^-= Bitwise exclusive equals


|*= Bitwise OR equals

LOGICAL OPERATORS-

OPERATOR DESCRIPTION

ALL TRUE if all of the subquery values


meet the condition
AND TRUE if all the conditions separated
by AND is TRUE
ANY TRUE if any of the subquery values
meet the condition
BETWEEN TRUE if the operand is within the
range of comparisons
EXISTS TRUE if the subquery returns one or
more records
IN TRUE if the operand is equal to one
of a list of expressions
LIKE TRUE if the operand matches a
pattern
NOT Displays a record if the condition(s)
is NOT TRUE
OR TRUE if any of the conditions
separated by OR is TRUE
SOME TRUE if any of the subquery values
meet the condition

BASIC DATABASE RELATED


OPERATIONS

QUES. Write queries in MySQL to-


a. Create database garment_store in Mysql.
b. Show currently selected database
c. Switch from default database to my database(garment_store)
d. List all the tables in a database
e. Show the schema of a table

Create database Garment_Store;


Use Garment_Store;
Create table Product(
Product_Id varchar(50),
PName varchar(60),
Brand varchar(100),
Season varchar(30),
Price int);
show tables;
desc Product;
GARMENT STORE TABLES

QUES.
Design the following tables in garment_store database along with constraints like
Primary Key, Foreign key, NOT NULL, and check constraint to the tables.
- product(product_id, Pname, brand, season, price)
- purchase(purchase_id, item_id, no_of_items, amount, purchase_date)
- stock(item_id, instock, status)
- sales(sale_id, item_id, no_of_items_sold, sale_rate, amount, date_of_sale)

CREATE TABLE Products (


Product_id varchar (10) PRIMARY KEY,
Pname varchar (50) NOT NULL,
brand varchar(50),
season varchar(20),
price int
);

CREATE TABLE Purchase (


Purchase_id varchar (10) NOT NULL,
item_id varchar (10) PRIMARY KEY,
no_of_items int,
amount int,
purchase_date date
);
CREATE TABLE stock (
item_id varchar (10) REFERENCES Purchase(item_id),
instock varchar (3) CHECK (instock = 'yes'),
status varchar (20)
);

CREATE TABLE sales (


sale_id varchar (10) PRIMARY KEY,
item_id varchar (10) REFERENCES Purchase(item_id),
no_of_items_sold int,
sale_rate int ,
sales_amount int,
date_of_sale date
);
IMPLEMENTATION OF ALTER, DROP

QUES
Write SQL statement for implementing ALTER, DROP at column level in Q3 tables.
a. ADD (e.g.: add a column “status” in stock table(if not present))
b. MODIFY
c. RENAME
d. DROP

a. Products table before add command is performed:

Products table after add command is performed:


ALTER TABLE Products
ADD p_color varchar (30);
b. sales table before modify command:

Sales table after modify command is performed:


ALTER TABLE sales
MODIFY COLUMN no_of_items_sold varchar(20);
c. Before rename command is performed:

After rename command is performed:


ALTER TABLE Purchase
RENAME COLUMN amount to purchase_amt;
d. Before applying drop command:
After applying drop command:
ALTER TABLE sales
DROP COLUMN sale_rate;
POPULATING TABLES

QUES
Perform following operations on tables in Q3
a. populate with few tuples as per specified constraints, and
b. display all tuples(data)

INSERT INTO Products (Product_id, Pname , brand, season, price, p_color)


VALUES ('PD1', 'Tshirt', 'Levis', 'S1', 799, 'black');

INSERT INTO Products (Product_id, Pname , brand, season, price, p_color)


VALUES ('PD2', 'Polo', 'Gant', 'S1', 1299, 'red');

INSERT INTO Purchase ( Purchase_id, item_id, no_of_items,


purchase_amt ,purchase_date)
VALUES ('PR1', 'ID1', 2, 2000, ‘28/MAR/22’);

INSERT INTO Purchase ( Purchase_id, item_id, no_of_items,


purchase_amt ,purchase_date)
VALUES ('PR2', 'ID2', 5, 4000, ‘25/MAR/22’);
INSERT INTO stock ( item_id, instock, status)
VALUES ('ID1', 'yes', 'available');
INSERT INTO stock ( item_id, instock, status)
VALUES ('ID2', 'yes', 'un-available');

INSERT INTO sales ( sale_id, item_id, no_of_items_sold, sales_amount,


date_of_sale)
VALUES ('S1', 'ID1', 200, 12000, '01/APR/22');

INSERT INTO sales ( sale_id, item_id, no_of_items_sold, sales_amount,


date_of_sale)
VALUES ('S2', 'ID2', 500, 15000, '03/APR/22');
b. SELECT * FROM Products;
SELECT * FROM Purchase;
SELECT * FROM stock;
SELECT * FROM sales;
CREATING COPY OF A TABLE

QUES
Write SQL query to
a. Create a copy of “product” table(along with data), and
b. Drop that copy

CREATE TABLE products_copy


AS (SELECT * FROM Products);

DROP TABLE products_copy;


AGGREGATE FUNCTIONS

QUES
Write SQL query to implement all aggregate functions: MAX(), MIN(), AVG(),
COUNT(), SUM() (eg: on price column of product table)

MIN-
SELECT MIN(Price) AS SmallestPrice
FROM Products;

MAX-
SELECT MAX(sales_amount) AS HighestPrice
FROM sales;

COUNT-
SELECT COUNT(Product_id)
FROM Products;

AVG-
SELECT AVG(price)
FROM Products;
SUM-
SELECT SUM(no_of_items)
FROM Purchase;
BASIC DBMS OPERATIONS

QUES. Perform the following operations on emp and dept tables.

Employee table- emp:


Department table- dept:

1.Change the name of employee with empno=7876.


UPDATE emp
SET ename = 'Ron'
WHERE empno = 7876;

2. Display the record of an employee with maximum salary.


SELECT * FROM emp WHERE sal=(select Max(sal) from emp)

3. Count total no of records from employee table.


SELECT COUNT(empno)
FROM emp;

4. Select all the records of an employee whose department location is “chicago”.


SELECT * FROM emp
WHERE deptno =
(SELECT deptno
FROM dept
WHERE LOC ='CHICAGO');
5. Write a query to get the records from emp table where deptno is 10 and job is
“president”.
SELECT * FROM emp
WHERE deptno= 10
AND job= 'PRESIDENT';

6. Write a query to get the records from emp table where deptno is 10 or salary is
greater than 2000.
SELECT * FROM emp
WHERE deptno= 10 OR sal>2000;
7. Write SQL query to select only distinct deptno from emp table.
SELECT DISTINCT deptno FROM emp;

8. Write SQL query to display the Ename starting with s,a or m in table employee.
SELECT * FROM emp
WHERE ename LIKE 'S%' OR ename LIKE 'A%' OR ename LIKE 'M%';
9. Write SQL query to display name of employees in descending order.
SELECT ename FROM emp
ORDER BY ename DESC;

10. Write SQL query to change the name of smith to “johnson” in employee table.
UPDATE emp
SET ename = 'Johnson'
WHERE ename = 'SMITH';
SELECT * FROM emp;
11. Write SQL query to display only top 5 rows from employee table.
SELECT * FROM emp
FETCH FIRST 5 ROWS ONLY;

12. Write SQL query to select all the employees name whose deptno is 10 and
20(using IN operator).
SELECT ename FROM emp
WHERE deptno IN (10, 20);
13. Write SQL query to select the records from emp table whose salary is in the
range of 1500 to 3000(using BETWEEN operator).
SELECT * FROM emp
WHERE sal BETWEEN 1500 AND 3000;
14. Delete the record of an employee from emp table whose name is ‘Ford’.

SELECT * FROM emp;


DELETE FROM emp WHERE ename='FORD';
SELECT * FROM emp;

Before deletion-

After deletion-
15. Apply all 4 joins on employee table and dept table(inner, left, right, outer) and
show the resultant tables.
emp table-
dept table-

INNER JOIN:
SELECT emp.ename, emp.empno, emp.job, emp.sal , dept.dname
FROM emp
INNER JOIN dept ON emp.deptno = dept.deptno;
LEFT JOIN:
SELECT emp.ename, emp.empno, emp.job, emp.sal , dept.dname
FROM emp
LEFT JOIN dept ON emp.deptno = dept.deptno;

RIGHT JOIN:
SELECT emp.ename, emp.empno, emp.job, emp.sal , dept.dname
FROM emp
RIGHT JOIN dept ON emp.deptno = dept.deptno;
FULL OUTER JOIN:
SELECT emp.ename, emp.empno, emp.job, emp.sal , dept.dname
FROM emp
FULL OUTER JOIN dept ON emp.deptno = dept.deptno;
16. Write SQL query to list the no of employees in each department(use Group
By)
SELECT COUNT(empno), deptno
FROM emp
GROUP BY deptno;
UNION OPERATOR

Write SQL query to display the result of UNION operator


Tables created-
CREATE TABLE Customers (
cid int PRIMARY KEY,
cname varchar (50),
city varchar (50),
contact_no int);

CREATE TABLE Suppliers(


sup_id int PRIMARY KEY,
sname varchar (50),
city varchar (50),
contact_no int);

INSERT INTO Customers ( cid, cname, city, contact_no)


VALUES (1,'Mary', 'Berlin', 9986536728 );

INSERT INTO Customers ( cid, cname, city, contact_no)


VALUES (2,'Sam', 'Sydney' , 7984539728 );

INSERT INTO Suppliers ( sup_id, sname, city, contact_no)


VALUES (1,'ITC', 'Delhi' , 7984444528 );

INSERT INTO Suppliers ( sup_id, sname, city, contact_no)


VALUES (2,'ABC', 'California' , 8922444528 );

SELECT * FROM Suppliers;


SELECT * FROM Customers;

Applying Union command-


SELECT city FROM Customers
UNION

SELECT city FROM Suppliers ;


VIEW OF TABLE

QUES- Write SQL query to create a view of employee table.


(from emp table)

CREATE VIEW manager_employees AS


SELECT empno, ename, job, deptno
FROM emp
WHERE job = 'MANAGER';
SELECT * FROM manager_employees;
AUTO-INCREMENT

QUES. Apply auto increment field on any table.

Create table Student(


Roll_No int NOT NULL AUTO_INCREMENT,
Student_Name varchar(50) NOT NULL,
Student_Age int,
PRIMARY KEY (Roll_No)
);
Insert into Student(Student_Name, Student_Age)
Values('Riya', 19);
Insert into Student(Student_Name, Student_Age)
Values('Sam', 22);
Select * from Student;

You might also like