RDBMS LAB USING ORACLE
Ex: 1 DDL COMMANDS
AIM:
To understand and perform basic Data Definition Language (DDL) operations in SQL such
as:
• Creating tables
• Altering tables
• Dropping tables
• Truncating tables
• Renaming tables
Command Description
CREATE Creates a new table or database
ALTER Modifies an existing database object
DROP Deletes database objects
TRUNCATE Removes all records from a table
RENAME Renames a database object
ALGORITHM:
Step 1: Start
Step 2: Connect to the Database System
• Open your SQL interface.
• Connect to the appropriate database server.
Step 3: Create a Table
• Use the CREATE TABLE command to define the table structure.
Step 4: Alter the Table
• The ALTER TABLE command is used to make changes to an existing table without
deleting it or creating a new one.
Step 5: Rename the Table
• Use the RENAME command to change the table name.
Step 6: Truncate the Table
• Use the TRUNCATE command to remove all records from the table.
Step 7: Drop the Table
• Use the DROP command to delete the table structure:
Step 8: Disconnect from the Database
• Close the SQL session/connection after completion.
1
Step 9: End
CODING:
1. CREATE Table
CREATE TABLE Employees (
EmpID INT PRIMARY KEY,
EmpName VARCHAR(100),
Salary DECIMAL(10, 2),
Dept VARCHAR(50)
);
2. ALTER Table
• Add a new column:
ALTER TABLE Employees ADD JoiningDate DATE;
• Modify a column:
ALTER TABLE Employees MODIFY Salary DECIMAL(12, 2);
• Drop a column:
ALTER TABLE Employees DROP COLUMN Dept;
3. RENAME Table
RENAME TABLE Employees TO Staff;
4. TRUNCATE Table
TRUNCATE TABLE Staff;
5. DROP Table
DROP TABLE Staff;
RESULT:
All DDL commands were successfully executed. The structure of the database was
created, modified, and deleted as expected without any errors.
2
EX:2 DML COMMANDS
AIM:
To perform Data Manipulation Language (DML) operations such as INSERT,
UPDATE, DELETE, and SELECT on database tables using SQL commands.
ALGORITHM:
1. Start the SQL database server and open the SQL editor.
2. Create a sample table (if not already created), for example, Students.
3. Insert new records into the table using the INSERT command.
4. Retrieve records from the table using the SELECT command.
5. Update existing records using the UPDATE command.
6. Delete specific records using the DELETE command.
7. Use SELECT again to verify changes made by UPDATE and DELETE commands.
8. End the session after verifying the results.
CODING:
CREATE the Students table:
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Department VARCHAR(50)
);
INSERT records:
INSERT INTO Students VALUES (1, 'Alice', 20, 'Computer Science');
INSERT INTO Students VALUES (2, 'Bob', 22, 'Mechanical');
INSERT INTO Students VALUES (3, 'Charlie', 19, 'Electrical');
SELECT all records:
SELECT * FROM Students;
UPDATE a record:
UPDATE Students SET Age = 21 WHERE StudentID = 1;
DELETE a record:
DELETE FROM Students WHERE StudentID = 3;
Select all records to verify changes:
SELECT * FROM Students;
3
Sample Output:
SELECT * FROM Students; (before update/delete):
StudentID Name Age Department
1 Alice 20 Computer Science
2 Bob 22 Mechanical
3 Charlie 19 Electrical
UPDATE Students SET Age = 21 WHERE StudentID = 1;
and
DELETE FROM Students WHERE StudentID = 3;
After UPDATE/DELETE:
SELECT * FROM Students;
StudentID Name Age Department
1 Alice 21 Computer Science
2 Bob 22 Mechanical
RESULT:
Records were inserted, updated, and deleted successfully in the Students table. The
final output displayed the correct, updated data as expected.
4
EX:2 TCL COMMANDS
AIM:
To understand and perform basic Transaction Control Language (TCL) operations in SQL
such as:
• COMMIT
• ROLLBACK
• SAVEPOINT
Command Purpose
BEGIN / START
Begins a new transaction block
TRANSACTION
SAVEPOINT name Defines a checkpoint to which you may roll back
ROLLBACK Undoes all changes since the last commit or start
ROLLBACK TO name Partially undoes changes back to a savepoint
Removes a named savepoint; can’t roll back to it
RELEASE SAVEPOINT name
afterward
COMMIT Permanently saves all changes since start
Sets properties like your isolation level or read/write
SET TRANSACTION
mode
ALGORITHM:
Step 1: Start
Step 2: Connect to the Database System
• Open your SQL interface.
• Connect to the appropriate database server.
Step 3: Create a Table
• Use the CREATE TABLE command to define the table structure.
Step 4: Alter the Table
• The ALTER TABLE command is used to make changes to an existing table without
deleting it or creating a new one.
Step 5: Perform COMMIT
• Setup
DROP TABLE IF EXISTS accounts;
5
CREATE TABLE accounts ( account_id INT PRIMARY KEY, account_name
VARCHAR(20), balance DECIMAL(10,2) );
INSERT INTO accounts VALUES (1, 'Alice', 1000.00), (2, 'Bob', 500.00);
• Perform a transfer
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;
UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;
COMMIT;
• Verify balances
SELECT * FROM accounts ORDER BY account_id;
Step 6: Perform ROLLBACK
• Reset state
UPDATE accounts SET balance = CASE
WHEN account_id = 1 THEN 1000.00
WHEN account_id = 2 THEN 500.00
END;
• Start a transaction with update
START TRANSACTION;
UPDATE accounts SET balance = balance - 200 WHERE account_id = 1;
ROLLBACK;
• Check balances after rollback
SELECT * FROM accounts ORDER BY account_id;
Step 7: Perform Savepoint
• Reset balances
UPDATE accounts SET balance = CASE
WHEN account_id = 1 THEN 1000.00
WHEN account_id = 2 THEN 500.00
6
END;
• Begin transaction & create savepoint
BEGIN;
SAVEPOINT sp1;
UPDATE accounts SET balance = balance - 300 WHERE account_id = 1;
UPDATE accounts SET balance = balance + 300 WHERE account_id = 3;
• On failure, rollback to savepoint, then commit the rest
ROLLBACK TO SAVEPOINT sp1;
UPDATE accounts SET balance = balance + 300 WHERE account_id = 2;
COMMIT;
• Final check
SELECT * FROM accounts ORDER BY account_id;
Step 8: Disconnect from the Database
• Close the SQL session/connection after completion.
Step 9: End
EXPECTED OUTPUT:
1) COMMIT
account_id account_name balance
1 Alice 900.00
2 Bob 600.00
2) ROLLBACK
account_id account_name balance
1 Alice 1000.00
2 Bob 500.00
3) SAVEPOINT
account_id account_name balance
1 Alice 700.00
7
account_id account_name balance
2 Bob 800.00
RESULT:
The TCL commands COMMIT, ROLLBACK, and SAVEPOINT, are essential for
managing transactions in SQL databases have been executed. The final output displayed the
correct, updated data as expected.
8
9