Home >
Database >
Mysql Tutorial >
What are the basic statements for database addition, deletion, modification and query?
What are the basic statements for database addition, deletion, modification and query?
青灯夜游
Release: 2020-11-04 10:25:44
Original
67658 people have browsed it
Basic statements for add, delete, modify and query in the database: "INSERT INTO table name field list VALUES (value list)", "DELETE FROM table name WHERE clause", "UPDATE table name SET column = value WHERE clause" , "SELECT * FROM table name".
If the data is character type, single quotes or double quotes must be used, such as: "value".
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
VALUES(…) can be used directly.
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.
Example: Insert a new record in the tb_courses table, the course_id value is 1, the course_name value is "Network", the course_grade value is 3, and the info value is "Computer Network".
Before performing the insert operation, check the tb_courses table
mysql> SELECT * FROM tb_courses;
Empty set (0.00 sec)
Copy after login
The query result shows that the current table content is empty and there is no data. Next, perform the insert data operation. The input SQL statement and execution process are as follows shown.
You can see that the record was inserted successfully. When inserting data, all fields of the tb_courses table are specified, so new values will be inserted for each field.
Note:
The order of column names after the INSERT statement may not be the order when the tb_courses table is defined, that is, when inserting data, there is no need to insert it in the order defined by the table. Just make sure that the order of the values is the same as the order of the column fields.
When using INSERT to insert data, the column name list column_list is allowed to be empty. At this time, the value list needs to specify a value for each field of the table, and the order of the values must be the same as in the data table. The fields are defined in the same order.
Note: Although you can ignore the column name of the inserted data when using INSERT to insert data, if the value does not contain the column name, the value after the VALUES keyword must not only be complete, but also The order must be the same as the order of the columns when the table was defined. If the structure of the table is modified and columns are added, deleted, or their positions are changed, these operations will cause the order in which data is inserted in this way to change at the same time. If you specify column names, they will not be affected by table structure changes.
Database deletion
In MySQL, you can use the DELETE statement to delete one or more rows of data in a table.
The syntax format is:
DELETE FROM 表名 [WHERE 子句] [ORDER BY 子句] [LIMIT 子句]
Copy after login
Delete specified data:
DELETE FROM 表名称 WHERE 列名称 = 值
Copy after login
The syntax description is as follows:
Table name: Specify the table name from which data is to be deleted.
ORDER BY clause: Optional. Indicates that when deleting, rows in the table will be deleted in the order specified in the clause.
WHERE clause: Optional. Indicates that the deletion conditions are limited for the deletion operation. If this clause is omitted, it means that all rows in the table are deleted.
LIMIT clause: Optional. Used to tell the server the maximum number of rows to be deleted before the control command is returned to the client.
Note: When the WHERE condition is not used, all data will be deleted.
Example 1: Delete all data in the table
Delete all data in the tb_courses_new table. The input SQL statement and execution results are as follows.
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)
Copy after login
Example 2: Delete data in the table based on conditions
In the tb_courses_new table, delete the record with course_id 4. The input SQL statement and execution results are as follows.
mysql> DELETE FROM tb_courses
-> WHERE course_id=4;
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM tb_courses;
+-----------+-------------+--------------+------------------+
| course_id | course_name | course_grade | course_info |
+-----------+-------------+--------------+------------------+
| 1 | Network | 3 | Computer Network |
| 2 | Database | 3 | MySQL |
| 3 | Java | 4 | Java EE |
+-----------+-------------+--------------+------------------+
3 rows in set (0.00 sec)
Copy after login
It can be seen from the running results that the record with course_id 4 has been deleted.
Database modification data
In MySQL, you can use the UPDATE statement to modify and update data in one or more tables.
The syntax format is:
UPDATE 表名 SET 字段=值 [WHERE 子句 ]
[ORDER BY 子句] [LIMIT 子句]
Copy after login
Example:
UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
Copy after login
The syntax description is as follows:
Table name: used to specify the update table name.
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 clause: Optional. Used to limit the order in which rows in a table are modified.
LIMIT clause: Optional. Used to limit the number of rows that are modified.
查询 tb_students_info 表中 name 列所有学生的姓名,SQL 语句和运行结果如下所示。
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 above is the detailed content of What are the basic statements for database addition, deletion, modification and query?. 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