0% found this document useful (0 votes)
12 views10 pages

Dbms Lab Manual

Uploaded by

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

Dbms Lab Manual

Uploaded by

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

1.

Create a Database Table, Add Constraints, Insert, Update, and


Delete Rows using DDL and DML Commands:
Aim: : To create a structured database table, enforce constraints, and perform basic data
manipulation operations.

Procedure:
Create Table:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Email VARCHAR(100) UNIQUE,
DepartmentID INT,
CONSTRAINT FK_Department FOREIGN KEY (DepartmentID) REFERENCES
Departments(DepartmentID)
);
Insert Data:
INSERT INTO Employees (EmployeeID, FirstName, LastName, Email, DepartmentID)
VALUES (1, 'John', 'Doe', '[email protected]', 101),
(2, 'Jane', 'Smith', '[email protected]', 102);
Update Data:
UPDATE Employees
SET DepartmentID = 103
WHERE EmployeeID = 2;
Delete Data:
DELETE FROM Employees
WHERE EmployeeID = 1;

Result: The Employees table is created with specified constraints (PRIMARY KEY,
NOT NULL, UNIQUE, FOREIGN KEY). Data can be inserted, updated, and deleted using
appropriate commands, ensuring data integrity and consistency.
2. Create a Set of Tables, Add Foreign Key Constraints, and
Incorporate Referential Integrity:
Aim: To design and link multiple tables using foreign key constraints for maintaining
relational integrity.

Procedure:
Create Tables:
Define multiple tables (CREATE TABLE ...) with primary keys.
Add Foreign Key Constraints:
ALTER TABLE Employees
ADD CONSTRAINT FK_Department FOREIGN KEY (DepartmentID) REFERENCES
Departments(DepartmentID);
Ensure Referential Integrity:
Establish relationships between tables to maintain consistency across related data.

Result: A structured database schema is created with interlinked tables (Employees,


Departments, etc.) using foreign key constraints to enforce referential integrity.

3. Query Database Tables Using WHERE Clauses and Implement


Aggregate Functions:
Aim: To retrieve specific data subsets and perform calculations on aggregated data.
Procedure:
Query with WHERE Clause:
SELECT * FROM Employees
WHERE DepartmentID = 103;
Implement Aggregate Functions:
SELECT COUNT(*) AS TotalEmployees, AVG(Salary) AS AvgSalary
FROM Employees
WHERE DepartmentID = 103;

Result: Data subsets are retrieved based on specified conditions (WHERE clauses), and
aggregate functions (COUNT, AVG, etc.) provide calculated results from the queried data.
4. Query Database Tables and Explore Subqueries and Simple
Join Operations:
Aim: : To combine data from multiple tables and use nested queries for complex data
retrieval.

Procedure:
Subqueries:
SELECT *
FROM Employees
WHERE DepartmentID IN (SELECT DepartmentID FROM Departments WHERE Location
= 'New York');
Simple Joins:
SELECT e.*, d.DepartmentName
FROM Employees e
INNER JOIN Departments d ON e.DepartmentID = d.DepartmentID;

Result: Subqueries and simple join operations successfully retrieve and combine data
from related tables (Employees and Departments), facilitating complex data analysis and
reporting.

5. Query Database Tables and Explore Natural, Equi, and Outer


Joins:
Aim: : To understand and utilize different types of joins for fetching data based on matching
criteria.

Procedure:
Natural Join:
SELECT *
FROM Employees
NATURAL JOIN Departments;
Equi-Join:
SELECT e.*, d.DepartmentName
FROM Employees e
JOIN Departments d ON e.DepartmentID = d.DepartmentID;
Outer Join:
SELECT e.*, d.DepartmentName
FROM Employees e
LEFT JOIN Departments d ON e.DepartmentID = d.DepartmentID;

Result: Various types of joins (NATURAL JOIN, INNER JOIN, LEFT OUTER JOIN)
are used to fetch data based on specified matching conditions, providing comprehensive data
retrieval capabilities.

6. Write User-Defined Functions and Stored Procedures in :

Aim: To create custom functions and procedures for performing complex calculations and
data operations.

Procedure:
User-Defined Function:
CREATE FUNCTION CalculateBonus (@Salary DECIMAL)
RETURNS DECIMAL
AS
BEGIN
DECLARE @Bonus DECIMAL;
SET @Bonus = @Salary * 0.1; -- 10% bonus
RETURN @Bonus;
END;
Stored Procedure:
CREATE PROCEDURE GetEmployeeDetails (@EmployeeID INT)
AS
BEGIN
SELECT * FROM Employees WHERE EmployeeID = @EmployeeID;
END;

Result: Custom functions (CalculateBonus) and stored procedures


(GetEmployeeDetails) are created to encapsulate business logic and simplify repetitive tasks
within the database environment.
7. Execute Complex Transactions and Realize DCL and TCL
Commands:
Aim: : To manage transactional integrity and control access to database resources using
Data Control Language (DCL) and Transaction Control Language (TCL) commands.

Procedure:
 Begin a Transaction:
BEGIN TRANSACTION;
 Execute DCL Commands (e.g., Grant Access):
GRANT SELECT, INSERT ON Employees TO User1;
 Commit or Rollback Transaction:
COMMIT;
-- or
ROLLBACK;

Result: Transactions (BEGIN TRANSACTION, COMMIT, ROLLBACK) ensure data


consistency and integrity while DCL commands (GRANT, REVOKE) control access
permissions, enhancing database security.

8. Write Triggers for Insert, Delete, and Update Operations in a


