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

  1. DROP – removes schema elements

  2. 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

DROP SCHEMA COMPANY CASCADE;

CASCADE vs RESTRICT

OptionMeaning
CASCADEDrops schema and all dependent objects
RESTRICTDrops schema only if empty

📌 With RESTRICT, user must first delete all tables, views, constraints manually.


DROP TABLE

DROP TABLE DEPENDENT CASCADE;

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

OptionBehavior
CASCADE            Deletes table + dependent objects
RESTRICT            Fails if table is referenced anywhere

DROP vs DELETE (Very Important for Exams)

DROP TABLEDELETE
Removes table structureKeeps table structure
Deletes all recordsDeletes selected/all records
Table no longer existsTable still usable

📌 Use DELETE when you want to keep the table for future use.


Dropping Other Schema Elements

DROP CONSTRAINT constraint_name; DROP DOMAIN domain_name;

📌 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

ALTER TABLE COMPANY.EMPLOYEE ADD COLUMN Job VARCHAR(12);

What Happens?

  • New column Job is 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

ALTER TABLE COMPANY.EMPLOYEE DROP COLUMN Address CASCADE;

Behavior Options

OptionEffect
CASCADE                Drops column and dependent constraints/views
RESTRICT                Drops column only if unused

ALTER TABLE – Change Default Value

Remove Default

ALTER TABLE COMPANY.DEPARTMENT ALTER COLUMN Mgr_ssn DROP DEFAULT;

Set Default

ALTER TABLE COMPANY.DEPARTMENT ALTER COLUMN Mgr_ssn SET DEFAULT '333445555';

📌 Affects future inserts, not existing data.


ALTER TABLE – Drop Constraint

ALTER TABLE COMPANY.EMPLOYEE DROP CONSTRAINT EMPSUPERFK CASCADE;

Key Points

  • Constraint must be named

  • CASCADE removes dependent objects

  • Once dropped, a new constraint can be added


ALTER TABLE – Add Constraint

ALTER TABLE COMPANY.EMPLOYEE ADD CONSTRAINT NewConstraint FOREIGN KEY (Super_ssn) REFERENCES EMPLOYEE(Ssn);

📌 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

CommandPurpose
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

Popular posts from this blog

Database Management Systems DBMS PCCST402 Semester 4 KTU CS 2024 Scheme

Data Models, Schemas and Instances

Introduction to Database Management System -DBMS