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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Chapter6.Data Manipulation</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="sql.html" title="PartII.The SQL Language">
<link rel="prev" href="ddl-depend.html" title="5.11.Dependency Tracking">
<link rel="next" href="dml-update.html" title="6.2.Updating Data">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" lang="en" id="dml">
<div class="titlepage"><div><div><h2 class="title">
<a name="dml"></a>Chapter6.Data Manipulation</h2></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="sect1"><a href="dml.html#dml-insert">6.1. Inserting Data</a></span></dt>
<dt><span class="sect1"><a href="dml-update.html">6.2. Updating Data</a></span></dt>
<dt><span class="sect1"><a href="dml-delete.html">6.3. Deleting Data</a></span></dt>
</dl>
</div>
<p> The previous chapter discussed how to create tables and other
structures to hold your data. Now it is time to fill the tables
with data. This chapter covers how to insert, update, and delete
table data. We also introduce ways to effect automatic data changes
when certain events occur: triggers and rewrite rules. The chapter
after this will finally explain how to extract your long-lost data
back out of the database.
</p>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="dml-insert"></a>6.1.Inserting Data</h2></div></div></div>
<a name="id579527"></a><a name="id579538"></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
conceptually inserted one row at a time. Of course you can also
insert more than one row, but there is no way to insert less than
one row at a time. Even if you know only some column values, a
complete row must be created.
</p>
<p> To create a new row, use the <a href="sql-insert.html" title="INSERT"><span class="refentrytitle"><a name="sql-insert-title"></a>INSERT</span></a> command. The command requires the
table name and a value for each of the columns of the table. For
example, consider the products table from <a href="ddl.html" title="Chapter5.Data Definition">Chapter5, <i>Data Definition</i></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 that 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>
<div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Tip</h3>
<p> To do “<span class="quote">bulk loads</span>”, that is, inserting a lot of data,
take a look at the <a href="sql-copy.html">COPY</a> command. It is not as flexible as the
<a href="sql-insert.html">INSERT</a> command,
but is more efficient. Refer to <a href="populate.html" title="13.4.Populating a Database">Section13.4, “Populating a Database”</a> for more
information on improving bulk loading performance.
</p>
</div>
</div>
</div></body>
</html>
|