Database Table:
Aim: : To automate actions based on data changes within a database table using triggers.
Procedure:
 Create Trigger for Insert:
CREATE TRIGGER trg_InsertEmployee
ON Employees
FOR INSERT
AS
BEGIN
-- Trigger logic (e.g., logging)
INSERT INTO EmployeeLog (Action, ActionDate)
VALUES ('New employee inserted', GETDATE());
END;
 Create Trigger for Delete:
CREATE TRIGGER trg_DeleteEmployee
ON Employees
FOR DELETE
AS
BEGIN
-- Trigger logic (e.g., archiving)
INSERT INTO DeletedEmployees SELECT * FROM DELETED;
END;
 Create Trigger for Update:
CREATE TRIGGER trg_UpdateEmployee
ON Employees
FOR UPDATE
AS
BEGIN
-- Trigger logic (e.g., audit trail)
INSERT INTO EmployeeChanges (EmployeeID, ChangeDate, OldValue, NewValue)
SELECT d.EmployeeID, GETDATE(), d.Salary, i.Salary
FROM DELETED d
INNER JOIN INSERTED i ON d.EmployeeID = i.EmployeeID;
END;

Result: triggers (trg_InsertEmployee, trg_DeleteEmployee, trg_UpdateEmployee) are


created to automatically perform actions (logging, archiving, audit trail) in response to
INSERT, DELETE, and UPDATE operations on the Employees table.
9. Create View and Index for Database Tables with a Large
Number of Records:
Aim: : To optimize query performance and simplify data access for tables containing large
datasets.

Procedure:
 Create View:
CREATE VIEW EmployeesSummary
AS
SELECT EmployeeID, FirstName, LastName, DepartmentID
FROM Employees
WHERE IsActive = 1;
 Create Index:
CREATE INDEX idx_DepartmentID ON Employees (DepartmentID);

Result: The EmployeesSummary view provides a simplified data subset (EmployeeID,


FirstName, LastName, DepartmentID) for active employees. The idx_DepartmentID index
enhances query performance when filtering or sorting data by DepartmentID.

10. Create XML Database and Validate It Using XML Schema:


Aim: : To store and manage XML data in a database while ensuring its structural validity
against an XML schema.

Procedure:
 Create XML Collection:
CREATE XML SCHEMA COLLECTION EmployeeSchema AS '
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Employee">
<xs:complexType>
<xs:sequence>
<xs:element name="EmployeeID" type="xs:int"/>
<xs:element name="FirstName" type="xs:string"/>
<xs:element name="LastName" type="xs:string"/>
<xs:element name="Email" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>';

 Insert XML Data:


INSERT INTO EmployeeXMLData (EmployeeData)
VALUES ('<Employee>
<EmployeeID>1</EmployeeID>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
<Email>[email protected]</Email>
</Employee>');
 Validate XML Data Against Schema:

SELECT EmployeeData
FROM EmployeeXMLData
WHERE EmployeeData.validate(EmployeeSchema) = 1;

Result: XML data (EmployeeXMLData) is stored in the database, validated against the
defined schema (EmployeeSchema), ensuring its structural integrity and adherence to
specified XML standards.
11. Create Document, Column, and Graph-Based Data Using No
Database Tools:
Aim: : To utilize No databases for handling diverse data models efficiently, including
document, column-family, and graph-based data.

Procedure:
 Document Store (e.g., MongoDB):
o Create collections for storing flexible JSON-like documents.
o Perform CRUD operations using MongoDB's BSON query language.
 Column-Family Store (e.g., Cassandra):
o Define column families to store data in rows and columns.
o Use CQL (Cassandra Query Language) for querying and managing data.
 Graph Database (e.g., Neo4j):
o Model data as nodes (entities) and relationships (edges).
o Execute Cypher queries to traverse and analyze graph structures.

Result: No databases (MongoDB, Cassandra, Neo4j) are employed to manage different


data models effectively, catering to specific application requirements for scalability and
performance.

12. Develop a Simple GUI-Based Database Application Incorporating All


the Above-Mentioned Features:
Aim: : To build a user-friendly graphical interface that integrates various database
functionalities (transactions, triggers, views, No data handling, etc.).

Procedure:
 Design GUI Components:
o Create forms, buttons, and input fields for data entry and display.
o Include navigation controls for querying and updating database content.
 Integrate Database Features:
o Implement backend logic to execute queries, triggers, and transactions.
o Connect to No databases and handle document, column, and graph-based data.
 Ensure Data Security and Integrity:
o Implement user authentication and authorization mechanisms.
o Validate user inputs and sanitize data to prevent security vulnerabilities.
Result: A functional GUI-based database application is developed, providing users with
an intuitive interface to interact with and manage complex database operations effectively.

13. Case Study Using Any Real-Life Database Application:


Aim: : To apply database concepts and technologies to solve practical business or
organizational challenges.

Procedure:
 Select a Real-Life Scenario:
o Choose an application domain (e.g., e-commerce, healthcare, logistics).
o Identify specific challenges related to data management and analysis.
 Design and Implement Database Solution:
o Define database schema and relationships based on application requirements.
o Implement transactional processes, triggers, views, and indexes to optimize
performance.
 Document Implementation Process:
o Record challenges faced, decisions made, and solutions implemented.
o Evaluate the effectiveness of the database solution in addressing business
needs.

Result: A comprehensive case study demonstrates the application of database


technologies to real-world scenarios, highlighting the strategic use of transactions, triggers,
views, No capabilities, GUI integration, and other advanced features to achieve operational
efficiency and business success.

You might also like