You are on page 1of 11

DEV BHOOMI UTTARAKHAND UNIVERSITY

INDEX

Page.no Date Signature


S.NO Name of the experiment

1 DDL Commands - Table Creation, Altering the table


structures, truncating a table and dropping a table.
2 DML Commands - Insert, Select Commands, update &
delete Commands.
3 Practice Queries using COUNT, SUM, AVG, MAX, MIN,
GROUP BY, HAVING, VIEWS Creation and Dropping.
4 Creating relationship between the databases - Nested
Queries & Join Queries
5 Creating a database and to set various possible
constraints.
6 Implementation of foreign key on database.
7 Views Create a Virtual table (Views) based on the result
set of an SQL statement.
8 To create PL/SQL functions and to implement the stored
procedures in SQL (Function and Procedures).
9 Cursors- Declaring Cursor, Opening Cursor, Fetching the
data, closing the cursor.
10 Practicing on Triggers - creation of trigger, Insertion
using trigger, Deletion using trigger, updating using.

EXPERIMENT NO.: 01
DEV BHOOMI UTTARAKHAND UNIVERSITY
Title of Experiment: DDL Commands - Table Creation, Altering the table structures,
truncating a table and dropping a table.
Program: Course: Advance Database Management System Semester:
B.TECH CSE IV
Code:21BTCS482
Page no: 1

Experiment: - 1. DDL Commands - Table Creation, Altering the table structures, truncating a
table and dropping a table.
Solution:
 Table Creation:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100) UNIQUE,
HireDate DATE
);

 Altering Table Structures:

ALTER TABLE Employees


ADD MiddleName VARCHAR(50);

ALTER TABLE Employees


ALTER COLUMN HireDate DATETIME;

ALTER TABLE Employees


DROP COLUMN MiddleName;

ALTER TABLE Employees


ADD CONSTRAINT CK_EmployeeAge CHECK (DATEDIFF(YEAR, HireDate, GETDATE()) >= 18);

 Truncating a Table:

TRUNCATE TABLE Employees;

 Dropping a Table:

DROP TABLE Employees;

EXPERIMENT NO.: 02
DEV BHOOMI UTTARAKHAND UNIVERSITY
Title of Experiment: DML Commands - Insert, Select Commands, update & delete
Commands.
Program: Course: Advance Database Management System Semester:
B.TECH CSE IV
Code:21BTCS482
Page no: 2
Experiment: - 2. DML Commands - Insert, Select Commands, update & delete Commands.
Solution:

 Insert Command:
INSERT INTO Employees (FirstName, LastName, Email, HireDate)
VALUES ('John', 'Doe', 'john.doe@example.com', '2023-01-15');
```

 Select Command

SELECT FirstName, LastName, Email


FROM Employees
WHERE HireDate > '2023-01-01'
ORDER BY LastName ASC;
```

 Update Command:

