SQL Insert into Query - How to Insert Into a Table Query



Demo wikitechydatabase

  • Below is a selection from the wikitechytable table used in the examples:
sql-insert-into-1

Insert a row by specifying the column name

  • To insert data into a Microsoft SQL Server (MSSQL) database table, you can use the INSERT INTO statement. Here's the basic syntax for inserting data into a table:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
  • table_name: The name of the table you want to insert data into.
  • (column1, column2, column3, ...): A list of column names in the table where you want to insert data. This part is optional if you're inserting data into all columns, in which case you can omit it.
  • VALUES (value1, value2, value3, ...): A list of values to insert into the specified columns. The number and order of values should match the number and order of columns.
  • Here's an example of inserting data into a hypothetical "wikitechytable" table with columns for "name," "course," and "fees":
INSERT INTO wikitechytable (name, course, fees)
VALUES ('Venkat', 'Flutter', 100);
  • In this example, we're inserting a new row into the "wikitechytable" table with the values ‘Venkat’ for "name" 'Flutter' for "course" and 100 for "fees"
sql-insert-into-2

Insert Multiple Rows

  • You can also insert data into multiple rows in a single INSERT INTO statement by specifying multiple sets of values, like this:
INSERT INTO wikitechytable (name, course, fees)
VALUES ('Wikitehcy', 'SQL', 200),
       ('Sharuk', 'Angular', 150),
       ('Kaashiv', 'Dotnet', 50);
sql-insert-into-3
  • This inserts three rows of data into the "wikitechytable" table in one go. Please note the following:
    • Make sure you have the necessary permissions to insert data into the table.
    • Data types and constraints must be satisfied when inserting data.
    • Be cautious when inserting data, especially in a production database, as incorrect or duplicate data can cause issues. Always validate and sanitize user input to prevent SQL injection attacks.

Insert a row without specifying a column name

  • If you want to insert data into all columns of a table in Microsoft SQL Server (MSSQL) without specifying the column names, you can use the following syntax:
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
  • table_name: The name of the table you want to insert data into.
  • VALUES (value1, value2, value3, ...): A list of values to insert into all columns of the specified table. The order of values should match the order of columns in the table.
  • Here's an example of inserting data into a hypothetical "wikitechytable" table without specifying column names:
INSERT INTO wikitechytable
VALUES ('Praveen', 'PHP', 250);
  • In this example, the values are inserted into the "Employees" table in the order they appear in the table's schema.
  • However, it's important to use caution when inserting data without specifying column names, especially if the table's schema changes in the future.
  • If column names or the order of columns change, your INSERT statement may no longer work correctly.
  • It's generally considered best practice to specify column names explicitly in your INSERT statements whenever possible to ensure data integrity and maintainability.
sql-insert-into-3

Related Searches to SQL Insert into Query - How to Insert Into a Table Query