How to add, delete, modify and query table data in MySQL?
青灯夜游
Release: 2020-10-05 12:16:18
Original
11755 people have browsed it
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>]
]
Copy after login
The meaning of each clause is as follows:
##{*| } A field list containing the asterisk wildcard character, indicating the name of the field to be queried.
##
,
…, Table 1 and Table 2 represent the source of query data, which can be single or multiple.
WHERE 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[,]], this clause tells MySQL to display the number of queried data items each time.
Example: Specified fields in the query tableThe syntax format of a certain field in the query table is:
SELECT < 列名 > FROM < 表名 >;
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> 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 <字段名1>,<字段名2>,…,<字段名n> FROM <表名>;
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 syntaxThe INSERT statement has two syntax forms, namely the INSERT…VALUES statement and the INSERT…SET statement. 1) INSERT...VALUES statement
: Specify the name of the table to be operated on.
: 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
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 <表名>
SET <列名1> = <值1>,
<列名2> = <值2>,
…
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 statementUse the UPDATE statement to modify a single table. The syntax format is:
UPDATE <表名> SET 字段 1=值 1 [,字段 2=值 2… ] [WHERE 子句 ]
[ORDER BY 子句] [LIMIT 子句]
Copy after login
The syntax description is as follows:
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.
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!
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