Database Reference
In-Depth Information
Creating tables
Now, let's perform some DDL operations starting with creating tables. To create
a table named warehouse_tbl , execute the following statements:
warehouse_db=# CREATE TABLE warehouse_tbl
(
warehouse_id INTEGER NOT NULL,
warehouse_name TEXT NOT NULL,
year_created INTEGER,
street_address TEXT,
city CHARACTER VARYING(100),
state CHARACTER VARYING(2),
zip CHARACTER VARYING(10),
CONSTRAINT "PRIM_KEY" PRIMARY KEY (warehouse_id)
);
The preceding statements created the table warehouse_tbl that has the primary key
warehouse_id . Now, as you are familiar with the table creation syntax, let's create a
sequence and use that in a table. You can create the hist_id_seq sequence using the
following statement:
warehouse_db=# CREATE SEQUENCE hist_id_seq;
The preceding CREATE SEQUENCE command creates a new sequence number
generator. This involves creating and initializing a new special single-row table
with the name hist_id_seq . The user issuing the command will own the generator.
You can now create the table that implements the hist_id_seq sequence using the
following statement:
warehouse_db=# CREATE TABLE history
(
history_id INTEGER NOT NULL DEFAULT nextval('hist_id_seq'),
date TIMESTAMP WITHOUT TIME ZONE,
amount INTEGER,
data TEXT,
customer_id INTEGER,
warehouse_id INTEGER,
CONSTRAINT "PRM_KEY" PRIMARY KEY (history_id),
CONSTRAINT "FORN_KEY"
FOREIGN KEY (warehouse_id)
REFERENCES warehouse_tbl(warehouse_id)
);
 
Search WWH ::




Custom Search