Schema Change Statements in SQL
Schema Change Statements in SQL
Schema change statements allow the structure of a database to be modified while the database is running, without rebuilding or recompiling the whole database.
These commands support schema evolution, meaning:
-
Adding or removing tables
-
Adding or removing attributes (columns)
-
Modifying constraints
-
Changing defaults and definitions
📌 The DBMS performs consistency checks to ensure that changes do not break references, constraints, or views.
Main Schema Change Commands in SQL
-
DROP – removes schema elements
-
ALTER – modifies existing schema elements
7.4.1 The DROP Command
The DROP command is used to delete schema objects completely.
It can remove:
-
Schemas
-
Tables
-
Domains
-
Types
-
Constraints
-
Views
⚠️ DROP removes both:
-
Data
-
Definition (from system catalog)
DROP SCHEMA
CASCADE vs RESTRICT
| Option | Meaning |
|---|---|
| CASCADE | Drops schema and all dependent objects |
| RESTRICT | Drops schema only if empty |
📌 With RESTRICT, user must first delete all tables, views, constraints manually.
DROP TABLE
Explanation
-
Removes the table and all its records
-
Deletes the table definition from the catalog
-
With CASCADE: dependent views, constraints, foreign keys are also removed
DROP TABLE: CASCADE vs RESTRICT
| Option | Behavior |
|---|---|
| CASCADE | Deletes table + dependent objects |
| RESTRICT | Fails if table is referenced anywhere |
DROP vs DELETE (Very Important for Exams)
| DROP TABLE | DELETE |
|---|---|
| Removes table structure | Keeps table structure |
| Deletes all records | Deletes selected/all records |
| Table no longer exists | Table still usable |
📌 Use DELETE when you want to keep the table for future use.
Dropping Other Schema Elements
📌 Constraint must be named to be dropped.
7.4.2 The ALTER Command
The ALTER command is used to modify existing schema objects, especially tables.
It can:
-
Add columns
-
Drop columns
-
Modify column definitions
-
Add or drop constraints
-
Change default values
ALTER TABLE – Add Column
What Happens?
-
New column
Jobis added -
Existing rows get:
-
NULL values (if no default specified)
-
⚠️ NOT NULL is not allowed unless:
-
A default value is provided
-
Or values are updated immediately
ALTER TABLE – Drop Column
Behavior Options
| Option | Effect |
|---|---|
| CASCADE | Drops column and dependent constraints/views |
| RESTRICT | Drops column only if unused |
ALTER TABLE – Change Default Value
Remove Default
Set Default
📌 Affects future inserts, not existing data.
ALTER TABLE – Drop Constraint
Key Points
-
Constraint must be named
-
CASCADE removes dependent objects
-
Once dropped, a new constraint can be added
ALTER TABLE – Add Constraint
📌 Supports:
-
PRIMARY KEY
-
FOREIGN KEY
-
UNIQUE
-
CHECK
-
NOT NULL
Important Observations
✔ Schema changes can be made without stopping the database
✔ DBMS ensures referential and structural consistency
✔ CASCADE removes dependencies automatically
✔ RESTRICT prevents unsafe changes
✔ ALTER modifies structure, DROP removes structure entirely
Summary Table
| Command | Purpose |
|---|---|
| DROP | Removes schema objects completely |
| ALTER | Modifies existing schema |
| CASCADE | Removes dependent objects |
| RESTRICT | Prevents deletion if dependencies exist |
One-Line Exam Definition
Schema change statements in SQL allow dynamic modification of database structure using DROP and ALTER commands while preserving database consistency.
Comments
Post a Comment