473,770 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Foreign Key, Big deal? (SQL DB)

Jax
I'm making my first ever database for my program.
I understand the concept of one to many relationships but
fail to see the advantage of using a secondary key over a
primary one.
I have a lot of tables and link them together by one key
the CaseKey.
What is the disadvantages of this method? Why should I
include more detailed item keys (e.g an arraylist of
customer credit items would be stored in the
CustomerCreditI tem table surely it's better for them to
share a universal primary key rather then following the
key from case to customer and then to creditItem, that
needing three keys).

Can anyone provide me with excellent advise? (i'll accept
mediocre if it's all you've got though, and happily :))

Many thanks

jax
Nov 15 '05 #1
9 2400
Jax
Oh, hang on, I get it, its for databases where all of the
data isn't necessarily relative.
As my database is, it would be better to use just the
primary. Is that it?
-----Original Message-----
I'm making my first ever database for my program.
I understand the concept of one to many relationships but
fail to see the advantage of using a secondary key over a
primary one.
I have a lot of tables and link them together by one key
the CaseKey.
What is the disadvantages of this method? Why should I
include more detailed item keys (e.g an arraylist of
customer credit items would be stored in the
CustomerCredit Item table surely it's better for them to
share a universal primary key rather then following the
key from case to customer and then to creditItem, that
needing three keys).

Can anyone provide me with excellent advise? (i'll accept
mediocre if it's all you've got though, and happily :))

Many thanks

jax
.

Nov 15 '05 #2
I completely did not understand your question.
I have no idea what is the CaseKey and the design you have for you tables.

But anyway.

Foreign Key exists only to enforce the Database integrity.
So you will not end up with OrderItems without Order itself.

PrinaryKey and ForeignKey are not interchangeable .
You can not use one over another.

PrimaryKey exists only to Identify the record in the table.
George.

"Jax" <an*******@disc ussions.microso ft.com> wrote in message
news:05******** *************** *****@phx.gbl.. .
I'm making my first ever database for my program.
I understand the concept of one to many relationships but
fail to see the advantage of using a secondary key over a
primary one.
I have a lot of tables and link them together by one key
the CaseKey.
What is the disadvantages of this method? Why should I
include more detailed item keys (e.g an arraylist of
customer credit items would be stored in the
CustomerCreditI tem table surely it's better for them to
share a universal primary key rather then following the
key from case to customer and then to creditItem, that
needing three keys).

Can anyone provide me with excellent advise? (i'll accept
mediocre if it's all you've got though, and happily :))

Many thanks

jax

Nov 15 '05 #3
I think you may be confused. A primary key is the thing that uniquely
identifies a record in a table. There is no "secondary key."

A foreign key is when you use that primary key in another table to reference
the other table. An example:

Table: Department
int DepartmentID <--------- Primary key
varchar(30) DepartmentName

Table: Employee
int EmployeeID <------------Primary key
int DepartmentID <------------Foreign key
varchar(30) EmployeeName

In this example, the DepartmentID is a primary key in the Department table,
but a foreign key in the Employee table. It is used to identify which
department an employee belongs to.

Pete

"Jax" <an*******@disc ussions.microso ft.com> wrote in message
news:05******** *************** *****@phx.gbl.. .
I'm making my first ever database for my program.
I understand the concept of one to many relationships but
fail to see the advantage of using a secondary key over a
primary one.
I have a lot of tables and link them together by one key
the CaseKey.
What is the disadvantages of this method? Why should I
include more detailed item keys (e.g an arraylist of
customer credit items would be stored in the
CustomerCreditI tem table surely it's better for them to
share a universal primary key rather then following the
key from case to customer and then to creditItem, that
needing three keys).

Can anyone provide me with excellent advise? (i'll accept
mediocre if it's all you've got though, and happily :))

Many thanks

jax

Nov 15 '05 #4
Jax
I understand, the reason I was asking is that I wasn't
using a foreign key (as all the tables are completely
relative) and I felt as though I was missing out on
functionality that would help.
Many thanks

jax
-----Original Message-----
I think you may be confused. A primary key is the thing that uniquelyidentifies a record in a table. There is no "secondary key."
A foreign key is when you use that primary key in another table to referencethe other table. An example:

Table: Department
int DepartmentID <--------- Primary key
varchar(30) DepartmentName

Table: Employee
int EmployeeID <------------Primary key
int DepartmentID <------------Foreign key
varchar(30) EmployeeName

In this example, the DepartmentID is a primary key in the Department table,but a foreign key in the Employee table. It is used to identify whichdepartment an employee belongs to.

Pete

