SQL 数据库:关系和关系图式
我是一名学生,我有一个关于数据库模式的问题,我已经创建了实体关系图 [ERD],在这一步中我应该做数据库模式,数据库中的所有实体必须在它们和其他实体之间有关系?即:每个实体之前都应该有一个实体的外键,因为我可以创建所有表,并且只有 2 个表可以在它们之间有关系,并且我使用我要创建的 C# 程序控制其他表。
** 在我的 ERD 中,所有实体之间都存在关系。
i am a student and i have a question about database schema , i already created the Entity Relationship Diagram [ERD] and in this step i should do the database schema , must all the entities on my database has a relation between them and the other entities ? i.e : each entity should have a foreign key for the entity before it , because i can create all the tables and only 2 tables can have a relation between them and i control the other tables using C# program i am going to create .
** in my ERD all the entities have a relation between each other .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不。实体当然可以独立存在。尽管在实践中,如果您发现关系很少,那么您可能做错了什么...
您说只有两个表可以有关系是正确的,但我会说它是关系可以只有两名参与者;表当然可以与多个其他表相关。
No. Entities of course can stand on their own. Although in practice if you're finding that you have very few relationships you're probably doing something wrong...
You're sort of right in saying that only two tables can have a relationship but I'd word it as a relationship can have only two participants; tables can certainly relate to more than just one other table.
任何时候在数据库中创建表时,如果表中的信息之间存在关系,那么您应该创建一个关系,以确保当数据输入到表中时,参考数据将存在。这将强制引用完整性。例如:
员工数据库:
因此,这将确保如果您向员工添加薪水,它必须存在于薪水表中,并且如果您添加登录名,那么您必须有一个员工可供参考。它只是增加了一层安全性,以确保输入数据库的数据有效且可以使用。
虽然您可以在技术上使用 C# 或任何您想要的编程语言来控制这一点,但很容易忘记这些小规则,尤其是在拥有 200 个或更多表的数据库中。因此,养成使用关系和保持引用完整性的习惯是一件非常好的事情。
Any time you create tables in the database if there is a relationship between the information in the tables then you should create a relationship to make sure that when the data is entered into the tables that the reference data will be there. This will enforce referential integrity. For example:
Employees Database:
So what this would do is make sure that if you add a salary to an employee it has to exist in the Salary table and if you added a login then you would have to have an employee to reference. It's just an added layer of security to make sure that the data that is being input into the database is valid and can be used.
While you can technically control this with C# or whatever programing language you want it's easy to forget these little rule especially in a database that has like 200 tables or more. So getting in the habit of using relationships and maintaining referential integrity is a very good thing to do.
所有实体彼此之间不需要有关系。仅当一个实体以某种方式与另一个实体相关时,ERD 中的两个实体之间才会存在关系。
创建表的一般经验法则是:
All entities need not have relationships with each other. A relationship will only exist between two entiries in the ERD if one entity somehow relates to the other.
The general rule of thumb for creating table is:
由于约定,主键应始终为
id
,并且您的外键可以称为othertablename_id
,因此如果一个表具有指向所有其他表的链接,则它将需要一个键对于其中的每一个。例如:如果
dogs
有多个主人,但owners
只能拥有一只狗,则您可以在owner 中只包含
表。但随后您必须查询dog_id
owners
表才能找到一只狗的所有所有者。如果狗有多个主人,并且主人可以拥有多个狗,那么您必须有一个名为
dogs_owners
的连接表,其中包含 id 以及外键dog_id
和owner_id
当然,您可以随意命名表和字段,但惯例是使用
id
和_id
是的,一个实体通常只相当于一个表,除了当你有实体之间的多对多关系。
the primary key should always be
id
due to convention, and your foreign keys could be calledothertablename_id
so if a table has a link to all the other tables it will need a key for each one of those.for example: if
dogs
has more than one owners butowners
can only have one dog, you could just havedog_id
in theowner
table. but then you would have to query theowners
table to find all the owners of one dog.if dogs have more than one owner and owner can have more than one dog, then you have to have a join table called
dogs_owners
with id and both foreign keydog_id
andowner_id
of course you can name your tables and fields whatever you want but it's convention to use
id
and_id
yes an entity usually equates to just one table, apart from when you have many-to-many relationships between your entities.