SQL DDL Statements With Example-1
SQL DDL Statements With Example-1
manage database structures like tables, views, indexes, and more. These commands affect the
schema and are automatically committed, meaning changes are permanent.
TRUNCATE
Removes all rows from a table TRUNCATE TABLE employees;
(no rollback)
RENAME Renames a database object RENAME employees TO staff;
COMMENT ON TABLE employees IS 'Stores
COMMENT Adds comments to objects employee data';
📌 Examples
✅ Create a Table
✏️Alter a Table
The ALTER TABLE statement in SQL is used to modify the structure of an existing table. Here's
a breakdown of its syntax and examples for common operations:
🧱 Basic Syntax
ALTER TABLE table_name
[ADD | DROP | MODIFY | RENAME COLUMN | RENAME TO] column_name datatype;
➕ Add a Column
ALTER TABLE employees ADD email VARCHAR2(100);
🧹 Drop a Column
🔄 Rename a Column
Add a Constraint
ALTER TABLE employees ADD CONSTRAINT emp_email_unique UNIQUE (email);
🧨 Drop a Constraint
🚫 Truncate a Table
🔄 Rename a Table
Absolutely! The CREATE statement in SQL is used to define new database objects, most
commonly tables. Here's a detailed breakdown with examples to help you understand how it
works:
🧪 Basic Syntax
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
...
);
id: Integer type, primary key (must be unique and not null).
name: Variable character string up to 50 characters, cannot be null.
email: Unique constraint ensures no duplicate emails.
salary: Decimal with 10 digits total, 2 after the decimal.
hire_date: Stores date values.
🔐 Adding Constraints
SQL constraints are rules applied to table columns to ensure the accuracy, integrity, and
validity of the data. They help prevent invalid data entry and enforce business logic directly at
the database level.
🧱 Types of SQL Constraints with Examples
CHECK Validates data based on a condition salary DECIMAL CHECK (salary > 0)
DEFAULT Sets a default value if none is provided status VARCHAR(10) DEFAULT 'active'
This table:
In this example:
🧱 Table-Level Constraints
In this example:
This creates a new table all_orders with all data from orders table.
This creates a new table recent_orders with data from the last 30 days.