What is the command used to add new data to the database?

青灯夜游
Release: 2022-07-05 11:11:13
Original
18413 people have browsed it

The command used to add a new data in the database is "insert". The INSERT command can insert one or more rows of tuple data into an existing table in the database. It has two syntax forms: 1. With the VALUES keyword, the syntax is "INSERT INTO table name (column name 1,...column name n) VALUES (value 1..., value n);"; 2. With the SET keyword, the syntax is "INSERT INTO table name SET column name 1 = value 1, column name 2 = value 2,...;".

What is the command used to add new data to the database?

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

The command used to add a new data in the database isinsert.

insert command to insert data

After the database and table are successfully created, data needs to be inserted into the database table. You can use the INSERT statement to insert one row or multiple rows of tuple data into an existing table in the database.

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

1,INSERT...VALUES statement

The syntax format is:

INSERT INTO 表名 (列名1,…列名n) VALUES(值1…,值n);
Copy after login
  • First, after the INSERT INTO clause, specify the table name and a comma-separated list of columns in parentheses.

  • Then, place the comma-separated values of the corresponding columns in parentheses after the VALUES keyword.

#Before executing the insert statement, you need to have the INSERT permission to execute the INSERT statement.

2,INSERT...SET statement

The syntax format is:

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

This statement is used to directly Certain columns in the table specify corresponding column values, 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 The value is assigned the default value for 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.

Insert command usage example:

1. Add values to all fields in the table

Create a course information table tb_courses in the test_db database, including course number course_id, course name course_name, course credits course_grade and course notes course_info

CREATE TABLE tb_courses( course_id INT NOT NULL AUTO_INCREMENT, course_name CHAR(40) NOT NULL, course_grade FLOAT NOT NULL, course_info CHAR(100) NULL, PRIMARY KEY(course_id) );
Copy after login

What is the command used to add new data to the database?

There are two ways to insert values into all fields in the table: one is to specify all field names; the other is to not specify field names at all.

[Example 1] 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".

First check the tb_courses table

What is the command used to add new data to the database?

The query result shows that the current table content is empty and there is no data. Next, perform the operation of inserting data

INSERT INTO tb_courses(course_id,course_name,course_grade,course_info) VALUES(1,'Network',3,'Computer Network');
Copy after login

What is the command used to add new data to the database?

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.

The order of the column names after the INSERT statement does not need to be the order when the tb_courses table is defined. That is, when inserting data, it is not necessary to insert in the order defined by the table, as long as the order of the values is the same as the order of the column fields.

2. Add values to specified fields in the table

Insert data into specified fields in the table. Insert values into only some fields in the INSERT statement, while leaving others The value of the field is the default value when the table is defined.

[Example 2] Insert a new record in the tb_courses table, the course_name value is "System", the course_grade value is 4, and the course_info value is "Operating System"

INSERT INTO tb_courses SET course_name = 'System', course_grade = 4, course_info = 'Operation System';
Copy after login

What is the command used to add new data to the database?

【相关推荐:mysql视频教程

The above is the detailed content of What is the command used to add new data to the database?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!