Primary & Foreign Keys in pgAdmin
Solution 1:
Yes, there is a way to add Primary & Foreign Keys in pgAdmin.
Tested in pgAdmin III Ver.1.16.1 (Windows 7)
- Select the table you want
- Ctrl+Alt+Enter or right-click / Properties
- Select "Constraints" tab
- At the left-bottom side of the form you will see the option "Primary Key"
- Click add
- Select "Columns" tab
- Select the column you want as a key
- Click add
And you are all set.
You can fill more things if you want, but now you know how to get there.
Solution 2:
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).
Solution 3:
In Pgadmin3,
-
Go to table which you want to add the PK or FK and right click and choose properties.
-
Go to constraints tab.
-
Choose Primary Key or Foreign Key in drop down list which beside of Add button.
-
And than click on add button.
-
Go to columns tab.
-
Choose the column name in drop down list ,which you want to add .
-
Click add button.
-
Click Ok button.
Hope it will helpful for you !