Home > Database > Mysql Tutorial > body text

How to use SQL statements to modify data in MySQL

PHPz
Release: 2023-04-20 13:41:14
Original
6219 people have browsed it

MySQL is an open source relational database management system that is widely used in the field of Web development. MySQL supports SQL language. Through SQL statements, we can add, delete, modify and query tables in MySQL. When we need to modify data in MySQL, we need to use SQL statements to achieve it.

This article will introduce how to use SQL statements to modify data in MySQL.

1. Modify the data in the table

When we need to modify the data in the table in MySQL, we can use the UPDATE statement to achieve it. 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". As you can see, using 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 of gender to the students table. Its Type is varchar(10), default value is 'male'. As you can see, the ALTER TABLE statement allows us to 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 gender column in the students table. 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

The above statement will modify the name column type in the students table to varchar (20). As you can see, the ALTER TABLE statement is also suitable for modifying the data type of a column.

3. Modify the table name

Sometimes we need to modify the table name, which can be achieved using the RENAME statement. The following is an example:

RENAME TABLE students TO new_students;
Copy after login

The above statement changes the students table name to new_students. As you can see, the RENAME statement can be used to rename the table name.

4. Summary

MySQL is a powerful database management system. Through SQL statements, we can modify the data in the table, modify the table structure, modify the table name, etc. This article introduces some commonly used SQL statements and hopes to be helpful to readers.

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!

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