|
| |
SQL INSERT Command |
INSERT command is used to append or add a record to a database. A new record will be added at the end of the table every time INSERT command is used. We have to specify what are the fields to be filled while inserting the record, we can add records even without specifying the field names but we have to maintain the order of the filed existed in the table and the the order of the data we are inserting. We have to take care of proper formatting of the data we are inserting to the table. We can't insert a string to a numeric field. So we have to format the data and apply the insert command. Here is one example of insert command.
INSERT INTO table_name(field1,field2,field3,field4) values('value1','value2','value3','value4')
Here the order of the fields need not be same as order in our MySQL table but the order we are specifying by saying the filed names, that order we must maintain for values. We have to take care of the fields where we have given NOT NULL or any other such requirements. Any violation of the field structure will generate an error message.
Note that it is always a good practice to use filed name and value pairs while inserting records. If we specify only the value by maintaining the order of the fields there will be problem in future once a new field is added or removed from the table. So always write the query specifying field names and its values.
Here are some sql commands that will create a table and then add three records to it by using insert command.
CREATE TABLE t1 (id int(11) NOT NULL default '0',name1 varchar(10) NOT NULL default '') TYPE=MyISAM;
Adding records to the table t1
INSERT INTO t1 VALUES (1, 'one1');
INSERT INTO t1 VALUES (2, 'two1');
INSERT INTO t1 VALUES (3, 'three1');
| |
|
|
|