Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primary & Foreign Keys in pgAdmin

I was wondering can some give me an explanation on how to assign primary and foreign keys in pgAdmin?

I can't find any information online.

For example...I've got a Student table with all their details (address, d.o.b. and etc.). I'm going to add a student_number to the table and make it a primary key.

I just want to know how do I do that using pgAdmin? And if you may be kind to explain give me further information on using Primary Keys in postgreSQL (and pgAdmin). The same case with the foreign keys.

like image 987
Mr Teeth Avatar asked Mar 15 '12 19:03

Mr Teeth


People also ask

What grade is the primary?

The term primary grades describes the Kindergarten to Grade 3 years in elementary school; it is not a separate or additional curriculum. During this phase of schooling, children experience rapid growth and development.

What are primary subjects?

Primary school students will be introduced to subject-based learning where they will learn subjects such as languages, mathematics, science, art, music and social studies.

What primary level means?

1 : a school usually including the first three grades of elementary school but sometimes also including kindergarten.


2 Answers

Yes, there is a way to add Primary & Foreign Keys in pgAdmin.

Tested in pgAdmin III Ver.1.16.1 (Windows 7)

  1. Select the table you want
  2. Ctrl+Alt+Enter or right-click / Properties
  3. Select "Constraints" tab
  4. At the left-bottom side of the form you will see the option "Primary Key"
  5. Click add
  6. Select "Columns" tab
  7. Select the column you want as a key
  8. Click add

And you are all set.

You can fill more things if you want, but now you know how to get there.

like image 127
Victor Barrantes Avatar answered Sep 20 '22 00:09

Victor Barrantes


There is no option in pgAdmin to add a column to an existing table and make it the primary key at the same time, because this is hardly possible.

A primary key column needs to hold unique non-null values. Upon adding a column to an existing table, it holds NULL values. So you have to enter unique values before you can add a UNIQUE or PRIMARY KEY constraint.

There is an exception to that rule, though: If you add a serial column, unique values are inserted automatically. In this case, you can also define it PRIMARY KEY right away:

ALTER TABLE student ADD COLUMN student_number serial PRIMARY KEY; 

This works in PostgreSQL 9.1. I am not sure it does in older versions, too.

pgAdmin does not incorporate this special case for serial columns in the "New column..." dialog at this time (version 1.14).

like image 32
Erwin Brandstetter Avatar answered Sep 21 '22 00:09

Erwin Brandstetter