MySQL must know must know the study notes Chapter 20 Update and delete data

update data:

UPDATE tableName
SET field1 = newValue1, field2 = newValue2 ...
WHERE condition;

Without the where clause, UPDATE will update the entire table.

You can use the results of select to update column values:

UPDATE tableName
SET field1 = (SELECT 语句)
WHERE condition;

If you use the UPDATE statement to update multiple rows, if an error occurs in one of the rows, the entire UPDATE statement is canceled, and the value changed before the error occurred in the statement is also restored, and the update continues even if an error occurs:

UPDATE IGNORE tableName
SET field1 = newValue1, field2 = newValue2 ...
WHERE condition;

delete data:

DELETE FROM tableName
WHERE condition;

If the WHERE clause is omitted, the entire table will be deleted.

To delete all data in the table faster, you can use:

TRUNCATE TABLE tableName;

It deletes the original table and re-creates a table instead of deleting the data in the table row by row like DELETE.

Before using the WHERE clause for UPDATE and DELETE statements, you should first test with SELECT to ensure that the correct records are filtered.

Use a database that enforces referential integrity so that MySQL will not allow you to delete rows that have data associated with other tables.

Guess you like

Origin blog.csdn.net/tus00000/article/details/111478679