英文:
How to implement ER Relationships :One to one, One To Many, Many to Many in Oracle?
问题
以下是您要翻译的内容:
也许,这可能看起来像数据库设计的基础。但是,我确实觉得这些概念在它们如何相互关联方面有点棘手。
根据我的理解。
一对一:
当每个特定实体具有一条记录时。
一对多:
当每个特定实体具有多条记录时。
多对多:
多个实体具有多条记录。
根据上面的内容,如果我能举一个例子,
- 一对一
每个员工都有唯一的护照号码。
员工表
员工ID(主键)
员工姓名
护照ID(外键指向护照)
护照表
护照ID(主键)
护照号码
- 一对多
一个组织有多个员工
组织表
组织ID(主键)
组织名称
员工ID(外键指向员工)
员工表
员工ID(主键)
员工姓名
工资
这是我想要了解更多的部分,即“多对多”。我的意思是,如果我们在这里看到一对多,作为一个整体,可以说是多对多,因为有很多组织有很多员工,但这是否意味着整个关系是多对多而不是一对多,或者一对多是上述示例中多对多的子集。
我想了解一对多和多对多之间的区别,无论是理论上还是在实际实施中。
英文:
Perhaps, this could seem like the basics of database design.But,certainly i find these concepts a lil tricky in the sense that how they relate to each other.
Theory as per my understanding.
One to One :
When each particular entity has one record.
One to Many :
When each particular entity has many record
Many to Many :
Multiple entities have multiple records.
As per the above if i could relate an example as
- ONE TO ONE
Each employee having a unique passport number.
Table Employee
Empid(pk)
empname
passpordid(fk to passport)
Table passport
passportid(pk)
passportno
- ONE TO MANY
An organisation having multiple employees
TABLE ORGANISATION
ORGID (PK)
ORGNAME
EMPID (FK TO EMPLOYEE)
TABLE EMPLOYEE
EMPID (PK)
EMPNAME
SALARY
This is the part that i want to know more that is many to many
. I mean if we see one to many here. As a whole it could be said as many to many as many organisations having many employees but does that mean the whole relationship is many to many not one to many or one to many is a subset of many to many in the above example.
I wanna know the difference mainly between one to many and many to many both theoritically and by implementation.
答案1
得分: 2
一个多对多关系的示例可以是员工和技能之间的关系,其中技能可以是诸如"SQL"、"Javascript"、"管理"等等的事物。一个员工可能拥有多个技能(例如,可能懂SQL 和 Javascript),而一个特定的技能可能被多个员工拥有(例如,杰克和吉尔都懂SQL)。
在像Oracle这样的数据库中,您需要一个第三个表来表示员工和技能之间的多对多关系:
create table EMPLOYEE_SKILLS
( empid references EMPLOYEES
, skillid references SKILLS
, constraint EMPLOYEE_SKILLS_PK primary key (empid, skillid)
);
请注意,这个第三个表具有对其他两个表的外键。
这个表还可以包含有关关系的进一步信息 - 例如:
create table EMPLOYEE_SKILLS
( empid references EMPLOYEES
, skillid references SKILLS
, rating number
, date_certified date
, constraint EMPLOYEE_SKILLS_PK primary key (empid, skillid)
);
英文:
An example of a many-to-many relationship would be EMPLOYEES and SKILLS, where SKILLS are things like "SQL", "Javascript", "Management" etc. An employee may have many skills (e.g. may know SQL and Javascript), and a particular skill by be possessed by many employees (e.g. Jack and Jill both know SQL).
In a database like Oracle, you need a third table to express the many-to-many relationship between EMPLOYEES and SKILLS:
create table EMPLOYEE_SKILLS
( empid references EMPLOYEES
, skillid references SKILLS
, constraint EMPLOYEE_SKILLS_PK primary key (empid, skillid)
);
Note that this third table has a foreign key to both of the other tables.
The table can also hold further information about the relationship - for example:
create table EMPLOYEE_SKILLS
( empid references EMPLOYEES
, skillid references SKILLS
, rating number
, date_certified date
, constraint EMPLOYEE_SKILLS_PK primary key (empid, skillid)
);
答案2
得分: 0
这更像是一个理论问题而不是一个编程问题,但为什么不呢。
你可以将"many to many"想象成一个表格是员工列表,另一个表格是销售产品列表。
每个员工可能会“处理”多个产品,而每个产品也很可能会由多个员工处理,产品A和B同时由员工C和D处理也是非常可能的。
英文:
This is more a theory question that a programming one, but why not.
You could imagine a "many to many" as 1 table being a list of employees, and another table being a list of products sold.
Each employee is likely to "handle" more than 1 product, and each product could very well be handled by multiple employees, and it's quite possible that products A and B can be handled by, at the same time, employees C and D.
答案3
得分: 0
你似乎已经理解了一对一和一对多的基本概念,从理论上来说。
一对一:每个项目A都有一个单独且唯一的项目B。
在数据库中,你可以将新数据包含在同一个表中。你也可以将其放在一个单独的表中,并通过将id变成远程表的“唯一”(索引约束)来强制执行一对一约束,以确保项目B的id不会在表中重复。在你的示例中,这将是对护照ID添加的约束。
一对多:每个项目A都有一些项目B。
在数据库中,你将键放在“多”一侧的表中。因此,许多项目B将具有相同的外键指向表A。在你的示例中,将orgid作为外键放入员工表中。这样,许多员工可以指向一个组织,而一个员工可以属于一个组织。
多对多:一些项目A可以链接到任意数量的项目B,反之亦然。
在数据库中,通常会将其实现为一个链接表,其中只包含来自A和B的ID。以你的示例为例,考虑一个员工可以是多个组(或项目)的一部分,可以跨越这些组进行多任务处理。你可以有一个组表:
组ID(主键)
组名
然后,链接表看起来会像这样:
链接ID(主键)
员工ID(员工的外键)
组ID(组的外键)
这样,任何员工都可以是任意数量的组的一部分,而组可以有任意数量的员工,也就是多对多关系。
一个附带的评论:
从技术上讲,在链接表中你不一定需要有LINKID。但是它可以更容易地管理该表中的数据,并且通常被认为是“最佳实践”。
英文:
You seem to have the basic idea of one-to-one and one-to many down, in theory.
One-to-one: each item A has a separate and unique item B
In a database, you could include the new data in the same table. You could put it in a separate table, and enforce the one-to-one constraint by making the id into the remote table "unique" (indexing constraint), forcing the id for item B to not be repeated in the table. In your example, this would be a constraint added to the passportid.
One to many: each item A has some number of item Bs
In a database, you put the key in the table on the "many" side. So many Bs would have the same foreign key into table A. In your example, put an orgid as a foireign key into the employee table. Many employees can then point to a single organization, and an employee can belong to one organization.
Many to many: some number of As can be linked to any number of Bs and vice-versa
In a database, this is usually implemented as a link table, having just the IDs from both A and B. Following you example, consider that an employee can be part of multiple groups (or projects), multitasking across those groups. You could have a group table:
GROUPID (PK)
GROUPNAME
And the link table would then look like
LINKID (PK)
EMPID (FK TO EMPLOYEE)
GROUPID (FK TO GROUP)
This lets any employee be part of any number of groups, and groups to have any number of employees, AKA many-to-many.
A side comment:
Technically, you don't HAVE to have a LINKID in the link table. But it makes it easier to manage the data in that table, and is generally considered "best practice".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论