Data Manipulation Language (DML): Commands at a Glance

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

Data Manipulation Language (DML): Commands at a Glance

Database Programming · Chapter 6: Data Retrieval and ModificationSection 6.2 · lesson 36 of 769 min · free with a free account

← Filtering Grouped Results and Advanced Aggregation with HAVINGNext lesson: Changing Records with the UPDATE Statement →

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

How INSERT, UPDATE, DELETE, SELECT, and upserts change or read rows.

  • Identify which DML command adds, changes, removes, or reads rows
  • Interpret affected-row counts in SQLite, MySQL, and SQL Server
  • Use transactions and safe-update checks to control data changes
  • Compare upsert syntax in SQLite, MySQL, and SQL Server

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

Opening Question

Add, Change, or Read a Row

Maya Rao owns Lantern Books, our fictional bookstore. A delivery mixes familiar products with a new eraser. Should we add a row, change one, or read one? A wrong command can erase useful history. Follow a row through its life, then process the delivery without treating every item as new.

Table Setup

Products Table With Keys and Checks

Our disposable database uses the course product rows. These prices and quantities are fictional teaching values. The primary key identifies each product; no two rows may share a product identifier, so a book is never confused with another product. The check constraints reject negative prices or stock. Creating this structure is data definition language, usually shortened to DDL. Run the setup file through the SQLite shell. Successful creation prints nothing here; the following queries will provide evidence of the stored rows.

INSERT and SELECT

Adding Delivery Rows Inside a Transaction

Data manipulation language, or DML, works with stored data. Insert adds rows; this delivery adds an eraser and a bookmark to our sandbox. Naming the columns makes the value order explicit. One values clause can supply several rows, separated by commas. Begin opens a transaction, keeping this demonstration provisional. The changes function reports the immediately preceding change statement, before another mutation replaces that count. Run the file. The count is two, and the query lists both new identifiers. Select reads rows without changing them; some classifications call it DQL.

Row Lifecycle

Insert, Update, Then Delete One Row

Here is the same row inside one complete transaction. Insert creates it, and update adds ten units to its existing stock. The condition targets only product nineteen. Update changes values in matching rows; delete removes matching rows altogether, while preserving its structure. Run this lifecycle. First we read sixty units; then the deletion reports one removed row. Rollback discards the sandbox changes. Without a condition, update or delete targets every row, subject to constraints. Neither command asks whether that broad scope was really intended.

Affected Rows

What an Affected-Row Count Means

A row count needs its database context. This first update matches the pen set, but assigns stock its existing value. The second update searches for an absent identifier. Zero affected rows can mean the condition found nothing; it is not automatically an error. Run both statements. SQLite reports one for the matching assignment and zero for the missing identifier. A count alone does not prove the business request was correct. MySQL normally counts changed rows, so its unchanged update reports zero. SQL Server reports the matching row; MySQL connectors can request matched-row reporting instead.

Dialect Comparison

Reading Affected Counts in MySQL and SQL Server

These are reference listings for other servers, not SQLite commands. MySQL exposes the preceding affected count through row count; SQL Server uses its row count variable. Read that value immediately after the change. In SQL Server, no count suppresses the usual affected-row messages, while the row count variable still updates. Compare the observed count with an expectation, then inspect the actual values. A matching count can still describe the wrong customers or the wrong products.

Transactions

Autocommit, Commit, and Rollback

A transaction groups work into a unit. With autocommit, each successful standalone change finishes its own transaction; a later rollback cannot reach back into it. This file instead begins explicitly. Our connection can inspect its provisional change, before deciding whether to commit it or roll it back. Run the file. Provisional stock is zero, and rollback restores twenty. Commit would accept the pending work, closing this opportunity to undo it. SQLite uses begin here. MySQL uses start transaction, and SQL Server uses begin transaction. Ordinary MySQL table definition commands commonly commit implicitly; exceptions require care.

Safety Guard

MySQL Safe-Update Mode

MySQL safe-update mode provides a guard. Its server default is off, though Workbench commonly enables it for client connections. It rejects updates or deletes lacking an accepted key condition or limit. Actual index use can matter, so mentioning a key is not an absolute guarantee. An equality on one primary key expresses a precise target. A limit only caps the number changed; it does not identify which business records belong. The broad key range can still remove every product. SQLite and SQL Server have no equivalent session switch; always preview the condition and inspect the count.

