INSERT

Name

INSERT  --  Inserts new rows into a table.

Synopsis

INSERT INTO table [ ( column [, ...] ) ]
    { DEFAULT VALUES |VALUES(expression [, ...] ) | SELECT query }
  

Parameters

table

This parameter takes the name of an existing table that you wish to insert data into.

column

Use this parameter to specify a column name within table that you wish to insert a value into.

expression

This parameter accepts the value that will be assigned to the column you've chosen.

query

This parameter takes a valid SQL query.

Results

INSERT oid 1

This message is displayed if one row of data is inserted correctly.

INSERT 0 #

This message is displayed if more than one row is updated. The # symbol represents how many rows were updated.

Description

Use the INSERT command to add new rows into a table, either singularly or in multiples. To insert mutliple rows, a query statement will need to be used, as a normal INSERT can only insert one row of data (or one column, if specified). Use a query with the INSERT command to feed the results into a specified table. If you are unsure of the exact data type and mis-use it or enter it in the incorrect data type for a field, PostgreSQL will attempt to use automatic type coercion.

When inserting values into columns (instead of whole rows), the columns can be listed in any order; however, the values for those columsn will need to be listed in the same order.

NoteIgnored fields
 

If you leave out values for certain fields in your table, the database will automatically make them either NULL, or (if they have default content setup) it will insert the default content of the field.

If the expression for each column is not of the correct data type, automatic type coercion will be attempted.

Examples

The following example inserts a single row into the employees table.

   INSERT INTO employees
        VALUES (106, 'Hall', 'Timothy');

Alternatively, we can insert only the ID number (and not the name). This will insert a NULL into the name column.

INSERT INTO employees (id)
    VALUES (107);