Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

INDIRA GANDHI DELHI TECHNICAL

UNIVERSITY FOR WOMEN

DBMS lab file


Database Management System(BIT-201)
B.Tech CSE (2022-2026)

Submitted By
Name- KHUSHBOO
Enrollment Number- 08701012022
Batch- CSE-2 (A2)
Year- Second
Semester- 3

Submitted To
Professor Devender Kumar Tayal
Department of Computer Science and Engineering
INDEX

S.NO LAB PAGE NO


1 Introduction to DBMS 3-4
2 ER Diagrams 5-7
3 DDL, DML and DCL 8
4 INTRODUCTION TO SQL 9-10
5 DATABASE QUERIES 11
6 SQL DATA TYPES 12
7 ATTRIBUTE CONSTRAINTS 13-14
8 CREATE TABLES 15-16
9 SQL QUERIES TO EDIT A TABLE 17-20
10 SQL QUERIES TO INSERT AND UPDATE DATA IN 21-23
TABLE

11 SQL QUERIES TO RETRIEVE DATA 24-25


12 AGGREGATE FUNCTIONS 26-27
13 SQL QUERIES WITH DATES 28-29
14 SET OPERATIONS 30-34
15 SQL QUERIES USING GROUP BY, HAVING, 35
EXISTS

16 SQL QUERIES FOR ALIASES 36


17 JOIN OPERATIONS 37-38
18 SQL QUERIES TO CREATE VIEWS 39-40
LAB - 1
INTRODUCTION TO DBMS
Database is a collection of interrelated data which helps in efficient
retrieval, insertion and deletion of data from database and
organises the data in the form of tables, views, schemas, reports
etc. Databases are widely used. Banking: For maintaining customer
information, accounts, and loans, and banking transactions.
Airlines: For reservations and schedule information. Universities:
For student information, course registrations, and grades , etc. The
software which is used to manage databases is called Database
Management System (DBMS). For Example, MySQL, Oracle etc.
are popular commercial DBMS used in different applications.

DBMS vs FILE SYSTEM

s.no File System DBMS


1 In the file system, the same There is no data redundancy.
information may be duplicated in
several places. Redundant data
can be present in a file system.
2 It does not provide backup and It provides backup and recovery of
recovery of data if it is lost. data even if it is lost.
3 File systems provide less security. DBMS provides data security
mechanisms and grants different
access rights to different users.

IMPORTANCE OF DBMS

1.Increased efficiency and Quality - They are essential for


organisations because they offer an efficient way of handling large
amounts and multiple types of data. The ability to access data
efficiently allows companies to make informed decisions quicker,
and improves quality of data.

2.Data Consistency and No Data Redundancy- Data inconsistency


occurs when different versions of the same data exist in different
places in an organisation. By using a database management
system, we ensure that the same(consistent) view of your data is
shared throughout the organisation and there is no data
redundancy.

3.Security and Multi-access - A database management system


makes it possible for multiple authorised users to access the same
database from different places, in different ways, to fulfil different
needs. And this is done in an efficient, time-saving manner without
the loss of any data. It gives access to certain parts of the database
to certain people.

LAB - 2
INTRODUCTION TO ER-DIAGRAMS

Entity Relationship Diagram(ER Diagram), is a diagram that


displays the relationship of entity sets stored in a database and
helps to explain the logical structure of databases. The entity
relationship (E-R) data model is based on a perception of a real
world that consists of a set of basic objects called entities, their
attributes and of relationships among these objects. The model is
intended primarily for the database-design process.

ENTITY- An entity is an object that exists in the real world and is


distinguishable from other objects. It is anything in the enterprise
that is to be represented in our database. It may be a physical thing
or simply a fact about the enterprise or an event that happens in the
real world. An entity can be a place, person, object, event or a
concept, which stores data in the database.
Example- student, car, account etc.

STRONG ENTITY- A strong entity is not dependent on any other


entity in the schema. A strong entity will always have a primary key.

WEAK ENTITY- A weak entity is dependent on a strong entity to


ensure its existence. Unlike a strong entity, a weak entity does not
have any primary key. It instead has a partial discriminator key.

ATTRIBUTE- An entity is represented by a set of attributes.


Attributes are descriptive properties possessed by each member of
an entity set.
Example- student has attributes name, course, enrolment number,
address etc. Assigning attributes for an entity set expresses that the
database stores similar information concerning each entity in the
entity set; however, each entity may have its own value for each
attribute. There are 5 types of attribute- simple, composite,
single-valued, multi-valued and derived attributes.
RELATIONSHIP- A relationship is an association or relationship
among several entities. Example- A customer depositor relationship
with his/her account. The collection of all entities of the same type is
an entity set, and the collection of all relationships of the same type
is a relationship set. Relationships can be one to one(1:1), one to
many(1:n), many to one(n:1) and many to many(n:m).

