You are on page 1of 40

Laboratory Manual of

Database Management System


(CE1002)

Name _________________________

Enrollment No. _________________

Division-Batch _________________

2021-22

Diploma in Computer Engineering


Sem: 3
Certificate
This is to certify that Mr./Ms. ______________________________
Enrollment No: _____________________________of Diploma in Computer
Engineering, Semester 3 has satisfactory completed his/her
laboratory work of Database Management System(CE1002)
during the regular term in academic year 2021-22.

No. of Practical Certified: ______/______

Date of Submission: __________

Sign of Subject Teacher Sign of Examiner Sign of


Head of Department/
Program coordinator,
Diploma Studies,
CGPIT, UTU
INDEX
Subject: Database Management System Semester: 3 Batch: ____________

Subject Teacher: Enrolment Number:

Viva Journal Total


Sr. No Title Date Sign
( ) ( ) ( )
Create tables and insert records
1.
Queries based on fetching the records
2.
from tables
Queries based on DDL and DML
3.
statements
4. Queries based on functions
Queries based on group by, having and
5.
order by clause
6. Queries based in constraints
Queries based on Joins
7.

Practical __ to ___ Practical __ to ___ Practical __ to ___


MCQ Journal MCQ Journal MCQ Journal
Attendance Total Attendance Total Attendance Total
Test & Viva Test & Viva Test & Viva
( ) ( ) ( ) ( ) ( ) ( )
( ) ( ) ( ) ( ) ( ) ( )

Name & Sign of Subject teacher:


Practical
Implement the following database schema:
Student ( Student_ID, FName, LName, Address, Mobile_No)
Subject (Subject_ID,SubjectName, Credits)
Instructor (Instructor_ID, Inst_FName,Inst_LName, DOJ)
Teaches (InstructorI_D,Subject_ID)
Studies (Student_ID, Subject_ID)
The tables have following values:
1. Student
Student_ID FName LName Address Mobile_No
1001 Ali Shaikh Surat 9898976765
1002 Ankit Desai Valsad 8987699000
1003 Roshani Patel Bombay 7990986543
1004 Aakash Panchal Surat 8785490090
1005 Arjun Kapoor Valsad 9879611133
1006 Smita Patil Navsari 9909666000
1007 Arun Desai Bharuch 9879867555
1008 Ankit Kharwar Surat 9099989677
1009 Chetna Patil Valsad 9090877844

2. Subject
Subject_ID SubjectName Credits
101 DBMS 24
102 Java 25
103 WTP 24
104 OS 25
105 FCP 23
3. Instructor
Instructor_ID Inst_Fname Inst_LName DOJ
F001 Ankit Singh 03-Jan-2006
F002 Pooja Jhaveri 03-Mar-2003
F003 Arvind Patel 10-Feb-2000
F004 Chhaya Shah 08-Feb-2004
F005 Sneha Mavani 07-Aug-2007
F006 Pooja Shah 07-Nov-2001

4. Teaches
Instructor_ID Subject_ID
F002 104
F003 101
F005 105
F002 103
F004 102
F001 104
F006 102

5. Studies
Student_ID Subject_ID
1001 101
1002 105
1004 102
1005 101
1001 102
1009 101
1004 104
1006 105
Practical 1: Create tables and Insert records
a. Create tables as given above with primary key and foreign key concept.
b. Insert all the records in created 5 tables as per given above.

Create command: Create command defines each column of table uniquely. Each
column has a minimum of three attributes: name, datatype, size. Each column definition
is separated from other by a comma.
To create new table:
CREATE TABLE tablename (columnname1 datatype(size), column_name2
datatype(size), ...);
To Create new table with Primary key :
CREATE TABLE tablename (columnname1 datatype(size) PRIMARY KEY,
column_name2 datatype(size), ...);
To create new with foreign key:
CREATE TABLE tablename (columnname1 REFERENCES table2name (columnname),
column_name2 datatype(size), ...);

Insert command: It is used for inserting data into tables.


