DBMS Lab.
Assignment - I
1. Write the general syntax and also give the examples for
the following:
a. CREATE command
b. INSERT command
c. UPDATE command
d. SELECT command
2. Write the syntax for the ALTER TABLE command to
add and remove the columns to the table.
3. Write the syntax for applying the constraints at the time
of creation of a table at the table level as well as column
level. The constraints should be specified by giving
suitable names.
4. Write the syntax for adding and removing the
constraints after creation of table.
5. Consider the following Employee table:
{empID, empName, empCity, empAge, empSalary,
empDesg, empDept}
1
data types: empIDvarchar(3),
empNamevarchar(20), empCityvarchar(10),
empSalary numeric(10,2), empDesgvarchar(15),
EmpDeptvarchar(3)
<employee> Table
emp empN emp emp empSa empDe empD
ID ame City Age lary sg ept
E1 Ram Jaipu 47 15000 Manage D1
r r
E2 Shyam Kota 42 18000 DBA D2
E3 Geeta Ajme 22 16000 Progra D1
r mmer
-- -- -- -- -- -- --
-- -- -- -- -- -- --
-- -- -- -- -- -- --
Write the following queries for the above table:
(i) Create the above table with the following
constraints:
2
a. EmpName can’t be null.
b. EmpSalary must not be less than 8000.
c. The default value of EmpCity is ‘Jaipur’
(ii) Insert the records of employees.
(iii) Insert the records of employees in a customized
manner.
(iv) Display the records of all employees living in
city ‘Jaipur’.
(v) Display the designations and IDs of employees
having department ‘D1’.
(vi) Display the ID’s and Names of all employees
having salary between 20000 and 30000 using
BETWEEN…AND OPERATOR.
(vii) Display the IDs, Names, and Salary of all
employees living in ‘Jaipur’, or ‘Kota’, or ‘Ajmer’
using IN operator.
(viii) Display the records of all employees having
designation ‘Manager’ or ‘Clerk’ and age is
between 30 to 40.
(ix) Display the all unique cities in table.
3
(x) Display the records of all employees having city
value as null.
(xi) Count the total different values of city.
(xii) Update the salary of all employees having
designation ‘Manager’ or ‘Clerk’ by 10%.
(xiii) Display the records of all employees in a sorted
manner using EmpName as ascending, and
EmpCity as descending.
(xiv) Display the maximum and minimum salary in
the table.
(xv) Display the sum and average of the salaries of
employees having the designation as ‘Manager’.
(xvi) Display the IDs, Salaries, and Names of all
employees, whose names starts with ‘aj’.
(xvii) Display the IDs, Salaries, and Names of all
employees, whose names are 4 characters long and
starts with starts with ‘a’, and ends with ‘t’.
(xviii) Display the IDs and Names of all
employees, whose names are at least 4 characters
4
long, and 3 and 4 characters are ‘i’ and ‘t’
respectively.
(xix) Display the list of all constraints applied on
Employee table.
(xx) Display the structure of table.
6. Consider the following database schema:
employee {empID, empName, empCity, empDOB,
empSalary, empDesg, empDeptID}
department {deptID, deptName, deptBuilding}
data types: empID char(3), empNamevarchar(20),
empCityvarchar(10), empDOB date, empSalary
numeric(10,2), empDesgvarchar(15)
deptIDvarchar(3), deptNamevarchar(10),
deptBuildingvarchar(15)
Sample Entries of the tables:
<employee> Table
em empN emp emp empS empDe empDe
pID ame City DOB alary sg ptID
5
E1 Ram Jaipu 10- 15000 Manag D1
r Mar- er
1985
E2 Shya Kota 12- 18000 DBA D2
m Apr-
1990
E3 Geeta Ajme 10- 16000 Progra D1
r Dec- mmer
2001
-- -- -- -- -- -- --
-- -- -- -- -- -- --
-- -- -- -- -- -- --
<department> Table
deptID deptName deptBuilding
D1 Accounts Vidhya Mandir
D2 HR Vidhya Mandir
D3 Education Surya Mandir
-- -- --
-- -- --
-- -- --
6
(a) Create the above database schema with the
following constraints:
i. empName can’t be null.
ii. deptName must be unique.
iii. empDeptIDis the foreign key and references the
deptID of the department table.
(b) Insert the records in the tables.
7. Consider the following database schema:
suppliers {suppID, suppName, suppStatus, suppCity}
items {itemID, itemName, itemPrice}
shipments {suppID, itemID, quantity}
data types: suppID char(3), suppNamevarchar(30),
suppStatus integer,suppCityvarchar(15)
itemIDvarchar(3), itemNamevarchar(15),
itemPricenumeric(10,2)
quantity integer
Sample Entries of the tables:
<suppliers> Table
7
suppID suppName suppStatus suppCity
S1 Amul 20 Jaipur
S2 Britania 10 Kota
S3 Mother 15 Ajmer
Dairy
-- -- -- --
-- -- -- --
-- -- -- --
-- -- -- --
-- -- -- --
<items> Table
itemID itemName itemPrice
I1 Cookies 35.50
I2 Bread 50.00
I3 Butter 75.25
-- -- --
-- -- --
-- -- --
-- -- --
<shipments> Table
8
suppID itemID quantity
S1 I1 50
S2 I2 70
S3 I3 30
-- -- --
-- -- --
-- -- --
(a) Create the above database schema with the
following constraints:
i. suppName can’t be null.
ii. itemNamecan’t be null and must be unique.
iii. suppID and itemID are the two foreign keys in
shipments table.
iv. suppID and itemID can’t be null.
v. Quantity can’t be 0 or negative
8. (a) Create a relational database that contains the
following tables and insert the following data into these
tables.
9
stud_member {rollNo, firstName, middleName,
surName}
department {deptID, deptName }
data types: rollNointeger, firstNamevarchar(15),
middleNamevarchar(15), surNamevarchar(15) ,
deptIDinteger, semester integer, contactNovarchar(10),
gender char(1)
deptIDinteger, deptName varchar(15)
<stud_member> Table
rollNofirstNammiddleNamsurNam depIDsemest contact gender
e e e er No
1 Ankur Samir Kahar 1 1 272121 M
2 Dhaval Dhiren Joshi 1 1 232122 M
3 Ankita Biren Shah 1 1 112121 F
10 Komal MaheshkumPandya 2 3 123123 F
ar
13 Amit Jitenkumar Mehta 3 3 453667 M
23 Jinal Ashish Gandhi 2 1 323232 M
22 Ganesh Asha Patel 2 3 124244 M
4 Shweta Mihir Patel 3 1 646342 F
7 Pooja Mayank Desai 3 3 328656 F
8 Komal Krishnaraj Bhatia 2 3 257422 F
43 Kiran Viraj Shah 1 1 754124 F
10
<department> Table
deptID deptName
1 Information
Technology
2 Electrical
3 Civil
4 Mechanical
5 Chemical
(b) Now, solve the following SQL Queries.
(i) Display the names and
contact numbers of all student members.
(ii) Give the names and roll
numbers of all students of Information Technology who
are members.
(iii) Display names of
Departments whose students are members.
(iv) Display names of
Departments for which no students are members.
(v) Display names of all
Departments.
(vi) Find the number of students
of Electrical Department who are members.
(vii)Display information of student members whose name
begins with the letter “A”.
(viii) Display all details of Male members only.
(ix) Display data of student
members who are currently in semester “3”.
(x) Display data of student
female members in alphabetical order.
11
9. Create the following sales table
sales {orderID, orderDate, orderPrice, orderQty,
customerName}
data types: orderIDinteger, orderDatedate,
orderPrice numeric(10,5), orderQty integer,
customerNamevarchar(20)
<sales> Table
orderIDorderDatorderPri orderQtycustomerN
e ce ame
1 12/22/20 160 2 Smith
05
2 08/10/20 190 2 Johnson
05
3 07/13/20 500 5 Baldwin
05
4 07/15/20 420 2 Smith
05
5 12/22/20 1000 4 Wood
05
6 10/2/200 820 4 Smith
5
7 11/03/20 2000 2 Baldwin
05
12
Solve following queries using Aggregate Function for
above table:
(i) Count how many orders have made a customer with
customerName of Smith.
(ii) Find number of unique customers that have ordered
from the store.
(iii) Find out total no. of items ordered by all the
customers.
(iv) Find out average number of items per order.
(v) Find out the average orderQty for all orders with
orderPrice greater than 200
(vi) Find out what was the minimum price paid for any of
the orders.
(vii)Find out the highest orderPrice from the given sales
table
(viii) List out unique customers‟ name only from the
table.
(ix) List out name of the customers who have given order
in the month of DECEMBER
(x) Find out the total amount of money spent for each of
the customers.
(xi) Select all unique customers, who have spent more
than 1200 in the store.
(xii)Select all customers that have ordered more than 5
items in total from all their orders.
13
(xiii) Select all customers who have spent more than
1000, after 10/01/2005.
(xiv) Select orders in increasing order of order price.
(xv) Select orders in decreasing order of order price.
14