er模型轉關係模式_ER模型到關係模型

er模型轉關係模式

ER模型到關係模型 (ER Model to Relational Model)

As we all know that ER Model can be represented using ER Diagrams which is a great way of designing and representing the database design in more of a flow chart form.

衆所周知,ER模型可以使用ER Diagrams表示,這是一種設計和表示數據庫設計的好方法,更多地採用流程圖形式。

It is very convenient to design the database using the ER Model by creating an ER diagram and later on converting it into relational model to design your tables.

使用ER模型設計數據庫非常方便,方法是創建一個ER圖,然後將其轉換爲關係模型以設計表。

Not all the ER Model constraints and components can be directly transformed into relational model, but an approximate schema can be derived.

並非所有的ER模型約束和組件都可以直接轉換爲關係模型,但是可以導出近似模式。

So let's take a few examples of ER diagrams and convert it into relational model schema, hence creating tables in RDBMS.

因此,讓我們以ER圖的一些示例爲例,並將其轉換爲關係模型架構,從而在RDBMS中創建表。

實體成爲表格 (Entity becomes Table)

Entity in ER Model is changed into tables, or we can say for every Entity in ER model, a table is created in Relational Model.

ER模型中的實體已更改爲表,或者可以說,對於ER模型中的每個實體,都會在關係模型中創建一個表。

And the attributes of the Entity gets converted to columns of the table.

實體的屬性將轉換爲表的列。

And the primary key specified for the entity in the ER model, will become the primary key for the table in relational model.

並且在ER模型中爲實體指定的主鍵將成爲關係模型中表的主鍵。

For example, for the below ER Diagram in ER Model,

例如,對於下面的ER模型中的ER圖,

ER model to Relational - Entity to Table

A table with name Student will be created in relational model, which will have 4 columns, id, name, age, address and id will be the primary key for this table.

將在關係模型中創建一個名稱爲Student的表,該表將有4列, idnameageaddressid將是該表的主鍵。

關係成爲關係表 (Relationship becomes a Relationship Table)

In ER diagram, we use diamond/rhombus to reprsent a relationship between two entities. In Relational model we create a relationship table for ER Model relationships too.

在ER圖中,我們使用菱形/菱形表示兩個實體之間的關係。 在關係模型中,我們也爲ER模型關係創建關係表。

In the ER diagram below, we have two entities Teacher and Student with a relationship between them.

在下面的ER圖中,我們有兩個實體TeacherStudent ,它們之間具有關係。

ER diagram relationship into table in rdbms

As discussd above, entity gets mapped to table, hence we will create table for Teacher and a table for Student with all the attributes converted into columns.

如上所述,實體被映射到表,因此我們將爲教師創建表,爲學生創建表,並將所有屬性轉換爲列。

Now, an additional table will be created for the relationship, for example StudentTeacher or give it any name you like. This table will hold the primary key for both Student and Teacher, in a tuple to describe the relationship, which teacher teaches which student.

現在,將爲該關係創建一個附加表,例如StudentTeacher或爲其指定任何您喜歡的名稱。 該表將在一個元組中包含「學生」和「老師」的主鍵,以描述這種關係,由哪個老師教給哪個學生。

If there are additional attributes related to this relationship, then they become the columns for this table, like subject name.

如果還有與此關係相關的其他屬性,則它們將成爲此表的列,例如主題名稱。

Also proper foriegn key constraints must be set for all the tables.

還必須爲所有表設置適當的前鍵約束。

要記住的要點 (Points to Remember)

Similarly we can generate relational database schema using the ER diagram. Following are some key points to keep in mind while doing so:

同樣,我們可以使用ER圖生成關係數據庫架構。 以下是在執行此操作時要記住的一些關鍵點:

  1. Entity gets converted into Table, with all the attributes becoming fields(columns) in the table.

    實體被轉換成表格,所有屬性變成表格中的字段(列)。
  2. Relationship between entities is also converted into table with primary keys of the related entities also stored in it as foreign keys.

    實體之間的關係也轉換爲表,同時相關實體的主鍵也作爲外鍵存儲在表中。
  3. Primary Keys should be properly set.

    應該正確設置主鍵。
  4. For any relationship of Weak Entity, if primary key of any other entity is included in a table, foriegn key constraint must be defined.

    對於弱實體的任何關係,如果表中包含任何其他實體的主鍵,則必須定義前鍵約束。

翻譯自: https://www.studytonight.com/dbms/er-to-relational-model.php

er模型轉關係模式