Data Base Management System Lab4

You might also like

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

DATA BASE MANAGEMENT SYSTEM LAB FILE

Database
A database management system (DBMS) is a software application
that enables users to efficiently store, organize, manage, and retrieve
vast amounts of data. It provides a systematic approach to handling
data by acting as an intermediary between the user and the underlying
database, allowing for convenient data manipulation, storage
optimization, data security, and data integrity. DBMSs offer a wide
range of functionalities such as data modeling, query processing,
transaction management, and user access control, enabling businesses
and organizations to effectively handle their data needs and make
informed decisions based on the stored information.
Some popular examples of DBMS include Oracle Database, MySQL,
Microsoft SQL Server, PostgreSQL, and MongoDB.

SQL
SQL stands for Structured Query Language. It is a standard
programming language used for managing and manipulating
relational databases. SQL provides a set of commands and syntax that
allows users to interact with the database, perform various operations,
and retrieve information.
SQL uses commands like SELECT, INSERT, UPDATE, and DELETE
to perform these operations. You can use SQL to create tables that
define the structure of the data, specify conditions to filter the data
you want to retrieve, and make changes to the data as needed.
It's important to note that SQL is a standardized language, but
different database management systems may have their own specific
variations and additional features.
However, the core concepts and syntax of SQL remain consistent
across different systems.

AMAN SAXENA 1 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

SQL PROCESSING
When we are executing an SQL command for any RDBMS, the
system determines the best way to carry out your request and SQL
engine figures out how to interpret the task.
There are various components included in this process. These
components are −
• Query Dispatcher
• Optimization Engines
• Classic Query Engine
• SQL Query Engine, etc.
A classic query engine handles all the non-SQL queries, but a SQL
query engine won't handle logical files. Following is a simple diagram
showing the SQL Architecture -

AMAN SAXENA 2 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

Types of SQL
There are several types or categories of SQL that serve different
purposes or work with specific types of databases. Here are some
common types of SQL:
1. Data Definition Language (DDL): DDL SQL statements are used to
define and manage the structure of the database objects. They include
commands such as CREATE, ALTER, and DROP, which are used to
create, modify, or delete tables, indexes, views, and other database
objects.
2. Data Manipulation Language (DML): DML SQL statements are
used to manipulate the data stored in the database. They include
commands such as SELECT, INSERT, UPDATE, and DELETE.
These statements are used to retrieve, add, modify, or remove data
from the database tables.
3. Data Control Language (DCL): DCL SQL statements are used to
manage the security and access privileges of the database. They
include commands such as GRANT and REVOKE, which grant or
revoke permissions to users and control their access to database
objects.
4. Transaction Control Language (TCL): TCL SQL statements are
used to manage transactions within a database. They include
commands such as COMMIT, ROLLBACK, and SAVEPOINT. These
statements allow you to control the atomicity, consistency, isolation,
and durability (ACID properties) of database transactions.
5. Data Query Language (DQL): DQL SQL statements are primarily
focused on retrieving and querying data from the database. The most
commonly used DQL statement is SELECT, which allows you to
retrieve specific data based on specified conditions and perform
complex queries using joins, filtering, sorting, and aggregating.
6. Data Definition Extensions (DDE): DDE SQL statements are
specific to certain database systems and provide extensions beyond

AMAN SAXENA 3 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

the standard DDL statements. These extensions allow you to define


additional properties, constraints, or behaviors for database objects.

Trigger in SQL
A trigger in MySQL is a set of SQL statements that reside in a system
catalog. It is a special type of stored procedure that is invoked
automatically in response to an event. Each trigger is associated with
a table, which is activated on any DML statement such as INSERT,
UPDATE, or DELETE.
A trigger is called a special procedure because it cannot be called
directly like a stored procedure. The main difference between the
trigger and procedure is that a trigger is called automatically when a
data modification event is made against a table. In contrast, a stored
procedure must be called explicitly.
Generally, triggers are of two types according to the SQL standard:
row-level triggers and statement-level triggers.
Row-Level Trigger: It is a trigger, which is activated for each row by
a triggering statement such as insert, update, or delete. For example, if
a table has inserted, updated, or deleted multiple rows, the row trigger
is fired automatically for each row affected by the insert, update,
or delete statement.
Statement-Level Trigger: It is a trigger, which is fired once for each
event that occurs on a table regardless of how many rows are inserted,
updated, or deleted.

AMAN SAXENA 4 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

Types of Triggers in MySQL


We can define the maximum six types of actions or events in the form
of triggers:
1. Before Insert: It is activated before the insertion of data into the
table.
2. After Insert: It is activated after the insertion of data into the
table.
3. Before Update: It is activated before the update of data in the
table.
4. After Update: It is activated after the update of the data in the
table.
5. Before Delete: It is activated before the data is removed from
the table.
6. After Delete: It is activated after the deletion of data from the
table.
When we use a statement that does not use INSERT, UPDATE or
DELETE query to change the data in a table, the triggers associated
with the trigger will not be invoked.

