0% found this document useful (0 votes)
27 views

DBMS - Lab - Sankara Karthik

File

Uploaded by

amitybhavya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

DBMS - Lab - Sankara Karthik

File

Uploaded by

amitybhavya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Database Management Systems

CSE201

Practical file

AMITY SCHOOL OF ENGINEERING AND TECHNOLOGY

AMITY UNIVERSITY, UTTAR PRADESH

Submitted by:
Sankarakarthik
A2305222294
5CSE4Y

Submitted to:
Dr Subhash Chand Gupta
Experiment-1
To examine and evaluate different SQL DDL, DQL, and DML commands

DDL (Data Definition Language):

● DDL queries are used to define or modify database structures, such as tables, schemas, or
indexes.
● Common DDL statements include:
○ CREATE: Used to create objects (tables, databases, etc.).

○ ALTER: Modifies existing database objects.


○ DROP: Deletes objects from the database.
○ TRUNCATE: Removes all records from a table but keeps the structure intact.

DML (Data Manipulation Language):

● DML queries are used for managing data within the database tables.
● Common DML statements include:
○ SELECT: Retrieves data from the database.

○ INSERT: Adds new data to a table.


○ UPDATE: Modifies existing data in a table.
○ DELETE: Removes data from a table.

DQL (Data Query Language):

DQL queries are used for querying and retrieving data from the database tables. Common DQL
statements include:

● SELECT: Retrieves data from one or more tables in the database.

CODE:

DDL: CREATE (Create Table), DML: INSERT (Insert Data)

CREATE TABLE StudentA2305222294 (


StudentID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
DateOfBirth DATE, Major
VARCHAR(50)
);
-- Insert initial 5 records
INSERT INTO StudentA2305222294 (StudentID, FirstName, LastName, DateOfBirth, Major)
VALUES
(1001, 'Sudiksha', 'Gunturi', '2002-05-22', 'CSE'),

(1002, 'Radhika', 'Darpe', '2003-07-14', 'Economics'),


(1003, 'Tarun', 'Digavalli', '2001-11-30', 'Mechanics'),
(1004, 'Priyanshi', 'Sahu', '2002-09-10', 'Civil'),
(1005, 'Ansh', 'Darpe', '2003-03-25', 'CSE'),
(1006, 'Aarav', 'Sharma', '2002-01-18', 'CSE'),
(1007, 'Ishita', 'Verma', '2002-12-05', 'Economics'),
(1008, 'Rohit', 'Mishra', '2001-02-16', 'Civil'),
(1009, 'Shreya', 'Patel', '2002-04-11', 'Physics'),
(1010, 'Arjun', 'Reddy', '2001-06-22', 'Mechanics'),
(1011, 'Meera', 'Singh', '2003-08-15', 'CSE'),
(1012, 'Dev', 'Raj', '2002-10-03', 'Mathematics'),
(1013, 'Nisha', 'Agarwal', '2002-11-19', 'Economics');

SELECT* FROM StudentA2305222294;

OUTPUT:

DML: UPDATE (Update Data)

-- Update the Major of Sudiksha Gunturi


UPDATE StudentA2305222229
SET Major = 'Computer
Science' WHERE StudentID
= 1001;

-- Show the table after updating


SELECT * FROM StudentA2305222294;

DML: DELETE (Delete Data)


-- Delete the record of Tarun Digavalli
DELETE FROM StudentA2305222294 WHERE
StudentID = 1003;

SELECT * FROM StudentA2305222294;

DQL: SELECT (Query Data)


-- Select and show all records of students majoring in 'CSE'
SELECT * FROM StudentA2305222226
WHERE Major = 'CSE';

DDL: ALTER (Modify Table Structure)

-- Add a new column 'Email' to the table


ALTER TABLE StudentA2305222294
ADD Email VARCHAR(100);
SELECT * FROM StudentA2305222294;
DDL: TRUNCATE (Remove All Data)
-- Truncate the table (delete all data but keep the structure)
TRUNCATE TABLE StudentA2305222294;
SELECT * FROM StudentA2305222294;

DDL: DROP (Delete Table)

-- Drop the table (completely remove the table from the database)
DROP TABLE StudentA2305222294;
SELECT * FROM StudentA2305222294;
Experiment -2
Functions in SQL

