简体   繁体   English

从数据库中指定关系可以得到什么?

[英]What do I gain from specifying relationships in my database?

I am building a project in MS Access 2010. I have previous experience in Oracle. 我正在MS Access 2010中建立一个项目。我以前在Oracle中有经验。 I am reading about MS Access and keep seeing references to table relationships. 我正在阅读有关MS Access的信息,并不断看到对表关系的引用。 It looks like a convenient way to assist the average person in data entry and validation and for query building, but I write queries exclusively in SQL mode and enforce data entry for users with forms that have their own validation rules. 它看起来像是一种协助普通人进行数据输入和验证以及进行查询构建的便捷方法,但是我仅以SQL模式编写查询,并为具有自己验证规则的表单的用户强制执行数据输入。

Is it really necessary to enforce relationships? 加强关系真的必要吗? It doesn't seem like it really gains me anything at an advanced level, and might actually cause problems for me or someone else who eventually takes over maintenance from me later. 看起来它并没有真正使我获得任何高级帮助,并且实际上可能给我或最终导致我接管维护工作的其他人带来问题。 I've never used them before and I'm not really seeing a benefit to starting now. 我以前从未使用过它们,现在也没有真正受益。 Can anyone shed some light on that? 有人可以阐明这一点吗?

You say you have previous experience of Oracle. 您说您曾经有过Oracle的经验。 Did you never define Foreign Key constraints in Oracle? 您是否从未在Oracle中定义外键约束? If you did, then that is what you are doing when you define relationships in Access. 如果这样做了,那么这就是在Access中定义关系时要做的事情。 You can use it for enforcing referential integrity (not allowing you to delete a parent record if child records still exist) or, if you use the cascade delete option, for automatically deleting child records if you delete a parent record. 您可以使用它来加强参照完整性(如果子记录仍然存在,则不允许删除父记录),或者,如果使用了cascade delete选项,则可以在删除父记录时自动删除子记录。 It's a useful backup to cover coding errors where you might have forgotten about possible child records that would otherwise be orphaned if you did not have the relationship (FK) defined. 这是一个有用的备份,用于覆盖编码错误,在这些错误中,您可能忘记了可能的子记录,如果未定义关系(FK),这些子记录将被孤立。

From a person just querying data, then the relationships are not that important. 对于仅查询数据的人,关系并不是那么重要。 However from an application point of view, they are VERY helpful if not outright important. 但是,从应用程序的角度来看,即使不是很重要,它们也非常有用。

For example, you might have a customer's table, and then say an orders table. 例如,您可能有一个客户表,然后说一个订单表。 The business rule is that you can't create an order unless you first have a customer. 业务规则是,除非先拥有一个客户,否则您无法创建订单。 So if you freely write some SQL to add an order without a customer, your update/insert query will NOT work. 因此,如果您自由编写一些SQL以添加没有客户的订单,则您的更新/插入查询将不起作用。 And if you need to delete a customer, then all orders for that customer can/will automatic delete for you without having to write a complex delete SQL statement. 而且,如果您需要删除客户,那么该客户的所有订单都可以/将自动为您删除,而不必编写复杂的delete SQL语句。 You might for example want to delete all customers older than 5 or 10 years (so they are inactive). 例如,您可能想删除所有5或10年以上的客户(因此他们是不活跃的)。 When you delete those customers, then you want all orders also deleted. 当您删除这些客户时,您希望所有订单也都被删除。 (This is a VERY difficult query to write if you have to delete the child records for each customer. with enforced relations, then all child records will automatic delete for you (enforced cascade delete)). (如果您必须删除具有强制关系的每个客户的子记录,这是一个很难编写的查询,那么所有子记录将自动为您删除(强制级联删除))。

