SQL VALUES Keyword
The VALUES
keyword in SQL is used within the INSERT INTO
statement to specify the data values for the new row being added to a table. It's a fundamental part of adding new information to your database.
VALUES: Definition and Usage
The VALUES
clause comes after the column list in an INSERT INTO
statement. You provide a list of values, and these values are inserted into the corresponding columns in the new row. The number of values you supply must match the number of columns specified (or all columns if you omit the column list).
Syntax
Syntax 1: Specifying Columns
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Syntax 2: Inserting into All Columns
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
In the second syntax, you don't list the column names, but the values must be in the exact same order as the columns are defined in the table.
Examples
Inserting a New Row (Specifying Columns)
This example adds a new customer to the 'Customers' table, providing values for all columns. (This assumes a 'Customers' table exists with the specified columns and an auto-incrementing `CustomerID`.)
Syntax
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
Output
(A new row is added to the Customers table. The CustomerID will be automatically generated.)
Inserting a New Row (Omitting Some Columns)
This example inserts a new customer, but only provides values for 'CustomerName', 'City', and 'Country'. Other columns will either use default values or be set to NULL
. (This assumes a 'Customers' table exists with the specified columns and an auto-incrementing `CustomerID`.)
Syntax
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Another Company', 'New City', 'New Country');
Output
(A new row is added to the Customers table with NULL values for unspecified columns. A new CustomerID is automatically generated.)