Home > Database > Mysql Tutorial > body text

How to use mysql modification command

王林
Release: 2023-05-30 14:19:37
forward
1645 people have browsed it

  1. Modify table
    In MySQL, we can modify the created table through the ALTER TABLE command. Usually we use the ALTER TABLE command to add, delete, modify table columns, primary keys, indexes and other information. The following are some commonly used ALTER TABLE commands:

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.

  1. Modify rows
    In MySQL, we can modify rows in the table through the UPDATE command. We can use WHERE clause to filter the rows to be updated. Here is 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.

  1. Modify data
    In MySQL, we can use INSERT INTO, REPLACE INTO, UPDATE and other commands to insert and update data into the table. The following are some commonly used INSERT INTO and UPDATE commands:

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 How to use mysql modification command. 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