SQL INSERT INTO |
SQL Sample Code - SQL INSERT INTO. |
The statement below inserts values into a specified table without indicating which column it is going into.
| INSERT INTO table_name VALUES (value1, value2, value3, value4…); |
or
| INSERT INTO table_name (column1, column2,...) VALUES (value1, value2...); |
See the SQL samples section below for an explanation of the INSERT INTO statement.
What does the SQL INSERT INTO statement do? |
The SQL INSERT INTO statement is used to insert values into a table in a database.
Either by specifying the Columns the relative values are to be put into or not.
Sample 1 – SQL INSERT INTO |
In this SQL samples we will use the database table ‘tblCompany’.
| Company_ID | CompanyName | Address | Town |
| 1 | SQL Sample | 1 Sample St | Hamburg |
| 2 | SQL Code Land | 2 Code Rd | Hamburg |
| 3 | Sample Code World | 66 SQL St | Curry |
| 4 | SQL Reference Ltd | 34 Reference St | Pisa |
We want to insert another row in the table above.
Use the SELECT statement below:
| INSERT INTO tblCompany VALUES (5,'MySQL Code Geeks', '5 Sample Ave', 'Las Vegas'); |
The table of results will look like this:
| Company_ID | CompanyName | Address | Town |
| 1 | SQL Sample | 1 Sample St | Hamburg |
| 2 | SQL Code Land | 2 Code Rd | Hamburg |
| 3 | Sample Code World | 66 SQL St | Curry |
| 4 | SQL Reference Ltd | 34 Reference St | Pisa |
| 5 | MySQL Code Geeks | 5 Sample Ave | Las Vegas |
Sample 2 – SQL INSERT INTO specified columns. |
In this SQL sample we will use the database table ‘tblCompany’.
| Company_ID | CompanyName | Address | Town |
| 1 | SQL Sample | 1 Sample St | Hamburg |
| 2 | SQL Code Land | 2 Code Rd | Hamburg |
| 3 | Sample Code World | 66 SQL St | Curry |
| 4 | SQL Reference Ltd | 34 Reference St | Pisa |
We want to insert data in another row but only for specified columns in the table above.
Use the SELECT statement below:
| INSERT INTO tblCompany (Company_ID, CompanyName, Town VALUES (5,'MySQL Code Geeks','Las Vegas'); |
The table of results will look like this:
| Company_ID | CompanyName | Address | Town |
| 1 | SQL Sample | 1 Sample St | Hamburg |
| 2 | SQL Code Land | 2 Code Rd | Hamburg |
| 3 | Sample Code World | 66 SQL St | Curry |
| 4 | SQL Reference Ltd | 34 Reference St | Pisa |
| 5 | MySQL Code Geeks | Las Vegas |
Related SQL Sample Code:
|
|