外键,大不了? (SQL DB) [英] Foreign Key, Big deal? (SQL DB)

查看:58
本文介绍了外键,大不了? (SQL DB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的程序创建我的第一个数据库。

我理解一对多关系的概念但是

未能看到使用a的优势辅助钥匙超过

主要钥匙。

我有很多桌子并用一把钥匙将它们连在一起

CaseKey。

这种方法的缺点是什么?我为什么要在
包含更详细的商品密钥(例如,

的arraylist客户信用项目将存储在

CustomerCreditItem表中肯定是'对他们来说更好的是

共享一个通用的主键而不是跟随

键从一个案例到另一个客户然后再到creditItem,那个

需要三把钥匙)。


有人能为我提供优质的建议吗? (我会接受

平庸,如果它是你所有的,并且很开心:))


非常感谢


jax

解决方案

哦,坚持下去,我明白了,它的数据库所有的

数据不一定是相对的。

作为我的数据库,最好只使用

主数据。是吗?

-----原始消息-----
我正在为我的程序制作我的第一个数据库。
我了解一对多关系的概念,但是没有看到使用辅助密钥而不是主要密钥的优势。
我有很多表,并通过一个键将它们链接在一起< CaseKey。
这种方法的缺点是什么?为什么我要包含更详细的项目键(例如,
客户信用项目的arraylist将存储在
CustomerCreditItem表中,当然它们更适合他们分享一个通用主键,而不是从案例到客户,然后到creditItem,
需要三个键)。

任何人都可以给我提供优秀的建议吗? (我会接受
平庸,如果它只是你所拥有的,并且很开心:))

非常感谢

jax
.



我完全不明白你的问题。

我不知道什么是CaseKey和你的设计有你的桌子。


但无论如何。


外键仅用于强制执行数据库完整性。

所以你不会在没有Order本身的情况下结束OrderItem。


PrinaryKey和ForeignKey不可互换。

你不能使用另一个。


PrimaryKey仅用于识别表中的记录。

George。


Jax <一个******* @ discussions.microsoft.com>在留言中写道

news:05 **************************** @ phx.gbl ... < blockquote class =post_quotes>我正在为我的程序创建我的第一个数据库。
我理解一对多关系的概念但是没有看到使用二级密钥而不是
主要的一个。
我有很多桌子并用一把钥匙将它们连在一起
CaseKey。
这种方法的缺点是什么?为什么我要包含更详细的项目键(例如,
客户信用项目的arraylist将存储在
CustomerCreditItem表中,当然它们更适合他们分享一个通用主键,而不是从案例到客户,然后到creditItem,
需要三个键)。

任何人都可以给我提供优秀的建议吗? (我会接受
平庸,如果它是你所有的,并且很开心:))

非常感谢

jax



我想你可能会感到困惑。主键是唯一的
标识表中的记录。没有辅助密钥。


外键是当你在另一个表中使用该主键来引用另一个表时的
。例如:


表:部门

int DepartmentID< ---------主键

varchar (30)DepartmentName


表:员工

int EmployeeID< ------------主键

int DepartmentID< ------------外键

varchar(30)员工名号


在这个例子中, DepartmentID是Department表中的主键,

但是Employee表中的外键。它用于识别员工属于哪个b $ b部门。


Pete


Jax <一个******* @ discussions.microsoft.com>在留言中写道

news:05 **************************** @ phx.gbl ... < blockquote class =post_quotes>我正在为我的程序创建我的第一个数据库。
我理解一对多关系的概念但是没有看到使用二级密钥而不是
主要的一个。
我有很多桌子并用一把钥匙将它们连在一起
CaseKey。
这种方法的缺点是什么?为什么我要包含更详细的项目键(例如,
客户信用项目的arraylist将存储在
CustomerCreditItem表中,当然它们更适合他们分享一个通用主键,而不是从案例到客户,然后到creditItem,
需要三个键)。

任何人都可以给我提供优秀的建议吗? (我会接受
平庸,如果它是你所有的,并且很开心:))

非常感谢

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
CustomerCreditItem 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

解决方案

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
CustomerCreditItem 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
.



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*******@discussions.microsoft.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
CustomerCreditItem 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



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*******@discussions.microsoft.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
CustomerCreditItem 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



这篇关于外键,大不了? (SQL DB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