CARDINALITY- Mapping cardinalities express the number of


entities to which another entity can be associated via a relationship
set.

Ex-college database

Ex-library database
LAB - 3
INTRODUCTION TO DDL, DML AND DCL
DDL-Data Definition Language
DDL Commands- ALTER DATABASE , ALTER EVENT , ALTER
FUNCTION , ALTER INSTANCE , ALTER PROCEDURE , ALTER
SERVER , ALTER TABLE , ALTER TABLESPACE , ALTER VIEW ,
CREATE DATABASE , CREATE EVENT , CREATE FUNCTION ,
CREATE INDEX , CREATE PROCEDURE and CREATE
FUNCTION , CREATE SERVER , CREATE TABLE , CREATE
TABLESPACE , CREATE TRIGGER, CREATE VIEW , DROP
DATABASE , DROP EVENT , DROP FUNCTION , DROP INDEX ,
DROP LOGFILE GROUP , DROP PROCEDURE and DROP
FUNCTION s, DROP SERVER , DROP TABLE , DROP
TABLESPACE , DROP TRIGGER , DROP VIEW , RENAME TABLE
, TRUNCATE TABLE ,COMMENT.

DML-Data Manipulation Language


DML Commands- CALL , DELETE , DO , HANDLER , IMPORT
TABLE , INSERT , LOAD DATA , REPLACE , SELECT , TABLE ,
UPDATE , VALUES , WITH , MERGE.

DCL-Data Control Language


DCL Commands- GRANT, REVOKE, DENY.

LAB – 4
INTRODUCTION TO SQL
SQL stands for Structured Query Language. SQL is a standard
language for accessing and manipulating databases. SQL can
execute queries against a database. SQL can retrieve data, insert
records, update records, delete records, create new databases,
create new tables, create stored procedures, can create views in a
database, set permissions on tables, procedures, and views, etc.

MySQL
MySQL is a relational database management system based on the
Structured Query Language. MySQL is open-source and free
software under the GNU licence. It is supported by Oracle
Company. MySQL is currently the most popular database
management system software used for managing the relational
database. It is fast, scalable, and an easy to use database
management system in comparison with Microsoft SQL Server and
Oracle Database. It is commonly used in conjunction with PHP
scripts for creating powerful and dynamic server-side or web-based
enterprise applications. It is developed, marketed, and supported by
MySQL AB, a Swedish company, and written in C programming
language and C++ programming language. MySQL supports many
Operating Systems like Windows, Linux, MacOS, etc. with C, C++,
and Java languages.

The advantages of using MySQL:-


1. MySQL is an open-source database, so it is free.
2. MySQL is a very powerful program that can handle a large set of
functionality of the most expensive and powerful database
packages.
3. MySQL is customizable because it is an open-source database, it
facilitates programmers to modify SQL software according to their
own specific environment.
4. MySQL is quicker than other databases, so it can work well with
large data sets.
5. MySQL supports many operating systems with many languages
like PHP, PERL, C, C++, JAVA, etc. and MySQL uses a standard
form of the well-known SQL data language.
6. MySQL is very friendly with PHP, the most popular language for
web development.
7. MySQL supports large databases, up to 50 million rows or more
in a table. The default file size limit for a table is 4GB, but can be
increased to a theoretical limit of 8 million terabytes (TB).

LAB – 5
DATABASE QUERIES
Q1) Write a query to create a database with the name IGDTUW.
ans)CREATE DATABASE IGDTUW;

Q2) Write a query to list all databases.


ans)SHOW DATABASE;

Q3) Write a query to delete a database.


ans)DROP DATABASE DATABASE NAME;

Q4) Write a query to select a database.


ans)USE DATABASE NAME;

LAB – 6
SQL DATA TYPES
1 int-int(size) data type stores a medium integer. Signed range is
from -2147483648 to 2147483647.

2. varchar-varchar(size) stores a variable length string (can contain


letters, numbers, and special characters).

3. char-char(size) 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. The default
value is 1.

4. date-Date data type stores a date in the format: YYYY-MM-DD.


The supported range is from '1000-01-01' to '9999-12-31' .

Q1) Write a query to insert age into a table.


Q2) Write a query to insert an aadhaar number into a table.
Q3) Write a query to insert student id into a table.
Q4) Write a query to insert the name of the student into a table.
ans)CREATE TABLE STUDENT(
Student_id int primary key,
Student name varchar(100) not null,
Student_aadhaar char(12) unique,
Student_age int not null
);

