Primary Key
AI said to update this record by id. Where does that id come from?
AI said to update this record by id. Where does that id come from?
| id | name | city | |
|---|---|---|---|
| 7 | Lin | lin@example.com | Hangzhou |
| 8 | Lin Xiao | lin@example.com | Suzhou |
| 9 | Chen Yu | chen@example.com | Xiamen |
Two accounts in this users table happen to share an email. Their primary keys, id values, differ, so each can identify one row; email is not this table’s primary key.
Locate one row, not a batch: A primary key uniquely identifies a row; still inspect the write condition, affected-row count, and read-back result. A repeated field can match multiple rows.
Ordinary fields repeat: Identical names and emails happen all the time; a primary key's values cannot repeat and cannot be empty.
Tables link through it: When an orders table records which user an order belongs to, it stores the users table's primary key, not the name.
Update this user record by primary key, not by email or name. Before updating, tell me how many rows match; if more than one, stop and explain before continuing. After the change, query the record again to confirm.