Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
dml-insert.html81 linesDownload Raw Back to html
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 16.3 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 to4   do before a database can be of much use is to insert data.  Data is5   inserted one row at a time.  You can also insert more than one row6   in a single command, but it is not possible to insert something that7   is not a complete row.  Even if you know only some column values, a8   complete row must be created.9  </p><p>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 the12   table name and column values.  For13   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">15CREATE TABLE products (16    product_no integer,17    name text,18    price numeric19);20</pre><p>21   An example command to insert a row would be:22</p><pre class="programlisting">23INSERT INTO products VALUES (1, 'Cheese', 9.99);24</pre><p>25   The data values are listed in the order in which the columns appear26   in the table, separated by commas.  Usually, the data values will27   be literals (constants), but scalar expressions are also allowed.28  </p><p>29   The above syntax has the drawback that you need to know the order30   of the columns in the table.  To avoid this you can also list the31   columns explicitly.  For example, both of the following commands32   have the same effect as the one above:33</p><pre class="programlisting">34INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese', 9.99);35INSERT INTO products (name, price, product_no) VALUES ('Cheese', 9.99, 1);36</pre><p>37   Many users consider it good practice to always list the column38   names.39  </p><p>40   If you don't have values for all the columns, you can omit some of41   them.  In that case, the columns will be filled with their default42   values.  For example:43</p><pre class="programlisting">44INSERT INTO products (product_no, name) VALUES (1, 'Cheese');45INSERT INTO products VALUES (1, 'Cheese');46</pre><p>47   The second form is a <span class="productname">PostgreSQL</span>48   extension.  It fills the columns from the left with as many values49   as are given, and the rest will be defaulted.50  </p><p>51   For clarity, you can also request default values explicitly, for52   individual columns or for the entire row:53</p><pre class="programlisting">54INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese', DEFAULT);55INSERT INTO products DEFAULT VALUES;56</pre><p>57  </p><p>58   You can insert multiple rows in a single command:59</p><pre class="programlisting">60INSERT INTO products (product_no, name, price) VALUES61    (1, 'Cheese', 9.99),62    (2, 'Bread', 1.99),63    (3, 'Milk', 2.99);64</pre><p>65  </p><p>66   It is also possible to insert the result of a query (which might be no67   rows, one row, or many rows):68</p><pre class="programlisting">69INSERT INTO products (product_no, name, price)70  SELECT product_no, name, price FROM new_products71    WHERE release_date = 'today';72</pre><p>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 using76    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. Refer79    to <a class="xref" href="populate.html" title="14.4. Populating a Database">Section 14.4</a> for more information on improving80    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 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 6.2. Updating Data</td></tr></table></div></body></html>
codekingpro/portable-devtools · Team Ai