Table of contents
Introduction
Data Definition Language (DDL) is a subset of SQL commands used to define, manage, and manipulate the structure of database objects.
DDL commands are primarily concerned with creating, altering, and dropping database objects like tables, views, indexes, and stored procedures. These commands allow you to define the schema of your database and determine how data is organized and stored.
Here are some of the most common DDL commands in SQL:
CREATE: Used to create database objects, such as tables, views, indexes, and stored procedures.
Example: Creating a new table named "employees."
CREATE TABLE employees ( emp_id INT PRIMARY KEY, emp_name VARCHAR(50), emp_age INT, emp_salary DECIMAL(10, 2) );
ALTER: Used to modify the structure of existing database objects.
Example: Adding a new column "emp_address" to the "employees" table.
ALTER TABLE employees ADD emp_address VARCHAR(100);
DROP: Used to delete or remove database objects from the database.
Example: Dropping the "employees" table.
DROP TABLE employees;
TRUNCATE: Used to remove all records from a table but keeps the table structure intact.We should take utmost care while using this command
Example: Truncating the "employees" table.
TRUNCATE TABLE employees;
RENAME: Used to rename database objects.
Example: Renaming the "employees" table to "staff."
ALTER TABLE employees RENAME TO staff;
COMMENT: Used to add comments to database objects for documentation purposes.
Example: Adding a comment to the "employees" table.
COMMENT ON TABLE employees IS 'This table stores employee information.';
CREATE INDEX: Used to create an index on one or more columns of a table, which enhances query performance.
Example: Creating an index on the "emp_id" column of the "employees" table.
CREATE INDEX idx_emp_id ON employees (emp_id);
DROP INDEX: Used to remove an index from a table.
Example: Dropping the index "idx_emp_id" from the "employees" table.
DROP INDEX idx_emp_id ON employees;
Constraints In SQL
Constraints in SQL are rules or conditions applied to database tables to ensure data integrity and enforce business rules.
As they are rules applied to the table we study them together with DDL commands.
They define restrictions on the data that can be stored in the tables, preventing the insertion of invalid or inconsistent data.
The goal of constraints is to make sure that Data Integrity is achieved.
Constraints help maintain the accuracy, consistency, and reliability of the database. There are several types of constraints in SQL:
Primary Key Constraint:
A primary key constraint ensures that a column or a combination of columns uniquely identify each row in the table.
It ensures that the primary key value cannot be null and must be unique for each row.
A table can have only one primary key.
Example: Defining a primary key on the "emp_id" column of the "employees" table.
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
emp_age INT,
emp_salary DECIMAL(10, 2)
);
Default :
In SQL, a default constraint is used to specify a default value for a column when a new row is inserted into a table, and no value is explicitly provided for that column. The default value is used as a fallback when no other value is supplied during data creation.
CREATE TABLE table_name ( column1 data_type DEFAULT default_value, column2 data_type DEFAULT default_value, -- Other columns... );
Not Null :
- The
NOT NULL
constraint is used in SQL to specify that a column must contain a value and cannot be left empty (NULL) when inserting or updating data in a table. It ensures that the column always has a valid value, and no rows can have NULL in that particular column. TheNOT NULL
constraint is commonly applied to columns that are essential and should always have meaningful data
- The
Example:
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
emp_age INT,
emp_salary DECIMAL(10, 2)
);
Unique Constraint:
A unique constraint ensures that the values in the specified column(s) are unique across all rows in the table.
Unlike the primary key, a unique constraint allows null values (excluding the case when all columns are involved).
Example: Adding a unique constraint on the "email" column of the "users" table.
CREATE TABLE users (
user_id INT PRIMARY KEY,
email VARCHAR(100) UNIQUE,
user_name VARCHAR(50),
-- Other columns...
);
Foreign Key Constraint:
A foreign key constraint establishes a relationship between two tables based on a column(s) in one table referring to the primary key of another table.
It enforces referential integrity, ensuring that values in the foreign key column(s) exist in the primary key column of the referenced table.
Example: Creating a foreign key constraint on the "dept_id" column of the "employees" table, referring to the "dept_id" column of the "departments" table.
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
emp_age INT,
dept_id INT,
-- Other columns...
FOREIGN KEY (dept_id) REFERENCES departments(dept_id)
);
CREATE TABLE departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(100)
-- Other columns...
);
- Auto Increment: In SQL, the concept of auto-increment (or auto-incrementing) is typically associated with generating unique, sequential values for a column automatically when new rows are inserted into a table. This is often used for primary key columns to ensure that each new row gets a unique identifier.
Example:
CREATE TABLE employees (
emp_id INT IDENTITY(1,1) PRIMARY KEY,
emp_name VARCHAR(50),
emp_age INT,
-- Other columns...
);
Check Constraint:
A check constraint defines a condition that must be true for each row in the table.
It restricts the data that can be inserted or updated based on the specified condition.
Example: Adding a check constraint on the "emp_age" column of the "employees" table to ensure that the age is greater than or equal to 18.
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
emp_age INT CHECK (emp_age >= 18),
emp_salary DECIMAL(10, 2)
);
Named Constraints v/s Constraints
In SQL, the main difference between named and unnamed (or normal) constraints lies in how they are identified and referenced in the database schema. Let's compare named and unnamed constraints:
Naming Convention:
Named Constraints: As the name suggests, named constraints are explicitly given a unique name when they are created. The user specifies the name for the constraint during its creation, and that name is used to identify and reference the constraint in the database schema.
Unnamed (Normal) Constraints: Unnamed constraints, also known as normal constraints, are not given specific names during their creation. Instead, the database system automatically generates a system-generated name for the constraint. These names are usually not meaningful and can be challenging to interpret.
Deleting: If we use named constraints they can be deleted without deleting the table.
Combination of Constraints :
When we use unnamed constraints we can't generate a combination of constraints.
ex: In a table, we want the combination of name and student_id to be unique. If we try to write this using unnamed constraints it will be name unique student_id unique but not the combination of both.
Readability and Understandability:
Named Constraints: Named constraints enhance the readability and understandability of the database schema. The use of meaningful names makes it easier for developers and database administrators to interpret the purpose and intent of the constraint.
Unnamed (Normal) Constraints: Unnamed constraints may make the schema harder to understand, especially when there are multiple constraints on a single table. Developers may need to investigate system-generated names to understand the purpose of each constraint.
Maintenance:
Named Constraints: Named constraints are easier to manage and maintain. If you need to modify or drop a specific constraint in the future, you can directly refer to it by its name.
Unnamed (Normal) Constraints: Unnamed constraints may be harder to manage, especially if you want to modify or drop a specific constraint. You may need to rely on system-generated names, which can be cryptic and subject to change between database versions.
Error Handling:
Named Constraints: When an error occurs due to a constraint violation, named constraints make it easier to identify the specific constraint that caused the issue, as the error messages usually include the constraint name.
Unnamed (Normal) Constraints: Error messages related to unnamed constraints may refer to system-generated names, which might not be immediately recognizable, leading to extra effort in identifying the problematic constraint.
Example of a named constraint (primary key):
CREATE TABLE employees (
emp_id INT,
emp_name VARCHAR(50),
CONSTRAINT pk_employees PRIMARY KEY (emp_id)
);
Example of an unnamed (normal) constraint (primary key):
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50)
);