-- Create the table People_294


CREATE TABLE People_294 (
Number INT PRIMARY KEY, -- Primary Key
Name VARCHAR(20), -- Name of the person
Occupation VARCHAR(20), -- Occupation of the person
Age INT -- Age of the person
);

-- Insert data into the People_294 table


INSERT INTO People_294 (Number, Name, Occupation, Age)
VALUES
(1, 'Sudiksha', 'Student', 22),
(2, 'Hakhilesh', 'Engineer', 28),
(3, 'Bhavnita', 'Doctor', 31),

(4, 'Shivika', 'Artist', 25);

UPPER FUNCTION

● Description: Converts all characters in a string to uppercase.


● Usage: Standardizes text to uppercase.
● Example:
SELECT Name, UPPER(Name) AS UppercaseName
FROM People_294;

LIKE CLAUSE
● Description: Searches for a specified pattern in a column.
● Usage: Used in WHERE clauses to filter results based on patterns.
● Example: SELECT Name, Occupation
● FROM People_294
● WHERE Name LIKE 'S';
CONCAT FUNCTION
● Description: Concatenates two or more strings into one.
● Usage: Joins multiple strings into a single string.
● Example:
● SELECT Name, Occupation, CONCAT(Name, ' - ', Occupation)

AS FullDescription FROM
People_294;

LENGTH / CHAR_LENGTH
● Description: Returns the number of characters in a string.
● Usage: Determines the length of a string.
● Example: SELECT Name, LENGTH(Name) AS NameLength
● FROM People_294;

SUBSTRING / SUBSTR Function


● Description: Extracts a substring from a string. ● Usage: Retrieves a specific portion of a
string.
● Example: SELECT Name, SUBSTRING(Name, 1, 3) AS ShortName
● FROM People_294;
REVERSE FUNCTION
● Description: Reverses the order of characters in a string.
● Usage: Returns the string in reverse order.
● Example:SELECT Name, REVERSE(Name) AS ReversedName FROM People_294;

TRIM FUNCTION
● Description: Removes leading and trailing spaces from a string.
● Usage: Cleans up spaces around the text. ●
Example:
● SELECT Name, TRIM(Name) AS TrimmedName ● FROM
People_294;

COUNT
● Description: Counts the number of rows in the table. ●

● Example:SELECT COUNT(*) AS TotalPeople

FROM People_294;
SUM

● Description: Calculates the total sum of the Age column.


● Example: SELECT SUM(Age) AS TotalAge

FROM People_294;

AVG
● Description: Computes the average age of people.
● Example:SELECT AVG(Age) AS AverageAge

FROM People_294;

MIN
● Description: Returns the smallest age in the table.
● Example:SELECT MIN(Age) AS YoungestAge

FROM People_294;

MAX
● Description: Returns the largest age in the table.
● Example:SELECT MAX(Age)

AS OldestAge FROM People_294;


Experiment-3
Foreign and Primary key

Keys are one of the most important elements in a relational database to maintain the
relationship between the tables and it also helps in uniquely identifying the data from a table.

Primary Key

A primary key is a column (or a combination of columns) in a database table that uniquely
identifies each row in that table. Primary keys must contain unique values and cannot contain
NULL values. Each table can have only one primary key.

Characteristics of Primary Keys:

• Uniqueness: Each value must be unique across the table.


• Non-nullability: No primary key field can be NULL.
• Immutability: The values of a primary key should not change frequently.

Example

Output
Foreign Key

A foreign key is a column (or a combination of columns) in one table that refers to the
primary key in another table. Foreign keys establish a relationship between two tables and
help maintain referential integrity.

Characteristics of Foreign Keys:

• Referential Integrity: A foreign key value must match a primary key value in another
table or be NULL.
• Relationship: They define how data in one table relates to data in another.

Example

Output
All Tables that used in Experiment 4,5,6
Experiment-4
Client product subqueries

Finding the non-moving products i.e. products not being sold.

Output:

b. Finding the name and complete address for the customer who has placed Order
number 'O19001'.

Output:

c. Finding the clients who have placed orders before the month of May'02.

Output:
d. Find out if the product 'Lycra Tops' has been ordered by any client and print the
ClientNo, Name to whom it was sold.

