官术网_书友最值得收藏!

Inserting data

Now that we have built our tables, it is time to insert records into the tables. Let us look at a few different methods for inserting a single row of data and inserting multiple rows of data. For insertion of data into a table, we will use the INSERT DML command, and supply the table name and the values for the available columns in the table. Let us begin by inserting student records into the students table:

Inserting data

In this example, we insert a new student record into the students table; we are supplying the data for that student record in the VALUES clause. This syntax, though it appears to be very simple, it is not a very safe method of inserting data. This INSERT statement is depending upon the order in which the columns were defined in the table structure, so the data in the VALUES clause will be mapped by position, 1 would go into the first column in the table, though it is intended to go into the student_id column. If the students table is rebuilt locally or on a different machine, there is no guarantee that the column order would remain the same as the order on the current MariaDB database server. The other approach that is considered safer when compared to this one is the INSERT statement, where the column names are explicitly mentioned in the SQL:

Inserting data

Though this might be a bit longer, this would guarantee that data that is being passed in via the VALUES clause is going into the right column. By using this INSERT syntax, the order in which the columns are mentioned is no longer important. When this query is executed, MariaDB matches each item in the columns list with its respective value in the VALUES list by position. This syntax can also be used for the case where the data is only available for a few columns. Let us come up with an INSERT statement that has data for a few columns and uses NULL for a column that does not have any data:

Note

In SQL, the term NULL is used to denote that a value does not exist.

Inserting data

In this example, we are inserting a student record whose address is not known, so we are using NULL to populate the column.

Note

Columns by default allow NULL values to be populated, unless it is explicitly mentioned not to allow NULL values.

Now that we have seen the different insertion syntaxes for inserting a single record row, let us take a step forward and look at how multiple records can be inserted. There are two ways of inserting multiple records into a table, the first method is where INSERT statements are created for each row, and are separated by the statement terminator (;):

Inserting data

The other way of inserting multiple records is by using a single VALUES clause while passing in multiple records, separating each record with a comma (,), and adding a statement terminator at the end of the last record:

Inserting data

Tip

We are currently not using any constraints to maintain any referential integrity among tables, so any integers can be inserted into the students_courses table. To allow only existing student IDs and course IDs to be inserted, we will have to use the primary key and foreign key constraints. We will be covering constraints in the next chapter.

In this example, we are inserting multiple records into the students_courses table. On execution of this SQL query, the first statement inserts an associative record into the students_courses table and the value for the column student_id is 1, which maps back to the student record of John Doe, and the value for course_id is 1 that corresponds to the course record CS-101. The inline comments at the end of each statement are used to describe the data that is being inserted via this statement. Though these comments are added to the INSERT statements, they are only intended to explain the purpose of the statements and will not be processed by MariaDB.

Note

MariaDB also supports multi-line comments. Syntax for creating multi-line comments is by using /* to start the comment and ending the comment with */.

/* multiple line

comments

go

here*/

The last method of insertion that we are skipping for now is to insert the data that has been retrieved on the fly from a table. We will be looking at that once we have covered the methods for retrieving data and filtering data.

主站蜘蛛池模板: 肇源县| 罗江县| 罗定市| 延寿县| 嘉善县| 深泽县| 固安县| 绥棱县| 突泉县| 虎林市| 锦州市| 丹东市| 雷波县| 寿光市| 洱源县| 濉溪县| 页游| 册亨县| 绍兴市| 丹棱县| 抚州市| 西藏| 大名县| 灵宝市| 同心县| 广德县| 青海省| 南召县| 马尔康县| 佛冈县| 镇巴县| 汤原县| 浦北县| 榆中县| 西乌| 东丰县| 绵阳市| 江陵县| 泸州市| 合川市| 屏东县|