1. Modify the data in the table
To change the data in the MySQL data table, you can use the UPDATE statement. The following is an example:
UPDATE students SET name = 'Tom' WHERE id = 1;
The above statement modifies the name field of the record with id 1 in the students table to "Tom". Through the UPDATE statement, we can easily modify the data in the table.
2. Modify the table structure
In addition to modifying the data in the table, sometimes we also need to modify the table structure. For example, we need to add a column, delete a column, modify the type of a column, etc.
Add a new column
The syntax for adding a new column is as follows:
ALTER TABLE students ADD COLUMN gender varchar(10) DEFAULT 'male';
The above statement will add a new column to the students table Add a gender column, its type is varchar(10), and the default value is 'male'. Using the ALTER TABLE statement, we can easily modify the table structure.
Delete a column
The syntax for deleting a column is as follows:
ALTER TABLE students DROP COLUMN gender;
The above statement will delete the column named gender from the table students of columns. As you can see, the ALTER TABLE statement is also suitable for deleting columns.
Modify column type
The syntax for modifying column type is as follows:
ALTER TABLE students MODIFY COLUMN name VARCHAR(20);
This sentence can be rewritten as: " In the students table, change the data type of the name column to varchar(20).". Depending on the context, the sentence can be rewritten using: The ALTER TABLE statement can modify the data type of a column.
3. Modify the table name
Sometimes we need to modify the table name, we can use the RENAME statement to achieve this. The following is an example:
RENAME TABLE students TO new_students;
Modify the students table name to new_students. As you can see, the RENAME statement can be used to rename the table name.
The above is the detailed content of How to use SQL statements to modify data in MySQL. For more information, please follow other related articles on the PHP Chinese website!