Database: SQL: Inserting Rows

There are many ways to insert rows into a table, either directly, or as a result of another query.

The following table is used in the examples below.
CREATE TABLE User {
    user_id    INT AUTOINCREMENT,
    user_email VARCHAR(255),
    user_pw    VARCHAR(15),
};

Inserting Rows

INSERT INTO table (column,...) VALUES (constant,...)
Inserts a row into table, where each column name is matched to the corresponding value. Specification of the column names is not required (see below), but it is generally better to do so to prevent errors if the column order is later changed. It also allows specification of only some columns so that autoincrement fields or columns that should have a NULL do not need to be specified.
INSERT INTO User (user_email, user_pw) VALUES ("bill@bill.com", "goody");
Note that this example omits the user_id field because it is an autoincrement field that is automatically updated to the next value when a row is entered with a NULL in that column.
INSERT INTO table VALUES (constant,...)
Inserts a row into table, where each value is put into the next sequential column. Altho this is commonly used, it is more robust to specify the column names as above.
INSERT INTO User VALUES (NULL, "bill@bill.com", "goody");
INSERT INTO table SET column=constant,...;
Inserts a row into table, where the value of columns is specified (not all colums need to be specified).
INSERT INTO User SET user_email="bill@bill.com", user_pw="goody";
INSERT INTO table [(column,...)] query
Inserts the rows which result from the query into table, matching the columns of the query result with columns in the table, either in order or by name if specified.