LAB – 7
ATTRIBUTE CONSTRAINTS
1. NOT NULL-The NOT NULL constraint enforces a column to not
accept NULL values. This enforces a field to always contain a
value, and we cannot insert a new record, or update a record
without adding a value to this field.

2. UNIQUE-The unique constraint ensures that all values in a


column are different.

3. PRIMARY KEY-The primary key constraint uniquely identifies


each record in a table. Primary keys must contain unique values,
and cannot contain NULL values.

4. FOREIGN KEY-A foreign key is a field (or collection of fields) in


one table, that refers to the primary key in another table.

Q1) Write a query to add a not null column into the table.
ans)USE THE ALTER TABLE;

Q2) Write a query to add a unique column into the table.


ans)alter table table name add UNIQUE(column name);

Q3) Write a query to add a primary key column into the table.
ans)ALTER TABLE [TABLE NAME] ADD PRIMARY KEY(ID);

Q4) Write a query to add a foreign key column into the table.
ans)ALTER TABLE TABLE_NAME ADD CONSTRAINT_NAME
FOREIGN KEY(FOREIGN KEY NAME) REFERENCES PARENT
TABLE(COLUMN NAME);

Q5) Write a query to add a check column into the table.


ans)COL_LENGHT(‘TABLE_NAME’,’COLUMN_NAME’)IS NOT
NULL PRINT ‘COLUMN EXISTS’; ELSE PRINT ’COLUMN DOES
NOT EXISTS’;
Q6)Write a query to add an auto-increment column into the table.
ans)ALTER TABLE sales MODIFY ID INT NOT NULL
AUTO_INCREMENT PRIMARY KEY;

LAB – 8
CREATE TABLES

Q) Create tables.
1)student table

create database college;


use college;
create table student (
student_id int primary key,
student_name varchar(50),
student_age int not null
);
insert into student
(student_id,student_name,student_age)
values
(101,"khushboo",20),
(102,"Ram",20),
(103,"tanu",19),
(104,"arjun",21),
(105,"pooja",18);
select * from student;

2)company table.
create database company_xyz;
use company_xyz;
create table employee_info(
id int primary key,
name varchar(50),
salary int not null
);
insert into employee_info
(id,name,salary)
values
(1,"adam",25000),
(2,"bob",30000),
(3,"casey",40000);
select* from employee_info;

LAB – 9
SQL QUERIES TO EDIT A TABLE

Q1) Write a query to add an email column to the student table.


alter table student add student_email varchar(100);

Q2) Write a query to change the id to roll no in the bank table.

alter table student change student_id roll_no int unique;

Q3) write sql queries to explain the difference between drop and
truncate command.

The DROP command is used to remove table definition and its


contents. The schema of the table is also deleted. Table space is
freed from memory.
Whereas the TRUNCATE command is used to delete all the rows
from the table. All data from the table is deleted, while the schema
of the table remains. It does not free the table space from memory.
use college;
create table account_info(
account_no int primary key,
account_name varchar(50) not null,
account_balance int
);
create table teacher_info(
id int primary key,
name varchar(50),
salary int not null
);

show tables;

insert into account_info


(account_no,account_name,account_balance )
values
(101,"kunal",30000),
(102,"khushi",50000);
insert into teacher_info
(id,name,salary)
values
(1010,"kesav",60000),
(1022,"kriti",50000);

select *from account_info;


truncate table account_info;
select *from account_info;

select*from teacher_info;

drop table teacher_info;


select*from teacher_info;

show tables;
LAB – 10
SQL QUERIES TO INSERT AND UPDATE DATA IN TABLE

Q1)write SQL queries to insert data into the student table.

insert into student_info


(student_id,student_name,student_age)
values
(101,"khushboo",20),
(102,"Ram",20),
(103,"tanu",19),
(104,"arjun",21),
(105,"pooja",18);
select * from student;

Q1)write SQL queries to insert data into the employee table.

insert into employee


(id,name,salary)
values
(1,"adam",25000),
(2,"bob",30000),
(3,"casey",40000);
select* from employee;
Q2) Write a query to update the name where student_id= 102.

update student_info set student_name='Anuj' where


student_id=102;

Q3) Write a query to delete student_info where student_id = 103.


SET SQL_SAFE_UPDATES=0;
delete FROM student_info where student_name='Tanu';

DELETE command can be used to delete specific rows or rows


from a table based on a condition, and DELETE command can also
be used to delete all the data from a table.
LAB – 11
SQL QUERIES TO RETRIEVE DATA

Q1) Write a query to retrieve all values from the table student.

select * from student_info;

Q2) Write a query to retrieve studnt_id from the database.

select student_id from student_info;

Q3) Write a query to select the first 3 records from the student_info
table.

select*from student_info limit 3;


Q4)write a query to select names with marks greater than 70 in the
student table.

