|
INSERT INTO <table>(<field1, field2,...>)
VALUES (<value1, value2,...>)
|
Data can be inserted into a table one row at a time or in groups. To
insert a single row, we can use the INSERT statement.
Table1: Customers
| CustID |
Name |
Address |
City |
Country |
PostCode |
Phone |
Fax |
| CUST001 |
Maria Anders |
Obere Str. 57 |
Berlin |
Germany |
12209 |
030-0074321 |
030-0076545 |
| CUST002 |
Ernst Handel |
Kirchgasse 6 |
Graz |
Austria |
8010 |
7675-3425 |
7675-3426 |
| CUST003 |
Helen Bennett |
Garden House Crowther Way |
Cowes |
UK |
PO31 7PJ |
(198) 555-8888 |
(198) 555-8889 |
| CUST004 |
Paula Wilson |
2817 Milton Dr. |
Albuquerque |
USA |
87110 |
(505) 555-5939 |
(505) 555-5940 |
| CUST005 |
Liu Wong |
55 Grizzly Peak Rd. |
Butte |
USA |
59801 |
(406) 555-5834 |
(406) 555-8083 |
The following INSERT statement will insert a new customer record to
the Customers table as shown in Table2.
INSERT INTO
Customers (CustID, Name, Address, City, Country,
PostalCode, Telephone, Fax) VALUES ('CUST006',
'Melissa', 54 Grizzly Peak Rd.', 'Butte', 'USA',
'59801', '(406)555-6483','(406)555-6484')
Table2: Customers
| CustID |
Name |
Address |
City |
Country |
Postcode |
Phone |
Fax |
| CUST001 |
Maria Anders |
Obere Str. 57 |
Berlin |
Germany |
12209 |
030-0074321 |
030-0076545 |
| CUST002 |
Ernst Handel |
Kirchgasse 6 |
Graz |
Austria |
8010 |
7675-3425 |
7675-3426 |
| CUST003 |
Helen Bennett |
Garden House Crowther Way |
Cowes |
UK |
PO31 7PJ |
(198) 555-8888 |
(198) 555-8889 |
| CUST004 |
Paula Wilson |
2817 Milton Dr. |
Albuquerque |
USA |
87110 |
(505) 555-5939 |
(505) 555-5940 |
| CUST005 |
Liu Wong |
55 Grizzly Peak Rd. |
Butte |
USA |
59801 |
(406) 555-5834 |
(406) 555-8083 |
| CUST006 |
Melissa |
54 Grizzly Peak Rd. |
Butte |
USA |
59801 |
(406) 555-6483 |
(406) 555-6484 |
Please take note that the
position
of the field names and the values are important.
We can also copy rows from other one table to another. For example,
if we want to fill the Customers table above with data from another table
called Customers2, we can use the following statement.
INSERT
INTO Customers VALUES (SELECT
CustID, Name, Address, City, Country,
PostalCode, Telephone, Fax FROM
Customers2)