我们可以连接两个没有主 - 外键关系的表吗? [英] Can we join two tables without primary-foreign key relation?

查看:637
本文介绍了我们可以连接两个没有主 - 外键关系的表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 on 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天全站免登陆