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

Lab-10 Mplementation of DML Commands in SQL__ __Objective___ - To Perform Calculations on Data Values. - To Group Data Rows Based on Specific Column Values. - To Filter Grouped Data Based on Specific Conditions. - To Sort the Result Set

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab-10 Mplementation of DML Commands in SQL__ __Objective___ - To Perform Calculations on Data Values. - To Group Data Rows Based on Specific Column Values. - To Filter Grouped Data Based on Specific Conditions. - To Sort the Result Set

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment 10: Keys in SQL

Lab Objective:
This lab aims to equip students with practical skills in implementing different types of
keys in SQL. Students will learn how to define and use keys such as primary keys,
foreign keys, unique keys, and composite keys to enforce data integrity and establish
relationships between tables.

Prerequisites:
●​ A foundational understanding of relational databases and SQL syntax.
●​ Familiarity with basic SQL operations such as creating and altering tables.
●​ Access to an SQL environment (e.g., MySQL, PostgreSQL, SQL Server).

Lab Exercises:
1. Creating a Table with a Primary Key
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY NOT NULL,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Email VARCHAR(100) UNIQUE NOT NULL
);

●​ The CustomerID column is set as the Primary Key, ensuring each customer has a
unique identifier.
●​ The Email column is defined as Unique, preventing duplicate entries.

2. Implementing a Foreign Key


CREATE TABLE Orders (
OrderID INT PRIMARY KEY NOT NULL,
OrderDate DATE NOT NULL,
CustomerID INT NOT NULL,
TotalAmount DECIMAL(10,2),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
●​ The CustomerID in Orders references CustomerID in Customers, establishing a
Foreign Key relationship.

Inserting Sample Data:


INSERT INTO Customers (CustomerID, FirstName, LastName, Email) VALUES
(1, 'John', 'Doe', '[email protected]'),
(2, 'Jane', 'Smith', '[email protected]');

INSERT INTO Orders (OrderID, OrderDate, CustomerID, TotalAmount) VALUES


(101, '2025-03-12', 1, 250.50),
(102, '2025-03-13', 2, 150.75);

3. Creating a Table with a Unique Key


CREATE TABLE Products (
ProductID INT PRIMARY KEY NOT NULL,
ProductName VARCHAR(100) NOT NULL,
SKU VARCHAR(50) UNIQUE NOT NULL,
Price DECIMAL(10,2)
);

●​ The SKU column is designated as Unique, ensuring no two products have the
same SKU.

4. Implementing a Composite Key


CREATE TABLE OrderDetails (
OrderID INT NOT NULL,
ProductID INT NOT NULL,
Quantity INT NOT NULL,
PRIMARY KEY (OrderID, ProductID),
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);

●​ The combination of OrderID and ProductID serves as a Composite Key,


ensuring each product appears only once per order.

Inserting Sample Data:


INSERT INTO Products (ProductID, ProductName, SKU, Price) VALUES
(1, 'Laptop', 'LAP123', 999.99),
(2, 'Mouse', 'MOU456', 25.99);

INSERT INTO OrderDetails (OrderID, ProductID, Quantity) VALUES


(101, 1, 2),
(102, 2, 3);

5. Exploring Key Constraints

Attempting to Insert Duplicate SKU


INSERT INTO Products (ProductID, ProductName, SKU, Price) VALUES
(3, 'Keyboard', 'MOU456', 49.99);

●​ This will fail due to the Unique Key constraint on SKU.

Attempting to Insert a Non-Existent OrderID


INSERT INTO OrderDetails (OrderID, ProductID, Quantity) VALUES
(999, 1, 1);

●​ This will fail due to the Foreign Key constraint, as OrderID 999 does not exist.

Outcome:
Upon completing this lab, students will:

●​ Be proficient in implementing various types of keys such as Primary Key, Foreign


Key, Unique Key, and Composite Key.
●​ Understand the importance of keys in maintaining data integrity and establishing
relationships between tables.
●​ Be able to apply keys in real-world scenarios to ensure the accuracy and reliability
of database systems.

You might also like