SQL Tutorial: 9

By alexxk5111


The SQL INSERT INTO syntax has 2 main forms and the result of either of them is adding a new row into the database table.

The first syntax form of the INSERT INTO SQL clause doesn’t specify the column names where the data will be inserted, but just their values:

VALUES (value1, value2, value3…)

The second form of the SQL INSERT INTO command, specifies both the columns and the values to be inserted in them:

INSERT INTO Table1 (Column1, Column2, Column3…)
VALUES (Value1, Value2, Value3…)

As you might already have guessed, the number of the columns in the second INSERT INTO syntax form must match the number of values into the SQL statement, otherwise you will get an error.

If we want to insert a new row into our Customers table, we are going to use one of the following 2 SQL statements:

VALUES (‘Peter’, ‘Hunt’, ‘[email protected]’, ‘1/1/1974’, ‘626 888-8888’)

INSERT INTO Customers (FirstName, LastName, Email, DOB, Phone)
VALUES (‘Peter’, ‘Hunt’, ‘[email protected]’, ‘1/1/1974’, ‘626 888-8888’)

The result of the execution of either of the 2 INSERT INTO SQL statements will be a new row added to our Customers database table:

JohnSmith[email protected]2/4/1968626 222-2222
StevenGoldfish[email protected]4/4/1974323 455-4545
PaulaBrown[email protected]5/24/1978416 323-3232
JamesSmith[email protected]20/10/1980416 323-8888
PeterHunt[email protected]1/1/1974626 888-8888

If you want to enter data for just a few of the table columns, you’ll have to use the second syntax form of the SQL INSERT INTO clause, because the first form will produce an error if you haven’t supplied values for all columns.

To insert only the FirstName and LastName columns, execute the following SQL statement:

INSERT INTO Customers (FirstName, LastName)
VALUES (‘Peter’, ‘Hunt’)