- Match C, R, U, D to SQL commands.
- What is SQL injection, and how do prepared statements prevent it?
- What is a primary key? Why use
AUTO_INCREMENT? - Why do we not run
DELETE FROM productswithoutWHERE? - What is the benefit of a transaction when saving an order?
Show answers
Create →
INSERT, Read →SELECT, Update →UPDATE, Delete →DELETE.SQL injection is an attack where input typed by a user is inserted into an SQL string and changes the meaning of the query, for example entering
' OR '1'='1to log in without a password, or deleting tables. Prepared statements send the SQL with placeholders (?or:name) first, then send the values separately; the database always treats the values as data, never as SQL code.PHP$stmt = $pdo->prepare('SELECT * FROM users WHERE email = ?'); $stmt->execute([$email]);A primary key is a column (or columns) whose value uniquely identifies each row; it cannot be NULL or repeated, and other tables refer to it with foreign keys.
AUTO_INCREMENTlets the database generate the next unique number automatically, so you never have to choose an id or risk duplicates.Without a
WHEREclause, the statement deletes every row in the table, and there is no undo. Always useWHERE id = ?(and test with aSELECTusing the same condition first).Saving an order involves several statements (insert the order, insert each order item, reduce stock). A transaction makes them all succeed together or none at all (
beginTransaction,commit, orrollBackon error), so the database is never left with half an order or incorrect stock.