w3resource

PostgreSQL Insert Record: Insert NULL values into a column of a table


4. Write a SQL statement to insert NULL values into region_id column for a row of countries table.

Sample Solution:

Code:

-- This SQL statement inserts a new row into the 'countries' table with specified values, including a NULL value.

INSERT INTO countries (country_id, country_name, region_id) VALUES('C3','UK',NULL);

Explanation:

  • The INSERT INTO statement is used to add new rows into a table.
  • countries is the name of the table where the new row will be inserted.
  • (country_id, country_name, region_id) specifies the columns into which the values will be inserted.
  • VALUES('C3','UK',NULL) provides the values to be inserted into the specified columns. In this case, 'C3' is inserted into the 'country_id' column, 'UK' is inserted into the 'country_name' column, and NULL is inserted into the 'region_id' column. This demonstrates inserting a NULL value explicitly into a column.

Here is the command to see the list of inserting rows :

postgres=# SELECT * FROM countries;
 country_id | country_name | region_id
------------+--------------+-----------
 C1         | India        |      1002
 C2         | USA          |
 C3         | UK           |
(3 rows)

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

Previous: Write a SQL statement to create duplicates of countries table named country_new with all structure and data.
Next: Write a SQL statement to insert 3 rows by a single insert statement

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.