What's changed: Initial version
3.2SQL fundamentals and DDL
Covers schema definition via DDL (CREATE/ALTER/DROP), design decisions around constraints (primary key, foreign key, CHECK, NOT NULL, unique constraint), privilege management via DCL (GRANT/REVOKE), and transaction finalization via COMMIT/ROLLBACK, from the perspective of practical schema-change and privilege-design work.
SQL is broadly divided into DDL (Data Definition Language: schema definition), DML (Data Manipulation Language: data manipulation), and DCL (Data Control Language: privilege control). Within DDL, the area a database specialist must watch most closely is constraint design. Constraints trade off the cost of "fixing data inconsistencies discovered later" against the cost of "rejecting invalid data up front," and how strictly to enforce them depends on business requirements. This section covers practical decisions around DDL, constraints, and DCL.
3.2.1DDL: CREATE, ALTER, DROP
- CREATE defines a new object such as a table, view, or index.
CREATE TABLEdeclares column names and data types along with the constraints described below. ALTER changes an existing object's definition (adding a column withADD COLUMN, dropping one withDROP COLUMN, adding a constraint, etc.). DROP permanently removes an object—data is lost, and in some products this cannot be rolled back. - Adding a column to a large production table with
ALTER TABLEcan, depending on the product, trigger a full table rewrite (with a lock), causing a long exclusive lock that blocks other operations. A DBA must check in advance whether to schedule the change during a late-night maintenance window or whether the product offers an online-DDL capability.
3.2.2Constraint design
- A primary key constraint identifies each row uniquely; it implies both uniqueness and NOT NULL automatically. A foreign key constraint (referential constraint) references another table's primary (or unique) key, preventing the insertion of a nonexistent value or the deletion of a still-referenced row, thereby preserving referential integrity.
- A CHECK constraint guarantees that a column's value satisfies a specific condition (a range, a pattern, etc.). A unique constraint (UNIQUE) prohibits duplicates in a non-primary-key column too (e.g., an email address column). A NOT NULL constraint disallows a missing (NULL) value in that column.
- The
ON DELETEclause attached to a foreign key constraint offersCASCADE(deleting the parent cascades to delete the children),RESTRICT/NO ACTION(deletion of the parent is refused as long as children exist), andSET NULL(the child's foreign-key column is set to NULL)—which behavior to choose requires careful design judgment matched to business requirements (mistakenly choosing CASCADE can cause unintended cascading deletion and massive data loss).
3.2.3DCL: GRANT, REVOKE, and transaction finalization
- GRANT grants a user or role the privilege to perform operations (
SELECT/INSERT/UPDATE/DELETE, etc.) on an object. REVOKE withdraws a previously granted privilege. Following the principle of least privilege—granting only the operations actually needed for the job—is the safe design. COMMIT(finalizing a transaction's changes) andROLLBACK(undoing changes), sometimes classified under DCL, are often called Transaction Control Language (TCL) statements. In some products, a DDL statement (such asCREATE) implicitly commits the preceding transaction (auto-commit), so care is needed when mixing the order of DDL with DML/TCL.
Most-tested: "foreign key ON DELETE CASCADE cascades deletion to children, SET NULL nulls the child's foreign key, RESTRICT refuses parent deletion while children exist," "GRANT grants a privilege, REVOKE withdraws one, and the principle of least privilege" applies. Also do not confuse the distinct roles of CHECK, NOT NULL, and unique constraints.
Suppose a DBA is asked, while reworking an order-management system, "there is a foreign key from the orders table referencing the customers table—how should this be handled when a customer withdraws (closes their account)?" Simply setting ON DELETE CASCADE means that every time a customer withdraws, all of that customer's order history is cascade-deleted, risking the loss of historical data needed for sales aggregation or audit logs. Conversely, ON DELETE RESTRICT means any customer with even one remaining order record cannot be physically deleted, so the withdrawal process itself would fail. A design commonly adopted in practice is to add an is_deleted (soft-delete flag) or retired_at (withdrawal timestamp) column to the customers table and represent withdrawal via a soft delete rather than a physical delete. In that case, the foreign key constraint can remain ON DELETE RESTRICT (since a soft delete never issues an actual DELETE statement, no referential-integrity violation occurs). Additionally, if there is a requirement to allow a withdrawn customer's email address to be reused, the DBA may need to change the email column's unique constraint to a partial unique constraint scoped to "unique only among non-withdrawn customers" (implemented via a partial index in products that support it). Designing the foreign key's ON DELETE behavior and the strictness of constraints in light of what deletion actually means (physical vs. soft) and future data-reuse requirements is the role of a database specialist.
| Clause | Behavior |
|---|---|
| CASCADE | Deleting the parent cascades to delete child rows too |
| RESTRICT / NO ACTION | Refuses to delete the parent while referencing child rows exist |
| SET NULL | Deletes the parent and sets the child row's foreign key column to NULL |
Trap: "Omitting the ON DELETE clause on a foreign key constraint automatically means CASCADE" is wrong—the default when omitted is generally equivalent to RESTRICT/NO ACTION; cascading deletion occurs only when CASCADE is explicitly specified. Also wrong: "a privilege granted via GRANT can only be removed by dropping the user"—it can be withdrawn individually with a REVOKE statement.
3.2.4Section summary
- DDL (CREATE/ALTER/DROP) defines schema; check for lock impact before ALTER on large production tables
- Choose a foreign key's ON DELETE behavior (CASCADE/RESTRICT/SET NULL) based on what deletion actually means (physical vs. soft)
- GRANT/REVOKE should follow the principle of least privilege, permitting only the operations actually needed
Sign in to track progress — Log in.
Quick check
(just a quick review)Q1. When a customer withdraws, you want to keep order history intact while still maintaining referential integrity. Which design is most appropriate?
Q2. There is a plan to add a new column via ALTER TABLE to a large, live production orders table. What is the most important thing to check beforehand?
Q3. You want to grant multiple application users only the operations they actually need for their jobs. Which privilege design best matches this policy?

