0% found this document useful (0 votes)
32 views8 pages

Lab Manual

Hyinnbjjhjj

Uploaded by

varnikhasree057
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)
32 views8 pages

Lab Manual

Hyinnbjjhjj

Uploaded by

varnikhasree057
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/ 8

24UDSMP202 LAB MANUAL DSC PRACTICAL IV: DBMS LAB

1. Data Definition Language (DDL) Commands for Base Tables and Views

Aim:
To understand and practice Data Definition Language (DDL) commands for creating base tables
and views in SQL.

Algorithm:

1. Use the CREATE TABLE command to define the structure of a base table.
2. Use the CREATE VIEW command to define a virtual table based on a query.
3. Use the ALTER TABLE command to modify an existing table.
4. Use the DROP TABLE and DROP VIEW commands to remove tables and views.

Program:

-- Create Base Table


CREATE TABLE Employee (
EmpID INT PRIMARY KEY,
EmpName VARCHAR(50),
Salary DECIMAL(10, 2)
);

-- Create View
CREATE VIEW EmployeeView AS
SELECT EmpID, EmpName
FROM Employee WHERE Salary > 5000;

-- Alter Table (Add a new column)


ALTER TABLE Employee ADD Department VARCHAR(30);

-- Drop Table
DROP TABLE Employee;

-- Drop View
DROP VIEW EmployeeView;

Output:

● Table Employee created with columns: EmpID, EmpName, Salary.


● View EmployeeView created displaying Employee ID and Name for those with salary
greater than 5000.
● Department column added to the Employee table.

Result:
Successfully demonstrated DDL commands like CREATE, ALTER, and DROP for base tables and
views.
24UDSMP202 LAB MANUAL DSC PRACTICAL IV: DBMS LAB

2. Data Manipulation Language (DML) Commands for Base Tables and Views

Aim:
To practice Data Manipulation Language (DML) commands for inserting, updating, and deleting
records in base tables and views.

Algorithm:

1. Use INSERT INTO to insert new records into the table.


2. Use UPDATE to modify existing records in the table.
3. Use DELETE to remove records from the table.
4. Use SELECT to retrieve data from a base table or view.

Program:

-- Inserting records into Employee table


INSERT INTO Employee (EmpID, EmpName, Salary) VALUES (101, 'John', 6000);
INSERT INTO Employee (EmpID, EmpName, Salary) VALUES (102, 'Alice', 4500);

-- Updating records in Employee table


UPDATE Employee SET Salary = 7000 WHERE EmpID = 101;

-- Deleting records from Employee table


DELETE FROM Employee WHERE EmpID = 102;

-- Select from View


SELECT * FROM EmployeeView;

Output:

● Records inserted and updated successfully.


● After deleting, only one record (John) remains in the Employee table.
● SELECT * retrieves data from the EmployeeView.

Result:
Successfully executed DML commands like INSERT, UPDATE, DELETE, and SELECT.

3. Creating an Employee Database and Setting Various Constraints

Aim:
To create an Employee database and set various constraints like NOT NULL, PRIMARY KEY,
FOREIGN KEY, UNIQUE, and CHECK.

Algorithm:

1. Create a new database for the Employee system.


24UDSMP202 LAB MANUAL DSC PRACTICAL IV: DBMS LAB

2. Define the Employee table with constraints.


3. Set foreign key constraints and other necessary constraints.

Program:

-- Create the Employee Database


CREATE DATABASE EmployeeDB;

-- Use the created database


USE EmployeeDB;

-- Create Employee table with constraints


CREATE TABLE Employee (
EmpID INT PRIMARY KEY,
EmpName VARCHAR(50) NOT NULL,
Salary DECIMAL(10, 2) CHECK (Salary > 3000),
DepartmentID INT,
CONSTRAINT fk_dept FOREIGN KEY (DepartmentID) REFERENCES Department(DeptID)
);

-- Create Department table


CREATE TABLE Department (
DeptID INT PRIMARY KEY,
DeptName VARCHAR(50) UNIQUE NOT NULL
);

Output:

● EmployeeDB database created.


● Employee table created with constraints: PRIMARY KEY, NOT NULL, CHECK, FOREIGN KEY.
● Department table created with a UNIQUE constraint on DeptName.

Result:
Successfully created the Employee database and applied constraints.

4. Creating Relationships Between Databases

Aim:
To establish relationships between different tables in a database using primary and foreign keys.

Algorithm:

1. Create multiple related tables.


2. Use FOREIGN KEY constraints to create relationships.

Program:

-- Create Manager table (Parent)


24UDSMP202 LAB MANUAL DSC PRACTICAL IV: DBMS LAB

CREATE TABLE Manager (


ManagerID INT PRIMARY KEY,
ManagerName VARCHAR(50) NOT NULL
);

-- Modify Employee table to include ManagerID (Foreign Key)


ALTER TABLE Employee ADD ManagerID INT;
ALTER TABLE Employee ADD CONSTRAINT fk_manager FOREIGN KEY (ManagerID)
REFERENCES Manager(ManagerID);

Output:

● The Manager table created with a PRIMARY KEY.


● The Employee table modified to include ManagerID as a foreign key.

Result:
Successfully created relationships between the Employee and Manager tables.

5. Create a Table and Perform Built-in Functions and Grouping

Aim:
To create a table and use built-in functions and perform data grouping with GROUP BY.

