Difference between revisions of "INSERT"

From braindump
Jump to navigation Jump to search
Line 1: Line 1:
[[Category: Module 105.3]]
[[Category: Module 105.3]]
[[Category: SQL]]
[[Category: SQL]]
{{#set:Related SQL command=UPDATE|Related SQL command=DELETE|Related SQL command=SELECT}}
{{#set:Related SQL command=UPDATE
|Related SQL command=DELETE
|Related SQL command=INSERT
|Related SQL command=FROM
|Related SQL command=GROUP BY
|Related SQL command=ORDER BY
}}


INSERT is refers to the SQL command '''INSERT'''.
INSERT is refers to the SQL command '''INSERT'''.
Line 10: Line 16:
== Syntax ==
== Syntax ==
<pre>
<pre>
SELECT
INSERT INTO <table> [ ( <column> [, <column> [, <column>] [, ... ]]] ) ]
VALUES ( <value> [, <value> [, <value>, [, ...]]] )
* | <column> [, <column> [, ...]]
FROM <table> [, <table> [, ...]]
[ WHERE <condition> [ AND | OR <condition> [, ...] ]
[ GROUP BY <column> [, <column> [, ...] ]]
[ ORDER BY <column> [ ASC | DESC ] [, <column> [ ASC | DESC ] [, ...]]]]
</pre>
</pre>


== Examples ==
== Examples ==
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.
For example to insert the value "foo" into a column called "bar" into a table with name "baz" do the following:
<pre>
<pre>
SELECT * FROM baz
INSERT INTO baz ( bar ) VALUES( "foo" )
</pre>
</pre>
Note: The foo is encased in double quotes this is required for values other than numeric values.


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

To only select rows with value "foo" or value "foofoo" in column "bar" from table "baz" do the following:
<pre>
SELECT bar FROM baz WHERE bar = "foo" OR bar = "foofoo"
</pre>



== Excersises ==
== Excersises ==
# Use the [[sample database]] and insert and populate it with the data from the [[File::/etc/passwd]].
# Use the [[sample database]] and select various values with changing conditions [[File::/etc/passwd]].

Revision as of 03:51, 18 January 2011

{{#set:Related SQL command=UPDATE |Related SQL command=DELETE |Related SQL command=INSERT |Related SQL command=FROM |Related SQL command=GROUP BY |Related SQL command=ORDER BY }}

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

 SELECT 
   * | <column> [, <column> [, ...]]
   FROM <table> [, <table> [, ...]]
   [ WHERE <condition> [ AND | OR <condition> [, ...] ]
   [ GROUP BY <column> [, <column> [, ...] ]]
   [ ORDER BY <column> [ ASC | DESC ] [, <column> [ ASC | DESC ] [, ...]]]]

Examples

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.

  SELECT * FROM baz

To only select rows with value "foo" in column "bar" from table "baz" do the following:

  SELECT bar FROM baz WHERE bar = "foo"

To only select rows with value "foo" or value "foofoo" in column "bar" from table "baz" do the following:

  SELECT bar FROM baz WHERE bar = "foo" OR bar = "foofoo"


Excersises

  1. Use the sample database and select various values with changing conditions [[File::/etc/passwd]].