And it also important from a reporting point of view. 从报告的角度来看,这也很重要。 If you write a query to display all customers this month and their billing totals then you get one total result. 如果您编写查询以显示本月的所有客户及其帐单总额,那么您将获得一个总计结果。 However if you decide that you do NOT want to display/include customers, you might hit just the orders table and get a total amount that way. 但是,如果您决定不希望显示/包含客户,则可以仅点击订单表并以这种方式获得总额。 The problem is without RI, then you might (by accident or even just some user launching the orders form) have entered order information (with a total amount) but NO customer. 问题出在没有RI的情况下,那么您可能(偶然或什至只是一些启动订单的用户)已经输入了订单信息(总金额),但没有客户。

Now what happens is when you run the two different reports/quires, you find the total is DIFFERENT! 现在发生的事情是,当您运行两个不同的报告/查询时,您发现总数是不同的! In a complex application as to “why” the two reports are different can take days, or with lots of data even a week to figure out why two reports on monthly sales do NOT agree with each other. 在一个复杂的应用程序中,“为什么”两个报告不同可能需要几天的时间,或者甚至需要一周才能获得大量数据,以弄清为什么两个月度销售报告彼此不一致。 If you enforce the business rule that no orders can be entered into the system UNLESS they have a customer, then you eliminate such errors in reporting. 如果您执行业务规则,除非他们有客户,否则无法将订单输入系统,那么您就可以消除报告中的此类错误。 You can “say” that you are perfect user of SQL, but with lots of code, lots of forms for data entry, how can you EVER be sure that orders are NEVER entered without a customer. 您可以“说”自己是SQL的完美用户,但是拥有大量的代码,大量的数据输入形式,如何确保没有客户输入订单。 The user during data entry may forget to enter the customer in that order form. 数据输入期间的用户可能会忘记以该订单形式输入客户。 And even if you write code in that order form to ENSURE that customer must be selected, maybe YOU during the writing of some SQL by accident insert an order record into the system without a customer. 而且,即使您以该订单的形式编写代码以确保必须选择客户,也许您在编写某些SQL的过程中偶然将订单记录插入到没有客户的系统中。 However your monthly customer total report query “assumes” that you have a customer record that you THEN join in the order totals data. 但是,您的每月客户总计报告查询“假设”您有一个客户记录,然后将其加入订单总计数据中。

However some reports must just run on the orders data (a monthly summary total does not need to include customers). 但是,某些报告必须仅基于订单数据运行(每月汇总总数不需要包括客户)。 The problem now is somewhere in the system you have an order record with total data that does not have a customer. 现在问题出在系统中某处,您的订单记录中包含没有客户的总数据。 The result is different reports and quires on sales total now don't agree. 结果是不同的报表,现在对销售总额的询问不同意。 This is an outright nightmare. 这是一场彻头彻尾的噩梦。

So some bug or error in the application code might occur and result in what is supposed to be relational data now having “orphaned” records. 因此,应用程序代码中可能会出现一些错误或错误,导致现在应该具有“孤立”记录的关系数据。 Perhaps your business rules allow entering of orders without a customer assigned, but then your monthly sales report will have to show that fact, or any query that hits the orders table and does not include customers will have to “check” for the possibility in those queries that no customer record yet exits. 也许您的业务规则允许在不分配客户的情况下输入订单,但是您的月度销售报告将必须显示该事实,或者任何命中订单表但不包括客户的查询都必须“检查”那些可能性查询尚无客户记录存在。

The above is only a SIMPLE scratching of the surface of the GAZILLION issues that crop up. 上面只是刮擦出现的GAZILLION问题的表面而已。 So while you might be just creating simple quires on the data, the problem is that data correctly related in the system? 因此,尽管您可能只是在数据上创建简单的查询,但问题在于系统中数据是否正确关联? The old saying about garbage in = garbage out rings true here. 关于垃圾进来=垃圾出的老话在这里是正确的。

At the end of the day when you're SQL quires pulls data with MULTIPLE tables, then you HAVE to make assumptions about that data and its relational integrity (RI). 最终,当您使用SQL查询具有MULTIPLE表的拉取数据时,您就必须对该数据及其关系完整性(RI)做出假设。 So when you write that query to display customers and their order totals, you ASSUME and drop in the customers table, and then relational join in the orders table. 因此,当您编写该查询以显示客户及其订单总数时,您将ASSUME拖放到客户表中,然后在订单表中进行关系联接。 However if orders exist without a customer record, then your query not going to produce the correct values. 但是,如果存在没有客户记录的订单,那么您的查询将不会产生正确的值。 And worse a report that hits the orders table will now produce different results. 更糟糕的是,命中订单表的报告现在将产生不同的结果。

