AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
SQL INSERT INTO |
|
SQL > SQL Commands >
Insert Into Statement
The INSERT INTO statement is used to add new records into a database table. The SQL INSERT INTO statement adds new rows to a table. You can specify which columns to populate — columns left out receive NULL or their default value. Always list column names explicitly to avoid inserting data into the wrong columns.
In SQL, there are basically two ways to INSERT data into a table: One is to insert it one row at a time, the other is to insert multiple rows at a time. In this section, we'll take a look at the first case. SyntaxThe syntax for inserting data into a table one row at a time is as follows:
ExamplesThe examples refer to a table that has the following structure, Table Store_Information
Example 1: All column names are specifiedWe want to insert one additional row into the table representing the sales data for Los Angeles on January 10, 1999. On that day, this store had $900 in sales, and the Manager_ID for this store is 10. We will use the following SQL script:
Now the table will hold the following data: Table Store_Information
Please note that we can specify the column names in any order -- the order does not have to be the same as that of the table. For example, the following SQL statement is equivalent to the SQL statement above:
Example 2: None of the column names are specifiedIf we leave off the column names in the INSERT INTO statement, we will need to make sure that data is inserted in the same column order as that in the table. For example,
will give us the desired result, while
will result in Store_Name being set to 900, Manager_ID being set to 'Los Angeles', and Sales being set to 10. Clearly this is not what we intend to accomplish. Example 3: Some of the column names are specifiedIn the first two examples, we insert a value for every column in the table. Sometimes, we may decide to insert value into some of the columns and leave the rest of the columns blank. For those cases, we simply specify the column names that we want to insert values into in our SQL statement. Below is an example:
Now the table becomes: Table Store_Information
In this case, the value for the Manager_ID column in the second row is NULL. NULL means that data does not exist, and we discuss the concept of NULL later in this tutorial. Frequently Asked QuestionsWhat is the difference between INSERT and UPSERT in SQL?INSERT only adds a new row. If a row with the same primary key already exists, a plain INSERT will fail (or duplicate the row if there's no unique constraint). UPSERT (INSERT ... ON CONFLICT / MERGE / INSERT OR REPLACE) inserts a new row or updates it if a conflict is detected — useful for avoiding duplicate key errors. Can I use INSERT INTO to copy data from another table?Yes. Use INSERT INTO ... SELECT: What happens if I INSERT a value that violates a constraint?The INSERT fails and an error is returned. Common constraint violations include NOT NULL (trying to insert NULL into a non-nullable column), UNIQUE or PRIMARY KEY (inserting a duplicate value), and FOREIGN KEY (referencing a parent row that doesn't exist). How do I INSERT into a table with an auto-increment primary key?Simply omit the auto-increment column from your INSERT statement and column list. The database will generate the next value automatically. For example: Exercises1. Let's assume we start with the Store_Information table shown above. What does the table look like after the following SQL statement is executed?
2. Continuing with Question 1. What does the table look like after the following SQL statement is executed?
3. Using the same table as above. What does the table look like after the following SQL statement is executed?
|
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.