473,396 Members | 1,913 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DB design opinions - Foreign Key usage

Intro:
Hi all, my name is Vams, and I am fairly new to postgresql and totally new to
mailing lists, so please bare with me. I have used hypersonic sql and mysql,
and now I am trying out postgresql... and so far, very impressive. GJ dev
team.

Problem:
For the location table, should I use two columns (ID PK, name) or just one
column (name PK)?
Should I FK the id column from the location table or should I FK the name
column?
Does postgresql keep a reference when using a FK (like pointers in C) or
does it actually make a copy and store it (like when C passes a copy in a
function call)? If its the later, is there a way to reference a value in
postgresql (and have it auto-dereference during a query)?
Finally, off topic, is there anyway (other than triggers) to keep the user
from inserting into the ID column when a sequence already takes care of it?
(I don't want someone adding id=5, name='blah' and when the sequence reaches
5, the insert will return error: can't have duplicates.)

Thank you all.
Situation:
Two tables - bookstore and location.

Bookstore has info on books and a column for location. So, I use a foreign
key for the location column. For Location, there are set of locations that
are described by _one_ word (downstairs, <city name>, warehouse, shop ...).
Now, the number of locations might change and so might their description word
(city name might become a warehouse name...). But each location name will
always be unique.

Usage:
The use for the tables is the end-user GUI will ask the user for book info,
but will provide a drop down list for the location input. The drop down list
will be filled by a query from table location.

I want it so that if the super DB manager ever changes one of the names in
the location table, the bookstore table will show the new value instead of
the old. Obviously, the GUI will auto update the drop down list.

PS, I gave my support stuff at the bottom so that people will read the
question first and if they can help, they can choose to read the rest of the
post. Again, thx.

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #1
1 1347
On Sat, May 22, 2004 at 12:42:53 -0400,
Vamsikrishna Mudrageda <gt*****@prism.gatech.edu> wrote:
Intro:
Hi all, my name is Vams, and I am fairly new to postgresql and totally new to
mailing lists, so please bare with me. I have used hypersonic sql and mysql,
and now I am trying out postgresql... and so far, very impressive. GJ dev
team.

Problem:
For the location table, should I use two columns (ID PK, name) or just one
column (name PK)?
It generally isn't a good idea to make a name the primary key unless you
are very sure the names aren't going to change.
Should I FK the id column from the location table or should I FK the name
column?
You should refer to the primary key when making foreign key references to
the table.
Does postgresql keep a reference when using a FK (like pointers in C) or
does it actually make a copy and store it (like when C passes a copy in a
function call)? If its the later, is there a way to reference a value in
postgresql (and have it auto-dereference during a query)?
The key is stored in the table. There are special triggers that make sure
that the key is actually in the referenced table.
Finally, off topic, is there anyway (other than triggers) to keep the user
from inserting into the ID column when a sequence already takes care of it?
(I don't want someone adding id=5, name='blah' and when the sequence reaches
5, the insert will return error: can't have duplicates.)


I think you can get this to happen with rules, but a before trigger is the
more normal way to modify rows being inserted. Note you will want to do
something for updates as well, since you can change the value of a primary
key in an update.

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael D | last post by:
What are the pros and cons of the following two design methods ? (1) Using foreign keys to form a composite primary key of a child tables -- as in Example. POOR MAN'S ERD PROVIDED FOR SUMMARY...
0
by: Krist | last post by:
Hi All, I have a database design question, pls give me some help.. I want to define tables for salesman's sales target commission . The commission could be given per EITHER sales amount of :...
1
by: Krist | last post by:
Hi All, There is some additional info I forget on this same topic I just posted. I have a database design question, pls give me some help.. I want to define tables for salesman's sales target...
2
by: PEK | last post by:
I have a database that looks something like this: Table: Creature Fields, CreatureId {PK}, ... Table: Bug Fields: BugId {PK}, CreatureId {FK}... Table: LadyBird Fields: LadyBirdId {PK},...
1
by: John | last post by:
Using these tables as an example, where the first key is the Primary Key and subsequent keys are Foreign Keys: tbl_A, PrimaryKey_A tbl_B, PrimaryKey_B, PrimaryKey_A tbl_C, PrimaryKey_C,...
11
by: John Fly | last post by:
I'm working on a large project(from scratch). The program is essentially a data file processor, the overall view is this: A data file is read in, validated and stored in a memory structure...
12
by: nyathancha | last post by:
Hi, I have a question regarding best practices in database design. In a relational database, is it wise/necessary to sometimes create tables that are not related to other tables through a...
3
by: vicky | last post by:
Hi All, Can u please suggest me some books for relational database design or database modelling(Knowledgeable yet simple) i.e. from which we could learn database relationships(one to many,many to...
4
by: gnewsacct1 | last post by:
I was going to design a simple movie database, but I'm just stuck in the very begining. It will contain the following information. Country, Movie, Plot, Actors So, I created tables...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.