"Jax" <an*******@disc ussions.microso ft.com> wrote in messagenews:05******* *************** ******@phx.gbl. ..
I'm making my first ever database for my program.
I understand the concept of one to many relationships but fail to see the advantage of using a secondary key over a primary one.
I have a lot of tables and link them together by one key
the CaseKey.
What is the disadvantages of this method? Why should I
include more detailed item keys (e.g an arraylist of
customer credit items would be stored in the
CustomerCreditI tem table surely it's better for them to
share a universal primary key rather then following the
key from case to customer and then to creditItem, that
needing three keys).

Can anyone provide me with excellent advise? (i'll accept mediocre if it's all you've got though, and happily :))

Many thanks

jax

.

Nov 15 '05 #5
Honestly, and I mean no offense by this, but I don't think you do get it. I
hope my other message clears it up a bit. Database design is not a simple
subject. There can be a great deal of complexity in it and it's very
important to do it right as doing it wrong can have a number of negative
consequences in the code. Everything from poor performance to loss of data
integrity.

Do a search on google for the following words: database normalization
tutorial

There are plenty of links that can help you understand database
normalization. Once you've read through some, I think it will start to make
more sense to you.

This one might be a good start:

http://www.vbmysql.com/articles/normalize.html

It's very simple and explains the first 3 normal forms. That's what you
really need to know.

Pete

--

http://www.petedavis.net
"Jax" <an*******@disc ussions.microso ft.com> wrote in message
news:05******** *************** *****@phx.gbl.. .
Oh, hang on, I get it, its for databases where all of the
data isn't necessarily relative.
As my database is, it would be better to use just the
primary. Is that it?
-----Original Message-----
I'm making my first ever database for my program.
I understand the concept of one to many relationships but
fail to see the advantage of using a secondary key over a
primary one.
I have a lot of tables and link them together by one key
the CaseKey.
What is the disadvantages of this method? Why should I
include more detailed item keys (e.g an arraylist of
customer credit items would be stored in the
CustomerCredit Item table surely it's better for them to
share a universal primary key rather then following the
key from case to customer and then to creditItem, that
needing three keys).

Can anyone provide me with excellent advise? (i'll accept
mediocre if it's all you've got though, and happily :))

Many thanks

jax
.

Nov 15 '05 #6
If you have any tables that are related, then you MUST be using foreign
keys. What you said in your last message makes no sense to me. Saying the
tables are "completely relative" doesn't mean anything to me.

Pete

--

http://www.petedavis.net
"Jax" <an*******@disc ussions.microso ft.com> wrote in message
news:0b******** *************** *****@phx.gbl.. .
I understand, the reason I was asking is that I wasn't
using a foreign key (as all the tables are completely
relative) and I felt as though I was missing out on
functionality that would help.
Many thanks

jax
-----Original Message-----
I think you may be confused. A primary key is the thing

that uniquely
identifies a record in a table. There is no "secondary

key."

A foreign key is when you use that primary key in another

table to reference
the other table. An example:

Table: Department
int DepartmentID <--------- Primary key
varchar(30) DepartmentName

Table: Employee
int EmployeeID <------------Primary key
int DepartmentID <------------Foreign key
varchar(30) EmployeeName

In this example, the DepartmentID is a primary key in the

Department table,
but a foreign key in the Employee table. It is used to

identify which
department an employee belongs to.

Pete

"Jax" <an*******@disc ussions.microso ft.com> wrote in

message
news:05******* *************** ******@phx.gbl. ..
I'm making my first ever database for my program.
I understand the concept of one to many relationships but fail to see the advantage of using a secondary key over a primary one.
I have a lot of tables and link them together by one key
the CaseKey.
What is the disadvantages of this method? Why should I
include more detailed item keys (e.g an arraylist of
customer credit items would be stored in the
CustomerCreditI tem table surely it's better for them to
share a universal primary key rather then following the
key from case to customer and then to creditItem, that
needing three keys).

