SQL INSERT

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.

Example :

In order to insert a new customer whose name is Arjun Sen in the customers table, you would use the following statement:

insert into customers
values (
‘Arjun’,
‘Sen’,
491234545456,
[email protected]
)

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,…
 

To test your understanding of this lesson, take the SQL Insert Quiz. You can also go to the next lesson about SQL INSERT and auto increment.
 
Share on:

Something not clear? Ask your question here:

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
 

Author:
Last udpated: September 10, 2017
Created: May 20, 2014