Adding a new record to a table

Previous Index Next

Syntax Examples

The command INSERT can be used to add a record to a table.

Syntax Top

INSERT INTO <table_name> [(<insert_field_list>)] VALUES (<value_list>)

The values in the <value_list> (separated by commas) are added to the corresponding fields listed in <insert_field_list> (specified by commas) at the end of the table named <table_name> to form a new record. It should be noted that:

Examples Top

Assume that the table s6a has been created (click here to see the statement which creates this table), and the following records already exist in this table:

class_num stud_id name email clc ue phy pm cs
1 92114 Chan Wai Man, Raymond s92114@sample.edu.hk 64 55 70 64 59
4 91131 Hung Wai Ming s91131@sample.edu.hk 52 48 55 39 59
 
This SQL statement adds a record at the end of the table. Here <insert_field_list> is not specified, so all the fields in the new record must be filled in. The order of the values in <value_list> must be in the same order as the field names in the table.
INSERT INTO s6a
  VALUES (3, '94302', 'Fung Ching Man, Mandy', 'mandyfung@example.com', 72, 50, 42, 59, 60)
After execution, the table becomes:
class_num stud_id name email clc ue phy pm cs
1 92114 Chan Wai Man, Raymond s92114@sample.edu.hk 64 55 70 64 59
4 91131 Hung Wai Ming s91131@sample.edu.hk 52 48 55 39 59
3 94302 Fung Ching Man, Mandy mandyfung@example.com 72 50 42 59 60
 
After adding a record using the previous INSERT statement, the execution of the following statement adds one more record at the end of the table. This time, values are inserted only in the fields specified in <insert_field_list>. The values of the other fields are null. The order of the fields in <insert_field_list> need not be the same as the order of the fields in the table, but the order of the values in <value_list> must be the same as the order of the fields in <insert_field_list>.
INSERT INTO s6a (clc, class_num, cs, name, stud_id)
  VALUES (54, 12, 61, 'Poon Man Yin', '93201')
After execution, the table becomes:
class_num stud_id name email clc ue phy pm cs
1 92114 Chan Wai Man, Raymond s92114@sample.edu.hk 64 55 70 64 59
4 91131 Hung Wai Ming s91131@sample.edu.hk 52 48 55 39 59
3 94302 Fung Ching Man, Mandy mandyfung@example.com 72 50 42 59 60
12 93201 Poon Man Yin   54       61

According to the syntax of the INSERT statement, the above statement cannot be written as
INSERT INTO s6a VALUES (12, '93201', 'Poon Man Yin', , 54, , , , 61)

Previous Index Next