SQL delete

1. Summary of ways to delete records:

  • Delete according to conditions: DELETE FROM tb_name [WHERE options] [ [ ORDER BY fields ] LIMIT n ]
  • Delete all (table is cleared, including self-increment counter reset): TRUNCATE tb_name

2. Time difference:

  • TIMESTAMPDIFF(interval, time_start, time_end) can calculate the time difference between time_start-time_end, the unit is based on the specified interval, commonly used and optional:
    • SECOND seconds
    • MINUTE minute (returns the integer part of the difference in seconds divided by 60)
    • HOUR hour (returns the integer part of the difference in seconds divided by 3600)
    • DAY Days (returns the integer part of the difference in seconds divided by 3600*24)
    • MONTH Number of months
    • YEAR Number of years

3. The difference between TRUNCATE and DELETE

  • Logically speaking, the TRUNCATE statement has the same effect as the DELETE statement, but in some cases, the two are used differently.
  • DELETE is a DML type statement; TRUNCATE is a DDL type statement. They are used to clear the data in the table.
  • DELETE deletes records one by one line by line; TRUNCATE deletes the original table directly, and then recreates an identical new table instead of deleting the data in the table line by line, and executes data faster than DELETE. Therefore, when you need to delete all the data rows in the table, try to use the TRUNCATE statement to shorten the execution time.
  • After DELETE deletes data, the data can be retrieved with event rollback; TRUNCATE does not support transaction rollback, and data cannot be retrieved after deletion.
  • After DELETE deletes data, the system will not reset the counter of the self-incrementing field; after TRUNCATE clears the table records, the system will reset the counter of the self-incrementing field.
  • DELETE is more widely used, because it can delete some data by specifying conditions in the WHERE clause; TRUNCATE does not support the WHERE clause, and can only delete the whole.
  • DELETE will return the number of rows deleted, but TRUNCATE will only return 0, meaningless

4. Case

(1) There is an examination paper answer record table exam_record, which contains user answer examination paper records for many years, and the structure is as follows:

Answer record table exam_record:

start_time is the test paper start time

submit_time is the submission time, that is, the end time

Filed Type Null Key Extra Default Comment
id int(11) NO AT auto_increment (NULL) auto-increment ID
uid int(11) NO (NULL) User ID
exam_id int(11) NO (NULL) paper ID
start_time datetime NO (NULL) Starting time
submit_time datetime YES (NULL) submission time
score tinyint(4) YES (NULL) Score
DELETE FROM exam_record
WHERE TIMESTAMPDIFF(MINUTE, start_time, submit_time) < 5
    AND score < 60;

Please delete the records in the exam_record table where the answer time is less than 5 minutes and the score is not passing (60 points);

(2) There is an examination paper answer record table exam_record, which contains user answer paper records for many years, and the structure is as follows:

Answer record table exam_record:

start_time is the test paper start time

submit_time is the submission time, that is, the end time, if it is not completed, it will be empty

Filed Type Null Key Extra Default Comment
id int(11) NO AT auto_increment (NULL) auto-increment ID
uid int(11) NO (NULL) User ID
exam_id int(11) NO (NULL) paper ID
start_time datetime NO (NULL) Starting time
submit_time datetime YES (NULL) submission time
score tinyint(4) YES (NULL) Score
Delete from exam_record
WHERE TIMESTAMPDIFF(MINUTE, start_time, submit_time) < 5
    or submit_time IS NULL
order by start_time
limit 3;

Please delete the 3 records with the earliest answering time among the records in the exam_record table that have not completed the answer or the answering time is less than 5 minutes.

(3) There is an examination paper answer record table exam_record, which contains user answer paper records for many years, and the structure is as follows:

Filed Type Null Key Extra Default Comment
id int(11) NO AT auto_increment (NULL) auto-increment ID
uid int(11) NO (NULL) User ID
exam_id int(11) NO (NULL) paper ID
start_time datetime NO (NULL) Starting time
submit_time datetime YES (NULL) submission time
score tinyint(4) YES (NULL) Score
DELETE FROM exam_record;
ALTER TABLE exam_record drop id;

Please delete all records in the exam_record table and reset the auto-increment primary key.

Guess you like

Origin blog.csdn.net/weixin_48272780/article/details/128318227