Home >Database >Mysql Tutorial >How to add, delete, modify and query table data in 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.
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>] ]
The meaning of each clause is as follows:
SELECT < 列名 > FROM < 表名 >;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> SELECT name FROM tb_students_info; +--------+ | name | +--------+ | Dany | | Green | | Henry | | Jane | | Jim | | John | | Lily | | Susan | | Thomas | | Tom | +--------+ 10 rows in set (0.00 sec)
The output shows all data under the name field in the tb_students_info table.
SELECT <字段名1>,<字段名2>,…,<字段名n> FROM <表名>;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 syntaxThe INSERT statement has two syntax forms, namely the INSERT…VALUES statement and the INSERT…SET statement. 1) INSERT...VALUES statementINSERT VALUES 的语法格式为: INSERT INTO <表名> [ <列名1> [ , … <列名n>] ] VALUES (值1) [… , (值n) ];The syntax is explained as follows. 722e3d59fd24604761db25f00f9b264f: Specify the name of the table to be operated on.
INSERT INTO <表名> SET <列名1> = <值1>, <列名2> = <值2>, …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;
In MySQL, you can use the UPDATE statement to modify and update the data of one or more tables.
Basic syntax of the UPDATE statementUse the UPDATE statement to modify a single table. The syntax format is:UPDATE <表名> SET 字段 1=值 1 [,字段 2=值 2… ] [WHERE 子句 ] [ORDER BY 子句] [LIMIT 子句]The syntax description is as follows: b5715d1c5343acc275feb1c04543e303:指定要删除数据的表名。
ORDER BY 子句:可选项。表示删除时,表中各行将按照子句中指定的顺序进行删除。
WHERE 子句:可选项。表示为删除操作限定删除条件,若省略该子句,则代表删除该表中的所有行。
LIMIT 子句:可选项。用于告知服务器在控制命令被返回到客户端前被删除行的最大值。
注意:在不使用 WHERE 条件的时候,将删除所有数据。
删除表中的全部数据
实例:删除 tb_courses_new 表中的全部数据,输入的 SQL 语句和执行结果如下所示。
mysql> DELETE FROM tb_courses_new; Query OK, 3 rows affected (0.12 sec) mysql> SELECT * FROM tb_courses_new; Empty set (0.00 sec)
推荐教程: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!