Insert data into all columns:
INSERT INTO tablename VALUES (value1, value2, …);
Insert data into specific columns:
INSERT INTO tablename (columnname1, columnname2, …) VALUES (value1,
value2, …);
 Student Table
Create Table:

Insert records:
1.

2.

3.

4.

5.

6.

7.

8.

9.

Display table:
 Subject Table
Create table:

Insert records:
1.

2.

3.

4.

5.

Display table:
 Instructor Table
Create table:

Insert records:
1.

2.

3.

4.

5.

6.

Display table:
 Teaches Table
Create table:

Insert records:
1.

2.

3.

4.

5.

6.

7.

Display table:
 Studies Table
Create table:

Insert records:
1.

2.

3.

4.

5.

6.

7.

8.

Display table,
Practical 2: Queries based on fetching the records from tables
Select command: It is used to fetch data from a database. It is used to extract only those
records that fulfill a specified condition.
To display all data from table:
SELECT * FROM tablename;
To display specific column from table:
SELECT columnname1, columnname2,… FROM tablename;
To display specific rows from table:
SELECT columnname1, columnname2,… FROM tablename WHERE condition;
To display unique values from column:
SELECT DISTINCT(columnname) FROM tablename;

1. List all the information of students.

2. List out the students who live in ‘Surat’.

3. List all the unique names of the students.

4. List the instructor’s name who joined on ‘03-Jan-2006’.


5. List the subject names which have credits as 25.

6. List the instructor’s name whose name starts with ‘Ar’.

7. List the student names which start with ‘A’ followed by any single character followed by
‘k’ followed by any characters.

8. List the instructor’s id and name whose date of joining is from ‘01-Jan-2000’ to ‘01- Feb-
2005’.

9. List the subject code with its credits of the subjects ‘DBMS’, ‘Java’ and ‘WTP’.

10. List all the students who do not live in ‘Bombay’ and ‘Surat’.
11. List the students name who live either in ‘Surat’ or ‘Valsad’.

12. List the student numbers who are not studying subject code 101,103 and 105.
Practical 3: Queries based on DDL and DML statements
Alter command: ALTER command is used to add, delete, or modify columns in an
existing table.
To ADD column in table:
ALTER TABLE table_name ADD ColumnName datatype(size);
To change datatype of column in table:
ALTER TABLE tablename MODIFY ColumnName datatype;
To Drop/Delete column from table:
ALTER TABLE tablename DROP COLUMN ColumnName;

Truncate command: The TRUNCATE TABLE statement is used to delete the data inside
a table, but not the table itself.
To remove all records from table:
TRUNCATE TABLE tablename;

Drop command: The DROP TABLE statement is used to drop an existing table in a
database.
To delete entire table:
DROP TABLE tablename;

Rename command: The rename command is used to change the name of an existing
database object to a new name.
To rename table name:
RENAME TABLE current_tablename TO new_tablename;

Update command: The UPDATE statement is used to modify the existing records in a
table.
Update/insert data into whole column:
UPDATE tablename SET column1=value1;
Update data into specific row:
UPDATE tablename SET column1=value1,.. WHERE condition;

Desc command:
DESC command use for describe the list of column definitions for specified table.
DESC tablename;
Delete command: The DELETE statement is used to delete existing records in a table.
Delete all records from table:
DELETE FROM tablename;
Delete specific records from table:
DELETE FROM tablename WHERE condition;

1. Modify the mobile number of the student with Student_ID = 1002 to 9909990909.

2. Change the table name ‘Subject’ to ‘Subjects’.

3. Display the structure of Teaches table.

4. Add new column of experience in instructor table.

5. Update experience of all instructors to 5.

6. Delete the column experience from the Instructor table.


7. Modify size of column first name to 30 in Student table.

8. Add record of new student in student table whose id is ‘1010’, first name is ‘Mihir’, last
name is ‘Patel’, address is ‘bardoli’ and mobile number is ‘9892919793’.
Practical 4: Queries based on functions
Demonstrate the use of following functions:
a) Aggregate/ Group functions:
AVG(): It is used to find average of a given column’s values.
select AVG(column_name) from tablename;

