Use delete to delete records
Category | Detailed explanation |
---|
Basic syntax | DELETE FROM table [where condition]; |
Example | DELETE FROM user where id > 10; |
Example description | Delete all users with ID greater than 10 in the user table |
user table, the table structure is as follows:
id | username | balance |
---|
1 | 王宝强 | 50000.00 |
2 | 黄晓明 | 150000000.00 |
15 | 马云 | 15000.00 |
##16 | Chen He | 1234131.00 |
mysql> DELETE FROM user where id = 1;
Query OK, 1 row affected (0.08 sec)
Delete the record of the row with ID 1, Li Wenkai.
Clear table records
delete and truncate are the same, but they have one difference, that is, DELETE can return the number of deleted records, while TRUNCATE TABLE returns 0.
If there is an auto-increment field in a table, use truncate table. This auto-increment field will restore the starting value to 1.
Category | Description |
Basic syntax | TRUNCATE TABLE table name; |
Example | TRUNCATE TABLE user; |
Example description | Clear the table data and let the auto-incremented id start from 1 |
【Remember】
Be sure to remember to add the where condition when deleting, otherwise the records in the entire table will be cleared. Be sure to back up, back up, back up before deleting important data.
Next Section