summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/html/dml-insert.html
blob: e5f22dedaf75be8ba8253caea78dad5762b4c6b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>6.1. Inserting Data</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="dml.html" title="Chapter 6. Data Manipulation" /><link rel="next" href="dml-update.html" title="6.2. Updating Data" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">6.1. Inserting Data</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="dml.html" title="Chapter 6. Data Manipulation">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="dml.html" title="Chapter 6. Data Manipulation">Up</a></td><th width="60%" align="center">Chapter 6. Data Manipulation</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 15.7 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="dml-update.html" title="6.2. Updating Data">Next</a></td></tr></table><hr /></div><div class="sect1" id="DML-INSERT"><div class="titlepage"><div><div><h2 class="title" style="clear: both">6.1. Inserting Data</h2></div></div></div><a id="id-1.5.5.3.2" class="indexterm"></a><a id="id-1.5.5.3.3" class="indexterm"></a><p>
   When a table is created, it contains no data.  The first thing to
   do before a database can be of much use is to insert data.  Data is
   inserted one row at a time.  You can also insert more than one row
   in a single command, but it is not possible to insert something that
   is not a complete row.  Even if you know only some column values, a
   complete row must be created.
  </p><p>
   To create a new row, use the <a class="xref" href="sql-insert.html" title="INSERT"><span class="refentrytitle">INSERT</span></a>
   command.  The command requires the
   table name and column values.  For
   example, consider the products table from <a class="xref" href="ddl.html" title="Chapter 5. Data Definition">Chapter 5</a>:
</p><pre class="programlisting">
CREATE TABLE products (
    product_no integer,
    name text,
    price numeric
);
</pre><p>
   An example command to insert a row would be:
</p><pre class="programlisting">
INSERT INTO products VALUES (1, 'Cheese', 9.99);
</pre><p>
   The data values are listed in the order in which the columns appear
   in the table, separated by commas.  Usually, the data values will
   be literals (constants), but scalar expressions are also allowed.
  </p><p>
   The above syntax has the drawback that you need to know the order
   of the columns in the table.  To avoid this you can also list the
   columns explicitly.  For example, both of the following commands
   have the same effect as the one above:
</p><pre class="programlisting">
INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese', 9.99);
INSERT INTO products (name, price, product_no) VALUES ('Cheese', 9.99, 1);
</pre><p>
   Many users consider it good practice to always list the column
   names.
  </p><p>
   If you don't have values for all the columns, you can omit some of
   them.  In that case, the columns will be filled with their default
   values.  For example:
</p><pre class="programlisting">
INSERT INTO products (product_no, name) VALUES (1, 'Cheese');
INSERT INTO products VALUES (1, 'Cheese');
</pre><p>
   The second form is a <span class="productname">PostgreSQL</span>
   extension.  It fills the columns from the left with as many values
   as are given, and the rest will be defaulted.
  </p><p>
   For clarity, you can also request default values explicitly, for
   individual columns or for the entire row:
</p><pre class="programlisting">
INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese', DEFAULT);
INSERT INTO products DEFAULT VALUES;
</pre><p>
  </p><p>
   You can insert multiple rows in a single command:
</p><pre class="programlisting">
INSERT INTO products (product_no, name, price) VALUES
    (1, 'Cheese', 9.99),
    (2, 'Bread', 1.99),
    (3, 'Milk', 2.99);
</pre><p>
  </p><p>
   It is also possible to insert the result of a query (which might be no
   rows, one row, or many rows):
</p><pre class="programlisting">
INSERT INTO products (product_no, name, price)
  SELECT product_no, name, price FROM new_products
    WHERE release_date = 'today';
</pre><p>
   This provides the full power of the SQL query mechanism (<a class="xref" href="queries.html" title="Chapter 7. Queries">Chapter 7</a>) for computing the rows to be inserted.
  </p><div class="tip"><h3 class="title">Tip</h3><p>
    When inserting a lot of data at the same time, consider using
    the <a class="xref" href="sql-copy.html" title="COPY"><span class="refentrytitle">COPY</span></a> command.
    It is not as flexible as the <a class="xref" href="sql-insert.html" title="INSERT"><span class="refentrytitle">INSERT</span></a>
    command, but is more efficient. Refer
    to <a class="xref" href="populate.html" title="14.4. Populating a Database">Section 14.4</a> for more information on improving
    bulk loading performance.
   </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="dml.html" title="Chapter 6. Data Manipulation">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="dml.html" title="Chapter 6. Data Manipulation">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="dml-update.html" title="6.2. Updating Data">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 6. Data Manipulation </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 15.7 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 6.2. Updating Data</td></tr></table></div></body></html>