SUM(): It is used to perform summation operation on given column’s values.


select SUM(column_name) from tablename;

MIN(): It is used to find minimum number from given column’s values.


select MIN(column_name) from tablename;

MAX(): It is used to find maximum number from given column’s values.


select MAX(column_name) from tablename;

COUNT(): It is used to count total number of rows in given column.


select COUNT(column_name) from tablename;

COUNT(*): It will count total number of rows in a given table.


select COUNT(*) from tablename;

AVG():

Output:

SUM():

Output:

MIN():

Output:
MAX():

Output:

COUNT():

Output:

COUNT(*):

Output:
b) Numeric functions:
ABS(): It will convert given value in positive number.
Select ABS(number) from dual;

POWER(): It returns the value of a number raised to the power of another number.
select POWER(x, y) from dual;

SQRT(): It returns the square root of a number.


select SQRT(number) from number;

ROUND(): It rounds a number to a specified number of decimal places.


select ROUND(number, decimals) from dual;

MOD(): It returns the remainder of a number divided by another number.


select MOD(x, y) from dual;

CEIL(): It will returns the smallest integer value that is bigger than or equal to a
number.
select CEIL(number) from dual;

FLOOR(): It returns the largest integer value that is smaller than or equal to a number.
select FLOOR(number) from dual;

ABS():

Output:

POWER():

Output:

SQRT():

Output:
ROUND():

Output:

MOD():

Output:

CEIL():

Output:

FLOOR():

Output:
c) Character functions:
ASCII(): It returns the ASCII value for the specific character.
select ASCII(character) from dual;

CONCAT(): It adds two or more strings together.


select CONCAT(string1, string2) from dual;

REVERSE(): It reverses a string and returns the result.


Select REVERSE(string) from dual;

LENGTH(): It returns the length of a string.


Select LENGTH(string) from dual;

LOWER(): It converts a string to lower-case.


Select LOWER(string) from dual;

UPPER(): It converts a string to upper-case.


Select UPPER(string) from dual;

INITCAP(): It will make all the first letters of given words into capital letter.
Select INITCAP(string) from dual;

LPAD(): The LPAD() function left-pads a string with another string, to a certain length.
Select LPAD(string, length, lpad_string) from dual;

RPAD(): The LPAD() function right-pads string with another string, to a certain length.
Select RPAD(string, length, rpad_string) from dual;

TRIM(): It is basically used to remove extra spaces from a given string. But by using
trim function we can also remove any character from starting or ending of a string.
Select TRIM(LEADING/TRAILING/BOTH ‘character_to_trim’ FROM ‘string’) from
dual;

TRANSLATE(): It will take three arguments as a parameter. And replace 2nd arguments
values with 3rd arguments values in a string given as 1st argument.
Select TRANSLATE(string, ‘characters_to_replace’, ‘replacement_characters’)
from dual;

SUBSTR(): The SUBSTR() function extracts a substring from a string (starting at any
position).
select SUBSTR(string, start, length) from dual;
ASCII():

Output:

CONCAT():

Output:

REVERSE():

Output:

LENGTH():

Output:

LOWER():

Output:

UPPER():

Output:
INITCAP():

Output:

LPAD():

Output:

RPAD():

Output:

TRANSLATE():

Output:

SUBSTR():

Output:
TRIM():
1)

Output:
2)

Output:
3)

Output:
d) Conversion functions:
TO_CHAR(number) & TO_CHAR(date): TO_CHAR() function converts a DATE or
number in a specified string format.
Select TO_CHAR(date/ number, string_format) from dual;

TO_DATE(): TO_DATE function converts a string to a date.


Select TO_DATE (string1 , format_mask)

TO_CHAR(number):
1)

Output:
2)

Output:

TO_CHAR(date):
1)

Output:
2)

Output:

TO_DATE():
1)

Output:
2)

Output:
e) Date functions:
MONTHS_BETWEEN(): It is used to get the number of months between given dates.
Select MONTHS_BETWEEN(date1,date2) from dual;

