INSERT INTO (Syntax)
Description
The INSERT INTO statement is used to insert a record of data into a table.
Syntax
INSERT INTO table_name [ ( attribute_list ) ] VALUES ( value_list ) [ ; ]
INSERT INTO table_name DEFAULT [ VALUES ] [ ; ]
- table_name : Specifies the name of the table where the data is to be inserted.
- attribute_list : Specifies column names for the values to be inserted. If attribute_list is not specified, all the specified columns in the table must be filled with values. If only some of the columns in the attribute_list are specified, a default value is assigned to the rest of the columns. If there is no default value, NULL is assigned.
- value_list : Specifies values corresponding to the columns of the attribute_list . The value_list field can be an expression, method or a call. Its attribute location and domain type must be the same as those of attribute_list. Each name and value is separated by a comma (,).
- DEFAULT : The INSERT statement in the second example above creates data by assigning a default value to each attribute. If the default value is not set for the column in the table definition, NULL is assigned as the column's value.
Example
- Example 1
- The following is an example of inserting the information about Nam Hyun-Hee, a 2008 Beijing Olympics silver medalist in men's fencing into the athlete table. code is an auto_increment column; a value should not be entered. For more information, see the Auto Increment section in Column.
INSERT INTO athlete (name, gender, nation_code, event) VALUES ('Nam Hyun-Hee', 'W', 'KOR', 'Fencing');
- Example 2
- The following is an example of inserting the general information about 2008 Beijing Olympics into the olympic table.
INSERT INTO olympic (host_year, host_nation, host_city, opening_date, closing_date) VALUES (2008, 'China', 'beijing', '2008-08-08','2008-08-24');
- Example 3
- The following is an example of inserting data using DEFAULT. For this example, create the de_test table and insert data. As shown below, the specified default value is inserted automatically.
CREATE TABLE de_test(
a INT DEFAULT 10,
b INT DEFAULT 20,
c CHAR(1) DEFAULT 'M' );
INSERT INTO de_test DEFAULT;
csql> SELECT * FROM de_test;
csql> ;x
=== <Result of SELECT Command in Line 1> ===
a b c
================================================
10 20 'M'
1 rows selected.