Commands of DDL (Data Definition Language) with Example

DDL (CREATE, TRUNCATE, ALTER, RENAME, DROP TABLE) commands with example

Commands of DDL Statements with Example

Data Definition Language (DDL) is the structural cornerstone of SQL—it’s how you define, shape, and maintain the architecture of your database. Just like an architect uses blueprints to design buildings, Data Defination commands allow you to define the schema of the application database with precision. In this article we will discuss the commands with DDL example.

Overview of Data Definition Language (Commands of DDL)

Data Definition Language (DDL) is a fundamental subset of Structured Query Language (SQL) used to define and manage the structure of databases and their objects. Think of DDL as the architectural blueprint of a database—it allows developers and database administrators to create, modify, and remove the structural components of a database without manipulating the actual data inside.

  • Creating Database Objects: DDL enables the creation of essential database objects, such as:
    • Tables: Organize data in rows and columns.
    • Indexes: Speed up data retrieval.
    • Views: Virtual tables generated from SQL queries.
    • Schemas: Logical containers for grouping related objects.
    • Users and Roles: Define access control and permissions.
  • Modifying Database Objects: DDL allows structural changes such as:
    • Adding or removing columns.
    • Altering data types and constraints.
    • Renaming objects to reflect updated business logic.
  • Removing Database Objects: Obsolete tables, views, or schemas can be dropped to streamline the schema.
  • Defining Data Types: Ensures that each column accepts only valid and appropriate data (e.g., VARCHAR, DATE, DECIMAL).
  • Setting Constraints: Enforces business rules and data integrity through constraints like PRIMARY KEY, FOREIGN KEY, NOT NULL, and CHECK.
CategoryFocusCommon Commands
DDLStructureCREATE, ALTER, DROP, TRUNCATE, RENAME
DMLData manipulationSELECT, INSERT, UPDATE, DELETE
DCLAccess controlGRANT, REVOKE

In summary, DDL defines what data structures exist and how they’re organized, while DML and DCL manage the data and access to those structures.

1. DDL Statement CREATE Commands

The CREATE command is used to build new database objects such as tables, indexes, or views. When creating a table, you define its name, columns, and data types, along with optional constraints.

ORACLE CREATE TABLE SYNTAX
CREATE TABLE table_name (column_name1 datatype constraints,    
                         column_name2 datatype constraints,
                         ...    
                         table_constraints
                         );
CREATE TABLE SYNTAX in ORACLE
  • CREATE TABLE table_name: Initiates the creation of a new table.
  • column_name datatype: Defines each column’s name and the kind of data it will store.
  • constraints: Enforce rules on column values (NOT NULL, UNIQUE etc.).
  • table_constraints: Rules applied to multiple columns (e.g., composite primary keys).
DDL Command Create Table
CREATE TABLE Books (
    BookID INT PRIMARY KEY,
    Title VARCHAR(255) NOT NULL,
    Author VARCHAR(100) NOT NULL,
    ISBN VARCHAR(20) UNIQUE NOT NULL,
    PublicationYear INT,
    Genre VARCHAR(50),
    Price DECIMAL(5, 2) NOT NULL CHECK (Price > 0),
    DateAdded TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
DDL command Create Table

2. DDL Statement ALTER Commands

The ALTER command allows you to change the schema of an existing table, including adding, modifying, or deleting columns and constraints.

3. DDL Statement TRUNCATE: The Quick Eraser

DDL command TRUNCATE is used to delete all rows in a table without affecting its structure. It is faster than DELETE and uses minimal logging.

DDL Statement truncate Table
TRUNCATE TABLE table_name;
DDL EXample truncate Table
DDL Statement truncate Table
TRUNCATE TABLE Books;
DDL EXample truncate Table
AspectTRUNCATEDELETE
SpeedFasterSlower
LoggingMinimalFull
RollbackNot possibleRollback supported
TriggersNot firedFired

4. Commands of DDL Statement DROP

The DROP command permanently removes a database object, including its data and structure.

DDL EXample drop Table
DROP TABLE table_name;
DDL EXample drop Table
DDL EXample drop Table
DROP TABLE BookDetails;
DDL Statememt drop table

Note: Use with caution— DROP operations are irreversible without a backup.

5. DDL Statement RENAME Commands

RENAME changes the name of an existing table, allowing for better naming conventions or evolving business needs.

DDL Example rename
RENAME TABLE old_table_name TO new_table_name;
DDL Statememt rename
DDL Example rename
RENAME TABLE CustomerInfo TO Customers;
DDL Statememt rename

6. Commands of DDL GRANT: Assigning Permissions

GRANT is technically categorized under Data Control Language (DCL) in strict SQL standards, but in Oracle it’s often discussed alongside DDL because it defines access to structural objects.

Allows a user or role to perform certain operations on database objects.

GRANT privilege [, privilege...]
ON object_name
TO user_or_role;

SQL
GRANT SELECT, INSERT, UPDATE 
ON Books
TO DEEN;
SQL

This allows the DEEN to read and modify data in the employees table.

7. Commands of DDL: REVOKE (Withdrawing Permissions)

REMOVE removes previously granted privileges to a user/schema.

REVOKE privilege [, privilege...]
ON object_name
FROM user_or_role;

SQL
REVOKE UPDATE 
ON Books
FROM DEEN;
SQL

This revokes the ability of DEEN to update rows in Books.

8. DDL Commands SYNONYM: Creating Aliases

In Oracle, a SYNONYM is a database object that serves as an alias for another object. This is especially useful to simplify SQL statements or hide the owner/schema name.

  • Create short, meaningful names for objects.
  • Mask schema changes from end users.

CREATE [PUBLIC] SYNONYM synonym_name
FOR schema.object_name;

SQL
CREATE SYNONYM MyBooks FOR Library.Books;
SQL

Now you can reference MyBooks in queries instead of Library.Books.

To Drop the synonym you need to use below command.

SQL
DROP SYNONYM MyBooks;
SQL

For public synonyms:

SQL
DROP PUBLIC SYNONYM MyBooks;
SQL

🔗 Optional: COMMENT and INDEX

While often discussed in DDL contexts:

  • COMMENT allows adding documentation to objects.
  • CREATE INDEX and DROP INDEX help optimize queries.

Examples:

SQL
COMMENT ON TABLE Books IS 'Stores the books information';
SQL
CREATE INDEX idx_books_title ON Books (Title);

📌 Next Steps

CommandPurpose
CREATEDefine new objects
ALTERModify existing objects
DROPRemove objects
TRUNCATEQuickly delete all data
RENAMEChange object names
GRANTAssign privileges
REVOKERemove privileges
SYNONYMAlias for objects
COMMENTAdd metadata
INDEXImprove query performance

Important Considerations of DDL Commands

  • Implicit Commit: Many systems auto-commit DDL changes, making them permanent.
  • Permissions: DDL actions often require elevated privileges.
  • Application Impact: Changes to structure may affect code and users—always assess dependencies.
  • Backups: Always back up before major structural changes, especially when using DROP.

Conclusion

Understanding and effectively using DDL commands is essential for shaping and evolving a database to match changing business needs. Whether you’re building new tables, modifying existing ones, or cleaning up outdated objects, DDL forms the foundation of your data architecture. Use these powerful commands wisely to ensure a robust and maintainable database system.

This Post Has One Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.