[MySQL] table operation

Table of contents

1. Use the Cmd command to perform operations

1. Use the (mysql -uroot -p) command to enter the database

2. Create a role table for operation (use the database before creating the table)

3. View table

 4. Insert data

5. View the content in the table

6. Modify table

1. Modify the table name

2. Modify the field name

3. Modify the data type of the field

ALTER TABLE table name MODIFY field name data type;

4. Add fields

5. Delete field

7. Update table data

1. Update a single data

2. Update all data

8. Delete table data

1. Single deletion

2. Delete all (that is, clear all data in the table)

2. Use SQLyog software to perform operations

1. Insert data

2. Modify table

1. Modify the table name

2. Modify the field name

 3. Modify the data type of the field

4. Add fields

5. Delete field

​edit 

3. Update table

1. Update a single

2. Update all data

3. delete

1. Delete part

2. Clear table data


1. Use the Cmd command to perform operations

1. Use the (mysql -uroot -p) command to enter the database

2. Create a role table for operation (use the database before creating the table)

3. View table

 4. Insert data

For example: insert role(name , text ,age, gender)values('Zhang Liang','Mage',20,'Male')

5. View the content in the table

Select *from role;

6. Modify table

1. Modify the table name

alter table original table name rename new table name;

 

For example, change the table name of the role table to roles

view table

Change successful

2. Modify the field name

ALTER TABLE table name CHANGE original field name new field name new data type;

View the structure of the table (descibe table name)

Field name: id name age gender text

For example, change text to type

view table

Successfully modified

3. Modify the data type of the field

ALTER TABLE table name MODIFY field name data type;

Example: Change char(1) of gender to char(10)

view table

 

Successfully modified

4. Add fields

ALTER TABLE table name ADD new field name new data type;

 For example: add field power data type is varchar (20)

 view table

5. Delete field

ALRER TABLE table name DROP field name;

 For example: delete the field power;

 view table

successfully deleted

7. Update table data

1. Update a single data

For example, change Zhang Liang's age to 21

 UPDATE roles  set age =21 where name='张良';

 view table

2. Update all data

For example, change the age of all heroes in the character table to 25

 update roles set age=25;

 view table

update completed

8. Delete table data

1. Single deletion

For example, delete hero information whose age is less than 20

 delete from roles where age<20; (note without *)

view table

successfully deleted

Note: It can also be deleted according to other field names

For example, delete the information named Angela

delete from roles where name='Angela';

2. Delete all (that is, clear all data in the table)

delete from table name;

 view table

 successfully deleted

2. Use SQLyog software to perform operations

first use a database

use 数据库名;

view table

show tables;

 

 

 Suppose you want to use roles to operate, first look at the structure of the roles table

describe 表名;

 

View the contents of the roles table 

select *from 表名;

 

At this point, the table does not have any data

1. Insert data

 

insert into 表名 (字段名1,字段名2,字段名3.....)values (    );

 view table

 

2. Modify table

1. Modify the table name

Change roles to role

alter table 原表名 rename 新表名;

Successfully modified

2. Modify the field name

alter table 表名 原字段名 新字段名 数据类型;

 View table structure

 3. Modify the data type of the field

alter table role gender 新数据类型;

For example, change the data type char(10) of the field named gender to varchar(10)

View table structure

Successfully modified

4. Add fields

alter table 表名  add 新字段 数据类型;

 

View table structure

Added successfully

5. Delete field

alter table 表名 drop 字段;

 

 

View the structure of the table

 

 successfully deleted

3. Update table

1. Update a single

UPDATE 表名
SET 字段名1=值1,字段名2=值2,……
(were 条件表达式);

For example, change Sikong Zhen's age to 21

Change successful

2. Update all data

For example, change the age of all heroes in the table to 22

view table

3. delete

1. Delete part

For example, delete hero information whose age is less than 22

 

Heroes younger than 22 have been removed

2. Clear table data

delete from 表名;

 

view table

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/m0_67930426/article/details/130671419