TQL Notes
TQL Notes
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
FROM table1
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.
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.
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:
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;
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;
START TRANSACTION;
INSERT INTO student (name, age, class) VALUES ('Michael Johnson', 16,
'11A');
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.
SET autocommit=0;