Focuses on the core commands for adding, updating, and removing data from tables.
Imagine you are the lead developer for a global social media platform. A new celebrity joins, a user changes their password, and a bot account needs to be banned instantly. How do you manage these millions of changes without crashing the entire system?
The INSERT INTO statement is your primary tool for adding new rows to a table. In professional environments, data is rarely static; it grows constantly. When writing an insert statement, you must ensure a 1-to-1 mapping between the columns you specify and the values you provide. If a table has a primary key that is auto-incremented, you typically omit that column from your statement, allowing the database engine to handle the numbering. For example, if we have columns, we must provide corresponding values. String values must be enclosed in single quotes, while numeric values like integers or decimals do not require them.
Step-by-step to add a user to the `Players` table: 1. Identify the table: `Players`. 2. Identify columns: `username`, `level`, `score`. 3. Write the statement: `INSERT INTO Players (username, level, score) VALUES ('PixelKnight', 1, 0);`.
Quick Check
What happens if you try to insert a string value into a column defined as an INTEGER?
Answer
The database will return a data type mismatch error and the record will not be added.
The UPDATE statement modifies existing records. The most critical component of an update is the WHERE clause. Without it, the database will apply your change to every single row in the table—a common and costly mistake. You can update multiple columns at once by separating them with commas. For instance, if you want to increase a player's score by 10%, you would use a mathematical expression like . This allows for dynamic updates based on current values rather than just static replacements.
Scenario: Update 'PixelKnight' to level 2 and add 500 to their score. 1. Target the table: `UPDATE Players`. 2. Set the new values: `SET level = 2, score = score + 500`. 3. Apply the filter: `WHERE username = 'PixelKnight';`.
Quick Check
What is the consequence of forgetting the WHERE clause in an UPDATE statement?
Answer
Every record in the table will be updated to the new value, potentially destroying original data.
The DELETE statement removes rows from a table entirely. Like the update statement, it relies heavily on the WHERE clause for safety. It is important to distinguish between `DELETE` and `DROP`. While `DROP` deletes the entire table structure, `DELETE` only removes the data inside the rows. In advanced systems, we often use a Soft Delete (updating a 'status' column to 'inactive') instead of a hard delete to preserve data history, but knowing how to perform a permanent removal is essential for database maintenance and privacy compliance (like GDPR).
Scenario: Remove all players who have a score of 0 and have not reached level 5. 1. Identify the command: `DELETE FROM Players`. 2. Construct the logic: `WHERE score = 0 AND level < 5;`. 3. Verify the logic: This ensures active low-level players are kept, while inactive ones are purged.
Which keyword is used to specify the new values in an UPDATE statement?
What is the result of executing: DELETE FROM Employees; ?
In an INSERT statement, you must always list every column in the table even if some allow NULL values.
Review Tomorrow
In 24 hours, try to write down the syntax for INSERT, UPDATE, and DELETE from memory. Can you explain the specific role of the WHERE clause in the latter two?
Practice Activity
Create a mock 'Library' table and write the SQL to: 1. Add a book, 2. Mark it as 'borrowed' using an UPDATE, and 3. Remove it using its unique ISBN.