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
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: Changing Records with the UPDATE Statement →← 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 →
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.
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 Data Manipulation Language (DML): Commands at a Glance
What are the main DML commands in SQL?
What is an upsert in SQL?
Why does an UPDATE report zero rows affected?
Why is REPLACE risky compared with an upsert?
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 →