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
|
<?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>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 16.2 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>
You can create a new table by specifying the table
name, along with all column names and their types:
</p><pre class="programlisting">
CREATE TABLE weather (
city varchar(80),
temp_lo int, -- low temperature
temp_hi int, -- high temperature
prcp real, -- precipitation
date date
);
</pre><p>
You can enter this into <code class="command">psql</code> with the line
breaks. <code class="command">psql</code> will recognize that the command
is not terminated until the semicolon.
</p><p>
White space (i.e., spaces, tabs, and newlines) can be used freely
in SQL commands. That means you can type the command aligned
differently than above, or even all on one line. Two dashes
(<span class="quote">“<span class="quote"><code class="literal">--</code></span>”</span>) introduce comments.
Whatever follows them is ignored up to the end of the line. SQL
is case-insensitive about key words and identifiers, except
when identifiers are double-quoted to preserve the case (not done
above).
</p><p>
<code class="type">varchar(80)</code> specifies a data type that can store
arbitrary character strings up to 80 characters in length.
<code class="type">int</code> is the normal integer type. <code class="type">real</code> is
a type for storing single precision floating-point numbers.
<code class="type">date</code> should be self-explanatory. (Yes, the column of
type <code class="type">date</code> is also named <code class="structfield">date</code>.
This might be convenient or confusing — you choose.)
</p><p>
<span class="productname">PostgreSQL</span> supports the standard
<acronym class="acronym">SQL</acronym> types <code class="type">int</code>,
<code class="type">smallint</code>, <code class="type">real</code>, <code class="type">double
precision</code>, <code class="type">char(<em class="replaceable"><code>N</code></em>)</code>,
<code class="type">varchar(<em class="replaceable"><code>N</code></em>)</code>, <code class="type">date</code>,
<code class="type">time</code>, <code class="type">timestamp</code>, and
<code class="type">interval</code>, as well as other types of general utility
and a rich set of geometric types.
<span class="productname">PostgreSQL</span> can be customized with an
arbitrary number of user-defined data types. Consequently, type
names are not key words in the syntax, except where required to
support special cases in the <acronym class="acronym">SQL</acronym> standard.
</p><p>
The second example will store cities and their associated
geographical location:
</p><pre class="programlisting">
CREATE TABLE cities (
name varchar(80),
location point
);
</pre><p>
The <code class="type">point</code> type is an example of a
<span class="productname">PostgreSQL</span>-specific data type.
</p><p>
<a id="id-1.4.4.4.8.1" class="indexterm"></a>
Finally, it should be mentioned that if you don't need a table any
longer or want to recreate it differently you can remove it using
the following command:
</p><pre class="synopsis">
DROP TABLE <em class="replaceable"><code>tablename</code></em>;
</pre><p>
</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 16.2 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 2.4. Populating a Table With Rows</td></tr></table></div></body></html>
|