UPI Study › Courses › Database Programming › Free lessons › Lesson 3.2 Free with a free account
A Complete Guide to DDL Commands
Database Programming · Chapter 3: Data Definition Language and Schema DesignSection 3.2 · lesson 16 of 769 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Database Programming.
Lesson complete
Earn the 3 college credits for Database Programming
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Schemas in SQL: Definitions and Design Fundamentals →← Data Definition Language (DDL): Concepts and ExamplesNext lesson: Schemas in SQL: Definitions and Design Fundamentals →
A free account plays all of Chapter 1 and 3 more lessons of Database Programming. Create a free account →
In this lesson
Create, alter, rename, empty, and drop tables across SQL dialects.
- Match CREATE, ALTER, RENAME, TRUNCATE, and DROP to the change required
- Translate common DDL syntax between MySQL, SQL Server, and SQLite
- Choose between DELETE, TRUNCATE, and DROP when removing rows or tables
- Write re-runnable scripts with existence guards and dependency-aware ordering
Free study notes
Get this lesson's notes by email
The key points and 4 practice questions with answers, in your inbox. One email, free.
One email. Unsubscribe in one click.
Transcript
Everything said in the video, section by section (1,018 words). Read along, or search it.
Opening Problem
Three Requests, Three Different Operations
Leo Park, developer at Lantern Books, receives three requests: add a contact field, clear a practice import, and retire an obsolete table. All three change a database, but they require different operations. Choosing the wrong removal command could erase tomorrow's table along with today's rows. Follow one disposable table through its life, then build a script whose dependencies make sense in both directions.
Command Map
Create, Alter, Rename, Truncate, and Drop
DDL means Data Definition Language. CREATE builds an object, and ALTER changes an existing definition. RENAME gives an object a new name. TRUNCATE removes every row while keeping its definition, and DROP removes the object itself. These are alternative operations, not a sequence you must always execute. INSERT, UPDATE, and DELETE manipulate data; SELECT reads results. A dialect is a product's version of SQL. Keep the intention stable, then choose that product's syntax.
Table Lifecycle
Staging Becomes Clients in SQLite
This SQLite sandbox starts with one shared customer name. Creating staging supplies a temporary place for a practice import. ALTER TABLE adds a city column. The next statement renames staging to clients; it is still the same table, with its existing row. The result still contains Asha, with an absent city displayed blank. Changing structure did not invent business data. SQLite spells table renaming as ALTER TABLE, followed by RENAME TO. No separate RENAME TABLE statement is available here. The command runs against an in-memory database. Every displayed result belongs to this disposable practice table.
ALTER Dialects
Adding, Changing, and Renaming Across Products
MySQL allows ADD COLUMN, while SQL Server uses ADD without COLUMN. Both can remove a column with DROP COLUMN. Changing a type uses MODIFY COLUMN in MySQL, but ALTER COLUMN in SQL Server. Restate required attributes and inspect the result afterward. MySQL renames tables with RENAME TABLE. SQL Server uses the system procedure sp rename; it can also rename columns when given the column option. Renaming does not rewrite every external application or saved script. Search those dependencies before changing a public name. These are non-run server references. This installed SQLite version does not support those server commands for changing a column's declared type.
Dialect Differences
Auto-Number, Boolean, and Quoting Syntax
An auto-number column generates an identifier when an insert omits it. MySQL uses AUTO_INCREMENT; SQL Server uses IDENTITY with a seed and increment. These examples start at one and advance by one. Different configured seeds mean a reset need not restart at one. SQLite's INTEGER PRIMARY KEY can allocate a row identifier without an extra keyword. AUTOINCREMENT adds a stronger nonreuse guarantee, with additional overhead. SQL Server tools use GO to separate batches. Boolean declarations also differ: MySQL BOOLEAN is a TINYINT synonym, while SQL Server uses BIT. For unusual names, MySQL commonly uses backticks and SQL Server brackets. Prefer simple identifiers so quotation rules rarely get in your way.
Removal Commands
DELETE, TRUNCATE, and DROP TABLE
DELETE can remove selected rows with WHERE, or all rows without it. The table definition remains, and its auto-number counter normally keeps counting. TRUNCATE removes all rows without WHERE, preserving structure and resetting the configured counter. MySQL and SQL Server support it; SQLite does not. DROP TABLE removes the table and its rows. DROP DATABASE removes the database; SQL Server requires switching away from the database being dropped. Ordinary MySQL TRUNCATE and DROP commit implicitly. SQL Server can roll back table operations inside explicit transactions; database-level restrictions differ. Foreign keys from other tables can block TRUNCATE. Empty child tables still contain those constraint definitions, so deleting their rows does not remove the dependency.
Existence Guards
IF EXISTS and IF NOT EXISTS
IF EXISTS lets DROP do nothing when an object is absent. IF NOT EXISTS lets CREATE skip an object already present. MySQL supports these creation guards. SQL Server instead tests OBJECT_ID for a table, or DB_ID for a database, before creating it. SQL Server supports DROP IF EXISTS from version twenty sixteen. A re-runnable script avoids predictable existence errors when executed again. That does not make it nondestructive. Dropping and recreating a practice database is convenient, but it deletes the previous data. For retained data, inspect the existing definition and apply an explicit migration. A guard is a condition, not a backup or a schema comparison.
Quick Check
A Guard Meets a Different Definition
Here is a fresh SQLite script with a guard. The first statement creates a table with one column. The second asks to create the same table name, now with another column. The final query counts the actual column definitions. Which count will it return? Pause, and decide. One column. IF NOT EXISTS leaves the existing table unchanged; it does not reconcile a different definition. Run the file and inspect the count. Adding the message column requires a deliberate ALTER or migration. Two assumes the guard edits an existing object. Zero assumes the guard deletes it; neither operation is requested here.
Build Order
Parents Before Children, Dependents Removed First
A foreign key links a child table to a parent key. In our shop, orders points to customers. Order items points to orders and products. Create customers and products before their dependents, then orders, then order items. This is the portable build order with constraints enabled. SQLite permits some forward references, so do not use its permissiveness as a cross-product guarantee. Alterations need an existing table, and inserts need existing structure. In server scripts, create and select the database before creating its tables. Remove dependents before their parents when tearing down. Check actual constraints and cascading behavior instead of relying on names alone.
Quick Check
Deleting Rows, Then Renaming the Table
A different SQLite import leaves one completed table behind. DELETE removes its row, then ALTER TABLE gives that table a new name. The final query counts catalog entries named finished. Predict the count. Pause, and decide. Count equals one. DELETE emptied staging, and the rename changed that surviving table's name to finished. The count confirms the container still exists. Its row count and its catalog entry count answer different questions. Zero confuses removing rows with dropping a table. Two treats renaming as copying, although ALTER changed a single object's name.
Key Takeaways
Pick the Operation, Then the Syntax
Leo can now separate the requests. ALTER adds the contact field; emptying the import preserves its structure; DROP retires a table completely. Choose the dialect's exact syntax, including auto-number declarations and rename commands. Inspect the resulting structure after a change. Build parents before children, and remove dependents first. Use guards deliberately, remembering that skipping an existing object never updates its design.
About Database Programming at UPI Study
Database Programming (Computer Science 381) is a self-paced online course of 76 video lessons in 12 chapters, from database applications, SQL setup, schema planning and normalization to keys and constraints, data types, filtering and sorting, joins, subqueries, views, indexes, ROLLUP and PIVOT, regular expressions and database security. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every credit course with PRO →
Why take it here
Why students take Database Programming with UPI Study
Where the credit goes
Universities that work with NCCRS
Database Programming carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About A Complete Guide to DDL Commands
How do you rename a table in MySQL, SQL Server, and SQLite?
Does CREATE TABLE IF NOT EXISTS update an existing table?
In what order should you create and drop tables that have foreign keys?
How does DDL syntax differ between MySQL and SQL Server?
About the Database Programming course
How many college credits is Database Programming?
How much does Database Programming cost and what is included?
Which universities accept UPI Study credits?
How long does Database Programming take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Database Programming.
76 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
Updated
3 credits · NCCRSEnroll →