Back
Next
Here in SQL Tutorial you will learn that how do these rows of data
get into these tables in the first place? This is what
this section, covering the INSERT statement, and next section,
covering table Select statement, how to select the stored information from
database table.
In SQL, there are essentially basically two ways
to INSERT data into a table: One is to insert it one
row at a time, the other is to insert multiple rows at a
time. Let's first look at how we may INSERT data one row at a time:
Syntax for SQL Insert Statement
INSERT INTO "table_name" ("column1", "column2", ...)
VALUES ("value1", "value2", ...)
| Assuming that we have a table that has the following
structure, Table Employees
| Column Name |
Data Type |
| first_name |
char(50) |
| last_name |
char(50) |
| phone |
integer |
| DBO |
datetime |
and now we wish to insert one additional row into the table
representing the information of a new employee with his first
name, last name phone number and date of birth. We will hence use the
following SQL script: for insert into table. |
|
INSERT INTO Employees (first_name, last_name, phone, DBO)
VALUES ('Austin', 'Hennery', 4461022222,'24/11/1978')
The second type of INSERT INTO statement allows us to insert multiple rows into a table.
Unlike the previous example, where we insert a single row by specifying its
values for all columns, we now use a SELECT statement to specify the data
that we want to insert into the table. We will discus the Select
statement in next lesson with details. If you are thinking whether this
means that you are using information from another table, you are correct.
The syntax is as follows:
INSERT INTO "table1" ("column1", "column2", ...)
SELECT "column3", "column4", ...
FROM "table2"
Note that this is the simplest form. The entire statement
can easily contain WHERE, GROUP BY, and HAVING clauses, as
well as table joins and aliases.
So for example, if we wish to have a table,
Employees, that contains the information of employees,
and you already know that the source data resides in the Old_Employees
table, we'll type in:
INSERT INTO Employees (first_name, last_name, phone, DBO)
SELECT first_name, last_name, phone, DBO
FROM Old_Employees
WHERE Gender ='f'
Here we have used the SQL Server syntax to extract the
information from the Old_Employees and the condition is used here that only
female employees records will be inserted into the Employees table. Hope
this SQL Tutorial section is enough to build a ground for SQL Insert Statement
for the beginners of SQL Developers.
Back
Next |