Can anyone provide me with excellent advise? (i'll accept mediocre if it's all you've got though, and happily :))

Many thanks

jax

.

Nov 15 '05 #7
> I understand, the reason I was asking is that I wasn't
using a foreign key (as all the tables are completely
relative) and I felt as though I was missing out on
functionality that would help.


Just from what you wrote above I can tell you need to study relational
database principles. A table is not related unless keys are involed. You
cannot have a related table without a foreign key.

--
gabriel
Nov 15 '05 #8
Hi Jax,

There's no room here for a full discussion of Third Normal Form, or
Relational design.

However, the point of a foreign key is to allow two different items to be
related, yet different.

One of the values of this is that you could have a one-to-many relationship
(100 people, all work for the same company. You'd need employee information
for each person, but the employer information should only be entered once).
If you have a Universal Key, then which key would you use for the company
record? Joe's key? Mary's key? You see the point?

And how would you create a many-to-many relationship if you had one record
in table A that refers to many records in table B, but you also have one
record in Table B referring to many records in Table A. Without unique
primary keys in each table, (and in this case, a third table), you can't do
it with relational databases.

Note: in relational theory, if you use a foreign key (which is the primary
key on another table) as the primary key, you have created a
one-to-zero-or-one relationship. In other words, one record in the main
table could link to one record in the dependent table or not. This is
sometimes used as a way to reduce the space consumed in a database for large
numbers of empty columns by placing the "sparse" columns in a dependent
table of this kind. It adds a good bit of useless complexity, and is often
not worth doing.

Normally, if you have the same key being used as the primary key in two
tables, you'd be better off moving all the fields to a single table.

I have no idea if this is helping you.

--- Nick

"Jax" <an*******@disc ussions.microso ft.com> wrote in message
news:05******** *************** *****@phx.gbl.. .
I'm making my first ever database for my program.
I understand the concept of one to many relationships but
fail to see the advantage of using a secondary key over a
primary one.
I have a lot of tables and link them together by one key
the CaseKey.
What is the disadvantages of this method? Why should I
include more detailed item keys (e.g an arraylist of
customer credit items would be stored in the
CustomerCreditI tem table surely it's better for them to
share a universal primary key rather then following the
key from case to customer and then to creditItem, that
needing three keys).

Can anyone provide me with excellent advise? (i'll accept
mediocre if it's all you've got though, and happily :))

Many thanks

jax

Nov 15 '05 #9
Jax
Well I have no problem with you guys telling me I dont understand, because I probably dont
My understanding of whole primary foreign thing is way off, (but thinking back to previous books i've read on the subject i'm finally starting to remember)
Actually thinking about it now (shoulda done that long ago) I do need other keys then just the one i've been talking abou
Nov 15 '05 #10

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

Similar topics

2
3915
by: geoff | last post by:
The table creation script(at the end of this post) works fine on 4.0.1-alpha-win, but the foreign key constraints fail on 4.0.15-win. I am starting the server with the same command for both versions: mysqld-max-nt --console --transaction-isolation=SERIALIZABLE In 4.0.15-win I can extract the following error after I run the table creation script: ERROR 1005: Can't create table '.\ibdata\#sql-a14_3.frm'
1
8905
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB automatically deletes also all those rows in the child table whose foreign key values are equal to the referenced key value in the parent row. However:
10
42422
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed to be optional, it's very definition is it's a necessary link to the parent table and part of the definition. If it's optional it shouldn't be part of the definition of a table and should be in a linking table instead. Comments?
1
5279
by: Jason Madison | last post by:
We sometimes get very large databases that we want to cut down to use for testing. The information is all related to a central accounts table. The way I thought of doing this is to grab all the foreign constraints and turn them into cascade delete constraints, then delete as many accounts as I want. After this I will restore the constraints back to their original state.
3
39979
by: . . | last post by:
Hi I am creating new SQL Server Tables using SQL Server 2005. I have set primary key to the tables .But I do not know how to assign Foreign key to the tables .I need to do some joins later and that is why I have to put Foreign key to the table . The Primary key is visible and can be assigned easily .But How do I assign foreign key . Thanks
1
3105
by: James E | last post by:
I have a question about best practices of how to deal with lookup data from my C# apps. On a couple of occasions I have come across a problem where I have to automate inserting a record into a table that has a foreign key constraint that is linked to a lookup table. E.g. Take the following database structure: SQL-Server Database: Table 1:
1
11011
by: apax999 | last post by:
Kinda new to SQL, using SQL Server 2005. I have some foreign keys in a couple of tables. I need to drop these tables, but can't since I'll get the error: Msg 3726, Level 16, State 1, Line 1 Could not drop object 'Client' because it is referenced by a FOREIGN KEY constraint.
3
45329
by: weird0 | last post by:
I have two tables accounts and ATM and i am trying to insert a tuple in ATM with accountId as foreign key. But even this simple work,I encounter the following error: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ATM_Accounts". The conflict occurred in database "G:\DOCUMENTS AND SETTINGS\ADMINISTRATOR\DESKTOP\VERSION2\APP_DATA\BANKINGDB.MDF", table "dbo.Accounts", column 'acc_Id'. please sugggest how can i fix...
6
5913
by: bobdurie | last post by:
If i create a simple table with a foreign key constraint, does it create an implicit index on that given ID? I've been told this is done in some databases, but i need to know for sure if SQL Server does it. Has anyone heard of this before, on any other databses perhaps? Heres an example of how the foreign key constraint is being added: ALTER TABLE . WITH CHECK ADD CONSTRAINT FOREIGN KEY() REFERENCES . ()
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10259
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.