Home Database Mysql Tutorial How to add, delete, modify and query table data in MySQL?

How to add, delete, modify and query table data in MySQL?

Oct 05, 2020 pm 12:16 PM
mysql

In mysql, you can use the SELECT statement to query table data, the INSERT statement to add table data, the UPDATE statement to modify table data, and the DELETE statement to delete table data.

How to add, delete, modify and query table data in MySQL?

Querying mysq table data

In MySQL, you can use the SELECT statement to Query data. Querying data refers to using different query methods to obtain different data from the database according to needs. It is the most frequently used and important operation.

The syntax format of SELECT is as follows:

SELECT
{* | <字段列名>}
[
FROM <表 1>, <表 2>…
[WHERE <表达式>
[GROUP BY <group by definition>
[HAVING <expression> [{<operator> <expression>}…]]
[ORDER BY <order by definition>]
[LIMIT[<offset>,] <row count>]
]
Copy after login

The meaning of each clause is as follows:

  • ##{*|<Field column name> } A field list containing the asterisk wildcard character, indicating the name of the field to be queried.

  • ##<Table 1>, <Table 2>…, Table 1 and Table 2 represent the source of query data, which can be single or multiple.
  • WHERE <Expression> is optional. If selected, the query data must meet the query conditions.
  • GROUP BY< Field >, this clause tells MySQL how to display the queried data and group it according to the specified field.
  • [ORDER BY< field>], this clause tells MySQL in what order to display the queried data. The possible sorting is ascending order (ASC) and descending order (DESC ), which is ascending by default.
  • ##[LIMIT[<offset>,]<row count>], this clause tells MySQL to display the number of queried data items each time.
Example: Specified fields in the query table

The syntax format of a certain field in the query table is:

SELECT &lt; 列名 &gt; FROM &lt; 表名 &gt;;
Copy after login

Query the names of all students in the name column of the tb_students_info table. The SQL statement and running results are shown below.

mysql&gt; SELECT name FROM tb_students_info;
+--------+
| name   |
+--------+
| Dany   |
| Green  |
| Henry  |
| Jane   |
| Jim    |
| John   |
| Lily   |
| Susan  |
| Thomas |
| Tom    |
+--------+
10 rows in set (0.00 sec)
Copy after login

The output shows all data under the name field in the tb_students_info table.

Use the SELECT statement to obtain data under multiple fields. You only need to specify the field name to be searched after the keyword SELECT. Different field names are separated by commas "," after the last field. There is no need to add commas. The syntax format is as follows:

SELECT &lt;字段名1&gt;,&lt;字段名2&gt;,…,&lt;字段名n&gt; FROM &lt;表名&gt;;
Copy after login

Add mysq table data

After the database and table are successfully created, you need to add Insert data into the table. In MySQL, you can use the INSERT statement to insert one or more rows of tuple data into an existing table in the database.

Basic syntax

The INSERT statement has two syntax forms, namely the INSERT…VALUES statement and the INSERT…SET statement.

1) INSERT...VALUES statement

INSERT VALUES 的语法格式为:
INSERT INTO &lt;表名&gt; [ &lt;列名1&gt; [ , … &lt;列名n&gt;] ]
VALUES (值1) [… , (值n) ];
Copy after login

The syntax is explained as follows.

<Table name>: Specify the name of the table to be operated on.
  • <Column Name>: Specify the column name into which data needs to be inserted. If data is inserted into all columns in the table, all column names can be omitted, and INSERT<table name>VALUES(…) can be used directly.
  • VALUES or VALUE clause: This clause contains the list of data to be inserted. The order of data in the data list should correspond to the order of columns.
  • 2) INSERT...SET statement
The syntax format is:

INSERT INTO &lt;表名&gt;
SET &lt;列名1&gt; = &lt;值1&gt;,
        &lt;列名2&gt; = &lt;值2&gt;,
        …
Copy after login

This statement is used to directly specify the corresponding values ​​for certain columns in the table. The column value, that is, the column name of the data to be inserted is specified in the SET clause. col_name is the specified column name, and the equal sign is followed by the specified data. For unspecified columns, the column value will be specified as the default value of the column. .

