DBMSS LAB RECORD
DBMSS LAB RECORD
2023- 2024
FOURTH SEMESTER
1
1
AIM:
PROCEDURE:
Add Constraints:
Define constraints such as primary key, unique, check, and not null for appropriate
columns.
Use SQL DDL commands PRIMARY KEY, UNIQUE, CHECK, and NOT NULL
within the CREATE TABLE statement.
Insert Rows:
Prepare the data to be inserted into the table.
Use SQL DML command INSERT INTO to add rows into the table.
Update Rows:
Identify the rows that need to be updated based on certain conditions.
Use SQL DML command UPDATE to modify the data in the table.
Delete Rows:
Identify the rows that need to be deleted based on certain conditions.
Use SQL DML command DELETE FROM to remove rows from the table.
2
SQL COMMANDS:
--
Create a database table
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Email VARCHAR(100) UNIQUE,
Age INT CHECK (Age >= 18),
Department VARCHAR(50)
);
OUTPUT
RESULT:
Thus the program to create a database table, add constraints (primary
key, unique, check, not null), insert rows, update and delete rows using SQL DDL
and DML commands is done successfully
4
AIM :
PROCEDURE:
Create Tables:
Use SQL DDL (CREATE TABLE) commands to create each table with their
respective columns.
PROGRAM:
OUTPUT :
Customers Table:
Orders Table:
OrderItems Table:
Products Table:
RESULT:
Thus to create set of tables, add foreign key constraints and incorporate
referential integrity is done successfully.
7
EX NO:3
QUERY THE DATABASE TABLES USING DIFFERENT
‘WHERE’ CLAUSE CONDITIONS AND ALSO
DATE: IMPLEMENT AGGREGATE FUNCTIONS.
AIM:
To Query the database tables using different ‘where’ clause conditions and
also implement aggregate functions.
PROCEDURE:
Define the SQL query: Determine the specific information you want to retrieve
from the database table and formulate the SQL query accordingly.
Construct the WHERE clause: Decide on the conditions that filter the rows
returned by the query based on specific criteria. This can include conditions on
columns, date ranges, or other relevant factors.
Execute the SQL query: Execute the SQL query against the database using an
appropriate database client or interface.
Retrieve and process the results: Retrieve the results returned by the query
and process them as needed. This can involve displaying the results, performing
further analysis, or using them for other purposes.
PROGRAM:
-- Average price
SELECT AVG(Price) AS AveragePrice FROM Sales;
OUTPUT:
RESULT:
Thus program for query the database tables using different ‘where’ clause
conditions and also implement aggregate functions executed successfully.
11
EX NO:4
QUERY THE DATABASE TABLES AND EXPLORE
DATE: SUB QUERIES AND SIMPLE JOIN OPERATIONS
AIM:
To Query the database tables and explore sub queries and simple join
operations.
PROCEDURE:
Identify the tables: Determine which tables you need to query and understand
their relationships.
Write the main query: Start by writing the main query to retrieve the data you
need from the primary table.
Add join operations: If you need data from multiple tables, add join operations to
connect them based on their relationships.
Execute the query: Run the SQL query against the database to retrieve the results.
Explore and analyze the results: Review the results returned by the query to ensure
they meet your requirements. You can also perform further analysis or processing
on the results as needed.
12
PROGRAM:
-- Students table
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Age INT
);
-- Courses table
CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(50) NOT NULL
);
OUTPUT :
RESULT:
Thus Query the database tables and explore sub queries and simple join
operations is done successfully.
14
EX NO:5
QUERY THE DATABASE TABLES AND EXPLORE
DATE: NATURAL, EQUI AND OUTER JOINS
AIM:
To query the database tables and explore natural, equi and outer joins.
PROCEDURE:
PROGRAM:
-- Create the tables
CREATE TABLE Customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(50) NOT NULL,
city VARCHAR(50)
);
-- Insert some sample data (ensure customer_id values match in both tables)
INSERT INTO Customers (customer_id, customer_name, city)
VALUES (1, 'John Doe', 'New York'),
(2, 'Jane Smith', 'Los Angeles'),
(3, 'Michael Chen', 'Chicago');
OUTPUT:
RESULT:
Thus query the database tables and explore natural, equi and outer joins is
done successfully.
17
EX NO:6
WRITE USER DEFINED FUNCTIONS AND STORED
DATE: PROCEDURES IN SQL
AIM:
PROCEDURE:
Define the Task: Determine the specific task or functionality that you want the
user-defined function or stored procedure to perform.
Test the Code: Execute the user-defined function or stored procedure to ensure
that it performs the intended task correctly.
Deploy and Use: Once tested, deploy the user-defined function or stored
procedure to your database environment.
18
PROGRAM:
OUTPUT
RESULT:
Thus to write user defined functions and stored procedures in SQL is done
successfully.
20
EX NO:7
EXECUTE COMPLEX TRANSACTIONS AND
DATE: REALIZE DCL AND TCL COMMANDS
AIM:
PROCEDURE:
Execute SQL Operations: Execute the necessary SQL statements within the
transaction block to perform the desired operations. These operations can include
data manipulation (DML) statements such as INSERT, UPDATE, DELETE, as
well as data definition (DDL) statements if needed.
Use COMMIT to save the changes made by the transaction to the database
permanently. This makes the changes visible to other users and commits the
transaction.
Use ROLLBACK to discard the changes made by the transaction and revert the
database to its state before the transaction began. This aborts the transaction.
Use SAVEPOINT to set a point within the transaction to which you can later roll
back if needed.
End Transaction: End the transaction by using the appropriate SQL command to
either commit or rollback the changes made during the transaction.
22
PROGRAM:
DELIMITER //
START TRANSACTION;
CASE user_role
WHEN 'Customer' THEN
IF has_sufficient_stock(product_id) THEN
INSERT INTO Orders (user_id, product_id)
VALUES (user_id, product_id);
ELSE
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Out of stock';
END IF
WHEN 'Admin' THEN
-- Admin can perform additional operations (replace with your logic)
-- ...
ELSE
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Invalid user role';
END CASE;
COMMIT;
END //
DELIMITER ;
24
OUTPUT
Error table:
RESULT:
Thus the Execution of complex transactions and realize DCL and TCL
commands is done successfully.
25
EX NO:8
WRITE SQL TRIGGERS FOR INSERT, DELETE, AND
UPDATE OPERATIONS IN DATABASE TABLE.
DATE:
AIM:
To write SQL triggers for insert, delete, and update operations in database
table.
PROCEDURE:
Identify the Trigger Event: Determine the type of database operation (insert,
delete, or update) that will trigger the execution of the trigger.
Choose the Trigger Timing: Decide whether the trigger should fire before or
after the triggering event (BEFORE or AFTER).
Test the Trigger: Before deploying the trigger, test it thoroughly to ensure it
behaves as expected under different scenarios.
Deploy the Trigger: Use the appropriate SQL command (CREATE TRIGGER)
to create the trigger in the database.
Monitor and Maintain the Trigger: Regularly monitor the trigger's behavior
and performance, and make any necessary adjustments or optimizations as needed.
27
PROGRAM:
OUTPUT
RESULT:
Thus to Write SQL Triggers for insert, delete, and update operations in
database table is done successfully
29
AIM
To use sqli to authenticate as administrator, to get unauthorized access over
sensitive data, to inject malicious statements into form field.
PROCEDURE
Setup Environment:
Install a vulnerable web application like DVWA.
Start the web server environment.
Access the application through a web browser.
Bypass Authentication:
Find the login form.
Enter ' OR '1'='1' -- in the username field.
Provide any value in the password field.
Attempt to log in.
PROGRAM
Bypassing Authentication:
Username: admin' OR '1'='1' --
Password: <any value>
OUTPUT
1. Bypassing Authentication:
Successful login as administrator or user with elevated privileges.
RESULTS:
Successfully bypassed authentication, accessed sensitive data, and
executed malicious statements.
33
AIM
To Write programs that will defend against the SQLi attacks given in
the previous exercise.
PROCEDURE
1. Parameterized Queries:
Use prepared statements with placeholders for user input.
Bind input values to parameters in the SQL query.
Execute the query to retrieve data securely.
2. Input Sanitization:
Filter and sanitize user input before using it in SQL queries.
Use built-in functions or libraries to sanitize input and remove
potentially harmful characters.
Validate input to ensure it meets expected criteria.
3. Limiting Privileges:
Implement role-based access control to restrict user privileges.
Assign specific permissions based on user roles.
Ensure that sensitive operations are only accessible to authorized
users.
34
PROGRAM
Parameterized Queries:
$username = $_POST['username'];
$password = $_POST['password'];
$stmt = $pdo->prepare("SELECT * FROM users WHERE username =
:username AND password = :password");
$stmt->execute(['username' => $username, 'password' => $password]);
$user = $stmt->fetch();
Limiting Privileges:
if ($user['role'] === 'admin') {
// Perform administrative tasks
} else {
// Perform regular user tasks
}
35
OUTPUT
Parameterized Queries:
The user is successfully retrieved from the database using parameterized
queries, ensuring protection against SQL injection attacks.
$user = [
'id' => 1,
'username' => 'example_user',
'password' => 'hashed_password',
'role' => 'regular'
];
Input Sanitization:
User input is sanitized before executing the query, preventing SQL
injection vulnerabilities and ensuring data integrity.
$username = 'example_user';
$password = 'password123';
Limiting Privileges:
Depending on the user's role, either administrative tasks or regular user
tasks are performed, demonstrating role-based access control and
protection against unauthorized actions
RESULT
Parameterized Queries: Secure user authentication. Input Sanitization:
Prevention of SQL injection vulnerabilities. Limiting Privileges: Role-based access
control for enhanced security.
36
EX NO:11
WRITE QUERIES TO INSERT ENCRYPTED DATA
DATE: INTO THE DATABASE AND TO RETRIEVE THE
DATA USING DECRYPTION
AIM
To Write queries to insert encrypted data into the database and to retrieve
the data using decryption.
PROCEDURE
PROGRAM
OUTPUT
Username: user1
Decrypted Password: password123
RESULT
Insert encrypted data into the database and retrieve it using decryption
was executed successfully.