Output:

e. Find the names of clients who have placed orders worth Rs. 10000 or more.

Output:
Experiment-5
Client product group by

Printing the description and total quantity sold for each product.

Output:

b. Finding the value of each product sold.

Output:

c. Calculating the average quantity sold for each client that has a maximum
order value of 15000.00.
Output:

d. Finding out the total of all the billed orders for the month of June.

Output:
Experiment-6
Client Product join

a. Find out the products, which have been sold to 'Ivan Bayross'.

Output:

b. Finding out the products and their quantities that will have to be delivered in
the current month.

Output:
c. Listing the ProductNo and description of constantly sold (i.e. rapidly moving)
products.

Output:

d. Finding the names of clients who have purchased 'Trousers'.

Output:

e. Listing the products and orders from customers who have ordered less than 5
units of 'Pull Overs'.

Output:
f. Finding the products and their quantities for the orders placed by 'Ivan Bayross'
and 'Mamta Muzumdar'.

Output:

g. Finding the products and their quantities for the orders placed by ClientNo
'C00001' and 'C00002'.

Output:
Experiment-7
Given a relational database, create expressions in QBE for each of the SQL queries.
Required Tables
Employee

Works

Company

Manages

i. Modify the databse so that Jones now lives in Newtown.

Updated table
ii. Give all employees of First Bank Corporation a 10% raise.

Updated table

iii. Give all mangers in the database a 10% raise.

Updated table

iv. Give all mangers in the database a 10% raise unless the salary would be greater
than Rs.100,000. In such a case give only a 3% raise.

Updated table
Experiment 8
Construct the SQL queries for the given relational database

Required Tables
Patients

Doctors

Visits

i. Who are the patients 10 years old or younger?

Output:
ii. Who are the surgeons?

Output:

iii. What are the phone numbers of doctors?

Output:

iv. What are the phone numbers of surgeons?

Output:
Experiment 9
Assignment 4
1. Create a database called COMPANY consisting of two tables - EMP & DEPT

EMP
DEPT
2. Perform the following queries on the tables just created:

1. List the names of analysts and salesmen.

Output;
2. List details of employees who have joined before 30 Sep 81.

Output:

3. List names of employees who are not managers.

Output:
4. List the names of employees whose employee numbers are 7369, 7521, 7839,

7934, 7788.

Output:

5. List employees not belonging to department 30, 40, or 10.

Output:

6. List employee names for those who have joined between 30 June and 31 Dec. ‘81.

Output:
7. List the different designations in the company.

Output:

8. List the names of employees who are not eligible for commission.

Output:

9. List the name and designation of the employee who does not report to anybody.

Output:
10. List the employees not assigned to any department.

Output:

11. List the employees who are eligible for commission.

Output:

12. List employees hose names either start or end with “S”.

Output:

13. List names of employees whose names have “i” as the second character.

Output:
14. List the number of employees working with the company.

Output:

15. List the number of designations available in the EMP table.

Output:

16. List the total salaries paid to the employees.

Output:

17. List the maximum, minimum and average salary in the company.

Output:

18. List the maximum salary paid to a salesman.

Output:
Experiment 10
Company Assignment

All Tables used in this Experiment

Employee Company

Works

Manages
a. Find the names, street address, and cities of residence for all employees who work for
'First Bank Corporation' and earn more than $10,000.

Output

b. Find the names of all employees in the database who live in the same cities as the
companies for which they work.

Output

c. Find the average salary company wise and display it with the heading “Average
Salary”.
Output

d. Find the names of all employees in the database who live in the same cities and on
the same streets as do their managers.

Output

e. Find the names of all employees in the database who do not work for 'First Bank
Corporation‘

Output
f. Find the names of all employees in the database who earn more than every employee
of 'Small Bank Corporation'. Assume that all people work for at most one company.

Output

g. Find the names, street address, and cities of residence for all employees who work for
'First Bank Corporation' and earn more than $10,000 and less than $20,000.

Output
h. Find the names of all employees in the database who live in the same cities as the
companies for which they work.

Output

i. Find the names of all employees in the database who earn more than every employee
of 'Small Bank Corporation'. Assume that all people work for at most one company.

Output

You might also like