Explain with example insert sql command

Explain with example insert sql command – Introduction, Why do we use SQL Insert command?, Where is SQL Insert command is required?, When do we use SQL Insert command?, How to use SQL Insert command?, SQL Query and Query function in Googlesheets:, Example of SQL Insert function:, Other related articles, How to “Undo” the “Insert Into” SQL statement.

Introduction

SQL is being used by data professionals, tech professionals and anyone who is in need to fetch data from the database.

The “INSERT” SQL command is a fundamental operation used to add new rows or records into a table within a relational database. It allows users to insert data either into specific columns of a table or for all columns, depending on the requirements. The “INSERT” command is a crucial aspect of data manipulation as it facilitates the addition of fresh information to an existing database.

Why do we use SQL Insert command?

Here we are talking about the insert command that is used to insert data in an existing table and so it has several uses like if you are doing sales and have no way to add data automatically in your database then you can use this command to insert data manually in your database.

Where is SQL Insert command is required?

With digital revolution and onset on internet age every industry and infact every online business is generating a lot of data. So, i is being used in almost all the industries.

When do we use SQL Insert command?

We use it when we are in need to insert the data in a database table. Also, note that while doing so make sure that you are adding data in the same format as the table, otherwise you will start seeing errors and so doing it precisely is very important.

How to use SQL Insert command?

The basic command or syntax to insert the data in a table is:

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

Note: Here it is important that values that you are inserting are of same format as columns for e.g. if column1 is of integer type then value1 should be integer as well.

So, once the syntax and values are correct you can run the command and data will be inserted in your table.

SQL Query and Query function in Googlesheets:

SQL query is so popular and so important that googlesheet has invented query function in googlesheeets as well.

Check our article Google sheets QUERY फ़ंक्शन का उपयोग कैसे करें to understand how googlesheet query function works.

Example of SQL Insert function:

Let’s illustrate how to use the “INSERT” command with a practical example. Consider a table named “students” with the following structure:

students table:
| student_id | name     | age | number |
|------------|----------|-----|--------|
| 1          | Nick     | 15  |   97   |
| 2          | Jason    | 21  |   86   |
| 3          | Miky     | 18  |   69   |

Now, suppose we want to add a new student, Peter, to the table with the following details:

  • student_id: 4
  • name: Peter
  • age: 17
  • Number: 81

To achieve this, we will utilize the “INSERT” command as shown below:

INSERT INTO students (student_id, name, age, number)
VALUES (4, 'Peter', 17, 81);

Upon executing the above SQL command, the “students” table will be updated as follows:

students table:
| student_id | name     | age | number |
|------------|----------|-----|--------|
| 1          | Nick     | 15  | 97     |
| 2          | Jason    | 21  | 86     |
| 3          | Mikey    | 18  | 69     |
| 4          | Peter    | 17  | 81     |

As evident from the result, the “INSERT” command successfully added a new row to the “students” table with the data for the new student, Peter.

It is crucial to understand that the “INSERT” command can be used to insert data for multiple rows in a single command if required. Additionally, providing column names when using the “INSERT” command is recommended for clarity and to avoid potential errors in the data insertion process.

How to “Undo” the “Insert Into” SQL statement

If you want to undo the insert into sql statement i.e. you want to delete the data you inserted then there are several ways i.e. you can use delete command, drop command, rollback command etc.

These delete, drop commands actually helps in dropping the rows that you want to undo.

Some articles are in hindi and some are in english, let us know if want us some information in any relevant language.

1 thought on “Explain with example insert sql command”

Leave a Comment