簡體   English   中英

設計SQL用戶表的問題

[英]Issues on designing SQL users table

我正在創建一個具有users表的數據庫,但有三種類型的用戶(管理員,教師和學生),有些類型有自己的屬性。 這是我的解決方案:

1 - 三個不同的表:

table_admin    
  id
  name
  email
  password

table_teachers
  id
  name
  email
  password
  teacher_only_a
  teacher_only_b

table_students 
  id
  name
  email
  password
  student_only_a
  student_only_b

2 - 一個表,允許NULL值:

table_users
  id
  name
  email
  password
  teacher_only_a (null on admin and student)
  teacher_only_b (null on admin and student)
  student_only_a (null on admin and teacher)
  student_only_b (null on admin and teacher)

3 - 相關表格:

table_users
  id
  name
  email
  password
  teacher_id (null on admin and student)
  student_id (null on admin and teacher)

table_teachers
  id
  teacher_only_a
  teacher_only_b

table_students 
  id
  student_only_a
  student_only_b

哪個是最好的設計選擇? 還有其他解決方案嗎?

為什么不

table_users
  id
  name
  email
  password
  is_admin

table_teachers
  user_id
  teacher_only_a
  teacher_only_b

table_students 
  user_id
  student_only_a
  student_only_b

這將抽象出用戶信息,因此沒有任何冗余。

我會選擇有兩個表,一個名為user ,用於存儲用戶名,角色和其他元數據,另一個表名為user_relation ,用於存儲用戶之間的關系。

用戶

id
name
email
password
role (admin, teacher, or student)

user_relation

id1
id2

我在設計中做了兩個假設。 首先,用戶的角色只是adminteacherstudent 如果用戶可以是多個角色,那么您將需要創建一個存儲此信息的新表user_role 第二個假設是,很明顯,關系的性質僅僅取決於用戶的類型。 例如,如果來自user_relation的記錄包含學生和教師,則將隱含地假定該學生屬於該教師的班級。 同樣,如果教師和管理員有條目,則會假設后者管理前者。 但是,如果要使關鍵類型更靈活,可以輕松添加關系類型列,例如,允許管理員也是學生。

由於用戶具有動態變化屬性,因此最好制作映射屬性並單獨檢索它們,同時考慮到它們具有相同的數據類型。 teacher_only_a和student_only_a的值是什么,這里存儲的數據類型是什么?

table_user
  id
  name
  email
  password
  type // T:Teacher S:Student

table_user_attributes
  user_id
  attribute_name
  attribute_value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM