SQL INSERT INTO Keyword
The INSERT INTO
statement in SQL is used to add new rows of data into a table. It's a fundamental command for populating and updating your database.
INSERT INTO: Definition and Usage
You can specify which columns to insert data into, and provide the corresponding values. If you don't specify all columns, the database will use default values (if defined) for the omitted columns or might generate values automatically (e.g., for auto-incrementing primary keys).
Syntax
Syntax
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Examples
Inserting a New Row with All Columns
This example adds a new customer record, specifying values for all columns. (This assumes a 'Customers' table with columns: CustomerName, ContactName, Address, City, PostalCode, Country).
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 with the specified data. A new CustomerID will likely be auto-generated.
(A new row is inserted into the Customers table)
Inserting a New Row with Some Columns Omitted
This inserts a new customer record, but only provides values for 'CustomerName', 'City', and 'Country'. The database will likely handle missing values using defaults or auto-incrementing IDs.
Syntax
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
Output
A new row is added to the 'Customers' table. 'ContactName' and 'Address' and 'PostalCode' would likely be assigned default values or remain NULL, and a new CustomerID is auto-generated.
(A new row is inserted into the Customers table)