UPDATE Employees
SET FirstName = 'Jane', LastName = 'Doe'
WHERE EmployeeID = 1;
```

 Delete Command:

DELETE FROM Employees


WHERE EmployeeID = 1;
DEV BHOOMI UTTARAKHAND UNIVERSITY
EXPERIMENT NO.: 03
Title of Experiment: Practice Queries using COUNT, SUM, AVG, MAX, MIN, GROUP BY,
HAVING, VIEWS Creation and Dropping.
Program: Course: Advance Database Management System Semester:
B.TECH CSE IV
Code:21BTCS482
Page no: 3

Experiment: - 3. Practice Queries using COUNT, SUM, AVG, MAX, MIN, GROUP BY, HAVING,
VIEWS Creation and Dropping.
Solution:

 Using COUNT, SUM, AVG, MAX, MIN with GROUP BY:

SELECT department_id,
COUNT(*) AS num_employees,
SUM(salary) AS total_salary,
AVG(salary) AS avg_salary,
MAX(salary) AS max_salary,
MIN(salary) AS min_salary
FROM employees
GROUP BY department_id;

 Using HAVING to filter aggregated results:

SELECT department_id,
COUNT(*) AS num_employees,
AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id
HAVING AVG(salary) > 50000;

 Creating a View:

CREATE VIEW high_salary_employees AS


SELECT * FROM employees WHERE salary > 80000;

 Dropping a View:

DROP VIEW high_salary_employees;


DEV BHOOMI UTTARAKHAND UNIVERSITY
EXPERIMENT NO.: 04
Title of Experiment: Creating relationship between the databases - Nested Queries & Join
Queries
Program: Course: Advance Database Management System Semester:
B.TECH CSE IV
Code:21BTCS482
Page no: 4
Experiment: - 4. Creating relationship between the databases - Nested Queries & Join Queries
Solution:

 Join Queries:

Join queries are used to retrieve data from multiple tables based on a related column between
them. There are different types of joins such as INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.
Here's an example:

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate


FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

 Nested Queries:

Nested queries, also known as subqueries, are queries within another query. They are used to
retrieve data based on conditions that involve the results of another query. Here's an example:

SELECT CustomerName
FROM Customers
WHERE CustomerID IN (SELECT CustomerID FROM Orders);
DEV BHOOMI UTTARAKHAND UNIVERSITY
EXPERIMENT NO.: 05
Title of Experiment: Creating a database and to set various possible constraints.
Program: Course: Advance Database Management System Semester:
B.TECH CSE IV
Code:21BTCS482
Page no: 5
Experiment: - 5. Creating a database and to set various possible constraints.
Solution:

-- Create the Company database


CREATE DATABASE Company;
USE Company;

-- Create the Departments table with constraints


CREATE TABLE Departments (
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(100) NOT NULL,
Location VARCHAR(100),
CONSTRAINT UC_DepartmentName UNIQUE (DepartmentName)
);

-- Create the Employees table with constraints


CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Email VARCHAR(100) UNIQUE,
DepartmentID INT,
HireDate DATE NOT NULL,
Salary DECIMAL(10, 2) DEFAULT 0,
CONSTRAINT FK_DepartmentID FOREIGN KEY (DepartmentID) REFERENCES
Departments(DepartmentID)
);
DEV BHOOMI UTTARAKHAND UNIVERSITY

EXPERIMENT NO.: 06
Title of Experiment: Implementation of foreign key on database.
Program: Course: Advance Database Management System Semester:
B.TECH CSE IV
Code:21BTCS482
Page no: 6
Experiment: - 6. Implementation of foreign key on database.
Solution:

-- Create the Customers table


CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(255) NOT NULL,
ContactName VARCHAR(255),
Country VARCHAR(100)
);

-- Create the Orders table with a foreign key constraint


CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
OrderDate DATE,
CustomerID INT,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
DEV BHOOMI UTTARAKHAND UNIVERSITY
EXPERIMENT NO.: 07
Title of Experiment: Views Create a Virtual table (Views) based on the result set of an SQL
statement.
Program: Course: Advance Database Management System Semester:
B.TECH CSE IV
Code:21BTCS482
Page no: ¼
Experiment: - 7Views Create a Virtual table (Views) based on the result set of an SQL
statement.
Solution:
DEV BHOOMI UTTARAKHAND UNIVERSITY
EXPERIMENT NO.: 08
Title of Experiment: To create PL/SQL functions and to implement the stored procedures
in SQL (Function and Procedures).
Program: Course: Advance Database Management System Semester:
B.TECH CSE IV
Code:21BTCS482
Page no: ¼
Experiment: - 8. To create PL/SQL functions and to implement the stored procedures in SQL
(Function and Procedures).
Solution:
DEV BHOOMI UTTARAKHAND UNIVERSITY
EXPERIMENT NO.: 09
Title of Experiment: Cursors- Declaring Cursor, Opening Cursor, Fetching the data, closing
the cursor.
Program: Course: Advance Database Management System Semester:
B.TECH CSE IV
Code:21BTCS482
Page no: ¼
Experiment: - 9. Cursors- Declaring Cursor, Opening Cursor, Fetching the data, closing the
cursor.
Solution:
DEV BHOOMI UTTARAKHAND UNIVERSITY
EXPERIMENT NO.: 10
Title of Experiment: Practicing on Triggers - creation of trigger, Insertion using trigger,
Deletion using trigger, updating using.
Program: Course: Advance Database Management System Semester:
B.TECH CSE IV
Code:21BTCS482
Page no: ¼
Experiment: - 10. Practicing on Triggers - creation of trigger, Insertion using trigger, Deletion
using trigger, updating using.
Solution:

You might also like