w3resource

PostgreSQL Create Table: Create a table to set default value, a minimum value and a NULL as default value for specific columns


9. Write a SQL statement to create a table named jobs, including job_id, job_title, min_salary and max_salary, and make sure that, the default value for job_title is blank and min_salary is 8000 and max_salary is NULL will be entered automatically at the time of insertion if no value assigned for the specified columns.

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 UNIQUE, -- Defines a column 'JOB_ID' to store job IDs as strings with a maximum length of 10 characters. The column cannot contain NULL values, and each value must be unique across all rows.
    JOB_TITLE varchar(35) NOT NULL DEFAULT ' ', -- Defines a column 'JOB_TITLE' to store job titles as strings with a maximum length of 35 characters. The column cannot contain NULL values, and if no value is provided during insertion, it defaults to an empty string.
    MIN_SALARY decimal(6,0) DEFAULT 8000, -- Defines a column 'MIN_SALARY' to store minimum salary amounts as decimal numbers with a precision of 6 digits and no decimal places. If no value is provided during insertion, it defaults to 8000.
    MAX_SALARY decimal(6,0) DEFAULT NULL -- Defines a column 'MAX_SALARY' to store maximum salary amounts as decimal numbers with a precision of 6 digits and no decimal places. If no value is provided during insertion, it defaults to NULL.
);

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 UNIQUE constraint ensures that each value in the 'JOB_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. 'JOB_TITLE' defaults to an empty string, 'MIN_SALARY' defaults to 8000, and 'MAX_SALARY' defaults to NULL.

Output:

postgres=# CREATE TABLE IF NOT EXISTS jobs (
postgres(# JOB_ID varchar(10) NOT NULL UNIQUE,
postgres(# JOB_TITLE varchar(35) NOT NULL DEFAULT ' ',
postgres(# MIN_SALARY decimal(6,0) DEFAULT 8000,
postgres(# MAX_SALARY decimal(6,0) DEFAULT NULL
postgres(# );
CREATE TABLE

Here is the 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 default ' '::character varying
 min_salary | numeric(6,0)          | default 8000
 max_salary | numeric(6,0)          | default NULL::numeric
Indexes:
    "jobs_job_id_key" UNIQUE CONSTRAINT, btree (job_id)

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

Previous: 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.
Next: Write a SQL statement to create a table named countries, including columns country_id, country_name and region_id and make sure that the country_id column will be a key field which will not contain any duplicate data at the time of insertion.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.