Practical - RDMS
Practical - RDMS
Submitted By:
B.Tech ECM Sem-5
(Batch 2022-26)
LIST OF PRACTICALS
S.No Name of Practical
1 Create a table named as Stu_info with columns
as Roll_No, Name, Ph_no, Email_id.
Theory of Practical:
In SQL, creating a table is a fundamental operation used to define the structure of
the data you want to store in a database. To create a table, we use the CREATE
TABLE statement, followed by the table name and the columns with their
respective data types. This structure is essential for defining how data is stored
and accessed in the database.
Roll_no: This column will store unique student roll numbers. It is an integer,
as roll numbers are typically numeric.
Names: This column will store students' names. We'll use a text or character-
based data type to accommodate various names.
Ph_no: This column will store students' phone numbers. It can also be
represented as a string to accommodate any formatting (like country codes).
Email_id: This column will store email addresses, so a text or character-
based data type will be used.
Names VARCHAR(50),
Ph_no (INT),
Email_id VARCHAR(50)
);
EXPLANATION:
1.CREATE TABLE Stu_info: This command creates a new table named
Stu_info.
2.Roll_no INT PRIMARY KEY: This column Roll_no is an integer and is set as
the primary key (unique for each student).
3.Names VARCHAR(50): This column Names stores the name of the student.
VARCHAR(100) allows names of up to 50 characters.
Workbench Output:
In this output:
Field represents the column names.
Type indicates the data type of each column.
Null shows whether the column can store NULL values.
Key displays whether the column is part of any key constraints (e.g., PRI
for primary key).
Default shows the default value for the column if not specified.
Extra includes any additional constraints or properties.
Aim: Create a table named as Course_Enrolled
with columns as Roll_No, Department, Name.
Theory of Practical
This SQL program demonstrates fundamental database operations such as table
creation, data insertion, and data retrieval.
Table Creation: A table in SQL is a structured set of data that consists of rows
and columns. In this case, the table Course_Enrolled is designed to store
information about students enrolled in various departments.
Roll_No: It is designated as the Primary Key, meaning it must be unique for each
record and cannot be NULL.
Name: A text field representing the name of the student. It is marked as NOT
NULL, meaning that this field must have a value.
Data Insertion: After defining the table structure, the program proceeds to insert
records into the Course_Enrolled table. Each INSERT INTO statement adds a
new student record with values for Roll_No(which is unique), name, and
Department.
Data Retrieval: Finally, the program includes a SELECT statement to fetch and
display all records in the Course_Enrolled table. This is useful for verifying that
the data was inserted correctly. This command retrieves all columns (*) for each
record in the Course_Enrolled table, displaying the information about each
enrolled student.
Creating Table
Inserting Values
Output
Aim: Truncate the above created tables.
Theory of Practical
Structured Query Language is a standard Database language that is used to
create, maintain, and retrieve the relational database. In this article, we will
discuss this in detail about SQL. Following are some interesting facts about
SQL. Let‟s focus on that.
Creating Table 1
INSERT INTO: This command inserts new data into a specified table.
Output
Roll_no INT: Defines a column named Roll_no of type INT, which will
store the roll number of the student.
INSERT INTO: This command inserts new data into a specified table.
Output
The TRUNCATE TABLE command deletes the data inside a table, but not the
table itself.
Theory of Practical
This experiment focuses on understanding and applying fundamental database
concepts using SQL (Structured Query Language) for data definition and data
manipulation. By working with relational tables, we learn the following core
database principles:
2. Primary Key: The primary key is a unique identifier for each record in a
table, ensuring that each row can be referenced individually. In this
experiment, the Roll_No field in the Stu_info table serves as the primary
key, uniquely identifying each student.
3. Foreign Key: A foreign key is a field in one table that uniquely identifies a
row in another table, creating a relationship between the two tables. Here,
the Roll_No in the Course_Enrolled table is a foreign key that references
Roll_No in Stu_info. This foreign key constraint enforces referential
integrity, ensuring that each enrollment record corresponds to a valid
student in Stu_info.
4. Data Insertion: Inserting rows into tables helps populate the database with
sample data. This step simulates real-world data entry, allowing for further
operations like querying and updating.
Procedure
Step 1: Create the Stu_info Table.
Table Name: Stu_info – This table will store basic information about
students.
Columns:
• Roll_No (INT): A unique identifier for each student, acting as the primary
key.
• Name (VARCHAR(50)): The name of the student, with a maximum of 50
characters, and cannot be null.
• Ph_no (VARCHAR(15)): The student's phone number, allowing up to 15
characters to accommodate various formats.
Using PRIMARY KEY on Roll_No ensures that each student has a unique roll
number in the Stu_info table.
• Columns:
o Roll_No (INT): This column links each entry to a specific student in the
Stu_info table using their roll number. o Department (VARCHAR(50)):
The department in which the student is enrolled, allowing up to 50
characters.
Theory of Practical
To insert 15 rows into a table named `course_enrolled` with
the following department (`dept`) values:
Requirements
- A database table named `course_enrolled` with at least one column `dept` to
store department values.
- Basic knowledge of SQL INSERT INTO statements.
Background
The `INSERT INTO` statement in SQL is used to insert data into a table. You
can:
1. Insert a single row using:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2,
...);
Steps
1. Connect to the database containing the `course_enrolled` table.
2. Prepare an `INSERT INTO` SQL statement to add rows with the specified
department values.
3. Execute the SQL statements to insert 15 rows:
- Insert 5 rows with dept = "CSE"
- Insert 5 rows with dept = "Punjabi"
- Insert 5 rows with dept = "Electronics"
SQL Queries
To insert the data into the table, use the following SQL statements:
Screenshots
Screenshot of the SQL Query execution in MySQL
Workbench:
Conclusion
This experiment demonstrates the use of SQL `INSERT INTO` statements to
populate a table with specific values across multiple rows. This process is
fundamental for database management tasks involving data insertion.
Aim: Select all the rows from table
Course_enrolled in which dept value is CSE.
Theory of Practical
In this experiment, we aim to retrieve specific data from a database using SQL
queries. SQL (Structured Query Language) is used to interact with databases,
enabling the retrieval, insertion, updating, and deletion of data.The focus of this
experiment is to select all rows from the Course_enrolled table where the dept
column equals CSE. This type of query is a basic SELECT statement with a
WHERE clause that filters results based on a specified condition.
Key Concepts:
1)SELECT Statement: Used to fetch data from a database.
2)WHERE Clause: Filters records to only include rows that meet the specified
condition.
SQL Query:
Here's the SQL query to select all rows from Course_enrolled where the dept is
CSE:
Example Result: Imagine the Course_enrolled table has the following sample
data
Theory of Practical
This practical is designed to demonstrate the process of retrieving specific data
from a table in a relational database using SQL (Structured Query Language).
SQL is the standard language used for managing and manipulating relational
databases. In this case, we are performing a simple *SELECT* operation to fetch
specific columns (name and address) from a table called stu_info.
The experiment aims to help understand the process of querying specific data
from a table, focusing on:
3. *Understanding SQL Query Syntax*: Writing and executing a valid SQL query
helps understand the interaction between the query and the RDBMS.
SQL Query:
1. *SELECT*: This keyword tells the RDBMS which columns to fetch. In this
case, it specifies two columns: name and address.
2. *name, address*: These are the names of the columns you wish to retrieve from
the stu_info table.
3. *FROM stu_info*: This specifies the table from which to retrieve the data, i.e.,
stu_info. When you run this query, it will return all the rows from the Stu_info
table but only show the Names and Addresses columns. This is useful when you
only need specific information rather than all the data in the table.
Sample Input:
To select the address and name from a database using SQL, you would typically
use a query like this:
1. SELECT: This keyword is used to specify which columns you want to retrieve
from the database.
2. name, address: These are the specific columns you want to retrieve. You can
list multiple columns separated by commas.
3. FROM: This keyword indicates the table from which you want to select the
data.
4. your_table_name: This is a placeholder for the actual name of the table where
the data is stored. You should replace this with the actual table name in your
database.
When you run this query, it will return all the rows from the specified table but
only show the name and address columns. This is useful for retrieving specific
information without pulling in all the data from the table.
Sample Output
This output shows the names and addresses of all records in the table, allowing
you to see just the information you requested without any other irrelevant data.
Aim: Select details of students from Stu_info and order
them by their names.
Theory of Practical
ORDER BY clause is a useful statement in SQL that sorts the output of a query in
a specific order. This makes it more accessible to analyze and view the data in an
ordered manner.
Descending (DESC): The inverted sequence, in which numbers are arranged from
highest to lowest (9, 8, 7) and text data are arranged in reverse alphabetical order
(Z-A).
● Descending order
Theory of Practical
We will start by creating a table called stu_info with columns for Roll_no, Name
and Email_id. Then ,we will select these columns to create a new table named
Student with the same structure and data.
SQL Query
To achieve this,we can write the SQL Query as following:
STEP 1: Create a table stu_info and add the required data into it as :
describe stu_info;
STEP 2: Create a table named as student and insert the values i.e roll_no,name
and email by selecting from stu_info table
describe student;
--insert into student (roll_no, name,email_id ) select roll_no, name, email_id from
stu_info
Theory of Practical
The UPDATE statement is used to modify the values in one or more columns of
an existing row in a table. To update rows in a table based on a specific condition,
we use the UPDATE SQL command. In this case, we want to update all rows in
the course_enrolled table where the Course_name column has the value 'CSE',
changing it to 'DCSE'.
Procedure
Step 1: Create the Course_Enrolled Table.
Column definitions:
After updation select * from course_enrolled; command retrieves all the data of
table after the changes.
The rows where course_name was 'CSE' have been updated to 'DCSE'.
Other rows, such as those with MECH and ECE, remain unchanged.
This output demonstrates how the UPDATE statement changes only the rows that
meet the WHERE condition.
Aim: Add a new column named as Aggr_perc
into Course_enrolled.
Theory of Practical
Structured Query Language(SQL) is a standard language for database
creation and manupilation. MySQL is a relational database program that uses SQL
queries.
In SQL the CREATE TABLE statement is used to define and create a new table
within the relational database . Tables are the fundamental components of a
database , used to store structured data in rows and columns .
Using UPDATE statement in the tables can modify the table such as we can add ,
delete , update the rows and columns of the table .
SQL Query:
_ _ create
);
_ _ insert
_ _ fetching
SELECT*FROM course_enrolled;
OUTPUT:
Aim: Delete the column named as Name from
table Course_enrolled.
Theory of Practical
In SQL, the ALTER TABLE statement allows modifications to the structure of an
existing table, which includes adding, modifying, or deleting columns. This
experiment specifically demonstrates the use of the ALTER TABLE command to
remove an unnecessary column, effectively cleaning up the database structure.
DELETING COLUMN
Theory of Practical
In this experiment, you will perform several operations that are fundamental to
SQL database management. The main aim is to retrieve the details of the student
with the second-highest Roll_No.
2.Table Renaming: Renaming a table can be useful for standardising table names,
improving readability, or adapting to new requirements. The ALTER TABLE
command in SQL allows you to rename an existing table.
3.Insert sample data into Stu_info to create records of students with unique
Roll_Nos.
4.Use the ALTER TABLE command to rename Stu_info to Student.
5.To find the second highest Roll_No, use a subquery to identify the maximum
Roll_No that is less than the absolute maximum Roll_No in the table.
SQL QUERY:
Conclusion:
This experiment demonstrates how to create and manipulate a table, rename it, and
retrieve specific data using subqueries in SQL. Finding the second-highest value is
a common use case in databases, and this experiment showcases the importance of
subqueries and aggregate functions in data retrieval tasks.
Aim: Drop the Stu_info table if already exists
and then create the new table Stu_info with
Roll_no values as unique and not null.
Theory of Practical
In summary: This line makes sure the Stu_info table is removed if it exists, so we
can create it from scratch.
Now, let‟s break down each part of the CREATE TABLE statement:
Each column in a SQL table is defined with a name, a data type, and optional
constraints. Let‟s go through each column in Stu_info:
These constraints help to enforce data integrity, meaning they ensure the data
stored in the Stu_info table is accurate and consistent. This setup prevents
duplicate or incomplete entries for roll numbers, which are crucial for identifying
students uniquely.
The constraints on roll_no guarantee that each student has a unique, non-empty roll
number, ensuring data consistency in the database.
Aim: Write a SQL procedure to show the use of
cursors.
Theory of Practical
Cursors in SQL:
A cursor in SQL is a database object used to retrieve, manipulate, and process
rows from a result set one at a time. It provides a way to work with individual rows
returned by a query, which is especially useful when operations cannot be
efficiently handled with a single SQL statement (i.e., when row-by-row processing
is required).
SQL QUERY:
Step 1: Create Sample Tables
First, we need to create some sample tables. For this demonstration, let's use a
table for Employees.
------|----------------|--------
Explanation:
1. Cursor Declaration: The cursor employee_cursor is declared to select
employees with a salary less than 60,000.
2. Looping Through Records: The cursor fetches each record (employee) one
by one. The salary is updated for each employee inside the loop.
3. Deallocating Resources: Once the cursor finishes processing all the rows, it
is closed and deallocated to release system resources.
Aim: Write a SQL procedure to show the use of
triggers.
Theory of Practical
This assignment demonstrates the usage of SQL triggers in a MySQL database
environment. The example provided here explains a procedure where triggers
automatically log changes to a user‟s table by recording modifications in a
user_changes table. This is particularly useful for auditing or tracking changes
within the database system.
username VARCHAR(100),
email VARCHAR(100),
);
user_id INT,
old_email VARCHAR(100),
new_email VARCHAR(100),
DELIMITER $$
BEGIN
END IF;
END$$
DELIMITER ;
Explanation
-- Insert a user
OUTPUT
The user_changes table will log the email change as follows:
Theory of Practical
In relational database management systems (RDBMS), triggers are special stored
procedures that are automatically executed, or "triggered," by events such as
INSERT, UPDATE, or DELETE on a specific table. Triggers are powerful tools in
SQL that help automate certain actions or enforce rules, ensuring data consistency
and integrity.
Types of Triggers
1. BEFORE Trigger: Executes before the event (INSERT, UPDATE,
DELETE) is applied. Useful for validation.
2. AFTER Trigger: Executes after the event has been applied. Useful for
logging, auditing, or performing secondary actions.
Steps:
1. Create two tables: employees and employee_audit.
2. Create a trigger that fires after a new employee is inserted, logging
information into employee_audit.
SQL Query
-- Step 1: Create the employees table
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
name VARCHAR(50),
position VARCHAR(50),
salary DECIMAL(10, 2)
);
After the insertion, the employee_audit table will have a new row
automatically added by the trigger:
Output:
Here, a new audit entry has been created with the employee_id and action as
INSERT, along with the current timestamp.
Summary
Theory: Triggers automate actions based on events in RDBMS tables,
enhancing data integrity and logging capabilities.
Query: Created a trigger to log INSERT actions in an audit table.
Output: Verified the automated audit entry after an INSERT into the
employees table.