Database Reference
In-Depth Information
warehouse_db=# CREATE TABLE tools
(
tool_id INTEGER UNIQUE,
tool_name TEXT,
tool_class NUMERIC
);
Alternatively, the same constraint can be declared at the end of all columns.
For instance, this can look like the following:
warehouse_db=# CREATE TABLE tools
(
tool_id INTEGER,
tool_name TEXT,
tool_class NUMERIC,
UNIQUE (tool_id)
);
When deining the unique constraints for a group of columns, all columns must be
listed separately using commas. Consider the following example:
warehouse_db=# CREATE TABLE cards
(
card_id INTEGER,
owner_number INTEGER,
owner_name TEXT,
UNIQUE (card_id, owner_number)
);
The preceding query will create the
cards
table with a unique constraint
implemented on the
card_id
and
owner_number
columns. Note that the unique
constraint is not applicable on null values. This means that in the
cards
table, two
records can have the same record if they have
card_id
and
owner_number
as null.
Not-null constraints
A
not-null constraint
makes sure that a column must have some values and a value
is not left as null. Drop the previously created
tools
table and create the
tools
table
again using this constraint using the following example:
warehouse_db=# CREATE TABLE tools
(
tool_id INTEGER NOT NULL,
tool_name TEXT,
tool_class NUMERIC
);