Session 5.4 – DCL and Transaction Control
Module 5: PHP Database Connectivity | Duration: 1 hr
Learning Objectives
By the end of this session, students will be able to:
- Understand Data Control Language (DCL) and its purpose
- Create and manage database users
- Grant and revoke privileges using GRANT and REVOKE commands
- Understand and implement database transactions
- Explain ACID properties of transactions
- Use COMMIT, ROLLBACK, and SAVEPOINT for transaction control
- Understand transaction isolation levels
Introduction to DCL
Data Control Language (DCL) is a subset of SQL used to control access to data in a database. DCL commands allow database administrators to manage user permissions and security.
Main DCL Commands
- GRANT: Gives users access privileges to database objects
- REVOKE: Removes access privileges from users
User Management
MySQL allows you to create and manage multiple user accounts with different privilege levels.
Creating Users
Modifying Users
Deleting Users
Viewing Users
GRANT & REVOKE Commands
GRANT and REVOKE commands manage user privileges on database objects.
GRANT Privileges
Common Privileges
- SELECT: Read data
- INSERT: Add new records
- UPDATE: Modify records
- DELETE: Remove records
- CREATE: Create tables/databases
- DROP: Delete tables/databases
- ALTER: Modify table structure
- INDEX: Create/drop indexes
Administrative Privileges
- CREATE USER: Create new users
- GRANT OPTION: Grant privileges
- RELOAD: Reload privileges
- SHUTDOWN: Shutdown server
- PROCESS: View all processes
- SUPER: Administrative tasks
- REPLICATION: Replication privileges
- ALL PRIVILEGES: All privileges
REVOKE Privileges
Flush Privileges
Note: FLUSH PRIVILEGES is needed only when modifying privilege tables directly. GRANT and REVOKE automatically update privileges.
Show Privileges
Database Transactions
A transaction is a sequence of one or more SQL operations treated as a single unit of work. Transactions ensure data consistency and integrity.
What is a Transaction?
A transaction groups multiple SQL statements into a single unit that either succeeds completely or fails completely. This prevents partial updates that could leave the database in an inconsistent state.
Starting Transactions
COMMIT - Save Changes
ROLLBACK - Undo Changes
Complete Transaction Example
Autocommit Mode
By default, MySQL runs in autocommit mode, meaning each statement is automatically committed.
ACID Properties
ACID is an acronym that describes four key properties that guarantee database transactions are processed reliably.
Atomicity
All or Nothing: A transaction is treated as a single unit. Either all operations succeed or all fail.
Consistency
Valid State: A transaction brings the database from one valid state to another, maintaining all defined rules and constraints.
Example: Balance constraints, foreign key relationships, and check constraints must remain valid.
Isolation
Concurrent Execution: Concurrent transactions execute independently without interference. Changes made in one transaction are not visible to other transactions until committed.
Prevents issues like dirty reads, non-repeatable reads, and phantom reads.
Durability
Permanent Changes: Once a transaction is committed, changes are permanent and survive system failures, crashes, or power outages.
Committed data is written to permanent storage and can be recovered.
Transaction Isolation Levels
Isolation levels control the degree to which transactions are isolated from each other. Different levels balance between consistency and performance.
Setting Isolation Level
READ UNCOMMITTED
Lowest Isolation: Transactions can read uncommitted changes from other transactions (dirty reads).
- Allows: Dirty reads, non-repeatable reads, phantom reads
- Best for: Performance-critical scenarios where accuracy is less important
READ COMMITTED
Default in many DBMS: Only committed changes are visible to other transactions.
- Prevents: Dirty reads
- Allows: Non-repeatable reads, phantom reads
- Good balance between performance and consistency
REPEATABLE READ
MySQL Default: Ensures same query returns same results within a transaction.
- Prevents: Dirty reads, non-repeatable reads
- Allows: Phantom reads (in some systems)
- Suitable for most applications
SERIALIZABLE
Highest Isolation: Complete isolation - transactions execute as if they were sequential.
- Prevents: All concurrency issues
- Slowest performance due to locking
- Use when data consistency is critical
Savepoints
Savepoints allow you to create checkpoint within a transaction that you can roll back to without rolling back the entire transaction.
Using Savepoints
Practical Example
Session Summary
Key Points
- DCL commands (GRANT, REVOKE) control database access and security
- User management includes CREATE USER, ALTER USER, and DROP USER
- GRANT assigns privileges to users; REVOKE removes them
- Transactions group SQL operations into atomic units
- COMMIT saves changes; ROLLBACK undoes them
- ACID properties ensure transaction reliability: Atomicity, Consistency, Isolation, Durability
- Isolation levels control concurrent transaction behavior
- Savepoints enable partial rollback within transactions
- Proper privilege management is crucial for database security
- Transactions prevent data inconsistencies in concurrent environments
Next Session Preview
In the next session, we will explore PHP-MySQL connectivity, learning how to connect PHP applications to MySQL databases and execute queries programmatically.