Tutorial by Examples

INSERT INTO `table_name` (`field_one`, `field_two`) VALUES ('value_one', 'value_two'); In this trivial example, table_name is where the data are to be added, field_one and field_two are fields to set data against, and value_one and value_two are the data to do against field_one and field_two resp...
INSERT INTO `table_name` (`index_field`, `other_field_1`, `other_field_2`) VALUES ('index_value', 'insert_value', 'other_value') ON DUPLICATE KEY UPDATE `other_field_1` = 'update_value', `other_field_2` = VALUES(`other_field_2`); This will INSERT into ta...
INSERT INTO `my_table` (`field_1`, `field_2`) VALUES ('data_1', 'data_2'), ('data_1', 'data_3'), ('data_4', 'data_5'); This is an easy way to add several rows at once with one INSERT statement. This kind of 'batch' insert is much faster than inserting rows one by one. Typically...
This is the basic way to insert data from another table with the SELECT statement. INSERT INTO `tableA` (`field_one`, `field_two`) SELECT `tableB`.`field_one`, `tableB`.`field_two` FROM `tableB` WHERE `tableB`.clmn <> 'someValue' ORDER BY `tableB`.`sorting_clmn`; You can...
When a table has an AUTO_INCREMENT PRIMARY KEY, normally one does not insert into that column. Instead, specify all the other columns, then ask what the new id was. CREATE TABLE t ( id SMALLINT UNSIGNED AUTO_INCREMENT NOT NULL, this ..., that ..., PRIMARY KEY(id) ); INSERT I...
Several 'insert' functions can "burn" ids. Here is an example, using InnoDB (other Engines may work differently): CREATE TABLE Burn ( id SMALLINT UNSIGNED AUTO_INCREMENT NOT NULL, name VARCHAR(99) NOT NULL, PRIMARY KEY(id), UNIQUE(name) ) ENGINE=InnoDB; IN...

Page 1 of 1