Add, delete, modify and query tables in MySQL

Table of contents

1. CRUD

2. Add (Create)        

(1) Grammar

(2) Single row of data + full column insertion

(3) Multiple rows of data + specified column insertion

3. Retrieve

(1) Grammar

(2) Full column query

(3) Specify column query

(4) The query field is an expression

        1. The expression does not contain fields:

        2. The expression contains a field

       The table structure is as follows:

        3. The expression contains multiple fields

(5) Alias

(6) Deduplication: DISTINCT        

(7) Sorting: ORDER BY

1. Grammar:

2. Ascending order

3. Descending order

4. Sort using expressions and aliases

5. Multiple priorities can be sorted. The sorting priorities follow the writing order.

(8) Conditional query: WHERE

 1. Comparison operators

2. Logical operators

3. Basic query:

4、AND和OR:

5. Range query

        (1) BETWEEN ... AND

        (2)IN

6. Fuzzy query: LIKE

7. NULL query: IS [NOT] NULL        

8. Paging query: LIMIT

4. Modification (Update)

Case:

-- Change Sun Wukong's math score to 80 points

-- Change Cao Mengde's math score to 60 points and his Chinese score to 70 points

--Add 30 points to the math scores of the three students with the lowest total scores

--Update the Chinese scores of all students to 2 times the original value

5. Delete

Case:

--Delete Sun Wukong's test scores

-- Delete the entire table data

6. Summary of key contents

NewEdit

QueryEdit

ModifyEdit

DeleteEdit

You’ve all seen this, give it a thumbs up and go away, thank you, thank you! ! !


1. CRUD

Note: You can use "--space + description" in SQL to indicate comments
CRUD, that is, add (Create), query (Retrieve), update (Update), and delete (Delete)Four-wordacronym
 


2. Add (Create)        

Existing table structure and table data:

(1) Grammar

INSERT [INTO] table_name
[(column [, column] ...)]
VALUES (value_list) [, (value_list)] ...
value_list: value, [, value] ...

 

(2) Single row of data + full column insertion

Syntax:insert into table name values(data to be inserted, ...)

Note: The order of inserted data must be the same as the table structure, otherwise the insertion will fail.

Code display:

insert into books values('数学', '张三', 6.66, '数学类');
insert into books values('英语', '李四', 8.88, NULL);

The result is as follows:

(3) Multiple rows of data + specified column insertion

Syntax:insert into table name (specified column name, ...) values ​​(specified column name data)

Code display

insert into books (name, price, sort) values('语文', 5.55, '语文类'), ('物理', 7.77, '物理类');

The result is as follows:


3. Retrieve

(1) Grammar