If you enforce RI then no matter what, you cannot enter an order by accident or force without FIRST having created a customer record. 如果您强制执行RI,那么无论如何,在没有FIRST创建客户记录之前,您都无法偶然或强制输入订单。 If you don't enforce such rules, then your data will produce incorrect results. 如果您不执行此类规则,那么您的数据将产生错误的结果。

And a typical complex application will have 40 or 70 related tables. 一个典型的复杂应用程序将具有40或70个相关表。 And EVERY ONE of those tables is going to have assumptions made as to if parent (or child) record are “assumed” to have been created correctly based on your set of business assumptions. 而且,这些表中的每个表都将基于您的一组业务假设,对是否假设“假设”已正确创建了父(或子)记录进行了假设。

You might have a tour booking system. 您可能有一个旅游预订系统。 Customers might phone up, put down a deposit but NOT yet be booked to a particular tour. 客户可能会打电话,付了定金,但还没有被预订参加特定的旅行。 If you allow this setup, then your query on customers this month and their booked tour will have to take this into account. 如果您允许此设置,则本月对客户的查询及其预订的游览必须考虑到这一点。 However maybe the business rules are that any customer in the system that puts money down MUST ALSO be booked to a tour (and thus you query to grab that information will take this rule into account). 但是,可能的业务规则是,系统中放下钱的任何客户也必须预订旅行团(因此,您查询以获取该信息将考虑此规则)。

If every query you always made never was to include data from more than one table, then you likely don't benefit much from enforcing relational data. 如果您始终进行的每个查询都不会包含多个表中的数据,那么执行关系数据可能不会给您带来太多好处。 However the instant you start bundling queries with multiple tables, then you MUST know the assumptions being made about that data before you can write a query. 但是,当您开始将查询与多个表捆绑在一起时,您必须知道有关该数据的假设,然后才能编写查询。 So do you allow customers with a deposit in the system without a tour booking or not? 那么,您是否允许没有系统预订的客户预订旅行团? This rule will decide how you must write that query. 该规则将决定您必须如何编写该查询。 If RI is enforced, then you can query on a customer “booking” that and you KNOW that it will be attached to a tour event. 如果强制执行RI,那么您可以查询客户的“预订”,并知道它将附加到巡回活动中。 And same goes if any booking + deposit does not need a booking – but you HAVE to know the assumption made about that data. 如果任何预订+押金不需要预订,情况也是如此-但是您必须知道有关该数据的假设。

So based on assumptions made about the data is the ONLY practical way to create a query to pull data based on those assumptions. 因此,基于对数据所做的假设是基于这些假设创建查询以提取数据的唯一实用方法。 And if you enforce RI, then you at least know the data MUST be related and setup based on those assumptions. 而且,如果您执行RI,那么您至少知道必须与数据相关并根据这些假设进行设置。

At the end of the day? 在一天结束时? Anyone creating a data base that models a business application and rules without enforcing RI is building a ship without rudder and without a compass. 任何在不强制执行RI的情况下创建可对业务应用程序和规则建模的数据库的人,都在建造没有方向舵和指南针的船。

And exporting data from each table is a NON issue. 从每个表中导出数据是一个非问题。 However if that data is a mess and has orphaned records, then you only wind up exporting a incorrect data model to another database and all of the issues and problems remain. 但是,如果该数据是一团糟并且具有孤立的记录,那么最终只能将不正确的数据模型导出到另一个数据库,而所有问题仍然存在。

