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

How to use SQL statements to modify data in MySQL

王林
Release: 2023-05-26 19:33:21
forward
4410 people have browsed it

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;
Copy after login

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';
Copy after login

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;
Copy after login

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);
Copy after login

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;
Copy after login

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!

Related labels:
source:yisu.com
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