The DELETE
statement is used to delete existing records in a table.
Note: Be careful when deleting records in a table! Notice the WHERE
clause in the DELETE
statement. The WHERE
clause specifies which record(s) should be deleted.
If you omit the WHERE
clause,
all records in the table will be deleted!.
To delete the record(s) where brand is 'Volvo', use this statement:
Delete all records where brand is 'Volvo':
DELETE FROM cars
WHERE brand = 'Volvo';
DELETE 1
Which means that 1
row was deleted.
To check the result we can display the table with this SQL statement:
It is possible to delete all rows in a table without deleting the table. This means that the table structure, attributes, and indexes will be intact.
The following SQL statement deletes all rows in the cars
table, without deleting the table:
Delete all records in the cars
table:
DELETE FROM cars;
DELETE 3
Which means that all 3
rows were deleted.
To check the result we can display the table with this SQL statement:
Because we omit the WHERE
clause in the DELETE
statement above, all records will be deleted from the cars table.
The same would have been achieved by using the TRUNCATE TABLE
statement:
Delete all records in the cars
table:
TRUNCATE TABLE cars;
TRUNCATE TABLE
To check the result we can display the table with this SQL statement:
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!