By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,689 Members | 1,915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,689 IT Pros & Developers. It's quick & easy.

Question about foreign key replication

P: 2
Hi everyone. I'm a beginner at MS SQL Server. This question is probably something simple but I just can't figure it out.

For the sake of simplicity, let's say I have two tables. Person_List and Position_List. The Person_List table would look like this:

Person_Name...Person_ID...Other_Column
Aaron...................1.................Blah
Jack.....................2.................Blah
Ann......................3.................Blah
etc . . .

The Person_ID is an integer identity/primary key that auto-increments as each new name is added to the table.

Now let's say the Position_List table would look like this:

Company_Postion...PID
Engineer..................5
Technician...............2
Technician...............4
etc . . .

This time the PID is a foreign key that connects to the Person_ID in the first table.

Here's my problem: Let's say that I wanted to manually add a new record to the Position_List table to record the information that Rob is an engineer. I'm not going to remember that his Person_ID/PID is 3. So I'd end up finding myself looking into the Person_List table to find his Person_ID. That takes time and I'd like to automate the process. What I'd really like the Position_List table to look like is this:

Company_Postion...Person_Name...PID
Engineer.................Frank.................5
Technician..............Jack...................2
Technician..............Jill...................... 4
etc . . .

I want it to be so that after the Person_Name field is entered, that field will automatically be compared to the Person_Name fields in the first table and the Person_ID will be fetched and automatically entered into column 3. You might think it's bad database design to store the exact same field in two different tables and it probably is, but I'd like it that way to improve readability and make manual data entry easier. I guess it would also be possible to just make Person_Name the primary key instead of Person_ID, but the real tables that I'm working with are so big that I need the query performance boost that you get from using integer keys.

Can this be done with triggers or something similar? It seems like this sort of thing should be pretty easy but I'm stumped. Anyone got an idea?

Thanks!
Mar 7 '08 #1
Share this Question
Share on Google+
3 Replies


ck9663
Expert 2.5K+
P: 2,878
I stop on the part that you want to use the Name as the search key and even considering it as the primary key. Names are not always recommended to be the primary key. Because two people can have exactly the same name. Your table looks fine to me, and the Person ID is still the best key for it.

-- CK
Mar 7 '08 #2

P: 2
CK, thanks for the response.

I know that Person_ID is the best key to use, and that's why I'm trying to use it. I just want to use it more easily. The first table already knows which ID belongs to which person, so I'm wondering if it's possible to set a trigger or something so that when I type in the name of the actual person in the second table, a quick query or something is run that fetches that person's ID and automatically enters it for me.

Basically, I want to make it so that whenever a new record (or a large chunk of records all at once) are added to the second table, they can be entered in such a way that I don't have to even deal with what the Person_ID and it's just taken care of for me in the background. Is something like this even possible or will I just have to take care of stuff like that on the front end data entry forms?

Anyways, I've given up on it for the weekend. Have a good one yourself.
Mar 8 '08 #3

ck9663
Expert 2.5K+
P: 2,878
CK, thanks for the response.

I know that Person_ID is the best key to use, and that's why I'm trying to use it. I just want to use it more easily. The first table already knows which ID belongs to which person, so I'm wondering if it's possible to set a trigger or something so that when I type in the name of the actual person in the second table, a quick query or something is run that fetches that person's ID and automatically enters it for me.

Basically, I want to make it so that whenever a new record (or a large chunk of records all at once) are added to the second table, they can be entered in such a way that I don't have to even deal with what the Person_ID and it's just taken care of for me in the background. Is something like this even possible or will I just have to take care of stuff like that on the front end data entry forms?

Anyways, I've given up on it for the weekend. Have a good one yourself.

For the first part, you might be able to handle that from your front-end apps.

For the second part, you still can do that as long as the name is unique.

-- CK
Mar 8 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.