LAST_DAY(): This function extracts the last day of the month for a given date.
Select LAST_DAY(date) from dual;

ADD_MONTHS(): It adds a number of month(n) to a date and returns the same day n of
month away.
Select ADD_MONTHS(date_expression, month) from dual;

NEXT_DAY(): This returns the date of the first weekday specified by day name that is
later than a date.
Select NEXT_DAY(date,weekday) from dual;

CURRENT_DATE: This function returns the current date in the session time zone.
Select CURRENT_DATE from dual;

SYSDATE: This function returns the current date and time of the Operating System
where the Oracle Database installed.
Select SYSDATE from dual;

SYSTIMESTAMP: This function returns a TIMESTAMP WITH TIME ZONE value that
represents the system date and time including fractional seconds and time zone.
Select SYSTIMESTAMP from dual;

MONTHS_BETWEEN():
1)

Output:
2)

Output:
LAST_DAY():

Output:

NEXT_DAY():
1)

Output:
2)

Output:

ADD_MONTHS():
1)

Output:
2)

Output:

CURRENT_DATE():

Output:
SYSDATE():

Output:

SYSTIMESTAMP():

Output:
f) Miscellaneous Functions:
UID: Returns an integer value corresponding to UserId of the user currently logged in.
select UID from dual;

USER: Returns the user name of currently logged in.


select USER from dual;

DECODE(): It is similar to if-then-else construct in programming language.


Select DECODE(value, if1, then1, if2, then2,… ,else) from dual;

NVL(): It returns exp2 if exp1 is null and returns exp1 if it is not null. Applicable to
numeric, character or date data type.
Select NVL(exp1, exp2) from dual;

VSIZE(): It returns storage size of expression.


Select VSIZE(exp) from dual;

GREATEST(): It is used to find greater values from given values.


Select GREATEST(value 1, value 2, …, value n) from dual;

LEAST(): It is used to find smaller values from given values.


Select LEAST(value 1, value 2, …, value n) from dual;

UID:

Output:

USER:

Output:

DECODE():

Output:
NVL():
1)

Output:
2)

Output:

VSIZE():
1)

Output:
2)

Output:

GREATEST():
1)

Output:
2)

Output:
3)

Output:
LEAST():
1)

Output:
2)

Output:
3)

