]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/tutorial-table.html
PG 18 docs from https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0-docs...
[ai-pg] / full-docs / src / sgml / html / tutorial-table.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>2.3. Creating a New Table</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="tutorial-concepts.html" title="2.2. Concepts" /><link rel="next" href="tutorial-populate.html" title="2.4. Populating a Table With Rows" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">2.3. Creating a New Table</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="tutorial-concepts.html" title="2.2. Concepts">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="tutorial-sql.html" title="Chapter 2. The SQL Language">Up</a></td><th width="60%" align="center">Chapter 2. The <acronym class="acronym">SQL</acronym> Language</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="tutorial-populate.html" title="2.4. Populating a Table With Rows">Next</a></td></tr></table><hr /></div><div class="sect1" id="TUTORIAL-TABLE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">2.3. Creating a New Table <a href="#TUTORIAL-TABLE" class="id_link">#</a></h2></div></div></div><a id="id-1.4.4.4.2" class="indexterm"></a><p>
3     You  can  create  a  new  table by specifying the table
4     name, along with all column names and their types:
5
6 </p><pre class="programlisting">
7 CREATE TABLE weather (
8     city            varchar(80),
9     temp_lo         int,           -- low temperature
10     temp_hi         int,           -- high temperature
11     prcp            real,          -- precipitation
12     date            date
13 );
14 </pre><p>
15
16     You can enter this into <code class="command">psql</code> with the line
17     breaks.  <code class="command">psql</code> will recognize that the command
18     is not terminated until the semicolon.
19    </p><p>
20     White space (i.e., spaces, tabs, and newlines) can be used freely
21     in SQL commands.  That means you can type the command aligned
22     differently than above, or even all on one line.  Two dashes
23     (<span class="quote">“<span class="quote"><code class="literal">--</code></span>”</span>) introduce comments.
24     Whatever follows them is ignored up to the end of the line.  SQL
25     is case-insensitive about key words and identifiers, except
26     when identifiers are double-quoted to preserve the case (not done
27     above).
28    </p><p>
29     <code class="type">varchar(80)</code> specifies a data type that can store
30     arbitrary character strings up to 80 characters in length.
31     <code class="type">int</code> is the normal integer type.  <code class="type">real</code> is
32     a type for storing single precision floating-point numbers.
33     <code class="type">date</code> should be self-explanatory.  (Yes, the column of
34     type <code class="type">date</code> is also named <code class="structfield">date</code>.
35     This might be convenient or confusing — you choose.)
36    </p><p>
37     <span class="productname">PostgreSQL</span> supports the standard
38     <acronym class="acronym">SQL</acronym> types <code class="type">int</code>,
39     <code class="type">smallint</code>, <code class="type">real</code>, <code class="type">double
40     precision</code>, <code class="type">char(<em class="replaceable"><code>N</code></em>)</code>,
41     <code class="type">varchar(<em class="replaceable"><code>N</code></em>)</code>, <code class="type">date</code>,
42     <code class="type">time</code>, <code class="type">timestamp</code>, and
43     <code class="type">interval</code>, as well as other types of general utility
44     and a rich set of geometric types.
45     <span class="productname">PostgreSQL</span> can be customized with an
46     arbitrary number of user-defined data types.  Consequently, type
47     names are not key words in the syntax, except where required to
48     support special cases in the <acronym class="acronym">SQL</acronym> standard.
49    </p><p>
50     The second example will store cities and their associated
51     geographical location:
52 </p><pre class="programlisting">
53 CREATE TABLE cities (
54     name            varchar(80),
55     location        point
56 );
57 </pre><p>
58     The <code class="type">point</code> type is an example of a
59     <span class="productname">PostgreSQL</span>-specific data type.
60    </p><p>
61     <a id="id-1.4.4.4.8.1" class="indexterm"></a>
62
63     Finally, it should be mentioned that if you don't need a table any
64     longer or want to recreate it differently you can remove it using
65     the following command:
66 </p><pre class="synopsis">
67 DROP TABLE <em class="replaceable"><code>tablename</code></em>;
68 </pre><p>
69    </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="tutorial-concepts.html" title="2.2. Concepts">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="tutorial-sql.html" title="Chapter 2. The SQL Language">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="tutorial-populate.html" title="2.4. Populating a Table With Rows">Next</a></td></tr><tr><td width="40%" align="left" valign="top">2.2. Concepts </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"> 2.4. Populating a Table With Rows</td></tr></table></div></body></html>