A Complete Guide to DDL Commands, Explained

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

← 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 →

Earn 3 college credits for Database ProgrammingRecommended for credit by NCCRS · Computer Science 381 · self-paced · one payment, lifetime access

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.

Compare: Database Fundamentals vs Database Programming

Why take it here

Why students take Database Programming with UPI Study

Write the SQL behind every appDatabase Programming teaches SQL in MySQL and SQL Server side by side: designing and normalizing tables, keys and constraints, data types, inserting, updating and deleting rows, filtering, grouping, joins, subqueries, views, indexes, transactions, and protecting data from SQL injection. It suits computer science and information technology degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

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.

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?
Each product spells it differently: MySQL uses RENAME TABLE old_name TO new_name, SQL Server uses the sp_rename system procedure, and SQLite uses ALTER TABLE old_name RENAME TO new_name. The table keeps its rows and remains the same object. Renaming does not update applications, saved scripts, or reports that use the old name, so search for those dependencies before changing a name others rely on.
Does CREATE TABLE IF NOT EXISTS update an existing table?
No, CREATE TABLE IF NOT EXISTS skips creation when a table with that name already exists, even if the new definition differs. A guarded statement that lists an extra message column still leaves the old one-column table unchanged. Change an existing table deliberately with ALTER TABLE or a reviewed migration. MySQL and SQLite support this guard; in SQL Server, a script can test OBJECT_ID before creating the table.
In what order should you create and drop tables that have foreign keys?
Create parent tables before the child tables that reference them, and drop dependent tables before their parents. In a shop schema, create customers and products, then orders, then order items, and tear down in the reverse order. This portable order works with constraints enabled. SQLite permits some forward references, but do not rely on that elsewhere. Check each actual constraint and cascade rule rather than guessing from table names.
How does DDL syntax differ between MySQL and SQL Server?
Many operations share the same intent but are spelled differently. MySQL accepts ADD COLUMN, while SQL Server uses ADD without COLUMN. Changing a column's type uses MODIFY COLUMN in MySQL and ALTER COLUMN in SQL Server. Auto-numbering uses AUTO_INCREMENT in MySQL and IDENTITY in SQL Server, and unusual names are commonly quoted with backticks in MySQL and square brackets in SQL Server.

About the Database Programming course

How many college credits is Database Programming?
Database Programming (Computer Science 381) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Database Programming cost and what is included?
$250, paid once, with lifetime access: all 76 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Database Programming take?
It is self-paced with no deadlines. The 76 lessons total about 11 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

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

Database Programming
3 credits · NCCRS
Enroll →