w3resource

SQL INSERT INTO STATEMENT

INSERT INTO STATEMENT

The SQL INSERT statement is used to insert a single record or multiple records into a table.

While inserting a row, if the columns are not specified, it means that vales are added for all of the columns of the table resulting addition of a single row. If it is required to insert values for one or more specific column(s), then it is necessary to specify the name(s) of the column(s) in the SQL query.

The SQL SELECT statement can also be used to insert the rows of one table into another identical table.

While inserting the values, it is needed to enclose the values with single quotes for character or date values.

Syntax:

INSERT INTO < table name > (col1,col2,col3...col n)
VALUES (value1,value2,value3…value n);

Parameters:

Name Description
table_name Name of the table where data will be inserted.
col1,col2,col3,con n Column of the table.
value1,value2,value3,value n Values against each column.

You can use another syntax to insert data. Here, you don't specify column names of the associated table. So, value1 is inserted into the first column of a table, value2 into column2 and so on.

Syntax:

INSERT INTO < table name >
VALUES (value1,value2,value3…value n);

Parameters:

Name Description
table_name Name of the table where data will be inserted.
value1,value2,value3,value n Values against each column.

Syntax diagram - INSERT INTO STATEMENT

Syntax diagram - INSERT INTO STATEMENT

Some points to be noted for the SQL INSERT INTO statement:

  • Table Name: Specify the name of the table into which you want to insert data.

  • Column Names: Optionally, specify the names of the columns into which you want to insert data. If you omit this, values must be provided for all columns in the order they appear in the table.

  • Values: Provide the values you want to insert into the specified columns. Each set of values should correspond to the columns specified, either explicitly or implicitly.

  • Data Type Compatibility: Ensure that the data types of the values being inserted match the data types of the columns in the table. Otherwise, you may encounter errors or unexpected behavior.

  • NULL Values: If a column allows NULL values and you don't provide a value for it, it will default to NULL. Ensure that NULL values are acceptable for the columns you're inserting into.

  • Constraints: Check for any constraints defined on the table, such as NOT NULL constraints, unique constraints, and foreign key constraints. Ensure that the data you're inserting complies with these constraints.

  • Default Values: If a column has a default value defined, you can omit inserting a value for that column, and it will default to the specified default value.

  • Batch Inserts: Consider using a single INSERT INTO statement with multiple sets of values for batch inserts. This can improve performance by reducing the number of round-trips to the database.

  • Transaction Management: Wrap your INSERT INTO statements in a transaction, especially when inserting data into multiple tables. This ensures data consistency and allows you to rollback changes if an error occurs.

  • Testing: Thoroughly test your INSERT INTO statements with different sets of data to ensure they work as expected and don't violate any business rules or constraints.

  • Example:

    Sample table: agents

     
    +------------+----------------------+--------------------+------------+-----------------+---------+
    | AGENT_CODE | AGENT_NAME           | WORKING_AREA       | COMMISSION | PHONE_NO        | COUNTRY |
    +------------+----------------------+--------------------+------------+-----------------+---------+
    | A007       | Ramasundar           | Bangalore          |       0.15 | 077-25814763    |         |
    | A003       | Alex                 | London             |       0.13 | 075-12458969    |         |
    | A008       | Alford               | New York           |       0.12 | 044-25874365    |         |
    | A011       | Ravi Kumar           | Bangalore          |       0.15 | 077-45625874    |         |
    | A010       | Santakumar           | Chennai            |       0.14 | 007-22388644    |         |
    | A012       | Lucida               | San Jose           |       0.12 | 044-52981425    |         |
    | A005       | Anderson             | Brisban            |       0.13 | 045-21447739    |         |
    | A001       | Subbarao             | Bangalore          |       0.14 | 077-12346674    |         |
    | A002       | Mukesh               | Mumbai             |       0.11 | 029-12358964    |         |
    | A006       | McDen                | London             |       0.15 | 078-22255588    |         |
    | A004       | Ivan                 | Torento            |       0.15 | 008-22544166    |         |
    | A009       | Benjamin             | Hampshair          |       0.11 | 008-22536178    |         |
    +------------+----------------------+--------------------+------------+-----------------+---------+
    

    To add values'A001','Jodi','London','.12','075-1248798' for a single row into the table 'agents', the following SQL statement can be used :

    INSERT INTO agents
    VALUES ("A001","Jodi","London",.12,"075-1248798");
    

    Visual Presentation:

    SQL INSERT INTO STATEMENT

    See our Model Database

    SQL INSERT INTO Statement can be used in various forms. In the consequent pages, we have discussed those with the example, explanation, and pictorial presentations. Have a go through -

    Insert null

    You can use INSERT INTO statement to insert NULL Values.

    Inserting the result of a query in another table

    What if you want to insert data into a table while collecting data from another SQL query?

    Insert using subqueries

    You can use SUBQUERIES to INSERT data.

    Insert using nested subqueries with any operator

    You can even use NESTED SUBQUERIES with ANY OPERATOR to insert data.

    Here is a new document which is a collection of questions with short and simple answers, useful for learning SQL as well as for interviews.

    Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.

    Previous: SELECT with DISTINCT Multiple Columns
    Next: Insert null

    

    Become a Patron!

    Follow us on Facebook and Twitter for latest update.

    It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

    https://w3resource.com/sql/insert-statement/sql-insert-command.php