How to write delete statement in PHP?

coldplay.xixi
Release: 2023-03-02 21:28:01
Original
3176 people have browsed it

How to write the PHP delete statement: 1. Use the delete statement to delete, the code is [delete from friends where user_name = ""]; 2. Use the truncate statement to delete, the code is [TRUNCATE [TABLE] tbl_name].

How to write delete statement in PHP?

How to write the PHP delete statement:

MySQL provides us with delete and truncate statements to delete data.

1. Definition of delete statement:

Most of the delete statements are used when deleting data. Now let's look at the definition of delete statement.

DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name    
[WHERE where_definition]    [ORDER BY ...]    [LIMIT row_count]
Copy after login
delete from friends where user_name = 'simaopig';
Copy after login

delete Note:

From the syntax structure, we can see that, like the update syntax, we can omit the where clause. But this is a very dangerous behavior. Because if you do not specify the where clause, delete will delete all records in the table, and delete them immediately.

2. A brief explanation of the truncate statement:

This statement I have never come into contact with or used it before. Because generally, everyone uses the delete statement to delete data. In fact, this truncate command is very simple. It means: delete all records in the table. It is equivalent to the delete statement without a where clause. The syntax structure is:

TRUNCATE [TABLE] tbl_name
Copy after login

Here is a simple example. I want to delete all the records in the friends table. I can use the following statement:

truncate table friends;
Copy after login

The efficiency of truncate and delete Question:

If you want to delete all data in the table, the truncate statement is faster than the delete statement. Because truncate deletes the table and then re-creates it according to the table structure, while delete deletes the records and does not try to modify the table. This is why when inserting data into a table that has been cleared using delete, MySQL will remember the previously generated AUTOINCREMENT sequence and continue to use it to number the AUTOINCREMENT fields. After truncate deletes the table, the table numbers the autoincrement fields starting from 1.

However, the truncate command is fast and fast, but it is not as safe for transaction processing as the delete command. Therefore, if the table we want to truncate is currently undergoing transaction processing, this command will exit with an error message.

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of How to write delete statement in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template