It can be seen from the two forms of INSERT statement:

Use INSERT...VALUES statement to insert one row of data or multiple rows of data into the table;
  • Use the INSERT…SET statement to specify the value of each column in the inserted row, or to specify the values ​​of some columns;
  • INSERT…SELECT statement Insert data from other tables into the table.
  • The INSERT…SET statement can be used to insert the values ​​of some columns into the table, which is more flexible;
  • INSERT…VALUES statement Multiple pieces of data can be inserted at one time.
  • In MySQL, processing multiple inserts with a single INSERT statement is faster than using multiple INSERT statements.
When using a single INSERT statement to insert multiple rows of data, you only need to enclose each row of data in parentheses.

Modification of mysq table data

In MySQL, you can use the UPDATE statement to modify and update the data of one or more tables.

Basic syntax of the UPDATE statement

Use the UPDATE statement to modify a single table. The syntax format is:

UPDATE &lt;表名&gt; SET 字段 1=值 1 [,字段 2=值 2… ] [WHERE 子句 ]
[ORDER BY 子句] [LIMIT 子句]
Copy after login

The syntax description is as follows:

<Table name> ;: Used to specify the name of the table to be updated.

SET clause: used to specify the column name and its column value to be modified in the table. Among them, each specified column value can be an expression or the default value corresponding to the column. If a default value is specified, the column value can be represented by the keyword DEFAULT.

WHERE clause: Optional. Used to limit the rows in the table to be modified. If not specified, all rows in the table will be modified.

ORDER BY 子句:可选项。用于限定表中的行被修改的次序。

LIMIT 子句:可选项。用于限定被修改的行数。

注意:修改一行数据的多个列值时,SET 子句的每个值用逗号分开即可。

实例:修改表中的数据

在 tb_courses_new 表中,更新所有行的 course_grade 字段值为 4,输入的 SQL 语句和执行结果如下所示。

mysql&gt; UPDATE tb_courses_new
    -&gt; SET course_grade=4;
Query OK, 3 rows affected (0.11 sec)
Rows matched: 4  Changed: 3  Warnings: 0
mysql&gt; SELECT * FROM tb_courses_new;
+-----------+-------------+--------------+------------------+
| course_id | course_name | course_grade | course_info      |
+-----------+-------------+--------------+------------------+
|         1 | Network     |            4 | Computer Network |
|         2 | Database    |            4 | MySQL            |
|         3 | Java        |            4 | Java EE          |
|         4 | System      |            4 | Operating System |
+-----------+-------------+--------------+------------------+
4 rows in set (0.00 sec)
Copy after login

mysq表数据的删除

在 MySQL 中,可以使用 DELETE 语句来删除表的一行或者多行数据。

删除单个表中的数据

使用 DELETE 语句从单个表中删除数据,语法格式为:

DELETE FROM &lt;表名&gt; [WHERE 子句] [ORDER BY 子句] [LIMIT 子句]
Copy after login

语法说明如下:

  • <表名>:指定要删除数据的表名。

  • ORDER BY 子句:可选项。表示删除时,表中各行将按照子句中指定的顺序进行删除。

  • WHERE 子句:可选项。表示为删除操作限定删除条件,若省略该子句,则代表删除该表中的所有行。

  • LIMIT 子句:可选项。用于告知服务器在控制命令被返回到客户端前被删除行的最大值。

注意:在不使用 WHERE 条件的时候,将删除所有数据。

删除表中的全部数据

实例:删除 tb_courses_new 表中的全部数据,输入的 SQL 语句和执行结果如下所示。

mysql&gt; DELETE FROM tb_courses_new;
Query OK, 3 rows affected (0.12 sec)
mysql&gt; SELECT * FROM tb_courses_new;
Empty set (0.00 sec)
Copy after login

推荐教程:mysql视频教程

The above is the detailed content of How to add, delete, modify and query table data in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

What are the application scenarios of Java enumeration types in databases?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

Performance optimization strategies for PHP array paging Performance optimization strategies for PHP array paging May 02, 2024 am 09:27 AM

Performance optimization strategies for PHP array paging

See all articles