Data Manipulation Flashcards

(15 cards)

1
Q

What does CRUD stand for and which SQL commands perform each operation?

A

CREATE: INSERT INTO - READ: SELECT - UPDATE: UPDATE - DELETE: DELETE FROM. These are the four fundamental data manipulation operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write the complete syntax for inserting a single record into a table.

A

INSERT INTO table_name (column1, column2, column3, …) VALUES (value1, value2, value3, …); - Column names in parentheses - VALUES keyword required - Values must match column order positionally

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you insert multiple records in a single INSERT statement?

A

INSERT INTO table_name (column1, column2, column3, …) VALUES (value1, value2, value3, …), (value1, value2, value3, …), (value1, value2, value3, …); - Separate each record’s values with commas - Each record in its own parentheses

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What happens to columns not mentioned in an INSERT statement?

A

They automatically take NULL values by default for all inserted records. This assumes the column allows NULL values or has a default constraint.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

You’re inserting data: INSERT INTO users (name, email) VALUES (‘John’, 25, ‘john@email.com’). What’s wrong?

A

Value count doesn’t match column count. Should be: INSERT INTO users (name, email) VALUES (‘John’, ‘john@email.com’); - Values must match columns positionally - Extra value (25) causes error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write the syntax to delete specific records from a table.

A

DELETE FROM table_name WHERE condition; - WHERE clause specifies which records to delete - Without WHERE, ALL records are deleted - No column names needed in DELETE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What’s the critical difference between these two commands? DELETE FROM users WHERE age > 65; vs DELETE FROM users;

A

First deletes only users over 65. Second deletes ALL users in the table! Always double-check WHERE clauses - missing WHERE in DELETE is dangerous.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write the complete syntax for updating records in a table.

A

UPDATE table_name SET column1 = value1, column2 = value2, … WHERE condition; - SET keyword required - Multiple columns separated by commas - WHERE clause specifies which records to update

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens if you forget the WHERE clause in an UPDATE statement?

A

ALL records in the table get updated with the new values! Always verify WHERE clause before running UPDATE commands to avoid mass data corruption.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Fix this UPDATE statement: UPDATE products SET price 29.99 WHERE id = 5;

A

UPDATE products SET price = 29.99 WHERE id = 5; - Missing equals sign (=) after price - SET requires column = value format

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

You need to insert a user with name ‘Sarah’ and email ‘sarah@test.com’ into a users table that has columns: id, name, email, created_date. Write the INSERT statement.

A

INSERT INTO users (name, email) VALUES (‘Sarah’, ‘sarah@test.com’); - Don’t need to specify id (likely auto-increment) or created_date (likely has default) - Only specify columns you’re providing values for

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What’s positional matching in INSERT statements and why is it critical?

A

Values must be in the same order as the columns listed. INSERT INTO table (col1, col2) VALUES (val1, val2) - val1 goes to col1, val2 goes to col2. Wrong order = wrong data in wrong columns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Compare the risk levels: INSERT vs UPDATE vs DELETE. Which is most dangerous and why?

A

DELETE without WHERE is most dangerous (loses all data). UPDATE without WHERE is very dangerous (corrupts all data). INSERT is safest (only adds data, doesn’t destroy existing records).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

You want to delete all products with price = 0 and update all remaining products to have a 10% price increase. Write both statements.

A
  1. DELETE FROM products WHERE price = 0; 2. UPDATE products SET price = price * 1.10 WHERE price > 0; - Delete first, then update remaining - Use WHERE in both for safety
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the three critical safety rules for data manipulation commands?

A
  1. Always use WHERE clauses with UPDATE and DELETE 2. Test conditions with SELECT first 3. Backup data before mass operations - These prevent accidental data loss or corruption
How well did you know this?
1
Not at all
2
3
4
5
Perfectly