[Software testing] SQL statements commonly used in MySQL operation data tables (summary)


foreword

What are the operations on the data table?

Create a data table;
view the table structure;
view the creation statement of the data table;
copy the data table;
modify the data table;
delete the data table;

Create data table

grammatical format

CREATE TABLE <表名> ( [表定义选项] )[表选项][分区选项];

Note:
Table name: two ways of writing, you can directly write the name of the data table tbl_name, or db_name.tbl_name, first specify the database and then specify the data table name; the latter way is to create a table under the specified database in this way, regardless of whether it is in the current database or not. Table definition options: generally composed of column names, column definitions, default values, constraints, and
indexes

example

# 创建数据表
CREATE TABLE yytest (
    id INT ( 10 ) NOT NULL UNIQUE PRIMARY KEY,
    uname VARCHAR ( 20 ) NOT NULL,
    sex VARCHAR ( 4 ),
    birth YEAR,
    department VARCHAR ( 20 ),
    address VARCHAR ( 50 ),
    yypolo VARCHAR ( 20 ) 
);

# 指定数据库,创建数据表
CREATE TABLE yytest.polotest (
    id INT ( 10 ) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT,
    stu_id INT ( 10 ) NOT NULL,
    c_name VARCHAR ( 20 ),
    istester VARCHAR ( 50 ),
    grade INT ( 10 ) 
);    

View table structure

desc yytest.yytest;

General field display instructions:
Null: Indicates whether the field can store NULL values
​​Key: Indicates whether the field has been indexed. PRI indicates the primary key, UNI indicates the UNIQUE index, and MUL indicates that a given value is allowed to appear multiple times
Default: indicates whether the field has a default value, and if so, what is the value
Extra: indicates additional information about the field, such as AUTO_INCREMENT, etc.

View the creation statement of the data table

show create table yytest;

Not only can you view the detailed statement when creating the table, but you can also view the storage engine and character encoding

copy data table

# 仅复制表结构
create table yytest2 like yytest;

# 复制表结构和数据
create table yytest3 as select  * from yytest;

# 仅复制表的指定字段结构
create table yytest4 as select id,uname,sex from yytest where 1<>1;

# 复制表的指定字段结构和数据
create table yytest5 as select id,uname,sex from yytest;

# 查看表创建语句:没有包含主键和自增
show create table yytest5;

Note:
Only copy the structure of all fields and directly add like
copy table does not include primary key, index, auto-increment, etc.

modify data table

grammatical format

ALTER TABLE <表名> [修改操作];

Commonly used table modification operations

Modify table name;
modify field data type or field name;
add and delete fields;
modify the arrangement position of fields;
add, drop, change, modify, rename;

modify table name

grammatical format

ALTER TABLE <旧表名> RENAME [TO] <新表名>;

Note:
[TO] can be added or not, and the result will not be affected.
Modifying the table name will not affect the table structure

example

alter table yytest2 rename to yytest22;
alter table yytest22 rename yytest22;

Modify the order of fields

grammatical format

ALTER TABLE <表名> MODIFY <字段名> <数据类型> [FIRST|AFTER 已存在的字段名];

example

# 放在首位
alter table yytest22 modify sex int(2) first;

# 放在birth字段后面
alter table yytest22 modify sex int(2) after birth;

Modify field data type

grammatical format

ALTER TABLE <表名> MODIFY <字段名> <数据类型>;

example

# 修改字段数据类型
alter table yytest22 modify sex int(2);

modify field name

grammatical format

ALTER TABLE <表名> CHANGE <旧字段> <新字段> <数据类型>;

example

修改字段名
alter table yytest22 change sex sexs int(2);

# 修改字段数据类型和字段名
alter table yytest22 change sexs sex varchar(4);

Note:
change can not only change the field name, but also change the field data type

add field

grammatical format

ALTER TABLE <表名> ADD <字段名> <数据类型>  [约束条件] [FIRST|AFTER 已存在的字段名];

example

# 添加字段
alter table yytest22 add  phone varchar(11);

# 添加字段到首位
alter table yytest22 add  phone varchar(11) not null default 2 first;

# 添加字段到某个字段后面
alter table yytest22 add  phone varchar(11) after sex;

delete field

grammatical format

ALTER TABLE <表名> DROP <字段名>

example

# 删除字段
alter table yytest22 drop  phone;

delete data table

grammatical format

DROP TABLE [IF EXISTS] 表名1 [ ,表名2, 表名3 ...]

You can see that it is almost the same as deleting the library

example

# 删除表如果存在
drop table if exists yytest,polotest

Be careful, the data will be cleared!

The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled

1. From entry to mastery of Python programming

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

Only by doing our best can we see the dawn of victory; only by striving hard can we pursue our dreams; only by firm belief can we conquer difficult peaks. Believe in yourself, move forward fearlessly, every effort is one step closer to success!

Only by constantly pursuing progress can we surpass the limit; only by working hard can we reap brilliance. Struggle is the password of life, and persistence is the secret of success. Don't be afraid of difficulties, not afraid of failure, go forward bravely, and live out your splendor!

As long as you have a dream, don't stop running; as long as you have a goal, don't give up the courage to pursue; as long as you are willing to pay, success will not be far away. Believe in yourself, keep working hard, and you will eventually sail to the other side of glory!

Guess you like

Origin blog.csdn.net/x2waiwai/article/details/131501640