w3resource

PostgreSQL Create Table: Create a table to allow any two of the column combination to contain unique values


12. Write a SQL statement to create a table countries, including country_id, country_name and region_id and make sure that the combination of columns country_id and region_id will be unique.

Sample Solution:

Code:

-- This SQL statement creates a new table named 'countries_n' if it does not already exist,
-- defining the structure and constraints for the table columns.

CREATE TABLE IF NOT EXISTS countries_n (
    COUNTRY_ID varchar(2) NOT NULL UNIQUE DEFAULT '', -- Defines a column 'COUNTRY_ID' to store country IDs as strings with a maximum length of 2 characters. The column cannot contain NULL values, and each value must be unique across all rows. If no value is provided during insertion, it defaults to an empty string.
    COUNTRY_NAME varchar(40) DEFAULT NULL, -- Defines a column 'COUNTRY_NAME' to store country names as strings with a maximum length of 40 characters. If no value is provided during insertion, it defaults to NULL.
    REGION_ID decimal(10,0) NOT NULL, -- Defines a column 'REGION_ID' to store region IDs as decimal numbers with a precision of 10 digits and no decimal places. The column cannot contain NULL values.
    PRIMARY KEY (COUNTRY_ID, REGION_ID) -- Defines a composite primary key using both 'COUNTRY_ID' and 'REGION_ID' columns.
);

Explanation:

  • The CREATE TABLE IF NOT EXISTS statement creates a new table only if it does not already exist in the database.
  • The table is named 'countries_n'.
  • Each column definition specifies the column name, data type, and optional constraints.
  • varchar(2) indicates a variable-length character string with a maximum length of 2 characters for the 'COUNTRY_ID' column.
  • The NOT NULL constraint ensures that 'COUNTRY_ID' column cannot contain NULL values.
  • The UNIQUE constraint ensures that each value in the 'COUNTRY_ID' column must be unique across all rows in the table.
  • The DEFAULT constraint specifies default values for columns when no value is provided during insertion. 'COUNTRY_ID' defaults to an empty string, and 'COUNTRY_NAME' defaults to NULL.
  • varchar(40) indicates a variable-length character string with a maximum length of 40 characters for the 'COUNTRY_NAME' column.
  • decimal(10,0) indicates a decimal number with a precision of 10 digits and no decimal places for the 'REGION_ID' column.
  • The NOT NULL constraint ensures that 'REGION_ID' column cannot contain NULL values.
  • The PRIMARY KEY constraint defines a composite primary key using both 'COUNTRY_ID' and 'REGION_ID' columns. This ensures uniqueness and provides a unique identifier for each row in the table.

Output:

postgres=# CREATE TABLE IF NOT EXISTS countries (
postgres(# COUNTRY_ID varchar(2) NOT NULL UNIQUE DEFAULT '',
postgres(# COUNTRY_NAME varchar(40) DEFAULT NULL,
postgres(# REGION_ID decimal(10,0) NOT NULL,
postgres(# PRIMARY KEY (COUNTRY_ID,REGION_ID));
CREATE TABLE

Here is the command to see the structure of the created table :

postgres=# \d countries
                          Table "public.countries"
    Column    |         Type          |               Modifiers
--------------+-----------------------+----------------------------------------
 country_id   | character varying(2)  | not null default ''::character varying
 country_name | character varying(40) | default NULL::character varying
 region_id    | numeric(10,0)         | not null
Indexes:
    "countries_pkey" PRIMARY KEY, btree (country_id, region_id)
    "countries_country_id_key" UNIQUE CONSTRAINT, btree (country_id)

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a SQL statement to create a table countries including columns country_id, country_name and region_id and make sure that the column country_id will be unique and store an auto-incremented value.
Next: Write a SQL statement to create a table job_history, including employee_id, start_date, end_date, job_id and department_id and make sure that, the employee_id column does not contain any duplicate values at the time of insertion and the foreign key column job_id contain only those values which exist in the jobs table.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.