1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!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 18.0 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 <a href="#DML-INSERT" class="id_link">#</a></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>
3 When a table is created, it contains no data. The first thing to
4 do before a database can be of much use is to insert data. Data is
5 inserted one row at a time. You can also insert more than one row
6 in a single command, but it is not possible to insert something that
7 is not a complete row. Even if you know only some column values, a
8 complete row must be created.
10 To create a new row, use the <a class="xref" href="sql-insert.html" title="INSERT"><span class="refentrytitle">INSERT</span></a>
11 command. The command requires the
12 table name and column values. For
13 example, consider the products table from <a class="xref" href="ddl.html" title="Chapter 5. Data Definition">Chapter 5</a>:
14 </p><pre class="programlisting">
15 CREATE TABLE products (
21 An example command to insert a row would be:
22 </p><pre class="programlisting">
23 INSERT INTO products VALUES (1, 'Cheese', 9.99);
25 The data values are listed in the order in which the columns appear
26 in the table, separated by commas. Usually, the data values will
27 be literals (constants), but scalar expressions are also allowed.
29 The above syntax has the drawback that you need to know the order
30 of the columns in the table. To avoid this you can also list the
31 columns explicitly. For example, both of the following commands
32 have the same effect as the one above:
33 </p><pre class="programlisting">
34 INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese', 9.99);
35 INSERT INTO products (name, price, product_no) VALUES ('Cheese', 9.99, 1);
37 Many users consider it good practice to always list the column
40 If you don't have values for all the columns, you can omit some of
41 them. In that case, the columns will be filled with their default
43 </p><pre class="programlisting">
44 INSERT INTO products (product_no, name) VALUES (1, 'Cheese');
45 INSERT INTO products VALUES (1, 'Cheese');
47 The second form is a <span class="productname">PostgreSQL</span>
48 extension. It fills the columns from the left with as many values
49 as are given, and the rest will be defaulted.
51 For clarity, you can also request default values explicitly, for
52 individual columns or for the entire row:
53 </p><pre class="programlisting">
54 INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese', DEFAULT);
55 INSERT INTO products DEFAULT VALUES;
58 You can insert multiple rows in a single command:
59 </p><pre class="programlisting">
60 INSERT INTO products (product_no, name, price) VALUES
66 It is also possible to insert the result of a query (which might be no
67 rows, one row, or many rows):
68 </p><pre class="programlisting">
69 INSERT INTO products (product_no, name, price)
70 SELECT product_no, name, price FROM new_products
71 WHERE release_date = 'today';
73 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.
74 </p><div class="tip"><h3 class="title">Tip</h3><p>
75 When inserting a lot of data at the same time, consider using
76 the <a class="xref" href="sql-copy.html" title="COPY"><span class="refentrytitle">COPY</span></a> command.
77 It is not as flexible as the <a class="xref" href="sql-insert.html" title="INSERT"><span class="refentrytitle">INSERT</span></a>
78 command, but is more efficient. Refer
79 to <a class="xref" href="populate.html" title="14.4. Populating a Database">Section 14.4</a> for more information on improving
80 bulk loading performance.
81 </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 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 6.2. Updating Data</td></tr></table></div></body></html>