If you are building queries purely in SQL mode, defining the relationships probably doesn't make any difference for you. 如果仅在SQL模式下构建查询,则定义关系可能对您没有任何影响。 The only thing that might be useful is that if you built something, then didn't look at it again for a few months, you would be able to quickly re-acquaint yourself with the relationships conceptually. 唯一有用的是,如果您构建了某个东西,但是几个月后再也不看它,您将能够在概念上快速重新熟悉自己。

For anyone using the access query builder, defining the relationships allows you to quickly add tables to the query while Access automatically builds the proper (GIGO) relationships for the query JOIN. 对于使用Access查询构建器的任何人,定义关系都可以使您快速向查询中添加表,而Access会自动为查询JOIN构建适当的(GIGO)关系。 Again, if you are writing in SQL, you probably already do this, so not much help for you in query building. 同样,如果您正在使用SQL编写代码,那么您可能已经这样做了,因此在查询构建方面对您没有太大帮助。

Bottom Line - it's more of a graphical tool to streamline the query process, at least until you try exporting the tables to a "real" RDBMS, as someone else already mentioned. 底线-至少在您尝试将表导出到“真实的” RDBMS之前,它更像是一种图形化工具,可以简化查询过程,就像其他人已经提到的那样。

If its not a requirement for you use case then you don't necessarily have use this. 如果不是用例的要求,则不必使用它。 A use case where this could be "required" is in an Order Based Scenario. 在“基于订单的方案”中可能是“需要”一个用例。

Lets say you have a Database that creates and tracks Orders. 假设您有一个创建和跟踪订单的数据库。 Each Order can have multiple Lines that are tied to the same Order. 每个订单可以有多个绑定到同一订单的行。 But for Normalization purposes, most people would separate these into two separate tables. 但是出于标准化的目的,大多数人会将它们分成两个单独的表。 OrderHead and OrderDetail. OrderHead和OrderDetail。 You would want to enforce Referential Integrity here to ensure that there is never a child record in OrderDetail that doesn't link back to a Parent Order. 您可能要在此处强制执行“引用完整性”,以确保OrderDetail中永远不会有子记录不链接回父订单。

I'm sure that you could prevent things like that without it, but it mainly just enforces it. 我敢肯定,没有它,您可以阻止类似的事情,但是它主要只是强制执行它。

Relationships helps in preserving data integrity and I agree with your point that if user is entering from access form, probability of errors due to integrity is lesser. 关系有助于保持数据的完整性,我同意您的观点,即如果用户是从访问表单进入的,则由于完整性而导致错误的可能性较小。 But in future if user is moving from MS Access to pure RDBMS, this relationship will definitely will be helpful. 但是将来,如果用户从MS Access转向纯RDBMS,这种关系肯定会有所帮助。

Though objective of relationship is not for migration at later point-in-time, for your case that is one valid reason I could think-of. 尽管关系的目标不是在以后的某个时间点迁移,但对于您而言,这是我可以想到的一个正当理由。

Other than that, for MS Access with its own forms relationship may not add specific values. 除此之外,对于具有自己的窗体关系的MS Access,可能不会添加特定的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关系如何运作以及如何在我的项目中实施它? - How do relationships work and how can I implement it in my project? 如何从数据库填充文本框的值? - How do I populate the value of a textbox from my database? 如果复选框为 false,如何从数据库中排除记录? - How do I exlude records from my database if a checkbox is false? 如何解密或访问受保护的Access数据库? - How can I decrypt or gain access to a protected Access database? 如何删除具有多个一对多关系的访问数据库中的重复项 - How do I delete duplicates in an access database with multiple one to many relationships 我的代码有什么问题? 为什么我不能为从数据库中检索到的数据分配“ 0” - What wrong with my code? Why couldn't I assign “0” to data that being retrieved from database 如何配置Microsoft Access数据库以直接从SAP BW中提取源数据? - How do I configure my Microsoft Access database to pull source data directly from SAP BW? 我在 c# 中写入什么来编辑访问数据库中的数据 - what do i write into c# to edit the data in an access database 我应将哪种16位数字数据存储到数据库中? - What data type do I store 16 digit number into database? 如何使用应用程序复制访问数据库文件? - How do I copy an access database file with my application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM