DBMS Lab SCMS
DBMS Lab SCMS
EXERCISE 1
Aim : To understand about DDL Commands
1. Create a database called “SUPERMARKET”
2. Create a table ‘Product’ with the following fields in the above database.
Col Name DataType Width
PtdId Integer 11
PdtCode Char 3
Name Varchar 20
Quantity Integer 11
Price Decimal (7,2)
11. Select name and price of the product where price is less than 10
12. Select name and quantity of those product whose quantity is greater than or
equal to 10
13. Select name and price of product where product code is Pen
Exercise 2
Aim : To understand about ALTER, UPDATE & RENAME Commands
1. Create the following table from the following description of the CDS table
Field Type Null Key Default Extra
CdId Int(3) Pk Null auto_increment
Artist Varchar(20) yes Null
Title Varchar(20) Yes Null
Year Int(4) Yes Null
Label Varchar(20) Yes Null
Bought date Yes Null
9. Write a SQL statement to add a column state after Address column to the table.
10. Write a SQL statement to change the name of the column state to
state_province, keeping the data type and size same.
Exercise 3
Aim : To understand about foreign key constraint
1.Create a table “SPECIES” with the following fields: ID Integer Primary Key,
Name Varchar 10.
3. Create anothet table “ZOO” with the fields: SerialNo Integer PK, F_Id
Integer FK, Name Varchar 23.
Exercise 4
Aim : FK Constarints defined with a DELETE Cascade.
1. Create a table EmployeeMaster with the fields –EmpId as its PK, EmpName,
EmpDesgn, ManagerId.
5. Delete the records in the table TransMaster with Custid as ‘C01’(c zero one)
EXERCISE 4
Aim : Check, Unique, Default and NotNull constraints
Note : CHECK constraint will not working on Mysql 5.5.
1. To set a unique constraint at the column level for the name field in the
“CategoryMaster” table: Category_id varchar,name varchar
2. To set a unique constraint at the table level for the EmployeeMaster table :
EmpId varchar, name varchar,design varchar,mangrId varchar. Name is unique
3. (To set a default value for the column PaymentTerms in the CompanyMaster
table : CompnyId varchar,name varchar,address varchar,contactPerson
varchar,PaymentTerms varchar default value is 100% advanced payment.
4. To set a CHECK Constarint on the Pid column and NotNull constraint for the
fields Pid and Lastname of the Person Table. The check constraint specifies
that the column Pid must only include integer greater than 0.
EXERCISE 5
Aim : Creating an Index for a table
Note: Index is used to reduce the execution time and maximize the processing
speed.
Here since we are dealing with very small table,the execution time wont show
much changes even before and after the index creation.
EXERCISE 6
Aim : Familiarizing SELECT query,LIKE operator,NOT LIKE
1. Create a table empdetails :eid varchar pk,ename varchar,edesgn varchar,esal
int, yearofJoining int
Query : CREATE TABLE EMPDETAILS(EID VARCHAR(5) PRIMARY
KEY, ENAME VARCHAR(20), EDESGN VARCHAR(20), ESAL INT, YOJ
INT);
6. Select specified column and rows like ename and edesgn where salary less than
25000.
7. Get the edesgn of all the employees who are not ‘Salesman’(symbol is <>)
8. Get the details of the employee whose edesgn starts with S.(hint s%)
9. Get the details of the employee whose name’s 3rd letter is ‘a’.(hint __a%)
10. Get the names of employee whose name has a letter ‘a’(hint %a%)
11. Get the names of employee whose name does not contain a letter ‘a’(hint NOT
LIKE ‘%a%’)
12. Get the names of employee whose name has first letter as either ‘a’ or “p” or
“t”(hint ‘[apt]%’ or use logical operators)
13. Get the employee names where their joining year us 2000,2015,2018 (use IN
operator).
14. Get the employee names whose salary in between 25000 and 50000
15. Get the names of employee whose joining year is after 2010 or eid is EMP001
or EMP005. (use IN operator).
16. Get the names of employee whose joining year is after 2017 and eid is E02 or
E01(use IN operator).
18. Sort the ename and edesgn in the descending order of joining year.
EXERCISE 7
Aim :Creation of table from another table
1. Create a table PRODUCT with the fields pid,pname,cost and insert 5 row values
into table
Query : CREATE TABLE PRODUCT(PID INT,PNAME
VARCHAR(20),COST DECIMAL(7,2));
2. Create a table PdtMaster from Product table and insert some values into new
table
3. Describe PdtMaster
EXERCISE 8
Aim :Performing Calculations
1. Select all columns of PdtMaster where PdtCost is increased by 10% as
rename the column name with an alias title “Increase in Pdt Cost” and then
do another calculation on the same column say PdtCose*1.10 as “NewCost”
EXERCISE 9
Aim : Familiarizing SET Operators
1. Create a table called Author with the following fields : AuthorId, AuthorName,
AuthorState,AuthorCountry,AuthorContact
2. Create another table called Publisher with the following fields : PublisherId,
PublisherName,PublisherState,PublisherCountry,PublisherContact.
4. Get the details of all authors and publisher who are from India.
EXERCISE 10
Aim : Familiarizing Aggregate functions.
1. CREATE TABLE Publishers with the fields : PubID char (4) NOT NULL
,PubName varchar (40), city varchar (20), PubAmount decimal,country varchar
(30) DEFAULT ‘Canada’,BasicPay Decimal, HRA Decimal,Tax Decimal,TA
Decimal.
Query : select count(*) as " No of Indian Publishers " from publishers where
country='india';
9. Calculate the total pay of publisher under the column name Salary(hint :
SUM(basic+ta+hra-tax)
10. Get the publisherId,average of basic,max of basic,min of basic from the table.
EXERCISE 11
Aim : Familiarizing Numeric Functions
1. ADD:
2. Subtract :
3. Product :
4. Division :
Query : SELECT 10/2 AS “DIVISION”;
6. ABSOLUTE :
Query 1 : SELECT ABS(2);
................................. ...........................
8. Floor(Rounding Function)
Query : SELECT FLOOR(1.23), FLOOR(-1.23);
9. MOD :
Query 1 : SELECT MOD(234, 10);
................................. ...........................
10. Power :
Query : SELECT POW(2,2);
11. Round :
Query : SELECT ROUND(1.298, 1), ROUND(1.58) , ROUND(1.298, 0);
12. SIGN :
Query : SELECT SIGN(-32), SIGN(0), SIGN(234);
13. SQRT :
Query : SELECT SQRT(4);
................................. ...........................
14. Truncate :
Query : SELECT TRUNCATE(1.223,1);
................................. ...........................
EXERCISE 12
Aim : Familiarizing String Functions
1. ASCII :
Query : SELECT ascii('A'), ascii('a’);
9. Concat :
Query 2 : Concat : SELECT CONCAT('SCMS', ' is', ' an', ' institution ', ' who
runs as a team.');
EXERCISE 13
AIM : Familiarizing Inner join,Left Outer Join,Right Outer Join
1. Create different tables say
SchemeMaster(Sid,Descrptn)
PdtMaster(PdtId,PdtName,CatId, Sid)
EmpMaster(EmpId,EmpName,Desgn)
ProdtMaster(Pid,Pname,Cost)
TransMaster(Tid,EmpId,Pid)
b) LeftOuter Join
Query : SELECT A.EmpId,A.EmpName , B.Tid FROM EmpMaster A
LEFT JOIN TransMaster B ON A.EmpId=B.EmpId;
EXERCISE 14
AIM : Familiarizing View Concept
1. Create a view( A temporary Table) names Products which will refer
columns from PdtMaster(Previously created table on Exercise 13)
EXERCISE 15
Aim : Familiarizing Groupby and Having Clause
2. Insert 5 rows
4. Display the total no of children, and their parent’s names from the table and
group it by Cid.
EXERCISE 16
Aim : Familiarizing Date conversion functions
Usage of date_format function
3. Convert a datetime into day month year weekday time day of the year
Query : SELECT date_format("2017-06-15 22:23:00","%D %M %y,%a %r::
%j");
................................. ...........................
Format Description
%a Abbreviated weekday name (Sun to Sat)
%b Abbreviated month name (Jan to Dec)
%c Numeric month name (0 to 12)
%D Day of the month as a numeric value, followed by suffix (1st, 2nd, 3rd...)
%d Day of the month as a numeric value (01 to 31)
%e Day of the month as a numeric value (0 to 31)
%f Microseconds (000000 to 999999)
%H Hour (00 to 23)
%h Hour (00 to 12)
%I Hour (00 to 12)
%i Minutes (00 to 59)
%j Day of the year (001 to 366)
%k Hour (0 to 23)
%l Hour (1 to 12)
%M Month name in full (January to December)
%m Month name as a numeric value (00 to 12)
%p AM or PM
%r Time in 12 hour AM or PM format (hh:mm:ss AM/PM)
%S Seconds (00 to 59)
%s Seconds (00 to 59)
%T Time in 24 hour format (hh:mm:ss)
%U Week where Sunday is the first day of the week (00 to 53)
%u Week where Monday is the first day of the week (00 to 53)
%V Week where Sunday is the first day of the week (01 to 53). Used with
%X
%v Week where Monday is the first day of the week (01 to 53). Used with
%x
%W Weekday name in full (Sunday to Saturday)
%w Day of the week where Sunday=0 and Saturday=6
%X Year for the week where Sunday is the first day of the week. Used with
%V
%x Year for the week where Monday is the first day of the week. Used with
%v
%Y Year as a numeric, 4-digit value
%y Year as a numeric, 2-digit value
................................. ...........................
EXERCISE 17
Aim : Familiarizing SubQuery
1. Create a table called “Cand” ( cid pk,cname,csal,cexprnc integer)
Query : create table cand(cid varchar(10) primary key, cname varchar(25), csal
varchar(25), cexprnc int(20));
2. Insert 5 rows
5. Select the data from cand where cand.cid = id from candidate if the year =2001
Query : SELECT * FROM CAND WHERE CAND.CID=(SELECT ID FROM
CANDIDATE WHERE YEAR=2001);
EXERCISE 18
Familiarizing TCL Commands
6. Select all the details from cand table.
Query : select * from cand;
8. Select the details of cand where cand experience is more than 3 years.
Query: select * from cand where cexprnc > 3;
MONGODB
................................. ...........................
QUERY:
use yournameDB
OUTPUT:
QUERY:
USE urnameMCADB
OUTPUT:
QUERY:
db.dropDatabase()
OUTPUT:
................................. ...........................
QUERY:
db.createCollection(“Friends”)
db.createCollection(“Family")
OUTPUT:
QUERY:
show collections
OUTPUT
QUERY:
db.Friends.drop()
OUTPUT:
................................. ...........................
QUERY:
db.Family.insertMany([{Relationship:"Father",Address:"Housename",by:"Fathername",Siblings:
["Father'sistername","Father's Brothersname"]},{Relationship:"Mother",Address:"Mothers
house name",by:"Mother's Name",Siblings:["Mother's sistername","Mother's
brothername"],comments: [{status:"married",date:new Date(2023,11,10,2,25),refered_by:"4
Friends"}]}])
OUTPUT:
................................. ...........................
QUERY:
OUTPUT:
................................. ...........................
QUERY:
OUTPUT:
................................. ...........................
10. To display the documents from family in a formatted way by using pretty () method
QUERY:
db.Family.find().pretty()
OUTPUT:
................................. ...........................
QUERY:
db.Family.findOne()
OUTPUT:
12. To show all the tutorials written by ‘ Karl’ and whose title is 'MongoDB Overview'.
QUERY:
OUTPUT:
................................. ...........................
13. To show all the tutorials written by 'Karl' or whose title is 'MongoDB Overview'.
QUERY:
OUTPUT:
14. Display the documents that have likes greater than 10 and whose
title is either'MongoDB Overview' or by is 'Karl’.
QUERY:
db.dummycol.find({"likes":{$gt:10},$or:[{"title":"MongoDB Overview”},{"by":"Karl"}]}).pretty()
................................. ...........................
OUTPUT:
15. Retrieve the document(s) from empdetails whose first name is not "Radhika" and
last name isnot "Christopher".
QUERY:
db.EmpDetails.find({$and:[{“First_Name":{$ne:"radhika"}},{"Last_Name":
{$ne:"Christopher"}}]}).pretty()
OUTPUT:
................................. ...........................
16. Retrieve the document(s) from empdetails whose age is not greater than 25
QUERY:
db.EmpDetails.find({“age":{$not:{$gt:"25"}}}).pretty()
OUTPUT:
................................. ...........................
17. Retrieve the document(s) from empdetails whose age is in the list {24,25,26}
QUERY:
db.EmpDetails.find({“Age":{$in:["24","25","26"]}}).pretty()
OUTPUT:
18. Set the new title 'New MongoDB Tutorial' of the documents whose title is
'MongoDBOverview'.
QUERY:
OUTPUT:
................................. ...........................
19. Updates the age and email values of the document with name 'Radhika'.
QUERY:
db.EmpDetails.update({“First_Name”:"Radhika"},{$set
{“age":"29","e_mail":"[email protected]"}},{multi:true})
OUTPUT:
20. Updates the age as 100, whose age is not greater than 25.
QUERY:
db.EmpDetails.update({“age":{$lt:"25"}},{$set:{"age":"100"}})
OUTPUT:
QUERY:
db.EmpDetails.remove({"title":"MongoDB Overview”})
OUTPUT:
................................. ...........................
QUERY:
db.Student.insert([{"student_name":"Tim","student_id":"1001","student_age":"23"},
{"student_name":"Tom","student_id":"1002","student_age":"24"},
{"student_name":"Jerry","student_id":"1003",
“student_age”:"25"}])db.Student.find({},{_id:0,"student_id":1})
OUTPUT:
................................. ...........................
QUERY:
db.Student.find({"student_id":{$gt:"1002"}}).pretty()
OUTPUT:
QUERY:
db.Student.find({"student_id":{$gt:"1002"}}).limit(1).pretty()
OUTPUT:
................................. ...........................
25.List the second record of the students, having the id > 1002.
QUERY:
OUTPUT:
................................. ...........................
QUERY:
db.Student.find().sort({“student_id":-1}).pretty()
OUTPUT:
................................. ...........................
27. To display the student_id field of all the students in ascending order.
QUERY:
db.Student.find({},{_id:0,"student_id":1}).sort({"student_id":1})
OUTPUT:
QUERY:
db.Student.find({},{_id:0,”student_name":1,"student_age":1}).sort({"student_id":1})
OUTPUT:
QUERY:
db.Student.createIndex({“student_name":1})
OUTPUT:
................................. ...........................
QUERY:
db.Student.getIndexes()
OUTPUT:
QUERY:
db.Student.dropIndex({“student_name":1})
OUTPUT:
QUERY:
mongodump
mongorestore
................................. ...........................
OUTPUT: