Inserting Data into Tables

After you create the tables, you can put data into them. You can insert data manually with the insert statement, which uses the following syntax:

INSERT INTO table_name VALUES('valuel', 'value2', 'value3', ...);

This statement inserts valuel, vaiue2, and so on into the table tabie_name. The values that are inserted constitute one row, or record, in the database. Unless specified otherwise, values are inserted in the order in which the columns are listed in the database table. If, for some reason, you want to insert values in a different order (or if you want to insert only a few values and they are not in sequential order), you can specify which columns you want the data to go in by using the following syntax:

INSERT INTO table_name (column1,column4) VALUES('valuel', 'value2');

You can also fill multiple rows with a single insert statement, using syntax such as the following:

INSERT INTO table_name VALUES('valuel', 'value2'),('value3', 'value4');

In this statement, valuel and value2 are inserted into the first row and value3 and value4 are inserted into the second row.

The following example shows how you would insert the Nevermind entry into the cd_collection table:

INSERT INTO cd_collection VALUES(9, 'Nevermind', ''Nirvana', '1991, ''NULL);

MySQL requires the null value for the last column (rating) if you do not want to include a rating. PostgreSQL, on the other hand, lets you get away with just omitting the last column. Of course, if you had columns in the middle that were null, you would need to explicitly state null in the insert statement.

Normally, insert statements are coded into a front-end program so users adding data to the database do not have to worry about the SQL statements involved.

Was this article helpful?

0 0

Post a comment