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

TQL Notes

Uploaded by

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

TQL Notes

Uploaded by

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

Understanding SQL Natural Join

A Natural Join in SQL is a type of join that automatically links tables based on columns with the same names
and compatible data types. It's a subset of the EQUI JOIN where the join condition is based on all columns in
both tables that have identical names. In a natural join, there is no need to specify the column names explicitly,
and the joined table will only include one column for each pair of identically named columns.
SQL Natural Join Syntax

The basic syntax for a natural join in SQL is as follows:


SELECT *

FROM table1

NATURAL JOIN table2;

This query selects all columns from table1 and table2 where the column names and data types match.
Key Characteristics of Natural Join

 Automatically joins tables: It uses the common columns from the joined tables for the join condition.
 Eliminates duplicate columns: In the result set, each pair of equally named columns appears as a single
column.

TQL (Transaction Query Language) in MySQL

Overview of TQL (Transaction Query Language)

Transaction Query Language (TQL) in MySQL is used to manage transactions in a relational database. A
transaction is a sequence of one or more SQL statements that are executed as a single unit of work.
Transactions ensure that the database remains in a consistent state, even in the event of a failure.

Transactions are essential for maintaining the integrity of data in databases that involve multiple operations
that must all succeed or fail together. TQL commands help control the execution of these transactions.

Key TQL Commands in MySQL

1. START TRANSACTION: Begins a new transaction.


2. COMMIT: Saves all changes made during the current transaction.
3. ROLLBACK: Reverts all changes made during the current transaction.
4. SAVEPOINT: Creates a point within a transaction to which you can later roll back.
5. RELEASE SAVEPOINT: Removes a savepoint.

1. START TRANSACTION

 This command is used to begin a new transaction. After this command, all SQL operations are
treated as a single transaction until a COMMIT or ROLLBACK is issued.

Syntax:

START TRANSACTION;

Example:
START TRANSACTION;
INSERT INTO student (name, age, class) VALUES ('John Doe', 15, '10B');
UPDATE marks SET marks_obtained = 90 WHERE student_id = 1 AND subject_id = 2;

2. COMMIT

 The COMMIT command is used to permanently save all changes made during the current transaction.
Once committed, the changes cannot be undone.

Syntax:

COMMIT;

Example:

COMMIT;

This command would save the changes made by the INSERT and UPDATE commands in the example above.

3. ROLLBACK

 The ROLLBACK command is used to undo all changes made during the current transaction. This is
useful if an error occurs, and you want to revert the database to its previous state.

Syntax:

ROLLBACK;

Example:

ROLLBACK;

This command would undo the changes made by the INSERT and UPDATE commands in the START
TRANSACTION example above.

4. SAVEPOINT

 The SAVEPOINT command is used to set a point within a transaction to which you can later roll back.
This allows for partial rollbacks within a transaction.

Syntax:

SAVEPOINT savepoint_name;

Example:

START TRANSACTION;
INSERT INTO student (name, age, class) VALUES ('Jane Smith', 14, '9A');
SAVEPOINT sp1; -- Create a savepoint after the first insertion
INSERT INTO marks (student_id, subject_id, marks_obtained) VALUES (2, 1, 85);
ROLLBACK TO sp1; -- Rollback to the savepoint, undoing the second insertion
COMMIT; -- Commit the transaction up to the savepoint

5. RELEASE SAVEPOINT

 The RELEASE SAVEPOINT command is used to delete a savepoint that you no longer need. Once
released, you cannot roll back to that savepoint.
Syntax:

RELEASE SAVEPOINT savepoint_name;

Example:

START TRANSACTION;
INSERT INTO student (name, age, class) VALUES ('Emma Brown', 15, '10C');
SAVEPOINT sp1;
INSERT INTO marks (student_id, subject_id, marks_obtained) VALUES (3, 2, 88);
RELEASE SAVEPOINT sp1; -- Remove the savepoint
COMMIT;

Examples to Implement TQL Commands

1. Atomic Update Example


o Suppose you want to update a student's marks in multiple subjects atomically:

START TRANSACTION;
UPDATE marks SET marks_obtained = 75 WHERE student_id = 4 AND subject_id =
1;
UPDATE marks SET marks_obtained = 80 WHERE student_id = 4 AND subject_id =
2;
COMMIT;

2. Error Handling with ROLLBACK


o If an error occurs while inserting data, use ROLLBACK:

START TRANSACTION;
INSERT INTO student (name, age, class) VALUES ('Michael Johnson', 16,
'11A');

-- Suppose this insert fails due to a foreign key constraint


INSERT INTO marks (student_id, subject_id, marks_obtained) VALUES (5, 99,
90);

ROLLBACK; -- Undo all changes if an error occurs

3. Using SAVEPOINT for Partial Rollback


o Save a point in the transaction and roll back only to that point if necessary:

START TRANSACTION;
INSERT INTO student (name, age, class) VALUES ('Sarah Connor', 17, '12A');
SAVEPOINT sp1;
INSERT INTO marks (student_id, subject_id, marks_obtained) VALUES (6, 3,
95);
-- Suppose you decide to undo the second insert
ROLLBACK TO sp1; -- Only the marks entry is undone
COMMIT;

Conclusion

Transaction Query Language (TQL) commands in MySQL provide powerful tools to manage transactions,
ensuring data consistency and integrity. By using commands like START TRANSACTION, COMMIT, ROLLBACK,
SAVEPOINT, and SET TRANSACTION, you can control how and when changes are made permanent in the
database. This is essential for handling complex, multi-step operations securely and reliably.

Notes:
SET autocommit disables or enables the default autocommit mode for the current session. By default,
the autocommit = 1. Which means it is enabled.

To disable autocommit mode explicitly, use the following statement:

SET autocommit=0;

You might also like