select student_name from student_info where student_marks>70;

Q5)Write a query to select the details of all students whose names


start with 'B'.

select*from student_info where student_name like 'k%';


LAB – 12
AGGREGATE FUNCTIONS

Q1) Write a query to count the number of students in the college.

select count(student_name) from student_info;

Q2)Write a query to find average marks of all the students.

select avg(student_marks) from student_info;

Q3) Write a query to find the minimum marks of all the students.

select min(student_marks) from student_info;

Q4) Write a query to find the maximum marks of all the students.

select max(student_marks) from student_info;


Q5) Write a query to find the sum of marks of all the students.

select sum(student_marks) from student_info;


LAB – 13
SQL QUERIES WITH DATES

Q1) Write a query to select all the teachers where joining date is
12-11-2004.

select *from teacher_info;

select* from teacher_info where joining_date='2001-12-12';

Q2) Write a query to select all the teachers who are joining in the
year 2022.

select *from teacher_info;


select* from teacher_info where joining_date='2022-11-11';

Q3) Write a query to select all the faculty who joined the university
at least 5 years ago.

select *from teacher_info;

select* from teacher_info where


datediff(current_date(),joining_date);
LAB – 14
SET OPERATIONS

Q1) Write a query to combine the details of students and CSE


students.

select * from student_info;

select * from studentcse_info;

select* from student_info union select* from studentcse_info;


Q2) Write a query to combine the student_id of students and CSE
students.
select student_id from student_info union select student_id from
studentcse_info;

Q3) What is the difference between union and union all?


Union means joining two or more data sets into a single set. Both
UNION and UNION ALL concatenate the result of two different
tables. They differ in the way they handle duplicates, and in return
of values in sorted or unsorted manner.
UNION performs a DISTINCT on the result set, eliminating any
duplicate rows. UNION returns the values in sorted order.
UNION ALL does not remove duplicates, and it therefore faster than
UNION. UNION ALL does not return the values in sorted order.
For example

select * from student_info;


select * from studentcse_info;

select* from student_info union select* from studentcse_info;


select* from student_info union all select* from studentcse_info;

Q4) Write the properties of all the four set operations.

The SET Operators in MySQL are used to combine the result of


more than 1 set and return the output as a single result set.

In SQL, 4 types of set operators are.


They are as follows:

1. UNION: It is used to combine two or more result sets into a single


set. It removes duplicate values and returns data in sorted order.

2. UNION ALL: It is used to combine two or more result sets into a


single set. It retains the duplicate values and does not return data in
sorted order.

3. INTERSECT: It is used to combine two result sets and returns the


data which are common in both the result set. It removes duplicate
values and returns data in sorted order.
4. MINUS: It is used to combine two result sets and returns the data
from the first result set which is not present in the second result set.
It removes duplicate values and returns data in sorted order.
LAB – 15
SQL QUERIES USING GROUP BY, HAVING, EXISTS

Q1) Write a query to list the number of students in each city.


select count(student_id),city from student_info group by city;

Q2) Write a query to list the number of students in each city sorted
from high to low.

Q3) Write a query to select the student name who has marks
greater than 85
LAB 16
SQL QUERIES FOR ALIASES

Q1) Write a sql query to create aliases for first name and emp_ID.

Q2) Write a sql query to create an alias “name” from Finance table
LAB - 17

JOIN OPERATIONS

Different Types Of SQL Joins


● SQL INNER JOIN creates a result table by combining rows that have
matching values in two or more tables.

● SQL LEFT OUTER JOIN includes in a result table unmatched rows from
the table that is specified before the LEFT OUTER JOIN clause.

● SQL RIGHT OUTER JOIN creates a result table and includes into it all the
records from the right table and only matching rows from the left table.

● SQL SELF JOIN joins the table to itself and allows comparing rows within
the same table.

● SQL CROSS JOIN creates a result table containing a paired combination


of each row of the first table with each row of the second table.

Q1)Inner join.

select* from student_info INNER JOIN course on student_info.


student_id= course.student_id;

Q2)Left join.
select* from student_info as s left join course as c on
s.student_id=c.student_id;
Q3)Right join.
select* from student_info as s right join course as c on
s.student_id=c.student_id;
LAB - 18
SQL QUERIES TO CREATE VIEWS

Advantages of view

• View data without storing the data into an object.

• Gives a personalised view relevant to that user.

• Restrict the access of a table so that nobody can insert the rows
into the table and only specific columns and data will be displayed.

• A view can present a consistent, unchanged image of the


structure of the database, even if the underlying source tables are
split, restructured, or renamed.

1.Write a SQL query to show all female employees from finance


table

2.Write a query to drop the Female_ employees view

You might also like