Output:
Practical 5: Queries based on group by, having and order by clause
GROUP BY CLAUSE: The GROUP BY statement groups rows that have the same values
into summary rows. The GROUP BY statement is often used with aggregate functions
(COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.
SELECT column_name from tablename where condition GROUP BY column_name;

HAVING CLAUSE: The HAVING clause was added to SQL because the WHERE keyword
could not be used with aggregate functions.
SELECT column_name FROM tablename WHERE condition GROUP BY column_name
HAVING condition;

ORDER BY CLAUSE: The ORDER BY keyword is used to sort the result-set in ascending
or descending order. The ORDER BY keyword sorts the records in ascending order by
default. To sort the records in descending order, use the DESC keyword.
To arrange in ascending order:
SELECT column1, column2, ... FROM tablename ORDER BY column1;
To arrange in descending order (Syntax):
SELECT column1, column2, ... FROM tablename ORDER BY column1 DESC;

1. Sort the student list according to student_id.

2. Sort the instructor list in descending order according to their name.

3. Display subject names in ascending order.


4. List the total number of students who belongs to same city.

5. List out total number of students who are studying same subjects.

6. List total number of subjects taught by each instructor.

7. List total number of subjects taught by each instructor who’s having more than or
equals to 2 subjects.

8. List out total number of students who are studying more than 1 subject.
Practical 6: Queries based in constraints

NOT NULL: NOT NULL constraint specifies that a column cannot contain NULL values.
Add NOT NULL constraint in new table:
CREATE TABLE tablename ( ..., column_name data_type(size) NOT NULL, …);
Add NOT NULL constraint in existing column of existing table:
Alter table tablename MODIFY column_name NOT NULL;
Add NOT NULL constraint in new column of existing table:
Alter table tablename ADD column_name data_type(size) NOT NULL;

CHECK: A check constraint allows you to specify a condition on each row in a table.
Add CHECK constraint in new table:
Create table tablename ( ..., column_name data_type(size) CHECK (condition), …);
Add CHECK constraint in existing column of existing table:
Alter table tablename ADD CONSTRAINT columnname CHECK (condition);;
Add NOT NULL constraint in new column of existing table:
Alter table tablename ADD columnname datatype(size) CHECK (condition);

1. Add NOT NULL constraint on column DOJ in instructor table.

2. Add Salary named column with check constraint that it cannot be negative.

3. Add Department named column with CHECK constraint that all values to be inserted
in column should be either ‘CE’ or ‘IT’.

4. Add check constraint on mobile_no that the length of the mobilel_no should be ‘10’.
Practical 7: Queries based on Joins
Inner join: The INNER JOIN keyword selects all rows from both the tables as long as the
condition satisfies. This keyword will create the result-set by combining all rows from
both the tables where the condition satisfies i.e value of the common field will be same.
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;

Outer join:
Left outer join: This join returns all the rows of the table on the left side of the join
and matching rows for the table on the right side of join. The rows for which there is
no matching row on right side, the result-set will contain null.
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;

Right outer join: This join returns all the rows of the table on the right side of the
join and matching rows for the table on the left side of join. The rows for which there
is no matching row on left side, the result-set will contain null.
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;

Full outer join: FULL JOIN creates the result-set by combining result of both LEFT
JOIN and RIGHT JOIN. The result-set will contain all the rows from both the tables.
The rows for which there is no matching, the result-set will contain NULL values
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
1. List all the details of student with their subject code.

2. List the instructor names who teach either subject id 101 or 102.

3. List the instructor names, DOJ and the subject code they teach.

4. List the student names who studies subject 102.

5. Find instructor id who taught subjects to student with Student id 1001.

6. List subject name taught by instructor using right join.


7. List subject name taught by instructor using left join.

8. List the subject detail which is studied by student number 1001.


Practical 8: Queries based on sub-queries using Operators
Subqueries: Subqueries are most frequently used with the SELECT statement.
SELECT column1name, column2name FROM table1name WHERE column_name
OPERATOR (SELECT column1name, column2name FROM table1name WHERE
condition);

IN Operator: The IN operator allows you to specify multiple values in a WHERE clause.
The IN operator is a shorthand for multiple OR conditions.
SELECT column_name(s) FROM tablename
WHERE column_name IN (value1, value2, ...);
or
SELECT column_name(s) FROM tablename
WHERE column_name IN (SELECT STATEMENT);

ANY Operator: The ANY operator is used with a WHERE or HAVING clause. The ANY
operator returns true if any of the subquery values meet the condition.
SELECT column_name(s) FROM tablename WHERE column_name operator
ANY
(SELECT column_name FROM tablename WHERE condition);

ALL Operator: The ANY operator are used with a WHERE or HAVING clause. The ALL
operator returns true if all of the subquery values meet the condition.
SELECT column_name(s) FROM table_name WHERE column_name operator
ALL
(SELECT column_name FROM table_name WHERE condition);

EXISTS Operator: The EXISTS operator is used to test for the existence of any record in
a subquery. The EXISTS operator returns true if the sub query returns one or more
records.
SELECT column_name(s) FROM table_name WHERE
EXISTS
(SELECT column_name FROM table_name WHERE condition);

NOT EXISTS Operator: The NOT EXISTS will check the Sub query for rows existence,
and if there are no rows then it will return TRUE, otherwise FALSE.
SELECT column_name(s) FROM table_name WHERE
NOT EXISTS
(SELECT column_name FROM table_name WHERE condition);
1. List the name of students who are studying the subject 105 using IN operator.

2. List the instructor’s first name and experience who is not teaching subject 103.

3. Find out maximum credits of subjects by using all operator.

4. List the student’s name, Address, Phone number who are studying subject 101 using
exists operator.

5. List the student’s name, Address, Phone number who are studying subject ‘WTP’ using
not exists operator.

You might also like