SQL INSERT VS UPDATE Statement

The key distinction between INSERT and UPDATE in SQL is that INSERT adds new records to the table while UPDATE modifies existing records.

RDBMS stands for Relational Database Management System, and it is a programme that stores and manages relational databases.

Tables are used to hold data in a relational database.

These tables are connected in some way. The Structured Query Language (SQL) is a programming language that may be used to execute various operations on data contained in a relational database management system.


SQL INSERT Statement

INSERT is a DML command that inserts one or more rows into a table in a relational database management system.

Insert is a SQL command that can be used to add a new row to an existing table.

DML statements are commands that can be used to maintain data without modifying the database schema.

If the user is adding values to all of the table's columns, the column names do not need to be mentioned in the SQL query. He should, however, ensure that the values are in the same order as the columns in the table.

Example: The below statements would build 6 records within the Book table.

ID Name Price Year Domain
1 Pro Oracle Administration 125 2017 Programming
2 SQL Made Simple... By Examples 150 2020 Security
3 Teach Yourself SQL in 10 Minutes 95 2017 Optimization
4 MySQL Cookbook 123.45 2017 Performance

Here, you have the option to create a record in the Book table by using the second syntax as shown under.

· INSERT INTO Book VALUES (5, ‘Java programming’, 120, 2014,'Database');

Output: All the above statements would present the below records in the Book table is presented under.

ID Name Price Year Domain
1 Pro Oracle Administration 125 2017 Programming
2 SQL Made Simple... By Examples 150 2020 Security
3 Teach Yourself SQL in 10 Minutes 95 2017 Optimization
4 MySQL Cookbook 123.45 2017 Performance
5 Java Programming 120 2013 Database

SQL UPDATE Statement

The UPDATE command can be used to make changes to existing records in a table.

We can also use it in conjunction with the WHERE clause to change a single record. A DML statement is another name for an UPDATE.

Example: Let’s understand the Book table having the following records.

ID Name Price
1 Easy Oracle PL/SQL Programming 205
2 Microsoft SQL Server 2008 155
3 The Gurus Guide To SQL Server 200
4 Oracle Database 11G New Features 136.33

The below mentioned query will update the Price for a customer whose ID number is 3 in the table.

UPDATE BookSimple SET Price = 250 WHERE ID = 3;

Output: The output of modified table is

ID Name Price
1 Easy Oracle PL/SQL Programming 205
2 Microsoft SQL Server 2008 155
3 The Gurus Guide To SQL Server 250
4 Oracle Database 11G New Features 136.33

Book should be substituted with the name of the table where the user wants to edit the records in the example above.

Under the SET clause, the names of the columns in the database where the values of the record that has to be altered are ID,Name,Price .

The new values that should be placed into the record are referred to as inserted values.

Furthermore, the WHERE clause specifies the set of records in the table that must be modified.

Furthermore, the WHERE clause in the UPDATE statement could be eliminated.

As a result, the values specified in the SET clause will be applied to all of the table's records. For more skills and approaches, take an online SQL server dba course.


SQL DIFFERENCE BETWEEN INSERT And UPDATE STATEMENT

There are 2 main distinguish in insert vs update discussed in below table:-

INSERT UPDATE
A DML command for inserting one or more rows into a table in a relational database management system. UPDATE is a DML command that modifies or updates values in an RDBMS table.
Assists in the addition of new values to the table. UPDATE is a command that allows you to change the values of a table.