Write a postgre SQL statement to insert rows into the table countries in which the value of country_id column will be unique and auto incremented

  • Postgre SQL

 Write a  postgre SQL statement to insert rows into the table countries in which the value of country_id column will be unique and auto incremented.

الأجوبة

Here is the code to create a sample table countries:

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

Now insert one record into the table:

INSERT INTO countries(COUNTRY_NAME,REGION_ID) VALUES('India',185);

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

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

Now insert another record into the table :

INSERT INTO countries(COUNTRY_NAME,REGION_ID) VALUES('Japan',102);

Now see the value of the key field incremented automatically :

postgres=# SELECT * FROM countries;
 country_id | country_name | region_id
------------+--------------+-----------
          1 | India        |       185
          2 | Japan        |       102
(2 rows)
هل كان المحتوى مفيد؟

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

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

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