MySQL is a popular relational database management system. Its simplicity, ease of use and high performance make it one of the first choices for web application development. When we need to modify the data in the MySQL database, we need to master certain MySQL modification commands.
This article will introduce the basic use of MySQL modification commands, and explain the specific meaning and common uses of each command.
1.1 Add columns
We can use the ALTER TABLE command to add new columns to an existing table. Here is a simple example:
ALTER TABLE users
ADD COLUMN age
INT(10) DEFAULT NULL AFTER name
;
The above command will add a column named age to the users table, with type INT, length 10, initial value NULL, and place it after the name column.
1.2 Modify columns
If we need to modify some columns of the table, such as modifying the column name, data type, length, etc., we can use the ALTER TABLE command to achieve this. The following is a simple example:
ALTER TABLE users
MODIFY COLUMN age
VARCHAR(10);
The above command will modify the users table The data type of the age column is VARCHAR and the length is 10.
1.3 Delete columns
We can use the ALTER TABLE command to delete columns in the table. The following is a simple example:
ALTER TABLE users
DROP COLUMN age
;
The above command will delete the name age from the users table of columns.
UPDATE users
SET age
= age
1 WHERE gender
= 'male';
The above command will update the age column of all rows with gender male in the users table and add 1 to their values.
3.1 INSERT INTO command
We can use the INSERT INTO command to insert data into the table. Here is a simple example:
INSERT INTO users
(name
, age
, gender
) VALUES (' Alice', 25, 'female');
The above command will add a user named Alice to the users table, with an age of 25 and a gender of female.
3.2 UPDATE command
We can use the UPDATE command to update the data in the table. Here's a simple example:
UPDATE users
SET age
= age
1 WHERE gender
= 'male' ;
The above command will update the age column of all rows with gender male in the users table and add 1 to their values.
The above is the detailed content of mysql modification command. For more information, please follow other related articles on the PHP Chinese website!