Home >Database >Mysql Tutorial >How to use SQL statements to modify data in MySQL

How to use SQL statements to modify data in MySQL

王林
王林forward
2023-05-26 19:33:214375browse

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.

  1. 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.

  1. 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.

  1. 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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete