]> begriffs open source - ai-pg/blob - full-docs/txt/tutorial-table.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / tutorial-table.txt
1
2 2.3. Creating a New Table #
3
4    You can create a new table by specifying the table name, along with all
5    column names and their types:
6 CREATE TABLE weather (
7     city            varchar(80),
8     temp_lo         int,           -- low temperature
9     temp_hi         int,           -- high temperature
10     prcp            real,          -- precipitation
11     date            date
12 );
13
14    You can enter this into psql with the line breaks. psql will recognize
15    that the command is not terminated until the semicolon.
16
17    White space (i.e., spaces, tabs, and newlines) can be used freely in
18    SQL commands. That means you can type the command aligned differently
19    than above, or even all on one line. Two dashes (“--”) introduce
20    comments. Whatever follows them is ignored up to the end of the line.
21    SQL is case-insensitive about key words and identifiers, except when
22    identifiers are double-quoted to preserve the case (not done above).
23
24    varchar(80) specifies a data type that can store arbitrary character
25    strings up to 80 characters in length. int is the normal integer type.
26    real is a type for storing single precision floating-point numbers.
27    date should be self-explanatory. (Yes, the column of type date is also
28    named date. This might be convenient or confusing — you choose.)
29
30    PostgreSQL supports the standard SQL types int, smallint, real, double
31    precision, char(N), varchar(N), date, time, timestamp, and interval, as
32    well as other types of general utility and a rich set of geometric
33    types. PostgreSQL can be customized with an arbitrary number of
34    user-defined data types. Consequently, type names are not key words in
35    the syntax, except where required to support special cases in the SQL
36    standard.
37
38    The second example will store cities and their associated geographical
39    location:
40 CREATE TABLE cities (
41     name            varchar(80),
42     location        point
43 );
44
45    The point type is an example of a PostgreSQL-specific data type.
46
47    Finally, it should be mentioned that if you don't need a table any
48    longer or want to recreate it differently you can remove it using the
49    following command:
50 DROP TABLE tablename;