5.9 KiB
DDL, DQL, DML, DCL, TCL
SQL provides the standard interface for interacting with databases. Understanding its command categories is fundamental for building applications that manage and retrieve data efficiently. This document outlines the core SQL command types and their practical use cases for Java developers.
Data Definition Language (DDL )
DDL commands manage database schema structure. They define, modify, or drop database objects. Developers typically use DDL during application setup, migration scripts, or when evolving the data model.
DDL Commands:
CREATE: Constructs a new database, table, index, view, stored procedure, or trigger. Defines the schema for persistent data storage.
CREATE TABLE Persons (
PersonID int,
LastName varchar(25 5),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
DROP: Deletes objects from the database. Irreversible and should be used with caution, particularly in production environments.
DROP TABLE table_name;
ALTER: Modifies the structure of existing database objects, such as adding, deleting, or modifying columns in a table. Essential for schema evolution.
ALTER TABLE Persons
ADD Age int;
TRUNCATE: Removes all rows from a table. This operation is significantly faster thanDELETEfor clearing an entire table because it logs fewer operations and resets the table's identity column.
TRUNCATE TABLE Persons;
COMMENT: Adds descriptive text to the data dictionary. Useful for documenting schema elements.
-- Example of an inline comment
SELECT * FROM Persons;
RENAME: Changes the name of a database object.
ALTER TABLE Persons
RENAME COLUMN Age TO Year;
Data Query Language (DQL)
DQL commands retrieve data from the database based on specified criteria. The SELECT statement is the primary command. Understanding DQL is critical for fetching the necessary information to populate application interfaces or perform backend logic.
DQL Command:
SELECT: Fetches data from one or more tables. The foundation for most read operations in applications.
SELECT * FROM table_name;
Example Result Set:
+--------+--------------+------------+--------+---------+
| emp_id | emp_name | hire_date | salary | dept_id |
+--------+--------------+------------+--------+---------+
| 1 | Ethan Hunt | 2001-05-01 | 5000 | 4 |
| 2 | Tony Montana | 2002-07-15 | 6500 | 1 |
| 3 | Sarah Connor | 2005 -10-18 | 8000 | 5 |
| 4 | Rick Deckard | 2007-01-03 | 7200 | 3 |
| 5 | Martin Blank | 2008-06-24 | 5600 | NULL |
+--------+--------------+------------+--------+---------+
Data Manipulation Language (DML)
DML commands manage the data within schema objects. They perform insert, update, and delete operations. DML operations form the core of application logic interacting with the database's contents.
DML Commands :
INSERT: Adds new rows of data into a table. Essential for persisting new records created by the application.
INSERT INTO Customers
(CustomerName, ContactName, Address, City, PostalCode, Country )
VALUES
('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
UPDATE: Modifies existing data within a table. Used to reflect changes to records from the application.
UPDATE Customers
SET ContactName='Alfred Schmidt', City='Frankfurt'
WHERE CustomerID = 1;
DELETE: Removes one or more rows from a table. UseWHEREclauses carefully to avoid unintended data loss.
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
``` * `LOCK`: Controls concurrent access to tables. Important for managing transactional integrity in high-concurrency applications.
```sql
LOCK TABLES table_name [READ | WRITE]
UNLOCK TABLES;
CALL: Executes a stored procedure or function. Useful for encapsulating complex database logic.
-- Example Stored Procedure Definition (database specific syntax may vary)
CREATE PROCEDURE procedure_name
AS sql_statement
GO ;
-- Example Execution
EXEC procedure_name;
EXPLAIN PLAN: (Database specific, e.g., Oracle, PostgreSQL) Provides details on how the database will execute a query. In valuable for performance tuning.
Data Control Language (DCL)
DCL commands manage database security by defining privileges and permissions for users. GRANT and REVOKE control user access to database objects and capabilities.
DCL Commands :
GRANT: Assigns specific access privileges on database objects to users or roles.
-- Grant SELECT privilege on the Persons table to a user
GRANT SELECT ON Persons TO 'username';
REVOKE: Removes previously granted access privileges.
-- Revoke the SELECT privilege on the Persons table from a user
REVOKE SELECT ON Persons FROM 'username';
Transaction Control Language (TCL)
TCL commands manage transactions within a database. A transaction is a sequence of operations performed as a single logical unit of work. TCL ensures data integrity by guaranteeing that transactions are atomic, consistent, isolated, and durable (ACID).
TCL Commands:
COMMIT: Makes all changes performed in the current transaction permanent. Essential after a series of successful DML operations.ROLLBACK: Undoes all changes performed in the current transaction since the lastCOMMITorSAVEPOINT. Used to revert changes if an error occurs or business logic dictates.SAVEPOINT: Sets a marker within a transaction. Allows rolling back to a specific point without rolling back the entire transaction.SET TRANSACTION: Configures characteristics of the current transaction, such as isolation level.
These command categories provide the fundamental building blocks for database interaction. Mastery of DDL, DQL, DML, DCL, and TCL empowers developers to build robust, efficient, and secure database-driven applications.