Database Reference
In-Depth Information
The preceding query will create a
history
table in the
warehouse_db
database,
and the
history_id
column uses the sequence as the default input value.
In this section, we successfully learned how to create a table and also learned how
to use a sequence inside the table creation syntax.
Downloading the example code
You can download the example code iles for all Packt topics you
have purchased from your account at
http://www.packtpub.com
.
If you purchased this topic elsewhere, you can visit
http://www.
packtpub.com/support
and register to have the iles e-mailed
directly to you.
Altering tables
Now that we have learned how to create multiple tables, we can practice some
ALTER TABLE
commands by following this section. With the
ALTER TABLE
command,
we can add, remove, or rename table columns.
Firstly, with the help of the following example, we will be able to add the
phone_no
column in the previously created table
warehouse_tbl
:
warehouse_db=# ALTER TABLE warehouse_tbl
ADD COLUMN phone_no INTEGER;
We can then verify that a column is added in the table by describing the table
as follows:
warehouse_db=# \d warehouse_tbl
Table "public.warehouse_tbl"
Column | Type | Modifiers
----------------+------------------------+-----------
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) |
phone_no | integer |
Indexes:
"PRIM_KEY" PRIMARY KEY, btree (warehouse_id)
Referenced by: