什么是自引用表 [英] What is a self referencing table

查看:102
本文介绍了什么是自引用表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们



我有一个关于自我引用表的问题。首先是什么是自引用表



(请你举个例子)第二个我应该在自引用表中有两个主键吗?第三,我如何将下拉列表绑定到自引用表中?

例如城市下降和省下降?请帮助我。

hi friends

i have a question about self referencing tables. first what is a self referencing tables

(Could you please give an example ) and second should i have two primary keys in a self referencing table? third how could i bind drop down list into a self referencing table?
for example a drop down for city and a drop down for province ? pleas help me .

推荐答案

首先,你永远不会有两个主键: http://en.wikipedia.org/wiki/Unique_key [ ^ ]。



请勿将此声明与可以拥有复合键的情况混淆,可以用作主键,但它仍然必须是唯一的: http://en.wikipedia.org/wiki/Compound_key [ ^ ]。



在使用唯一复合键作为主键时,每个表仍然只有一个主键,即使这样的键使用多个表列。



至于自我引用表,我认为这不是任何特殊术语或概念。自引用表的存在是关系模型的一个微不足道的结果。我不知道你想知道他们是什么以及为什么。自引用没有任何问题。我想,用你的大脑来分析自我引用的情况会更好。您可以在此处查看: http://bit.ly/1u5magb [ ^ ]。



-SA
First of all, you can never have two primary keys: http://en.wikipedia.org/wiki/Unique_key[^].

Don't confuse this statement with the situation when you can have a compound key, which can be uses as a primary key, but it still has to be unique: http://en.wikipedia.org/wiki/Compound_key[^].

In you use a unique compound key as a primary key, you still have only one primary key per table, even though such key uses more than one table columns.

As to "self-referencing tables", I don't think this is any special term or a notion. The existence of self-referencing tables is a trivial consequence of relational model. I have no idea what do you want to know about them and why. There is nothing wrong with self-referencing. I guess, it would be better to use your brain to analyze self-referencing situations. You can look here: http://bit.ly/1u5magb[^].

—SA


引用:

例如城市下拉和省下拉?

for example a drop down for city and a drop down for province ?

这很简单。两张桌子就是这样。



1. 城市 - CityId,CityName

2. - ProvinceId,FK_CityId,省名称



City 绑定 DropDownList ,直接从 City 表绑定。选择某些 City 时,应绑定 DropDownList 。因此,您需要在 City DropDownList SelectedIndexChanged 事件中进行绑定。你也可以去 Ajax CascadingDropDowList 。参考 - 使用asp.net 使用数据库的Ajax级联下拉列表示例 [ ^ ]

This is very simple. Two tables that's it.

1. City - CityId, CityName
2. Province - ProvinceId, FK_CityId, ProvinceName

While binding DropDownList for City, directly bind from City Table. The DropDownList for Province should be bound when some City is selected. So, you need to bind inside the SelectedIndexChanged Event of City DropDownList. You can also go for Ajax CascadingDropDowList. Refer - Ajax Cascading Dropdownlist Sample with database using asp.net[^]


自引用表是一个表,其中的字段被定义为同一个表的主键的外键。



A self referencing table is a table which has a field which is defined as foreign key to the same table's primary key.

CREATE TABLE DBREGISTRY
(
  ID		INTEGER,
  PARENT        INTEGER,    // PARENT references ID of table DBREGISTRY 
  DESCRIPTION   CHAR(30),
  INTVALUE      INTEGER,
  CONSTRAINT    PK_DBREGISTRY PRIMARY KEY (ID),
  CONSTRAINT	FK_DBREGISTRY_PARENT FOREIGN KEY (PARENT) REFERENCES DBREGISTRY (ID)
)





这种表允许以分层方式保存数据行。





This kind of table does allow to save data rows in a hierarchical way.

ID    PARENET     DESCRIPTION
1     1           "ROOT"
2     1           "FIRST LEVEL ITEM A"
3     2           "ITEM 1 OF FIRST LEVEL ITEM A"
4     1           "FIRST LEVEL ITEM B"
5     4           "ITEM 1 OF FIRST LEVEL ITEM B"

For imaging the hirarchy:

"ROOT"
     "FIRST LEVEL ITEM A"
          "ITEM 1 OF FIRST LEVEL ITEM A"
     "FIRST LEVEL ITEM B"
          "ITEM 1 OF FIRST LEVEL ITEM B"





p有几件事(其中一些是陷阱)在实践中注意:

a。)对于外键父类,定义ON DELETE CASCADE也是有意义的

b。)导入这样的表通常不是简单的事情,因为物品和身份证可以完全随机出现。





Eg :Windows注册表似乎是使用类似的结构。



There are several things (some of them are pitfalls) to pay attention in praxis:
a.) For foreign key parent it makes sense to define also "ON DELETE CASCADE"
b.) Importing such a table is usually not an easy thing, this because items and there ID can be arise completely random.


E.g. : Windows registry seems to be made with a similar structure.


这篇关于什么是自引用表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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