w3resource

PostgreSQL Create Table: Create a table to restrict the value of a column within some specified values


7. Write a SQL statement to create a table named countries, including country_id, country_name and region_id and make sure that no countries except Italy, India and China will be entered in the table.

Sample Solution:

Code:

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

CREATE TABLE IF NOT EXISTS countries ( 
    COUNTRY_ID varchar(2), -- Defines a column 'COUNTRY_ID' to store country IDs as strings with a maximum length of 2 characters. This column can contain NULL values.
    COUNTRY_NAME varchar(40), -- Defines a column 'COUNTRY_NAME' to store country names as strings with a maximum length of 40 characters. This column can contain NULL values.
CHECK(COUNTRY_NAME IN ('Italy', 'India', 'China')), -- Defines a check constraint to ensure that the 'COUNTRY_NAME' value is one of 'Italy', 'India', or 'China'.
    REGION_ID decimal(10,0) -- Defines a column 'REGION_ID' to store region IDs as decimal numbers with a precision of 10 digits and no decimal places. This column can contain NULL values.
);

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'.
  • 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.
  • varchar(40) indicates a variable-length character string with a maximum length of 40 characters for the 'COUNTRY_NAME' column.
  • The CHECK constraint ensures that the value of the 'COUNTRY_NAME' column must be one of 'Italy', 'India', or 'China'.
  • decimal(10,0) indicates a decimal number with a precision of 10 digits and no decimal places for the 'REGION_ID' column.
  • All columns can contain NULL values since the NOT NULL constraint is not specified for any column.

Output:

postgres=# CREATE TABLE IF NOT EXISTS countries (
postgres(# COUNTRY_ID varchar(2),
postgres(# COUNTRY_NAME varchar(40)
postgres(# CHECK(COUNTRY_NAME IN('Italy','India','China')),
postgres(# REGION_ID decimal(10,0)
postgres(# );
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)  |
 country_name | character varying(40) |
 region_id    | numeric(10,0)         |
Check constraints:
    "countries_country_name_check" CHECK (country_name::text = 
	ANY (ARRAY['Italy'::character varying, 'India'::character varying, 
	'China'::character varying]::text[]))

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

Previous: Write a SQL statement to create a table named jobs, including job_id, job_title, min_salary, max_salary and check whether the max_salary amount exceeding the upper limit 25000.
Next: Write a SQL statement to create a table named countries, including country_id, country_name and region_id and make sure that no duplicate data against column country_id will be allowed at the time of insertion.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.