Upsert

Insert or Update With ON CONFLICT

An upsert combines insert and update. If the incoming key already exists, add its delivered quantity; otherwise, create a row. SQLite spells the conflict branch on conflict, do update. Excluded names incoming values, while products names the stored values. The pen set already exists, but the eraser does not. Run both paths: pen stock becomes twenty-five, and the new eraser starts at fifty. Both paths depend on a declared uniqueness rule. Checking first in application code is a different pattern, with a race if another session inserts between those steps.

MySQL Upsert

ON DUPLICATE KEY UPDATE Counts

MySQL uses on duplicate key update. Modern row aliases name incoming values; older examples use the values function, whose use here is deprecated. Under default changed-row reporting, an inserted row counts as one, an updated existing row as two, and an unchanged existing row as zero. Found-rows connector settings can alter the unchanged count. Those MySQL numbers are documented semantics; they are not the output of our SQLite upsert.

SQL Server MERGE

Matching a Source Against a Target

SQL Server merge compares a source with a target, using the on condition. A matched branch updates; a missing match inserts. Merge can also delete in an appropriate matched branch, and requires a terminating semicolon. Its count totals inserted, updated, and deleted rows. Holdlock can prevent certain concurrent key races, but concurrency still needs testing. MySQL and SQLite do not execute this merge syntax.

Common Mistake

REPLACE Deletes Before Inserting

Replace is another trap: MySQL and SQLite can delete a conflicting row before inserting. Omitted columns may reset, and related rows may be affected. This listing lacks required details, so it can fail. Use an upsert when the intended operation is an update.

Quick Check

Seven Campus Maps Added to Fifteen

A separate delivery contains seven campus maps. The existing stock is fifteen, and this script processes that delivery once. What stock will the query print? Read the conflict branch, then pause and predict. Twenty-two units. The existing map row takes the update branch, adding seven to fifteen. Seven would replace the quantity instead of adding it. Fifteen would ignore the delivery; this branch does neither. The rollback restores our starting data.

Key Takeaways

Choose the Row Action Deliberately

Choose the row action deliberately: insert adds, update changes, delete removes, and select reads. Check both scope and feedback. Preview the same condition, then compare the affected count and stored values. Protect the decision with a transaction. Maya can now handle familiar products and new products, using an upsert appropriate to her database.

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 Manipulation Language (DML): Commands at a Glance

What are the main DML commands in SQL?
The main data manipulation commands are INSERT, which adds rows, UPDATE, which changes existing rows, and DELETE, which removes rows, all while leaving the table structure in place. SELECT reads rows and is sometimes grouped separately as data query language. Commands such as CREATE TABLE belong to DDL because they change structure. UPDATE and DELETE without a WHERE clause target every row in the table.
What is an upsert in SQL?
An upsert inserts a row when its key is new and updates the existing row when the key already exists, in one statement. SQLite writes INSERT ... ON CONFLICT (product_id) DO UPDATE SET stock = stock + excluded.stock, MySQL uses INSERT ... ON DUPLICATE KEY UPDATE, and SQL Server commonly uses MERGE. The SQLite and MySQL forms detect the conflict through a primary key or unique index, while MERGE matches rows with its ON condition.
Why does an UPDATE report zero rows affected?
An UPDATE reports zero rows when no row matched its WHERE condition, which is not an error. In MySQL, the default count includes only rows whose values actually changed, so setting a column to its current value also reports 0. SQL Server and SQLite count matched rows instead. Read the count with ROW_COUNT() in MySQL or @@ROWCOUNT in SQL Server, then inspect the data before assuming success.
Why is REPLACE risky compared with an upsert?
In MySQL and SQLite, REPLACE handles a key conflict by deleting the existing row and inserting a new one, rather than updating it in place. Columns omitted from the new row fall back to their defaults, so stored values can be lost, and the delete step can remove related rows through ON DELETE CASCADE. Prefer ON DUPLICATE KEY UPDATE in MySQL or ON CONFLICT DO UPDATE in SQLite to change only the named columns.

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 →