The insert clause has one function; to insert data into a table. Insert populates each table column with a value. Rows are inserted one right after another into the coresponding column.
A D V E R T I S E M E N T
SQL Code:
INSERT INTO employees (Lastname,Firstname,Title)
VALUES(Johnson,David,crew);
Output
Lastname
Firstname
Title
Johnson
David
crew
SQL - Insert defaults and nulls
We mentioned setting up default or null values for table columns. Simply placing the word default or null, in place of a value is the solution.
SQL Code:
INSERT INTO employees (Lastname,Firstname,Title)
VALUES('Hively','Jessica',DEFAULT);
or
INSERT INTO employees (Lastname,Firstname,Title)
VALUES('Hively','Jessica',NULL);
SQL - Inserting multilpe values
Here's an example of how to insert more than one record at a time. Many web developers will use the single example above along with HTML forms to continually insert and update their SQL tables.
SQL Code:
INSERT INTO employees VALUES
(DEFAULT,'Hicks','Freddy','crew'),
(DEFAULT,'Harris','Joel','crew'),
(DEFAULT,'Davis','Julie','manager');
SQL - Insert into multiple tables
This concept isn't widely supported by open source database programs, however they do offer alternative methods to achieve the same goal. The idea is to insert similar record values into 2 or more tables with one statement. Using the example from above, we want to place Julie's information into our manager table as well as the general employee table.
SQL Code:
INSERT ALL INTO employees (Lastname,Firstname,Title)
VALUES('Davis','Julie','manager')
INTO manager (training,salary)
VALUES ('yadayada','22500');