DBMS Lab Report
DBMS Lab Report
Part-
1
Sl Topic Page
01. University Database
Part-2
Sl Name of experiment
01. Write SQL queries using integrity constrains to create tables
for a database.
02. Write SQL queries to insert values into tables in the
university database.
03 Write SQL query using insert and delete, drop table,
alter table command.
04 Write a query for searching an attribute.
Full Schema
Experiment No: 01
Experiment Name: Write SQL queries using integrity constraints to create
tables for a database.
Introduction:
Data integrity is the cornerstone of a reliable database. To ensure
consistency and enforce business rules, we turn to integrity constraints,
acting as watchful guardians ensuring data's accuracy and validity. This
exploration dives into writing SQL queries incorporating such constraints
to create robust and reliable tables, laying the groundwork for a healthy
database.
Program Code:
create table
classroom( buildin
g varchar (15),
room_number varchar (7),
capacity numeric (4,0),
primary key (building, room_number));
create table
department( dept_na
me varchar (20),
building varchar (15),
budget numeric (12,2),
primary key
(dept_name));
create table
course( course_id
varchar (7),
title varchar (50),
dept_name varchar (20),
credits numeric (2,0),
primary key
(course_id));
create table
instructor( ID
varchar (5),
name varchar (20) not
null, dept_name
varchar (20),
salary numeric (8,2),
primary key (ID));
create table
section( course_id
varchar (8),
sec_id varchar (8),
semester varchar (6),
year1 numeric (4,0),
building varchar (15),
room_number varchar (7),
time_slot_id varchar (4),
primary key (course_id, sec_id, semester, year1));
create table
teaches( ID
varchar (5),
course_id varchar (8),
sec_id varchar (8),
semester varchar (6),
year1 numeric (4,0),
primary key (ID, course_id, sec_id, semester, year1));
create table student(
ID varchar (5),
name varchar (20),
dept_name varchar (20),
tot_cred numeric (3,0),
primary key (ID));
create table
takes( ID
varchar (5),
course_id varchar (8),
sec_id varchar (8),
semester varchar (6),
year1 numeric (4,0),
grade varchar (2),
primary key (ID, course_id, sec_id, semester, year1));
create table
prereq( course_id
varchar(8),
prereq_id varchar(8),
primary key (course_id, prereq_id));
create table
timeslot( time_slot_id
varchar (4),
day varchar (1),
start_hr numeric (2),
start_min numeric (2),
end_hr numeric (2),
end_min numeric (2),
primary key (time_slot_id, day, start_hr, start_min));
Output:
Integrity constraints are used to create tables for university database.
Discussion:
We have successfully used integrity constraints and created the
necessary tables using SQL with the help of XAMPP software. The create
table keyword is used to create the tables or relations required for the
university database. The integrity constraints specifiy the attributes value.
Experiment No: 02
Experiment Name: Write SQL queries to insert values into tables in the
university database.
Introduction:
One of the most common operations in SQL is inserting data into tables.
We can use the INSERT INTO statement to add new records to a table. We
can specify the column names and the values to be inserted, or we can
omit the column names if we are inserting values for all the columns in
the same order as they are defined in the table.We can also insert
multiple records in one statement by using a comma-separated list of
values, or we can insert data from another table or a subquery by using
the INSERT INTO SELECT statement.
Program Code:
INSERT INTO classroom (building, room_number, capacity)
VALUES
('Packard', 101, 500),
('Painter', 514, 10),
('Taylor', 3128, 70),
('Watson', 100, 30),
('Watson', 120, 50);
Output:
Classroom:
Instructor:
Section:
Discussion: We have successfully insert values into tables using SQL with
the help of XAMPP software. Here we apply SQL query for insert the value
of the attribute of the table of student database. For this we use INSERT
INTO key ward and write the table name then we use VALUES key ward to
give the value of the field of the table.
Experiment No: 03
Experiment Name: Write SQL query using insert and delete, drop table,
alter table command.
Introduction:
The insert command is used to add new records to a table. We can specify
the column names and the values to be inserted, or we can omit the
column names if we are inserting values for all the columns in the same
order as they are defined in the table.The delete command is used to
remove existing records from a table.We can specify a condition to
delete only the records that match the condition, or we can omit the
condition to delete all the records from the table.The drop table command
is used to delete a table and all its data from the database. We can
specify the name of the table to be deleted. Be careful before using this
command, as it will result in loss of all information stored in the table.The
alter table command is used to add, modify, or delete columns or
constraints in an existing table.We can specify the name of the table and
the column or constraint to be added, modified, or deleted. We can also
use multiple alter table commands in one statement by separating them
with commas.
Program Code:
MySQL returned an empty result set (i.e. zero rows). (Query took 0.0010
seconds.)
DROP TABLE teaches;
MySQL returned an empty result set (i.e. zero rows). (Query took 0.0009
seconds.)
ALTER TABLE student ADD Height NUMERIC (2,0);
MySQL returned an empty result set (i.e. zero rows). (Query took 0.0010
seconds.)
ALTER TABLE student DROP height;
Discussion: We have successfully used the insert and delete, drop table,
alter table command using SQL with the help of XAMPP software. Here we
apply SQL query for insert the value, delete the of the attribute of the
table of student database. For this we use INSERT INTO keyword and write
the table name then we use VALUES keyword to give the value of the
field of the table. We use DELETE keyword to delete all the record of the
table. We used DROP keyword to dropped the table from the database.
Experiment No: 04
Experiment Name:
Write a query for searching an attribute.
Introduction:
Compose and execute a structured query (SELECT attribute FROM table;)
to search and retrieve specific attributes from designated table,
contributing to the exploration and analysis of relational database data.
Program
Code: SELECT
name FROM
student;
SELECT
dept_name FROM
instructor;
Output:
Discussion:
The first query fetches names from the student table, and the second
retrieves department names from the instructor table, aiding in data
exploration and analysis.
Experiment No: 05
Experiment Name:
Write queries by implementing the ‘distinct’ and ‘all’ord.
Introduction:
In this context, the focus is on crafting SQL queries with the integration of
the 'DISTINCT' and 'ALL' keywords. These keywords play a crucial role in
fine-tuning query results. 'DISTINCT' ensures unique values, while 'ALL'
includes all values, offering versatility in data retrieval and analysis.
Program Code:
SELECT DISTINCT dept_name
FROM instructor;
Output:
Discussion:
Successfully employed SQL queries with 'DISTINCT' and 'ALL' keywords
using XAMPP. 'DISTINCT' ensured unique values, eliminating duplicates for
clarity. 'ALL' facilitated comprehensive data retrieval, showcasing the
versatility of these keywords. This implementation on XAMPP underscores
enhanced query precision and adaptability in tailoring results.
Experiment No: 06
Experiment Name:
Write queries using arithmetic, logical and relational operator.
Instruction:
This exploration delves into crafting SQL queries by leveraging the power
of arithmetic, logical, and relational operators. By incorporating these
operators, users can perform calculations, create logical conditions, and
establish relationships between data, enabling precise and dynamic data
retrieval from relational databases.
Program Code:
SELECT ID, name, dept_name, salary *
1.1 FROM instructor;
SELECT name
FROM
instructor
WHERE dept_name = 'Comp. Sci.' AND salary > 70000;
SELECT name,
course_id FROM
instructor, teaches
WHERE instructor.ID = teaches.ID AND instructor.dept_name = 'Comp. Sci.';
Output:
Discussion: We have successfully use arithmetic, logical and relational
operator using SQL with the help of XAMPP software.
Experiment No: 07
Experiment Name:
Write queries on multiple relation and the using of ‘natural join’ keyword..
Introduction:
Compose queries for multiple relations by employing the 'natural join'
keyword to seamlessly combine tables with matching columns. Specify
common attributes to link tables effortlessly, simplifying complex
database retrievals. Enhance data retrieval efficiency by leveraging the
inherent relationships between tables through the natural join operation
in your SQL queries.
Program Code:
SELECT name,
course_id FROM
instructor, teaches
WHERE instructor.ID = teaches.ID;
Output:
Discussion: We've effectively harnessed the power of SQL and XAMPP
software to implement 'natural join' in multiple relations. NATURAL JOIN
streamlines data integration by focusing on shared attributes in the
schemas of related tables. This synergy enhances precision in tuple
pairing, optimizing our database queries for seamless and efficient data
retrieval.
Experiment No: 08
Experiment Name: Write queries using renaming (‘as’ clause) operation.
Program Code:
SELECT name,
course_id FROM
instructor, teaches
WHERE instructor.ID = teaches.ID;
Experiment No:09
Experiment Name:
Write queries using String operation, attribute specification and ‘order by’ clause.
Program Code:
SELECT dept_name FROM department
WHERE building LIKE '%Watson%';
SELECT instructor.*
FROM instructor,
teaches
WHERE instructor.ID = teaches.ID;
SELECT name
FROM
instructor
WHERE dept_name =
'Physics' ORDER BY name;
SELECT *
FROM instructor
ORDER BY salary DESC, name ASC;
Output:
Discussion:
We adeptly employed SQL's String operations, attribute specification, and
'ORDER BY' clause via XAMPP, optimizing queries. Leveraging functions
like CONCAT, we manipulated strings, specified attributes, and organized
results for enhanced data presentation, showcasing the versatility of SQL
in conjunction with XAMPP.
Experiment No:10
Experiment Name:Write queries using ‘between’ keyword and comparison
operation.
Instruction:
Leverage SQL's 'BETWEEN' keyword and comparison operations to refine
queries. This dynamic duo enables precise data retrieval by specifying a
range of values or conditions, ensuring tailored results that meet specific
criteria within a defined parameter range.
Program Code:
SELECT name
FROM
instructor
WHERE salary BETWEEN 90000 AND 100000;
SELECT name,
course_id FROM
instructor, teaches
WHERE instructor.ID = teaches.ID AND dept_name = 'Biology';
SELECT name,
course_id FROM
instructor, teaches
WHERE (instructor.ID, dept_name) =(teaches.ID, 'Biology');
Output:
Discussion:
We've seamlessly harnessed the power of SQL's 'BETWEEN' keyword and
comparison operations through XAMPP, refining data retrieval. This
dynamic combination allows us to specify ranges and conditions, ensuring
precise results tailored to our criteria. The synergy of SQL and XAMPP
enhances our ability to extract relevant information efficiently.
Experiment No:
11 Experiment
Name:
Write queries using Set operation.
Introduction:
Compose SQL queries employing set operations, such as UNION or
INTERSECT, to combine or compare data from multiple tables. For
instance, use SELECT column FROM table1 UNION SELECT column FROM
table2; to merge distinct values.
Program Code:
SELECT
course_id FROM
section
WHERE semester = 'Fall' AND YEAR = 2017;
SELECT
course_id FROM
section
WHERE semester = 'Spring' AND year = 2018;
Union operation:
(SELECT
course_id FROM
section
WHERE semester = "Fall" AND year=
2017) UNION
(SELECT
course_id FROM
section
WHERE semester = "Spring" AND year= 2018);
Intersect operation:
(SELECT
course_id FROM
section
WHERE semester = "Fall" AND year= 2009)
INTERSECT
(SELECT
course_id FROM
section
WHERE semester = "Spring" AND year= 2010);
Except operation:
(SELECT
course_id FROM
section
WHERE semester = "Fall" AND year=
2009) EXCEPT
(SELECT course_id
FROM section WHERE semester = "Spring" AND year= 2010);
Output:
Discussion:
Set operations in SQL enhance data manipulation. The provided query
combines unique values from 'table1' and 'table2,' showcasing how set
operations enable powerful data synthesis or comparison, crucial for
diverse analytical tasks in a laboratory environment.
Experiment No:12
Experiment Name:Write queries using Aggregate function.
Introduction:
Aggregate functions in SQL empower users to extract valuable insights
from data by performing operations like sum, average, count, and more.
This set of queries focuses on harnessing the power of aggregate
functions to analyze and summarize information, providing a
comprehensive view of the data stored in a relational database.
Program Code:
select avg (salary) from
instructor where dept_name=
"Comp. Sci.";
Output:
Introduction:
This experiment aims to develop SQL queries to implement set
membership, set comparison, and test for empty relationships. By
exploring these operations, participants will gain a deeper understanding
of how SQL handles set-related tasks, enabling them to efficiently manage
and analyze data within relational databases.
Program Code:
select distinct course_id from section
where semester = "Fall" and year= 2017 and course_id
in (select course_id from section where semester = "Spring" and year= 2018);
year= 2018); select distinct name from instructor where name not in
("Mozart", "Einstein");
select dept_name
from instructor group by dept_name
having avg (salary) >= all (select avg (salary) from instructor group by dept_name);
Discussion:
The successful utilization of set membership, set comparison, and testing
for empty relationships in SQL, facilitated by XAMPP software, underscores
the versatility of these operations in database management.
Experiment No:14
Experiment Name: Write queries to delete, insert and update data into relationship.
Introduction:
The primary goal of this experiment is to delve into the practical aspects
of database management, focusing on the crucial operations of deleting,
inserting, and updating data within a relational database. Through these
actions, participants will gain valuable insights into the dynamic nature of
database manipulation and the nuances of executing SQL commands
effectively.
Program Code:
delete from instructor
where dept_name=
"Finance"; delete from
instructor
where salary between 13000 and 15000;
update instructor set salary = salary * 1.03 where salary > 100000;
update instructor set salary = salary * 1.05 where salary <= 100000;
Output:
insert into instructor select ID, name, dept_name, 18000 from student
where dept_name = "Music" and tot_cred > 144;
update instructor set salary = salary * 1.05 where salary < 70000;
update instructor set salary = salary * 1.05 where salary < (select
avg (salary) from instructor);
update instructor set salary = salary * 1.03 where salary > 100000;
update instructor set salary = salary * 1.05 where salary <= 100000;
Discussion:
We've successfully executed operations to delete, insert, and update data
within a relational database using SQL through XAMPP. Leveraging
commands like INSERT,
DELETE, and UPDATE, we manipulated data effectively. For insertion, we
utilized the FROM clause, employed DELETE with WHERE conditions, and
updated records using the UPDATE statement with specified conditions for
precision.
Experiment No:15
Introduction:
In this experiment, the focus is on mastering the art of SQL queries
through the exploration of different join types. By delving into INNER,
LEFT, RIGHT, and FULL joins, participants aim to enhance their skills in
crafting precise and versatile queries, gaining a comprehensive
understanding of how to retrieve and combine data effectively from
diverse tables in a relational database.
Program Code:
Select * from student join takes on student.ID=
takes.ID; Select * from student, takes where
student.ID = takes.ID; Select * from student natural
left outer join takes;
Select ID from student natural left outer join takes where
course_id is null; Select * from takes natural right outer join
student;
Output:
Discussion:
Successful implementation of diverse SQL joins in this experiment
underscores their pivotal role in data retrieval. By mastering INNER, LEFT,
RIGHT, and FULL joins, participants gain a nuanced understanding of
combining information from related tables. This proficiency enhances
query flexibility and precision, crucial in database management.