Database Reference
In-Depth Information
tool_id INTEGER PRIMARY KEY,
tool_name TEXT,
tool_class NUMERIC
);
You can also create a primary key constraint based on two columns. Consider the
following example:
warehouse_db=# CREATE TABLE tools
(
tool_id INTEGER,
tool_name TEXT,
tool_class NUMERIC,
PRIMARY KEY (tool_id, tool_name)
);
Foreign key constraints
Foreign key constraints state that the value in a column must be the same as
the value present in another table's row. This is for the sake of maintaining the
referential integrity between two interlinked tables. Consider the following examples
to understand the concept of foreign key constraints. We will create two tables, and
we will use the column of one table in the second table as a foreign key constraint:
warehouse_db=# CREATE TABLE tools
(
tool_id INTEGER PRIMARY KEY,
tool_name TEXT,
tool_class NUMERIC
);
This will create a table with primary key constraints:
warehouse_db=# CREATE TABLE tools_list
(
list_id INTEGER PRIMARY KEY,
tool_id INTEGER REFERENCES tools (tool_id),
list_name TEXT
);
In the preceding query, we created a table with the name of tools_list that has
a foreign key on the tool_id column with the tool_id reference column from the
tools table.
 
Search WWH ::




Custom Search