Database Reference
In-Depth Information
▼
Input
INSERT INTO customers(cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email)
VALUES('Pep E. LaPew',
'100 Main Street',
'Los Angeles',
'CA',
'90046',
'USA',
NULL,
NULL);
▼
Analysis
This example does the exact same thing as the previous
INSERT
statement, but
this time the column names are explicitly stated in parentheses after the table
name. When the row is inserted MariaDB matches each item in the columns
list with the appropriate value in the
VALUES
list. The first entry in
VALUES
corresponds to the first specified column name. The second value corresponds
to the second column name, and so on.
Because column names are provided, the
VALUES
must match the specified col-
umn names in the order in which they are specified, and not necessarily in the
order that the columns appear in the actual table. The advantage of this is that,
even if the table layout changes, the
INSERT
statement will still work correctly.
You'll also notice that the
NULL
for
cust_id
was not needed; the
cust_id
column was not listed in the column list and so no value was needed.
The following
INSERT
statement populates all the row columns (just as before),
but it does so in a different order. Because the column names are specified, the
insertion works correctly:
▼
Input
INSERT INTO customers(cust_name,
cust_contact,
cust_email,
cust_address,
cust_city,
cust_state,
cust_zip,