为什么没有它就可以加入时需要主-外键关系? [英] Why is a primary-foreign key relation required when we can join without it?

查看:49
本文介绍了为什么没有它就可以加入时需要主-外键关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们可以从两个表中获取数据而又没有主键和外键关系,那为什么我们需要这个规则?你能用合适的例子清楚地解释一下我吗?这是一个测试数据库,不要介意不良的结构.

If we can get data from two tables without having primary and foreign key relation, then why we need this rule? Can you please explain me clearly, with suitable example? It's a test database, don't mind the bad structure.

表的结构:

**

table - 'test1'
columns - id,lname,fname,dob
no primary and foreign key and also not unique(without any constraints)

**

**table - 'test2'
columns- id,native_city
again, no relations and no constraints** 

我仍然可以将这些表与相同的列"id"连接起来,因此,如果没有主外键,那么它的用途是什么?

I can still join these tables with same columns 'id', so if there's no primary-foreign key, then what is the use of that?

推荐答案

使用主键和外键的主要原因是为了实现数据一致性.

The main reason for primary and foreign keys is to enforce data consistency.

主键可在一个或多个列上强制值唯一性的一致性.如果ID列具有主键,则不可能有两个具有相同ID值的行.没有该主键,许多行可能具有相同的ID值,并且您将无法仅根据ID值来区分它们.

A primary key enforces the consistency of uniqueness of values over one or more columns. If an ID column has a primary key then it is impossible to have two rows with the same ID value. Without that primary key, many rows could have the same ID value and you wouldn't be able to distinguish between them based on the ID value alone.

外键可增强指向其他位置的数据的一致性.它确保所指向的数据确实存在.在典型的父子关系中,外键可确保每个孩子始终指向父母,并且父母实际存在.没有外键,您可能会有孤立的"孩子指向一个不存在的父母.

A foreign key enforces the consistency of data that points elsewhere. It ensures that the data which is pointed to actually exists. In a typical parent-child relationship, a foreign key ensures that every child always points at a parent and that the parent actually exists. Without the foreign key you could have "orphaned" children that point at a parent that doesn't exist.

这篇关于为什么没有它就可以加入时需要主-外键关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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