AMAN SAXENA 5 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

Q)Write a SQL query to calculate total marks and


percentage of students table by using TRIGGER.

AMAN SAXENA 6 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

MY SQL JOINS
MySQL JOINS are used with SELECT statement. It is used to
retrieve data from multiple tables. It is performed whenever you need
to fetch records from two or more tables.
There are three types of MySQL joins:
o MySQL INNER JOIN (or sometimes called simple join)
o MySQL LEFT OUTER JOIN (or sometimes called LEFT JOIN)
o MySQL RIGHT OUTER JOIN (or sometimes called RIGHT
JOIN)

MySQL Inner JOIN (Simple Join)


The MySQL INNER JOIN is used to return all rows from multiple
tables where the join condition is satisfied. It is the most common type
of join.
Syntax:
1. SELECT columns
2. FROM table1
3. INNER JOIN table2
4. ON table1.column = table2.column;

MySQL Left Outer Join


The LEFT OUTER JOIN returns all rows from the left hand table
specified in the ON condition and only those rows from the other table
where the join condition is fulfilled.

AMAN SAXENA 7 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

Syntax:

1. SELECT columns
2. FROM table1
3. LEFT [OUTER] JOIN table2
4. ON table1.column = table2.column;

MySQL Right Outer Join


The MySQL Right Outer Join returns all rows from the RIGHT-hand
table specified in the ON condition and only those rows from the
other table where he join condition is fulfilled.

Syntax:

1. SELECT columns
2. FROM table1
3. RIGHT [OUTER] JOIN table2
4. ON table1.column = table2.column;

AMAN SAXENA 8 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

Q) Write a SQL query to perform Join:-

AMAN SAXENA 9 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

QUERIES
• Write a SQL query to create and use a database:-

• Write a SQL query to create a table:-

AMAN SAXENA 10 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

• Write a SQL query to insert values in the table:-

• Write a SQL query to show the table:-

AMAN SAXENA 11 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

• Write a query in SQL to add one more coloumn:-

• Write a SQL query to update :-

AMAN SAXENA 12 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

• Write a SQL query to count total number of employees :-

• Write a SQL query to calculate total salary:-

• Write a SQL query to find the maximum salary:-

AMAN SAXENA 13 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

• Write a SQL query to find Employees whose name start with


“A”:-

• Write a SQL query to find Employees whose name ends with


“A”:-

AMAN SAXENA 14 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

• Write a SQL query to calculate total average:-

• Write a SQL query for increment of 10% in salary:-

AMAN SAXENA 15 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

• Write a SQL query for decrement 500 rupees from salary:-

• Write a SQL query for deleting one row:-

AMAN SAXENA 16 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

Projects
Project-1
Library Management System:-Create the following schema, enter at
least 5 records in each table and answer the queries given below.
LibraryBooks (Accession number, Title, Author, Department,
PurchaseDate, Price) IssuedBooks (Accession number, Borrower)
a) Identify primary and foreign keys. Create the tables and insert at
least 5 records in each table.

AMAN SAXENA 17 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

b) Delete the record of book titled “Database System Concepts”.

AMAN SAXENA 18 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

c) Change the Department of the book titled “Discrete Mathematics”


to “CSE”.

d) List all books that belong to “CSE” department.

e) List all books that belong to “CSE” department and are written by
author “Navathe”.

AMAN SAXENA 19 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

f) List all computer (Department=”CSE”) that have been issued.

g) List all books which have a price less than 500 or purchased
between “01/01/2015” and “01/01/2019”.

*********************************************

AMAN SAXENA 20 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

Project-2
Write SQL queries to create three different tables named Salesman,
Customer and Orders respectively with the data given and then
perform the given queries:-
a)Write a SQL query to create table Salesman:-

b)Write a SQL query to create table Customer:-

AMAN SAXENA 21 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

c)Write a SQL query to create table Orders:-

Q1.Write a SQL query to find the salespeople and customers who live
in the same city. Return customer name, salesperson name and
salesperson city.

AMAN SAXENA 22 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

Q2. Write a SQL query to locate all the customers and the salesperson
who works for them. Return customer name, and salesperson name.

Q3. Write a SQL query to find those salespeople who generated


orders for their customers but are not located in the same city. Return
ord_no, cust_name, customer_id (orders table), salesman_id (orders
table)

Q4. Write a SQL query to locate the orders made by customers.


Return order number, customer name.

AMAN SAXENA 23 2111730580011


DATA BASE MANAGEMENT SYSTEM LAB FILE

Q5. write a SQL query to find those customers where each customer
has a grade and is served by a salesperson who belongs to a city.
Return cust_name as "Customer", grade as "Grade".

Q6. Write a SQL query to find all orders executed by the salesperson
and ordered by the customer whose grade is greater than or equal to
200. Compute purch_amt*commission as “Commission”. Return
customer name, commission as “Commission%” and Commission.

*********************************************

AMAN SAXENA 24 2111730580011

You might also like