MySQL database 13 - insert data (INSERT)

The following statement is used to insert data into the student table.

Insert statement:

INSERT INTO student(ID,name,sex,birthday,origin,contact1,contact2,institute)
VALUES ('0013','塔赛努','男','1997/9/15','内蒙古自治区',NULL,NULL,'计算机学院');

INSERT INTO student
VALUES ('0014','呼和嘎拉','男','1995-02-16','青海省','0471-6599999','010-88888888','物理系',NULL);

INSERT INTO student(ID,name,sex,birthday,institute)
VALUES ('0015','孔乙己','男','1995-05-29','中文系');

Since the student information to be inserted is incomplete, the specified field list must be added after the table name.

If complete, then no fields are required.

By running the results, it can be found that the fields with no data inserted are all NULL values. In fact, in the VALUES clause, you can directly specify which field is set to the NULL value, so that the field list after the table name can be omitted since there is a placeholder for the NULL value.

Notice:

If some fields have non-empty restrictions, omitting them when inserting will result in an error.

But if there is a default value setting, then no error will be reported: (gender omitted)

For example

INSERT INTO student(ID,name,birthday,institute)
VALUES ('0016','鲁十八','1997-07-07','中文系');

The default male is filled up.

You can modify the default value in the table:

 


 Insert query results into table

 Insert all the data in the student table into the student_copy table through INSERT SELECT.

Analysis: Because the table structures of the two tables are the same, and the contents of all the fields in the student are to be inserted into the student_copy table, the field list can be omitted in the INSERT clause.

INSERT INTO  student_copy
SELECT  * FROM    student;

 


The difference between INSERT SELECT and SELECT INTO

I have introduced the use of SELECT INTO to save the query results as a new table, and just introduced the use of INSERT SELECT to insert the query results into the newly created data table. It seems that these two statements perform the same function, and there is no difference. In fact, they are different, and the specific differences are as follows.

ØSELECT INTO creates a table first when there is no data table, and then puts the query results into the table. If the name of the table to be created is the same as the name of the existing table, an error message will appear.
ØINSERT SELECT can only insert query results to it on the premise that the data table exists, it cannot automatically create a table. If the table to insert data does not exist, an error message will appear.

Insert data through the view

Generally speaking, for a large database system, only the database administrator (DBA) can directly operate the data table, while other users must operate the data through the view, so that the DBA can also operate the table while the ordinary user modifies the data. The operation table mentioned here refers to query, insert, modify and delete data. The following introduces the specific method of inserting data through the view. First, create a vw_computer view for the experiment.

CREATE VIEW vw_computer AS
SELECT * FROM   student
WHERE  institute='计算机学院';

For users, inserting data through a view is basically the same as inserting data directly into a table, the only difference is that the table name becomes the view name. This is illustrated below with an example.

Example  Insert a piece of student information into the student table through the vw_computer view.

INSERT INTO vw_computer
VALUES ('0017','蒋十九','女','1999-05-29','山东省',NULL,NULL,'计算机学院',NULL);

View, there is one more:

 


 Use a view with the WITH CHECK OPTION option

In fact, the example in the previous section leads to another security problem, that is, the administrator of the computer science department can insert the information of students who are not in the computer science department through the vw_bb view. How to control users to only input "Department of Computer Science" into the field of their department has become the biggest problem at present. The answer is to add the WITH CHECK OPTION option when creating a view, because adding this option can prevent users from accidentally or intentionally operating basic table data that does not belong to the scope of the view when inserting, deleting, and updating data through the view. For example, a view is defined as follows:

CREATE VIEW vw_bb AS
SELECT  ID, name, sex, birthday, institute
FROM   student
WHERE  institute='计算机学院'
WITH CHECK OPTION

Then the user can only insert the string "Institute of Computer Science" into the department field, and inserting other strings will make the condition in the WHERE clause (institute='Institute of Computer Science') false, thus restricting the user from inserting non-computer department Access to Student Information. If you want to insert student information from other departments, an error will occur,

To sum up, if you want to restrict users to insert data that does not belong to the scope of view authority through the view, you should add the WITH CHECK OPTION option when creating the view.

Guess you like

Origin blog.csdn.net/weixin_46277779/article/details/129006855