Professional Documents
Culture Documents
OS Labreport - 1
OS Labreport - 1
LAB REPORT
OF
Database Management System
GUIDE HOD
Er. Suresh Kr. Mahato Mr. Rajkumar Shah
Asst. Professor (Department of BBM)
------------------------- -------------------------
Signature with Date Signature with Date
1
Tribhuvan University
Faculty of Management
Swoyambhu International College
LETTER OF APPROVAL
This lab report bind with the entitle “Database Management System -Lab Report”
prepared
By
Kamlesh Kumar Mahato
Batch of 2020 of Swoyambhu Int’l College, Lagankhel, Lalitpur
Registration No: 7-2-927-708-2020
As a partial fulfillment for requirement for the degree of Bachelor in Business
Management has been evaluated, in our view, this project includes all the quality
elements for the required degree.
…………………………… ……………………………….
Er. Suresh Kumar Mahato External Examiner
Internal Examiner
2
ACKNOWLEDGMENT
To make any lab report, essential guidance and references is mostly required
without which proper report cannot be prepared. So, I would like to express
my gratitude towards Mr. Suresh Kr. Mahato, who enable me to complete
this lab report successfully in all the aspects. I am also very thankful to Mr.
Rajkumar Sah, BBM coordinator for his encouragement and importance
guidance regarding this project. Similarly, I would like to express thank to all
the teaching and non-teaching staff of Swoyambhu Int’l College for
providing importance resources to complete this report.
3
PREFACE
This report covers all the lab requirement prescribed by TU and each
practical topics are included in this project; this lab report may help
upcoming batch students as a sources of references. I have tried to
minimize all the errors presents here as far as possible. Although, every
effort has been made to make this lab report as clear and accurate,
however suggestion for the improvement needed for the further report
are most welcome and will be highly appreciated.
4
Table of Contents
THEORY OF SQL..............................................................................................................................5
1.1 . Introduction to SQL.........................................................................................................5
1.2 SQL Commands......................................................................................................................6
1.3 SQL Integrity Constraints.....................................................................................................6
1.5 Types of Join in SQL............................................................................................................10
DATA QUERY LANGUAGE...........................................................................................................12
2.1 Different ways of select statement.......................................................................................13
2.2 SQL statement using ‘where’ clause....................................................................................14
2.3 SQL statement using different operator.............................................................................14
2.4 SQL statement using wildcard operators............................................................................15
2.5 SQL statement using regular expression.............................................................................15
Assignment.........................................................................................................................................17
1. Entity-Relationship Diagram....................................................................................................18
2. Queries:.....................................................................................................................................18
Tables Creation:............................................................................................................................18
Description:....................................................................................................................................19
Insertion Value:.............................................................................................................................20
Selection Values:............................................................................................................................22
Queries’s Solution:........................................................................................................................23
Conclusion...........................................................................................................................................24
References..........................................................................................................................................25
THEORY OF SQL
1.1. Introduction to SQL
5
SQL stands for “Structured Query Language” and is term as a query language used for
accessing and modifying information in the database, SQL was first developed in 1970s by
IBM and also an ANSI/ISO standard. It has become a Standard Universal Language used by
most of the relational database management system (RDBMS).
6
Integrity Constraints are used to apply business rules for the database tables. The constraints
available in SQL are Primary Key, Foreign Key, Not Null, Unique and Check. Constraints
can be defined in two ways:
The constraints can be specified immediately after the column definition. This is
called column-level definition.
The constraints can be specified after all the columns are defined. This is called table-
level definition.
1) Primary Key:
This constraint defines a column or combination of columns which uniquely identifies
each row in the table.
Syntax:
Column Name datatype [constraint constraint name] PRIMARY KEY
Queries:
Mysql > create table practical (
o Id int Primary key,
o Assignment name varchar (50)
o );
Query OK, 0 rows affected (0.08 sec)
2) Foreign Key:
This constraint identifies any column referring the PRIMARY KEY in another table.
It establishes a relationship between two columns in the same table or between
different tables. For a column to be defined as a foreign key, it should be a defined as
a primary key in the table which it is referring. One or more columns can be defined
as foreign key.
Syntax:
[Constraint constraint name] References
Queries:
Mysql > create table table ones(
o Id int
o Name varchar (20),,
o Foreign key(id) reference practical(id)
o );
o Query ok,0 rows affected(0.04sec)
3) Not Null Constraint:
This constraint ensures all rows in the table contains a definite value for the column
which is specified as not null which means null value is not allowed.
Syntax:
[Constraint constraint name] Not Null
7
Queries:
Mysql > create table practical one(
o id int not null,
o name varchar(30)
o );
Query OK, 0 rows affected (0.08 sec)
2) Unique Key:
This constraint ensures that a column or a group of columns in each row have a
distinct value. A columns can have a null value but the values cannot be repeated.
Syntax:
[Constraint constraint_name] Unique
Queries:
Mysql > create table practical_two(
o Id int ,
o name varchar(10) unique
o );
Query OK, 0 rows affected (0.08 sec)
Query OK, 0 rows affected (0.08 sec)
Insert Statement:
The INSERT INTO statement is used to insert a new row in a table.
Syntax:
INSERT INTO table_name Values (value1, value2, …….);
Queries:
Mysql > Insert into queries(‘id’,’name’)
o Values (‘1’,’sanjana’),
o (‘2’,’Shrijana’);
Query OK, 0 rows affected (0.00sec)
Select Statement:
The SELECT statement is used to select data from a database. The result is stored in a
result table, called as the result-set.
Syntax:
SELECT * from table_name;
8
Queries:
Myssql>select * from queries;
Id name
1 Sanjana
2 Shrijana
Update Statement:
The UPDATE statement is used to update existing records in the table.
Syntax:
UPDATE table_name SET column=value where some_column=some_value;
Queries:
Mysql > update queries set name=”sanj where=’1’;
Query OK, 0 rows affected (0.08 sec)
Rows matched:1 Changed: 1 warnings:0
Delete Statement:
The DELETE statement is used to delete rows in a table.
Syntax:
DELETE from table_name where some_column=some_value;
Queries:
Mysql> Delete from queries where id=”1”;
Query ok,1 row affected(0.00sec)
Alter Statement:
The SQL ALTER table commands is used to modify the definition structure of the table.
Syntax:
ALTER table table_name ADD column_name datatype;
Queries:
Mysql>Alter table queries add address varchar(50);
Query OK,1 row affected(1.16 sec)
9
Records:1Duplicates:0 warnings:0
Mysql>select*from queries;
Id Name Address
2 Shrijana NULL
Truncate Table:
The SQL Turncate table is used when we want to delete the data inside the table but not
the table itself.
Syntax:
TRUNCATE table table_name;
Queries:
Mysql > Truncate table queries;
Query OK, 0 rows affected (0.00 sec)
Mysql > select* from queries;
Empty set (0.00 sec
Drop Table
The DROP table statement is used to delete an entire table.
Syntax:
DROP table table_name;
Queries:
Mysql > Drop table queries;
Query OK, 0 rows affected (0.96 sec)
Inner Join:
This type of join returns records that have matching values in both tables.
Syntax:
SELECT column1, column2, …..
10
From table_name1
Inner Join table_name2
On table_name1.field=table_name2.field;
Queries:
Mysql > select id,name,amount,date
o From customers
o Inner join orders
o On customers.id=orders.cus_id;
4 rows in set (0.11 sec)
Id Name Amount Date
Left Join:
This types of join returns all records from the left table, and the matched records from
the right table
Syntax:
SELECT column1, column2, …..
From table_name1
Left Join table_name2
On table_name1.field=table_name2.field;
11
o left join orders
o On customers.id=orders.cus_id;
Right Join:
This types of join returns all records from the right table, and the matched records from
the left table
Syntax:
SELECT column1, column2, …..
From table_name1
Right Join table_name2
On table_name1.field=table_name2.field;
DQL commands are basically SELECT statement. SELECT statements let you query the
database to find information in one or more tables, and return the query as a result set. It is
used to fetch the information from the database which is already store there.
12
Syntax:
SELECT [ALL|DISTINCT] fieldname
FROM table_name
[where condition];
Query:
Mysql>select dept_id, dept_block,dept_dean from department;
Dept_id Dept_block Dept_dean
1 20 Sanjana
2 40 Sujata
3 5 Baburam
Query:
Mysql>select dept_name from department;
Dept_id Dept_name Dept_block Dept_dean
1 BCA 20 Sanjana
2 MCA 40 Shrijana
3 BIT 5 Sujata
13
2.2 SQL statement using ‘where’ clause
Queries:
Mysql>select * from departm where dept_dept=’Shrijana’
Dept_id Dept_name Dept_block Dept_dean
2 MCA 40 Shrijana
1 row in set(0.00 sec)
Queries:
Queries:
Queries:
14
Mysql>select * from department where dept_dean like’u%’;
Dept_id Dept_name Dept_block Dept_dean
3 BIT 5 Sujata
Queries:
Mysql>select * from department where dept_block like ‘_’;
Dept_id Dept_name Dept_block Dept_dean
1 BCA 20 Sanjana
2 MCA 40 Shrijana
15
1 MCA 40 Shrijana
16
Assignment
Q.No.1. Consider the schema for Movie Database:
ACTOR (Act_id, Act_Name, Act_Gender)
DIRECTOR (Dir_id, Dir_Name, Dir_Phone)
MOVIES (Mov_id, Mov_Title, Mov_Year, Mov_Lang, Dir_id)
MOVIE_CAST (Act_id, Mov_id, Role)
RATING (Mov_id, Rev_Stars)
Write SQL queries to
1. List the titles of all movies directed by ‘Santosh Sen’.
2. Find the movie names where one or more actors acted in two or more movies.
3. List all actors who acted in a movie before 2000 and also in a movie after 2015 (use JOIN
operation).
4. Find the title of movies and number of stars for each movie that has at least one rating
and find the highest number of stars that movie received. Sort the result by movie title.
5. Update rating of all movies directed by ‘Bhuwan K C’ to 5.
17
1. Entity-Relationship Diagram
Solution,
Actor Dir_phone
Act_Gender Director
Has
Movie_
Cast
Role
Rev_Sta
r
Movies
Mov_Lang
Mov_id
Mov_Title Mov_Year
2. Queries:
Tables Creation:
18
Description:
Description of tables:
Insert Values:
Insert Values:
19
Insertion Value:
20
21
Selection Values:
22
Queries’s Solution:
2. Find the movie names where one or more actors acted in two or more movies.
Solution,
3. List all actors who acted in a movie before 2010 and also in a movie after 2015 (use
JOIN operation).
Solution,
4. Find the title of movies and number of stars for each movie that has at least one
rating and find the highest number of stars that movie received. Sort the result by
movie title.
Solution,
23
Conclusion
In summary, the SQL queries we used offer a detailed look into the Movie Database
setup, helping us understand its structure and contents better. These queries show
how SQL can efficiently handle different tasks like finding specific movie titles or
analyzing relationships between actors and movies.
For instance, our first query simply found all movies directed by 'Santosh Sen',
demonstrating how SQL can quickly fetch specific information. The second query
went a step further, identifying movies where actors appeared in multiple films,
showcasing SQL's ability to handle more complex tasks.
In our third query, we connected different tables to find actors who acted in movies
before 2000 and after 2015, showing how SQL can bring together different pieces of
data for analysis. The fourth query used SQL's functions to calculate movie ratings
and find the highest-rated film, giving insights into viewer preferences.
Finally, our fifth query demonstrated SQL's ability to update data, like changing movie
ratings. Alongside these queries, the Entity-Relationship Diagram visually represents
the database's structure, making it easier to understand.
In conclusion, these SQL queries and the ER Diagram underline the importance of
structured query languages in working with databases. SQL's flexibility and efficiency
make it essential for tasks like data analysis and management. With SQL, users can
effectively explore databases, uncover insights, and make informed decisions based
on the information at hand.
24
References
Er.Shankar N.Adhikary; Database Management System. Advance Saraswati
Prakashan Pvt. Ltd., 1 st Edition (2022 March)
Bhupendra Singh Saud & Indra Chaudhary; Database Management System.
KEC Publication & Distribution Pvt. Ltd., 1 st Edition (2022).
Wikipedia. (2024, March 22). Database Management System. Retrieved from
https://en.wikipedia.org/wiki/Database_management_system.
25