Write a postgre SQL statement to insert a record into the table countries to ensure that, at country_id and the region_id combination will be entered once in the table

  • Postgre SQL

Write a  postgre SQL statement to insert a record into the table countries to ensure that, at country_id and the region_id combination will be entered once in the table.

الأجوبة

Here is the code to create a sample table countries:

CREATE TABLE countries ( 
COUNTRY_ID integer NOT NULL,
COUNTRY_NAME varchar(40) NOT NULL,
REGION_ID integer NOT NULL,
PRIMARY KEY (COUNTRY_ID,REGION_ID)
);

Now insert the value into the table countries :

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

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

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

Now try to insert another row with the same combination of values for country_id and region_id, and see what happened:

postgres=# INSERT INTO kan VALUES(501,'Italy',185);
ERROR:  duplicate key value violates unique constraint "countries_pkey"
DETAIL:  Key (country_id, region_id)=(501, 185) already exists.
هل كان المحتوى مفيد؟

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

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

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