Dbms Lab Manual
Dbms Lab Manual
1.
1
EX NO:1 CREATE A DATABASE TABLE ADD CONSTRAINTS
NSERT ROWS,UPDATE AND DELETE ROWS USING SQL DDL AND
DML COMMANDSI
DATE:
AIM:
To write SQL query to create a database table and manipulate data using sql ddl and
dml commands.
ALGORITHM:
4. Define the table schema, including column names, data types, and constraints
(primary key, unique, check, not null).
6. Insert rows into the table using the insert into sql command.
8. Delete rows from the table using the delete sql command.
9. Use the select sql command to retrieve data from the table.
2
VALUES
(1, 'Prakash', 20, '[email protected]', 'Male'), (2, 'Rose', 22, '[email protected]', 'Female');
Query OK, 2 rows affected (0.13 sec)
Records: 2 Duplicates: 0 Warnings: 0
Select*from students;
+----+---------+------+-------------------+--------+
| id | name | age | email | gender |
+----+---------+------+-------------------+--------+
| 1 | Prakash | 20 | [email protected] | Male |
| 2 | Rose | 22 | [email protected] | Female |
+----+---------+------+-------------------+--------+
2 rows in set (0.00 sec)
-- Update a row
UPDATE students
SET age = 23
WHERE id = 2;
Query OK, 1 row affected (0.09 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Select*from students;
+----+---------+------+-------------------+--------+
| id | name | age | email | gender |
+----+---------+------+-------------------+--------+
| 1 | Prakash | 20 | [email protected] | Male |
| 2 | Rose | 23 | [email protected] | Female |
+----+---------+------+-------------------+--------+
2 rows in set (0.00 sec)
-- Delete a row
DELETE FROM students
WHERE id = 2;
Query OK, 1 row affected (0.09 sec)
Select*from students;
+----+---------+------+-------------------+--------+
| id | name | age | email | gender |
+----+---------+------+-------------------+--------+
| 1 | Prakash | 20 | [email protected] | Male |
+----+---------+------+-------------------+--------+
1 row in set (0.00 sec)
RESULT:
Thus the SQL query to create a database table and manipulate data using sql ddl and dml
commands has been written and executed successfully.
3
EX NO:2 CREATE A SET OF TABLES, ADD FOREIGN KEY
CONSTRAINTS AND INCORPORATE REFERENTIAL INTEGRITY
DATE:
AIM:
To write SQL query to create a set of tables, add foreign key constraints and
incorporate referential integrity.
ALGORITHM:
4. Define the table schema for each table, including column names, data types, and
constraints.
6. Add foreign key constraints to the appropriate columns in each table using the
ALTER TABLE SQL command.
7. Incorporate referential integrity by ensuring that foreign key values match the primary
key values in the referenced table.
);
department_id INT,
salary DECIMAL(10,2),
4
);
SELECT*FROM DEPARTMENTS;
+---------------+-----------------+
| department_id | department_name |
+---------------+-----------------+
| 1 | Sales |
| 2 | Marketing |
| 3 | IT |
| 4 | Finance |
+---------------+-----------------+
SELECT*FROM EMPLOYEES;
+-------------+-----------------+---------------+----------+
+-------------+-----------------+---------------+----------+
5
| 3 | Jim Smith | 3 | 60000.00 |
+-------------+-----------------+---------------+----------+
RESULT:
Thus the SQL query to create a set of tables, add foreign key constraints and
incorporate referential integrity has been written and executed successfully.
6
EX NO:3 QUERY DATABASE TABLES USING WHERE CLAUSE
CONDITIONS AND AGGREGATE FUNCTIONS
DATE:
AIM:
To write SQL query to query database tables using where clause conditions and
aggregate functions.
ALGORITHM:
1. Connect to the database server.
5. Use the WHERE clause to specify the condition(s) for selecting the rows to be
returned.
6. Use the ORDER BY clause to sort the returned rows based on a specific column.
);
department_id INT,
salary DECIMAL(10,2),
7
);
SELECT*FROM DEPARTMENTS;
+---------------+-----------------+
| department_id | department_name |
+---------------+-----------------+
| 1 | Sales |
| 2 | Marketing |
| 3 | IT |
| 4 | Finance |
+---------------+-----------------+
SELECT*FROM EMPLOYEES;
+-------------+-----------------+---------------+----------+
+-------------+-----------------+---------------+----------+
8
+-------------+-----------------+---------------+----------+
WHERE department_id = 1;
+-------------+-----------------+---------------+----------+
+-------------+-----------------+---------------+----------+
+-------------+-----------------+---------------+----------+
-- Select employee names and department names from both tables using a join
FROM employees
JOIN departments
ON employees.department_id = departments.department_id;
+-----------------+-----------------+
| employee_name | department_name |
+-----------------+-----------------+
| Jim Smith | IT |
+-----------------+-----------------+
9
-- Select the average salary of employees in each department
FROM employees
JOIN departments
ON employees.department_id = departments.department_id
GROUP BY departments.department_name;
+-----------------+--------------+
| department_name | AVG(salary) |
+-----------------+--------------+
| Finance | 65000.000000 |
| IT | 60000.000000 |
| Marketing | 55000.000000 |
| Sales | 52500.000000 |
+-----------------+--------------+
RESULT:
Thus the SQL query to query database tables using where clause conditions and
aggregate functions has been written and executed successfully.
10
EX NO:4 QUERY DATABASE TABLES USING SUBQUERIES AND
SIMPLE JOIN OPERATIONS
DATE:
AIM:
To write the query database tables using subqueries and simple join operations in
SQL.
ALGORITHM:
5. Use the WHERE clause to filter the rows based on a specific condition.
);
department_id INT,
salary DECIMAL(10,2),
);
11
Query OK, 0 rows affected (0.14 sec)
SELECT*FROM DEPARTMENTS;
+---------------+-----------------+
| department_id | department_name |
+---------------+-----------------+
| 1 | Sales |
| 2 | Marketing |
| 3 | IT |
| 4 | Finance |
+---------------+-----------------+
+-------------+-----------------+---------------+----------+
+-------------+-----------------+---------------+----------+
12
| 3 | Jim Smith | 3 | 60000.00 |
+-------------+-----------------+---------------+----------+
+-------------+---------------+---------------+----------+
+-------------+---------------+---------------+----------+
+-------------+---------------+---------------+----------+
-- Select employee names and department names from both tables using a join
FROM employees
JOIN departments
ON employees.department_id = departments.department_id;
+-----------------+-----------------+
| employee_name | department_name |
+-----------------+-----------------+
| Jim Smith | IT |
13
| Sarah Johnson | Finance |
+-----------------+-----------------+
SELECT AVG(salary)
+--------------+
| AVG(salary) |
+--------------+
| 57000.000000 |
+--------------+
FROM employees
JOIN departments
ON employees.department_id = departments.department_id;
+-----------------+-----------------+----------+
+-----------------+-----------------+----------+
+-----------------+-----------------+----------+
14
RESULT:
Thus the query database tables using subqueries and simple join operations in SQL has
been written and executed successfully.
15
EX NO:5 QUERY DATABASE TABLES USING NATURAL, EQUI AND
OUTER JOINS
DATE:
AIM:
To write the query database tables using natural, equi and outer joins.
ALGORITHM:
1. Connect to the database server.
6. Use natural, equi, or outer joins to combine data from multiple tables.
7. Use the WHERE clause to filter the rows based on a specific condition.
name VARCHAR(50),
email VARCHAR(50)
);
customer_id INT,
product VARCHAR(50),
CONSTRAINT fk_customer
16
REFERENCES customers(id)
);
SELECT*FROM CUSTOMERS;
+----+-------------+-------------------------+
| id | name | email |
+----+-------------+-------------------------+
| 1 | Prakash | [email protected] |
+----+-------------+-------------------------+
SELECT*FROM ORDERS;
+----+-------------+-------------+-------+
17
+----+-------------+-------------+-------+
| 1| 1 | Widget | 10.00 |
| 2| 2 | Gizmo | 15.00 |
| 3| 1 | Thingamajig | 20.00 |
| 4| 3 | Widget | 10.00 |
+----+-------------+-------------+-------+
SELECT *
FROM customers
+----+-------------+-------------------------+-------------+-------------+-------+
+----+-------------+-------------------------+-------------+-------------+-------+
+----+-------------+-------------------------+-------------+-------------+-------+
SELECT *
FROM customers
JOIN orders
ON customers.id = orders.customer_id;
+----+-------------+-------------------------+----+-------------+-------------+-------+
+----+-------------+-------------------------+----+-------------+-------------+-------+
18
| 3 | Bob Johnson | [email protected] | 4 | 3 | Widget | 10.00 |
+----+-------------+-------------------------+----+-------------+-------------+-------+
SELECT *
FROM orders
+------+-------------+-------------+-------+----+-------------+-------------------------+
+------+-------------+-------------+-------+----+-------------+-------------------------+
+------+-------------+-------------+-------+----+-------------+-------------------------+
SELECT *
FROM customers
+----+-------------+-------------------------+------+-------------+-------------+-------+
+----+-------------+-------------------------+------+-------------+-------------+-------+
+----+-------------+-------------------------+------+-------------+-------------+-------+
19
RESULT:
Thus the query database tables using natural, equi and outer joins has been written
and executed successfully.
20
EX NO:6 WRITE USER DEFINED FUNCTIONS AND STORED
PROCEDURES IN SQL.
DATE:
AIM:
ALGORITHM:
1. Connect to the database server.
);
21
(3, 'JENNY', 'KIM', '2019-01-01', 50000.00);
SELECT*FROM EMPLOYEES;
+-------------+------------+-----------+------------+----------+
+-------------+------------+-----------+------------+----------+
+-------------+------------+-----------+------------+----------+
DELIMITER $$
RETURNS VARCHAR(100)
BEGIN
RETURN fullName;
END$$
DELIMITER ;
SELECT*FROM EMPLOYEES;
DELIMITER //
22
BEGIN
UPDATE employees
END //
DELIMITER ;
SELECT*FROM EMPLOYEES;
+-------------+------------+-----------+------------+----------+
+-------------+------------+-----------+------------+----------+
+-------------+------------+-----------+------------+----------+
SELECT*FROM EMPLOYEES;
+-------------+------------+-----------+------------+----------+
+-------------+------------+-----------+------------+----------+
23
RESULT:
Thus the user defined functions and stored procedures in SQL has been written and
executed successfully.
24
EX NO:7 EXECUTE COMPLEX TRANSACTIONS AND REALIZE
DCL AND TCL COMMANDS.
DATE:
AIM:
To write the query to execute complex transactions and realize dcl and tcl commands.
ALGORITHM:
5. Use the SAVEPOINT SQL command to create a savepoint within the transaction.
7. Use the COMMIT SQL command to commit the transaction and make the changes
permanent.
8. Use the ROLLBACK SQL command to rollback the entire transaction and undo all
changes.
10. Use the REVOKE SQL command to revoke privileges from a user or group.
);
25
START TRANSACTION;
VALUES (1, 'ROSE', 'PARK', '2020-01-01', 50000.00), (2, 'LISA', 'MANABONAN', '2020-01-01',
50000.00),(3, 'JENNY', 'KIM', '2019-01-01', 50000.00), (4, 'JISOO', 'KIM', '2019-01-01', 50000.00);
SELECT*FROM EMPLOYEES;
+-------------+------------+-----------+------------+----------+
+-------------+------------+-----------+------------+----------+
+-------------+------------+-----------+------------+----------+
UPDATE employees
SELECT*FROM EMPLOYEES;
+-------------+------------+-----------+------------+----------+
+-------------+------------+-----------+------------+----------+
26
| 4 | JISOO | KIM | 2019-01-01 | 55000.00 |
+-------------+------------+-----------+------------+----------+
COMMIT;
27
RESULT:
Thus the query to execute complex transactions and realize dcl and tcl commands has been
written and executed successfully.
28
EX NO:8 WRITE SQL TRIGGERS FOR INSERT, DELETE, AND
UPDATE OPERATIONS IN A DATABASE TABLE.
DATE:
AIM:
To write sql triggers for insert, delete, and update operations in a database table..
ALGORITHM:
INSERT OPERATION:
2. Check if the data being inserted is valid and does not violate any constraints or rules
defined for the table.
5. If there is any error during the process, rollback the transaction to undo any changes
made.
DELETE OPERATION:
5. If there is any error during the process, rollback the transaction to undo any changes
made.
UPDATE OPERATION:
3. If the data exists, check if the new data being updated is valid and does not violate any
constraints or rules defined for the table.
29
6. If there is any error during the process, rollback the transaction to undo any changes
made.
-- Create a table
name VARCHAR(50),
department VARCHAR(50),
salary INT
);
action VARCHAR(10),
action_date TIMESTAMP
);
-- Change delimiter to //
DELIMITER //
BEGIN
END//
30
Query OK, 0 rows affected (0.14 sec)
BEGIN
END//
BEGIN
END//
-- Restore delimiter to ;
DELIMITER ;
31
(5, 'PRAKASH SN', 'Finance', 70000);
SELECT*FROM EMPLOYEES;
+----+-------------+------------+--------+
+----+-------------+------------+--------+
+----+-------------+------------+--------+
SELECT*FROM EMPLOYEE_AUDIT;
+----+--------+---------------------+
| id | action | action_date |
+----+--------+---------------------+
+----+--------+---------------------+
RESULT:
Thus the SQL striggers for insert, delete, and update operations in a database table has been written
and executed successfully.
32
EX NO: 9 CREATE VIEW AND INDEX FOR DATABASE TABLES
WITH A LARGE NUMBER OF RECORDS
DATE:
AIM:
To write the query to create view and index for database tables with a large number of
records.
ALGORITHM:
Creating a View:
1. Identify the query that retrieves the data you want to view.
2. Write the SQL statement that creates the view. For example: CREATE VIEW
view_name AS SELECT column1, column2, ... FROM table_name WHERE
condition;
4. Use the view in place of the original query in your application code.
Creating an Index:
1. Identify the column or columns that are frequently used in queries and have a large
number of unique values.
2. Write the SQL statement that creates the index. For example: CREATE INDEX
index_name ON table_name (column1, column2);
name VARCHAR(50),
email VARCHAR(100)
);
33
INSERT INTO users (id, name, email)
VALUES
FROM users;
+---------------+-------------------+
| name | email |
+---------------+-------------------+
34
| John Smith | [email protected] |
+---------------+-------------------+
+----+------------+------------------+
| id | name | email |
+----+------------+------------------+
+----+------------+------------------+
RESULT:
Thus the query to create view and index for database tables with a large number of
records has been written and executed successfully.
35
EX NO:10 CREATE AN XML DATABASE AND VALIDATE IT USING
XML SCHEMA.
DATE:
AIM:
ALGORITHM:
1. Define the data structure for your XML database using an XML schema. You can create
an XML schema using a text editor, or a graphical tool such as XMLSpy or Oxygen
XML Editor.
3. Create a new XML file and enter data based on the structure defined in the XML
schema.
5. Validate the XML file against the XML schema using a validation tool or XML editor
that supports XML schema validation.
6. If there are any errors or warnings, correct them in the XML file and repeat the
validation process until the file passes validation.
7. Once the XML file is validated, you can store it in your XML database.
<books>
<book>
<author>J.K. Rowling</author>
<year>1997</year>
<publisher>Bloomsbury</publisher>
</book>
<book>
<author>J.R.R. Tolkien</author>
36
<year>1954</year>
</book>
</books>
xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="books">
<xs:complexType>
<xs:sequence>
<xs:complexType>
<xs:sequence>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
import xmlschema
37
# Load the schema file
schema = xmlschema.XMLSchema('books.xsd')
xml = 'books.xml'
if schema.is_valid(xml):
print('Validation successful')
else:
print('Validation failed')
RESULT:
Thus the Create an XML database and validate it using XML schema has been created
and validated.
38
EX NO: 11 CREATE DOCUMENT, COLUMN AND GRAPH BASED
DATA USING NOSQL DATABASE TOOLS
DATE:
AIM:
To create document, column and graph based data using NOSQL database tools
ALGORITHM:
1. Choose a NoSQL database tool that supports the type of data model you need
(document, column, or graph). Examples of popular NoSQL database tools include
MongoDB, Cassandra, and Neo4j.
2. Install and configure the NoSQL database tool according to the instructions provided
by the vendor. This typically involves setting up a database instance, defining security
settings, and configuring the database for optimal performance.
3. For document-based data, create a database and collection within the NoSQL database
tool. Define the structure of your documents using a schema or by allowing the database
to create a flexible schema on-the-fly. Insert documents into the collection using the
tool's API or a client application.
4. For column-based data, create a keyspace and column family within the NoSQL
database tool. Define the structure of your columns using a schema or by allowing the
database to create a flexible schema on-the-fly. Insert columns into the column family
using the tool's API or a client application.
5. For graph-based data, create a graph within the NoSQL database tool. Define the
structure of your nodes and edges using a schema or by allowing the database to create
a flexible schema on-the-fly. Insert nodes and edges into the graph using the tool's API
or a client application.
6. Create indexes and views to optimize queries against your data. NoSQL databases
typically provide flexible indexing options that allow you to optimize queries based on
the type of data you are working with.
7. Use the tool's API or client applications to retrieve and manipulate data stored in the
NoSQL database. The API will vary depending on the specific NoSQL database tool
you are using.
8. Periodically backup your NoSQL database to ensure data is protected in case of system
failure or other issues.
39
9. Monitor performance of the NoSQL database tool to ensure it is performing optimally.
NoSQL databases can be highly scalable and can handle large amounts of data, but it
is important to monitor performance to ensure the database remains responsive as data
volumes grow.
RESULT:
Thus the create document, column and graph based data using NOSQL database tools
has been created and executed successfully.
40
EX NO: 12 DEVELOP A SIMPLE GUI BASED DATABASE
APPLICATION AND INCORPORATE ALL THE ABOVE-
MENTIONED FEATURES
DATE:
AIM:
To develop a simple GUI based database application and incorporate all the above-
mentioned features.
ALGORITHM:
2. Choose a NoSQL database tool that supports the type of data model you need
(document, column, or graph). Examples of popular NoSQL database tools include
MongoDB for document-based data, Apache Cassandra for column-based data, and
Neo4j for graph-based data.
3. Install and configure the NoSQL database tool according to the instructions provided
by the vendor. This typically involves setting up a database instance, defining security
settings, and configuring the database for optimal performance.
4. Design the GUI for your application using the GUI framework of your choice. Your
GUI should allow users to view, add, update, and delete data stored in the NoSQL
database.
5. Implement code to connect to the NoSQL database from your application. This code
will vary depending on the specific NoSQL database tool you are using.
6. Implement code to retrieve and display data from the NoSQL database in the GUI. This
code will also vary depending on the specific NoSQL database tool you are using and
the type of data model (document, column, or graph-based) you are working with.
7. Implement code to add, update, and delete data in the NoSQL database from the GUI.
This code will also vary depending on the specific NoSQL database tool you are using
and the type of data model you are working with.
8. Implement indexing and view options in your application to optimize queries against
the data stored in the NoSQL database. This will involve using the indexing features
provided by your NoSQL database tool.
41
9. Test your application to ensure it is functioning properly and data is being stored and
retrieved correctly from the NoSQL database.
By following these steps, you can develop a simple GUI-based database application that
incorporates document, column, and graph-based data models. The specific steps and tools
used will depend on the requirements of your project and the specific NoSQL database tool
being used.
RESULT:
Thus to Develop a simple GUI based database application and incorporate all the
above-mentioned features has been written and executed successfully.
42