![]() |
VOOZH | about |
Data Definition and Control (DDL, DCL, TCL) in SQL deals with structuring, securing, and managing transactions in a database. DDL defines objects (CREATE, ALTER, DROP), DCL manages access (GRANT, REVOKE), and TCL ensures transaction integrity (COMMIT, ROLLBACK, SAVEPOINT). Together, they control how data is organized, secured, and consistently maintained.
SQL commands are broadly classified into:
A primary key is a column (or set of columns) in a table that uniquely identifies each row. It cannot have duplicate or NULL values, ensuring every record is distinct. Example: student_id in a Students table.
A foreign key is a column (or set of columns) in one table that references the primary key of another table. It is used to maintain relationships between tables and ensure referential integrity. Example: dept_id in the Employees table referencing dept_id in the Departments table.
The DEFAULT constraint assigns a default value to a column when no value is provided during an INSERT operation. This helps maintain consistent data and simplifies data entry.
The UNIQUE constraint ensures that all values in a column (or a group of columns) are distinct and no duplicates are allowed. Unlike a primary key, a table can have multiple UNIQUE constraints, and they can accept NULL values (but only once per column).
Common constraints include:
A trigger is a set of SQL statements that automatically execute in response to certain events on a table, such as INSERT, UPDATE, or DELETE. Triggers help maintain data consistency, enforce business rules, and implement complex integrity constraints.
A stored procedure is a precompiled set of SQL statements stored in the database. It can take input parameters, perform logic and queries, and return output values or result sets. Stored procedures improve performance and maintainability by centralizing business logic.
1. DDL (Data Definition Language):
These commands are used to define and modify the structure of database objects such as tables, indexes, and views. For example, the CREATE command creates a new table, the ALTER command modifies an existing table, and the DROP command removes a table entirely. DDL commands primarily focus on the schema or structure of the database.
Example:
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(50)
);
2. DML (Data Manipulation Language):
These commands deal with the actual data stored within database objects. For instance, the INSERT command adds rows of data to a table, the UPDATE command modifies existing data, and the DELETE command removes rows from a table. In short, DML commands allow you to query and manipulate the data itself rather than the structure.
Example:
INSERT INTO Employees (ID, Name) VALUES (1, 'Alice');
The ALTER command is used to modify the structure of an existing database object. This command is essential for adapting our database schema as requirements evolve.
A composite primary key is a primary key made up of two or more columns. Together, these columns must form a unique combination for each row in the table. It’s used when a single column isn’t sufficient to uniquely identify a record.
Example:
Consider an Orders table where OrderID and ProductID together uniquely identify each record because multiple orders might include the same product, but not within the same order.
CREATE TABLE OrderDetails (
OrderID INT,
ProductID INT,
Quantity INT,
PRIMARY KEY (OrderID, ProductID)
);
1. Clustered Index:
EmployeeID is the clustered index, the rows in the table are stored physically sorted by EmployeeID.2. Non-Clustered Index:
LastName allows fast lookups by last name even if the table is sorted by another column.Temporary tables are tables that exist only for the duration of a session or a transaction. They are useful for storing intermediate results, simplifying complex queries, or performing operations on subsets of data without modifying the main tables.
1. Local Temporary Tables:
# (e.g., #TempTable).2. Global Temporary Tables:
## (e.g., ##GlobalTempTable).Example:
CREATE TABLE #TempResults (ID INT, Value VARCHAR(50));
INSERT INTO #TempResults VALUES (1, 'Test');
SELECT * FROM #TempResults;
1. Standard View:
2. Materialized View:
A sequence is a database object that generates a series of unique numeric values. It’s often used to produce unique identifiers for primary keys or other columns requiring sequential values.
Example:
CREATE SEQUENCE seq_emp_id START WITH 1 INCREMENT BY 1;
SELECT NEXT VALUE FOR seq_emp_id; -- Returns 1
SELECT NEXT VALUE FOR seq_emp_id; -- Returns 2
1. Greater Flexibility:
2. Dynamic Adjustment: Can alter the sequence without modifying the table structure.
3. Cross-Table Consistency: Use a single sequence for multiple related tables to ensure unique identifiers across them.
In short, sequences offer more control and reusability than identity columns.
Constraints enforce rules that the data must follow, preventing invalid or inconsistent data from being entered:
CHECK (Salary > 0)).