The purpose of the insert statement is to add one or more new rows in a table.
How to insert a new row in a table?
In order to insert a new row in a table, you have to :
- write which table you want to insert the row in.
- write the values that each field belonging to this row should take.
In order to insert a new customer whose name is Arjun Sen in the customers table, you would use the following statement:
The following diagrams illustrates how the insert statement works.
Why some values are between quotes and other not?
It is the same rule as for the update statement:
- when the value that the column should take contains text, then you put this value inside quote.
- when the value that the column should take contains numbers, then you do not put this value inside quote.
In the example, how do you know which values correspond to which column?:
In the example, we wrote the values that columns should take without indicating to which column they correspond. This is possible because columns in a table have an order.
In your insert statement, you write the value that column number 1 should take (first name), followed by the value that column number 2 should take (last name), and so on until you reach the last column.
How can you tell the order of columns in a table ?
When showing all columns in a table using a select statement, you will see these columns appear from left to right always in the same order. Column number 1 is on the first from the left, column number two the second from the left,…