2015-10-26 15 views
0

我對我正在製作的這個數據庫模式有疑問。 我有兩個相似的用戶,而是一個具有比其它額外信息:兩個相似用戶的數據庫模式

Type 1 : Administrator 
- Name 
- Lastname 
- Email 
- Password 

Type 2: Student 
- Name 
- Lastname 
- Email 
- Password 
- Status 
- Sex 
- Among other type of personal information fields 

所以,我猶豫要麼使這兩個單獨的表,當他們要登錄,查詢他們倆(因爲我只有一個日誌記錄屏幕),或者將它們統一爲只有一個表用戶,並使用指向後者的用戶的外鍵創建另一個名爲「額外」的名稱。

什麼是最有效的方法來實現這一目標? 。感謝您的時間

回答

0

我會做兩個表,並做登錄後,加入緩存有關用戶額外的事實,他們在登錄後

你應該有一個User表,這些列:

Id, Name, Lastname, Email, Password, IsAdmin

與學生表:

UserId, Status, Sex, ...

A Student也必須是User - 這將減少重複的數據。

如果您需要比IsAdmin更多的權限,請刪除該列,並製作UserPermissionsPermission表。

如果您真的關心加入,那麼只需將所有內容都設爲空即可,並在一個User表中。我懷疑這在你的用例中是否重要(這是一個更大的話題)。

0

管理員是一個人扮演的角色。

學生是一個人扮演的角色。

一個人可以一次扮演一個角色,也可以扮演多個角色。這是一個業務規則,不應該考慮到你的數據庫模式。

使用single table inheritance允許在同一個表中使用不同類型的角色。

create table people (
    person_id int primary key, 
    given_name varchar(...), 
    surname varchar(...), 
    password varchar(...)--consider a `users` table instead 
); 

create table roles (
    role_id int primary key, 
    person_id int not null references people(person_id), --use the long-hand foreign key syntax for mysql 
    type varchar(...), --admin or student 
    status varchar(...), --consider PostgreSQL over mysql, as it supports check constraints 
    sex varchar(...) 
); 
相關問題