Data Definition Language (DDL): Concepts and Examples

UPI Study › Courses › Database Programming › Free lessons › Lesson 3.1 Free with a free account

Data Definition Language (DDL): Concepts and Examples

Database Programming · Chapter 3: Data Definition Language and Schema DesignSection 3.1 · lesson 15 of 769 min · free with a free account

← SQL Constraints: Enforcing Rules on Your DataNext lesson: A Complete Guide to DDL Commands →

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

Build, inspect, and test database structure with CREATE, ALTER, and DROP.

  • Distinguish DDL statements that change structure from DML statements that change data
  • Create a table with a primary key, required columns, unique rules, and defaults
  • Inspect structure through the system catalog and INFORMATION_SCHEMA views
  • Compare how SQLite, MySQL, and SQL Server handle DDL inside transactions

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,055 words). Read along, or search it.

Opening Problem

An Email Column That Does Not Exist

Maya Rao, owner of Lantern Books, wants customer emails stored beside customer names. Leo Park, the developer, can change one address. But what if the database has no email column at all? Changing a value cannot create its container. The difference decides whether Leo needs a data statement, or a change to the database structure. These fictional examples let us build, inspect, and test a disposable database.

Database Objects

Tables, Views, and Indexes

Data Definition Language, shortened to DDL, defines database structure. CREATE builds an object, ALTER changes it, and DROP removes it. A database groups related objects. A table holds rows under named columns, with rules attached to those columns. A view stores a query definition, giving readers a tablelike interface. An ordinary view does not keep a separate copy of every result row. An index supplies an additional lookup structure. Searches can improve, at a storage and maintenance cost. Objects are containers and helpers, rather than sequential stages.

Structure Versus Data

Adding a Column Versus Updating a Value

Compare these two statements. Adding an email column changes the design, even before any customer receives an address. Updating one customer's city changes a stored value. The column already exists, so that operation belongs to Data Manipulation Language, or DML. INSERT adds rows, UPDATE changes rows, and DELETE removes rows. SELECT reads results; some classifications call it a separate query language. Two other commands commonly grouped with DDL are RENAME and TRUNCATE. Renaming changes an object's name; truncating empties a table while keeping its structure. Product syntax varies. Identify the operation before choosing its spelling.

Server Setup

Creating and Selecting the Shop Database

On MySQL or SQL Server, create a database named shop, then select it with USE before creating tables. These are server syntax listings, not commands for SQLite. SQL Server tools separate database setup into batches with GO. A semicolon terminates a SQL statement. GO is a tool instruction, rather than part of the SQL language. SQLite instead opens a database file through its shell command. Our executable demonstrations use an in-memory database that disappears when the command finishes. Our practice is isolated from the shared shop database.

CREATE TABLE

A Disposable Customer Table with Email

Here is a disposable customer table, using the shared shop's customer identifier and names. We add email as a lesson-specific design extension. The opening line names the table. Parentheses contain column definitions; commas separate them, and a semicolon finishes the statement. The primary key identifies a customer uniquely. Required names use NOT NULL, while city may be absent. Email is required and unique. A missing email fails the required-value rule; a repeated email fails the uniqueness rule. Points receives a default of zero when an insert leaves that column out. A default supplies a value; it does not prohibit an explicitly supplied null. SQLite accepts these type names, but does not enforce the declared VARCHAR length. MySQL counts that length in characters; SQL Server VARCHAR counts bytes.

Data Dictionary

Reading the SQLite System Catalog

The database records its design in metadata, often called the data dictionary or system catalog. Read it rather than trusting a handwritten diagram. SQLite exposes object definitions through sqlite schema. This query asks which named objects exist, rather than which customer rows they contain. The earlier run listed the customer table. A separate column inspection can show names, declared types, defaults, and required-value flags. Structural changes update metadata. Inspect it through supported commands.

Server Metadata

INFORMATION_SCHEMA Views and System Catalogs

MySQL and SQL Server offer INFORMATION_SCHEMA views for inspecting tables and columns. Filter by database or schema to avoid confusing same-named objects. The column view reports a column's name, type, and nullability. Ordering by ordinal position reconstructs the table's column order. MySQL also provides SHOW TABLES and DESCRIBE. SQL Server offers system catalog views such as sys tables, and the help procedure sp help. Each product can describe its own running structure.

