Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Data Definition Language (DDL) commands in SQL are used to define, modify, and manage the structure of database objects like tables, indexes, and views. Here are some common DDL commands:
Creates a new table with specified columns, data types, constraints, and other attributes.
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Department VARCHAR(100),
Salary DECIMAL(10, 2)
);
Modifies an existing table, adding or dropping columns, constraints, or other properties.
-- Add a new column ALTER TABLE Employees ADD Email VARCHAR(100); -- Drop a column ALTER TABLE Employees DROP COLUMN Department;
Removes an existing table and all its data permanently.
DROP TABLE Employees;
Creates an index on one or more columns of a table, which enhances data retrieval performance.
CREATE INDEX idx_LastName ON Employees(LastName);
Removes an existing index from a table.
DROP INDEX idx_LastName ON Employees;
Creates a virtual table based on the result of a SELECT query, providing a convenient way to access complex data.
CREATE VIEW HighSalaryEmployees AS SELECT * FROM Employees WHERE Salary > 50000;
Removes an existing view.
DROP VIEW HighSalaryEmployees;
Creates a new database.
CREATE DATABASE CompanyDB;
Modifies properties of an existing database.
ALTER DATABASE CompanyDB SET RECOVERY SIMPLE; -- Change recovery model
Remember that DDL commands have a significant impact on the structure of your database. Use them with caution, as they can result in permanent changes to your data. Also, keep in mind that syntax and specific options may vary depending on the database management system you’re using. Always refer to your DBMS documentation for accurate details.
