MySQL is a popular open source relational database management system. Sometimes you need to delete some fields in the data table. This article will introduce how to delete field data in MySQL.
First make sure you are connected to the MySQL server and open the database that needs to be changed. Then, use the following sql statement to delete the data of a field in the table:
DELETE FROM table_name WHERE field_name = 'field_value';
Among them, table_name is the name of the data table, field_name is the name of the field to be deleted, and field_value is the value of the field to be deleted.
So, what if you want to delete the entire field? MySQL does not directly support the operation of deleting columns, but it can be simulated through the following steps:
In this new table, you need to explicitly list the name and data type of each field. For example, if you want to delete the "address" field in the data table, the new table will look like this:
CREATE TABLE new_table_name( id INT NOT NULL AUTO_INCREMENT, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, age INT NOT NULL, PRIMARY KEY (id) );
In the above example, the CREATE TABLE statement first specifies a new table name new_table_name, Then four fields are listed (id, firstname, lastname and age) which will remain in the new table (except the address field which is to be deleted).
Use the following statement to insert data from the original table into the new table:
INSERT INTO new_table_name (id, firstname, lastname, age) SELECT id, firstname, lastname, age FROM table_name;
In the above example, the INSERT INTO statement changes the id, firstname, lastname and age fields from Select from the original table and insert into the new table.
This completes deleting the data for the fields in the original table. Use the following statement to drop the original table:
DROP TABLE table_name;
Then, use the following statement to rename the new table to the original table:
RENAME TABLE new_table_name TO table_name;
This will rename the new_table_name table to the table_name table, keeping it consistent with The original table names are the same.
Summary:
This article introduces two methods of deleting field data in MySQL. Use basic SQL statements to delete data in a field in a table or indirectly delete columns by creating a new table and inserting data. These methods can successfully complete the operation that requires data deletion.
The above is the detailed content of mysql delete field data. For more information, please follow other related articles on the PHP Chinese website!