Write a postgre SQL statement to insert records into the table countries to ensure that the country_id

  • Postgre SQL

Write a  postgre SQL statement to insert records into the table countries to ensure that the country_id column will not contain any duplicate data and this will be automatically incremented and the column country_name will be filled up by 'N/A' if no value assigned to that column.

الأجوبة

Here is the code to create a sample table countries:

CREATE TABLE countries ( 
COUNTRY_ID SERIAL PRIMARY KEY,
COUNTRY_NAME varchar(40) NOT NULL DEFAULT 'N/A',
REGION_ID integer NOT NULL
);

Now insert one record into the table:

INSERT INTO countries VALUES(501,'India',102);

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

postgres=# SELECT * FROM countries;
 country_id | country_name | region_id
------------+--------------+-----------
        501 | India        |       102
(1 row)

Now insert another two records into the table :

INSERT INTO countries(region_id) VALUES(109);
INSERT INTO countries(country_name,region_id) VALUES('Australia',121);

Now see the value of the key field incremented automatically:

postgres=# SELECT * FROM countries;
 country_id | country_name | region_id
------------+--------------+-----------
        501 | India        |       102
          1 | N/A          |       109
          2 | Australia    |       121
(3 rows)
هل كان المحتوى مفيد؟

معلومات ذات صلة

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...