Difference between revisions of "INSERT"

From braindump
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 3: Line 3:
{{#set:Related SQL command=UPDATE
{{#set:Related SQL command=UPDATE
|Related SQL command=DELETE
|Related SQL command=DELETE
|Related SQL command=INSERT
|Related SQL command=SELECT
|Related SQL command=FROM
|Related SQL command=GROUP BY
|Related SQL command=ORDER BY
}}
}}


Line 16: Line 13:
== Syntax ==
== Syntax ==
<pre>
<pre>
INSERT INTO <table> [ ( <column> [, <column> [, <column>] [, ... ]]] ) ]
SELECT
* | <column> [, <column> [, ...]]
VALUES ( <value> [, <value> [, <value>, [, ...]]] )
FROM <table> [, <table> [, ...]]
[ WHERE <condition> [ AND | OR <condition> [, ...] ]
[ GROUP BY <column> [, <column> [, ...] ]]
[ ORDER BY <column> [ ASC | DESC ] [, <column> [ ASC | DESC ] [, ...]]]]
</pre>
</pre>


== Examples ==
== Examples ==
For example to insert the value "foo" into a column called "bar" into a table with name "baz" do the following:
To select all the rows from table "baz" we use the star expression "*". This will select all the columns in the table and list them up.
<pre>
<pre>
INSERT INTO baz ( bar ) VALUES( "foo" )
SELECT * FROM baz
</pre>
</pre>
Note: The foo is encased in double quotes this is required for values other than numeric values.


Using the same example as above but use a numeric value of 400 we will not use the qouting.
To only select rows with value "foo" in column "bar" from table "baz" do the following:
<pre>
<pre>
SELECT bar FROM baz WHERE bar = "foo"
INSERT INTO baz ( bar ) VALUES( 400 )
</pre>
</pre>


== Excersises ==
To only select rows with value "foo" or value "foofoo" in column "bar" from table "baz" do the following:
# Use the [[sample database]] and insert and populate it with the data from the [[File::/etc/passwd]].
<pre>
SELECT bar FROM baz WHERE bar = "foo" OR bar = "foofoo"
</pre>


== Additional Information ==

* [http://en.wikipedia.org/wiki/Insert_%28SQL%29 Wikipedia on SQL INSERT]
== Excersises ==
* [http://www.w3schools.com/SQL/sql_insert.asp W3Schools on SQL INSERT]
# Use the [[sample database]] and select various values with changing conditions [[File::/etc/passwd]].
* [http://dev.mysql.com/doc/refman/5.0/en/insert.html MySQL 5.0 INSERT documentation]
* [http://www.postgresql.org/docs/9.0/static/sql-insert.html PostgreSQL 9.0 INSERT documentation]
* [http://www.sqlite.org/lang_insert.html SQLite INSERT documentation]

Latest revision as of 02:49, 19 January 2011

{{#set:Related SQL command=UPDATE |Related SQL command=DELETE |Related SQL command=SELECT }}

INSERT is refers to the SQL command INSERT.

Summary

The INSERT command is used to add or insert data into a table. Syntax may vary between certain types of databases but is generally uniform across multiple vendors.

Syntax

 INSERT INTO <table> [ ( <column> [, <column> [, <column>] [, ... ]]] ) ]
   VALUES ( <value> [, <value> [, <value>, [, ...]]] )

Examples

For example to insert the value "foo" into a column called "bar" into a table with name "baz" do the following:

  INSERT INTO baz ( bar ) VALUES( "foo" )

Note: The foo is encased in double quotes this is required for values other than numeric values.

Using the same example as above but use a numeric value of 400 we will not use the qouting.

  INSERT INTO baz ( bar ) VALUES( 400 )

Excersises

  1. Use the sample database and insert and populate it with the data from the [[File::/etc/passwd]].

Additional Information