w3resource

PostgreSQL Insert Record: Insert one row into a table against the specific column country_name


2. Write a SQL statement to insert one row into the table countries against the column country_id and country_name.

Here in the following is the structure of the table countries.

    Column    |         Type          | Modifiers
--------------+-----------------------+-----------
 country_id   | character varying(2)  |
 country_name | character varying(40) |
 region_id    | numeric(10,0)         |

Sample Solution:

Code:

-- This SQL statement inserts a new row into the 'countries' table with specified values for specific columns.

INSERT INTO countries (country_id, country_name) VALUES('C2','USA');

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) specifies the columns into which the values will be inserted.
  • VALUES('C2','USA') provides the values to be inserted into the specified columns. In this case, 'C2' is inserted into the 'COUNTRY_ID' column, and 'USA' is inserted into the 'COUNTRY_NAME' column. The 'REGION_ID' column is not specified in the column list, so it will either be set to its default value (if one exists) or to NULL.

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

postgres=# SELECT * FROM countries;
 country_id | country_name | region_id
------------+--------------+-----------
 C1         | India        |      1002
 C2         | USA          |

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

Previous: Write a SQL statement to insert a record with your own value into the table countries against each column.
Next: Write a SQL statement to create duplicates of countries table named country_new with all structure and data.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.