w3resource

PostgreSQL Create Table: Create a table to restrict the upper limit for a column


6. 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.

Sample Solution:

Code:

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

CREATE TABLE IF NOT EXISTS jobs ( 
    JOB_ID varchar(10) NOT NULL, -- Defines a column 'JOB_ID' to store job IDs as strings with a maximum length of 10 characters. The column cannot contain NULL values.
    JOB_TITLE varchar(35) NOT NULL, -- Defines a column 'JOB_TITLE' to store job titles as strings with a maximum length of 35 characters. The column cannot contain NULL values.
    MIN_SALARY decimal(6,0), -- Defines a column 'MIN_SALARY' to store minimum salary amounts as decimal numbers with a precision of 6 digits and no decimal places. This column can contain NULL values.
    MAX_SALARY decimal(6,0), -- Defines a column 'MAX_SALARY' to store maximum salary amounts as decimal numbers with a precision of 6 digits and no decimal places. This column can contain NULL values.
CHECK(MAX_SALARY<=25000) -- Defines a check constraint to ensure that the 'MAX_SALARY' value is less than or equal to 25000.
);

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 'jobs'.
  • Each column definition specifies the column name, data type, and optional constraints.
  • varchar(10) indicates a variable-length character string with a maximum length of 10 characters for the 'JOB_ID' column.
  • varchar(35) indicates a variable-length character string with a maximum length of 35 characters for the 'JOB_TITLE' column.
  • .
  • decimal(6,0) indicates a decimal number with a precision of 6 digits and no decimal places for both 'MIN_SALARY' and 'MAX_SALARY' columns.
  • The NOT NULL constraint ensures that 'JOB_ID' and 'JOB_TITLE' columns cannot be left empty.
  • The CHECK constraint ensures that the 'MAX_SALARY' value does not exceed 25000.

Output:

postgres=# CREATE TABLE IF NOT EXISTS jobs (
postgres(# JOB_ID varchar(10) NOT NULL ,
postgres(# JOB_TITLE varchar(35) NOT NULL,
postgres(# MIN_SALARY decimal(6,0),
postgres(# MAX_SALARY decimal(6,0)
postgres(# CHECK(MAX_SALARY<=25000)
postgres(# );
CREATE TABLE

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

postgres=# \d jobs;
             Table "public.jobs"
   Column   |         Type          | Modifiers
------------+-----------------------+-----------
 job_id     | character varying(10) | not null
 job_title  | character varying(35) | not null
 min_salary | numeric(6,0)          |
 max_salary | numeric(6,0)          |
Check constraints:
    "jobs_max_salary_check" CHECK (max_salary <= 25000::numeric)

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

Previous: Write a SQL statement to create a table countries, set a constraint NULL.
Next: 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.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.