SELECT
[DISTINCT] {* | {column [, column] ...}
[FROM table_name]
[WHERE ...]
[ORDER BY column [ASC | DESC], ...]
LIMIT ...

 

(2) Full column query

Note:Select query operations are all temporary tables and will not change the original data

Syntax:select * from table name

Code display:

select * from books;

The result is as follows:

Note: Under normal circumstances it is not recommended to use * for full column query
-- 1. query The more columns there are, the greater the amount of data that needs to be transmitted; (too much information needs to be processed, and the server may be clogged. A large amount of hard disk IO and network IO may fill up the bandwidth of the hard disk or network card. Once the bandwidth is full, If it is full, the server will not be able to respond to other clients' requests normally. In the view of other clients, it will be considered that the server is down)
-- 2. < a i=5>may affect the use of index

(3) Specify column query

Syntax:select the specified column name,... from table name

Code display:

select name, author, price from books;

The result is as follows:

Note: The specified column query can not be queried according to the column order of this table, as shown in the figure

(4) The query field is an expression

        1. The expression does not contain fields:

Syntax:select expression, ... from books

Code demo:

 select name, price, 10 from books;

The results are as follows

Because there is no 10, a field 10 will be created and 10 will be placed below.

        2. The expression contains a field

Syntax:select expression containing one field, ... froms table name

Code demo:

select name, author, price + 10 from books;

The result is as follows:

       The table structure is as follows:

        3. The expression contains multiple fields

Syntax:select expression containing multiple fields,... from table name

Integer data code demonstration:

 select name, chinese + math + english from report;

The result is as follows:

Not an integer data code demonstration:

select price, name + author + sort from books;

The result is as follows:

As you can see, the expression contains multiple rows of fields. As long as there is NULL, it will be NULL, and the sum of the strings equals 0.

(5) Alias

Syntax:select column name,... as alias from report

Note (as can be omitted, but it is better to add it and it is easy to distinguish)

Code demo:

 select id, chinese + math + english as 总分 from report;

The result is as follows:

(6) Deduplication: DISTINCT        

Existing table:

Syntax:select distinct column name,... from table name

Code display:

 select distinct math from exam_result;

The result is as follows:

(7) Sorting: ORDER BY

1. Grammar:

--ASC is ascending order (from small to large)
--DESC is descending order (from large to small)
--The default is ASC< /span> ORDER BY column [ASC|DESC], [...];
SELECT ... FROM table_name [WHERE ...]

 

2. Ascending order

Syntax:select * ( or other column names are also acceptable) from table name order by column name

Code display:

 select * from exam_result order by math;

The result is as follows:

3. Descending order

Syntax:select * ( or other column names are also acceptable) from table name order by column name desc

Code display:

select * from exam_result order by math desc;

The result is as follows:

4. Sort using expressions and aliases

Syntax:select column name, expression from table name order by expression;

Code display:

 select name, math + chinese + english as total from exam_result order by math + chinese + english;

The result is as follows:

Notice:

Here you can use aliases after order by

5. Multiple priorities can be sorted. The sorting priorities follow the writing order.

Syntax:select the column to be queried exam_result order by column name, column name...;

Code demo:

 select * from exam_result order by math, english;

The result is as follows:

(8) Conditional query: WHERE

Note:where conditions can use expressions, but not aliases

 1. Comparison operators

2. Logical operators

Note: 1. AND has a higher priority than OR. When used at the same time, you need to use parentheses () to wrap the part that is executed first

           2. WHERE conditions can use expressions, but not aliases< a i=4>.

As shown in the figure:

                Execution order of sql statements:

                Step 1: Take out a record (traverse the table)

                Step 2: Bring records into conditions and determine whether they are met

                Step 3: If the conditions are met, take out the columns specified by select and perform some expression operations

Therefore, aliases cannot be used after where in conditional query.

3. Basic query:

Query students whose English scores are greater than 70 points:

code show as below:

 select name, english from exam_result where english > 70;

The result is as follows:

Check students whose English score is higher than Chinese score:

code show as below:

 select name, english, chinese from exam_result where english > chinese;

The result is as follows:

Query students whose total score is less than 200:

code show as below:

 select name, chinese + math + english from exam_result where (chinese + math + english) < 200;

The result is as follows:

4、AND和OR:

Query students whose Chinese scores are greater than 80 points and whose English scores are greater than 80 points

code show as below:

select name, chinese, english from exam_result where (chinese > 80) and (english > 80);

The result is as follows:

Search for students whose Chinese scores are greater than 80 points, or whose English scores are greater than 80 points

code show as below:

 select name, chinese, english from exam_result where (chinese > 80) or (english > 80);

The result is as follows:

Note:and has higher priority than or. When using it, you can use parentheses if necessary

5. Range query

        (1) BETWEEN ... AND

        Code display:

select * from exam_result where chinese between 80 and 90;

        Results display:


     This can also be achieved using and

Code:

 select * from exam_result where chinese >= 80 and chinese <= 90;

        (2)IN

code show as below:

 select name, math from exam_result where math in(58, 59, 98, 99);

Results display:

You can also use or here

Code:

select name, math from exam_result where math = 58 or math = 59 or math = 98 or math = 99;

6. Fuzzy query: LIKE

        out out out out of any


Code display:

 select name from exam_result where name like '孙%';

The result is as follows:

Code display:

 select name from exam_result where name like '孙_';

The result is as follows:

Note:% can also be used like this: %Sun, which is the field ending with "Sun"; %Sun%, as long as the field contains "Sun"

        ​ ​ _Can also be used like this: _Sun, which is a field with only one character in front and "Sun" at the end, multiple underscores _ can also be used.

7. NULL query: IS [NOT] NULL        

Determine whether a column in the table is NULL or not NULL

code show as below:

select name from exam_result where name is not null;

The result is as follows:

code show as below:

select name from exam_result where name is NULL;

The result is as follows:

8. Paging query: LIMIT

Syntax:
-- The starting index is 0
-- Starting from 0, filter n results
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n;
-- Starting from s, filter n results
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n;
-- Starting from s, filter n results, More clear than the second usage, it is recommended to use
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;
 

Starting from 0, filter 3 results

Code display:

select * from exam_result limit 3;

The result is as follows:

Starting from 0, filter 3 records, paginate by id, in order

Code display:

select * from exam_result limit 3;

The result is as follows:

Starting from 3, filter 3 records, paginate by id, in order

Code display:

 select * from exam_result order by id limit 3 offset 3;

The result is as follows:


4. Modification (Update)
 

grammar:

UPDATE table_name SET column = expr [, column = expr ...]
[WHERE ...] [ORDER BY ...] [LIMIT ...]

 

Case:
 

-- Change Sun Wukong's math score to 80 points

grammar:

update table name set column name to be modified = value to be modified where column name = value in the column to be specified to be modified

code show as below:

mysql> update exam_result set math = 80 where name = '孙悟空';

The result is as follows:

-- Change Cao Mengde's math score to 60 points and his Chinese score to 70 points

code show as below:

 update exam_result set math = 60, chinese = 70 where name = '曹孟德';

The result is as follows:


--Add 30 points to the math scores of the three students with the lowest total scores

code show as below:

The result is as follows:


--Update the Chinese scores of all students to 2 times the original value

code show as below:

 update exam_result set chinese = chinese * 2;

The result is as follows:


5. Delete

grammar:

DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]
 

Case:
 

--Delete Sun Wukong's test scores

code show as below:

delete from exam_result where name = '孙悟空';

The result is as follows:


-- Delete the entire table data

Prepare a table as follows:

code show as below:

 delete from for_delete;

The result is as follows:


6. Summary of key contents

New

Inquire

Revise

delete


You’ve all seen this, give it a thumbs up and go away, thank you, thank you! ! !

Guess you like

Origin blog.csdn.net/cool_tao6/article/details/134148942