Test Row

Verifying Defaults and Nulls with Asha

Now test the design with Asha Verma, the shop's first customer. This file creates a small disposable subset of the table. The insert supplies the required identifier, name, and email. It leaves out city and points. The result shows an absent city as the word NULL, using a shell display setting. Points becomes zero through its default. Empty text is a value. Null means absence. Leo has verified a representative row, rather than just successful table creation.

DDL and Transactions

Implicit Commits and Atomic DDL

A transaction groups work for a decision. COMMIT makes pending changes permanent; ROLLBACK undoes work still pending in that transaction. Ordinary MySQL table DDL can implicitly commit, including earlier data changes. A later rollback cannot restore that earlier pending state. Atomic DDL means a supported statement succeeds or fails as a unit. It does not mean a user can roll back successfully completed MySQL DDL. SQL Server supports rollback for ordinary table DDL inside explicit transactions. Database creation has different restrictions, so do not generalize from tables to every object. Test SQLite directly, keeping its behavior separate from MySQL.

Quick Check

A Rolled-Back Notes Table

Amina Diallo, the database administrator, reviews this fresh SQLite migration. It creates a notes table inside an explicit transaction, then rolls back. The final query counts tables named notes in the catalog. Predict the final count, rather than the number of statements in the file. Will it return one, zero, or an error? Pause, and decide. Zero tables remain. SQLite rolls back the table creation, so the catalog contains no notes table. Run the file to confirm the count. The deciding relationship is between BEGIN, CREATE TABLE, and ROLLBACK. One would describe a surviving table, which is not SQLite's result here. The count query itself is valid, even when its filter matches nothing. Do not transfer this outcome to ordinary MySQL table creation. Its implicit commit changes the transaction boundary.

Cleanup

Repeated Creation and DROP Statements

Creating customers again without a guard fails because the name already exists. That failed attempt does not replace the original table. DROP TABLE removes a table and its rows. DROP DATABASE removes the database; these server commands are shown only as a non-run listing. In SQL Server, switch away from shop before dropping it. In MySQL, ordinary DROP is not recoverable through a later rollback. Closing our in-memory connection discards the practice database.

Key Takeaways

Change Structure, Then Verify It

Leo's email problem needed a structural change before any address could be stored. DDL builds and reshapes those containers. Read the system catalog to verify what actually exists. Then insert a representative row and inspect defaults, nulls, and constraints. Before changing structure, check the product's transaction behavior. A rollback is useful only when the work remains inside its undo boundary. Review real structural changes, and keep recoverable backups.

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 Data Definition Language (DDL): Concepts and Examples

What is Data Definition Language (DDL) in SQL?
Data Definition Language is the part of SQL that defines database structure: CREATE builds an object, ALTER changes it, and DROP removes it. Those objects include databases, tables, views, and indexes. Adding an email column to a customers table is DDL, because it changes the design. Changing one customer's stored email afterward is DML, the language for inserting, updating, and deleting rows.
How can you see a table's structure in SQL?
Query the database's own metadata, often called the system catalog or data dictionary. MySQL and SQL Server both offer INFORMATION_SCHEMA views, such as INFORMATION_SCHEMA.COLUMNS, which report each column's name, type, and nullability. MySQL also provides SHOW TABLES and DESCRIBE, SQL Server offers catalog views such as sys.tables and the sp_help procedure, and SQLite exposes object definitions through its sqlite_schema table.
Can you roll back a CREATE TABLE statement?
It depends on the system: SQLite and SQL Server can roll back CREATE TABLE inside an explicit transaction, but MySQL cannot. In SQLite, running BEGIN, CREATE TABLE, and then ROLLBACK leaves no table in the catalog. SQL Server treats ordinary table DDL this way, though database creation has different restrictions. In MySQL, ordinary table DDL causes an implicit commit, which also commits any earlier pending data changes.
Is GO a SQL statement?
No, GO is a batch separator recognized by Microsoft tools such as SSMS and sqlcmd, not part of the SQL language. The tool uses it to split a script into batches that it sends to SQL Server one after another, while a semicolon is what ends an individual statement. Similarly, the MySQL client's source command is a client instruction rather than portable SQL.

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 →