Algorithm:

1. Create a table.
2. Use built-in functions like COUNT(), SUM(), AVG().
3. Group data based on a specific column using GROUP BY.

Program:

-- Create Sales table


CREATE TABLE Sales (
SaleID INT PRIMARY KEY,
Amount DECIMAL(10, 2),
SalespersonID INT
);

-- Insert sample data into Sales table


INSERT INTO Sales (SaleID, Amount, SalespersonID) VALUES (1, 1000, 101);
INSERT INTO Sales (SaleID, Amount, SalespersonID) VALUES (2, 1500, 102);
INSERT INTO Sales (SaleID, Amount, SalespersonID) VALUES (3, 2000, 101);

-- Using built-in functions and grouping


SELECT SalespersonID, COUNT(*) AS SaleCount, SUM(Amount) AS TotalSales
FROM Sales
GROUP BY SalespersonID;
24UDSMP202 LAB MANUAL DSC PRACTICAL IV: DBMS LAB

Output:

● Displays the number of sales and total sales amount per salesperson.

Result:
Successfully performed built-in functions like COUNT(), SUM(), and grouped data using GROUP
BY.

6. Create a Table and Implementation of Different Types of Constraints

Aim:
To create a table and implement different types of constraints like NOT NULL, CHECK, DEFAULT,
UNIQUE, and PRIMARY KEY.

Algorithm:

1. Create a table with multiple constraints.


2. Apply PRIMARY KEY, UNIQUE, CHECK, NOT NULL, and DEFAULT constraints.

Program:

-- Create Employee table with various constraints


CREATE TABLE Employee (
EmpID INT PRIMARY KEY,
EmpName VARCHAR(50) NOT NULL,
Salary DECIMAL(10, 2) CHECK (Salary >= 3000),
Department VARCHAR(50) DEFAULT 'HR',
Email VARCHAR(100) UNIQUE
);

Output:

● Employee table created with the mentioned constraints.

Result:
Successfully created the table with different types of constraints.

7. Create a Table and Perform Join and Set Operations

Aim:
To perform SQL joins and set operations (UNION, INTERSECT, EXCEPT).

Algorithm:
24UDSMP202 LAB MANUAL DSC PRACTICAL IV: DBMS LAB

1. Create two tables.


2. Perform different types of JOIN operations.
3. Use UNION, INTERSECT, and EXCEPT for set operations.

Program:

-- Create two tables: Orders and Customers


CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
Amount DECIMAL(10, 2)
);

CREATE TABLE Customers (


CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50)
);

-- Perform JOIN operation (INNER JOIN)


SELECT o.OrderID, c.CustomerName, o.Amount
FROM Orders o
INNER JOIN Customers c ON o.CustomerID = c.CustomerID;

-- Perform UNION operation


SELECT CustomerName FROM Customers
UNION
SELECT CustomerName FROM Orders;

Output:

● Data from Orders and Customers tables is retrieved using an INNER JOIN.
● A union of customer names from both tables is displayed.

Result:
Successfully performed JOIN and set operations (UNION, INTERSECT, EXCEPT).

8. Create a Table and Perform Aggregate Functions

Aim:
To create a table and perform aggregate functions like SUM(), AVG(), MAX(), and MIN().

Algorithm:

1. Create a table.
2. Use aggregate functions to calculate summaries.

Program:
24UDSMP202 LAB MANUAL DSC PRACTICAL IV: DBMS LAB

-- Create Products table


CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(50),
Price DECIMAL(10, 2)
);

-- Use aggregate functions


SELECT SUM(Price) AS TotalPrice, AVG(Price) AS AveragePrice
FROM Products;

Output:

● Displays the total and average price of all products.

Result:
Successfully performed aggregate functions.

9. Create a Table and Perform Triggers

Aim:
To create a table and implement a trigger in SQL.

Algorithm:

1. Create a table.
2. Write a trigger to perform actions automatically on certain events.

Program:

-- Create Employees table


CREATE TABLE Employees (
EmpID INT PRIMARY KEY,
EmpName VARCHAR(50),
Salary DECIMAL(10, 2)
);

-- Create a trigger to check if salary is updated


CREATE TRIGGER SalaryCheck
AFTER UPDATE ON Employees
FOR EACH ROW
BEGIN
IF NEW.Salary < 4000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary cannot be less than 4000');
END IF;
END;

Output:
24UDSMP202 LAB MANUAL DSC PRACTICAL IV: DBMS LAB

● Trigger created to prevent salary updates below 4000.

Result:
Successfully implemented a trigger to enforce salary constraints.

10. Create a Table and Perform PL/SQL

Aim:
To understand and perform basic PL/SQL operations.

Algorithm:

1. Create a table.
2. Write a PL/SQL block for inserting records and handling errors.

Program:

-- Create a simple table


CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(50),
Price DECIMAL(10, 2)
);

-- PL/SQL block for inserting data


DECLARE
v_ProductID INT := 1;
v_ProductName VARCHAR(50) := 'Laptop';
v_Price DECIMAL(10, 2) := 1200.00;
BEGIN
INSERT INTO Products (ProductID, ProductName, Price)
VALUES (v_ProductID, v_ProductName, v_Price);
COMMIT;
END;

Output:

● Record inserted into the Products table via PL/SQL.

Result:
Successfully implemented PL/SQL for inserting data into the table.

You might also like