我是使用mysql的初學者,我正在嘗試學習最佳實踐。我已經建立了如下所示的類似結構。這是很好的數據庫標準化嗎?
表= 'main_content'
+------------+---------------+------------------------------+-----------+
| content_id | (deleted) | title | member_id |
+------------+---------------+------------------------------+-----------+
| 6 | | This is a very spe?cal t|_st | 1 |
+------------+---------------+------------------------------+-----------+
(包含所有唯一條目主表)(提供總每個難度和連接ID - >實際名稱)表= '困難'
+---------------+-------------------+------------------+
| difficulty_id | difficulty_name | difficulty_total |
+---------------+-------------------+------------------+
| 1 | Absolute Beginner | 1 |
| 2 | Beginner | 1 |
| 3 | Intermediate | 0 |
| 4 | Advanced | 0 |
| 5 | Expert | 0 |
+---------------+-------------------+------------------+
(此表可確保多個值可以插入針對每個條目。例如, 此特定條目指示有2 d與提交相關ifficulties) 表=「lookup_difficulty」
+------------+---------------+
| content_id | difficulty_id |
+------------+---------------+
| 6 | 1 |
| 6 | 2 |
+------------+---------------+
我加入這一切變成可讀查詢:
SELECT group_concat(difficulty.difficulty_name) as difficulty, member.member_name
FROM main_content
INNER JOIN difficulty ON difficulty.difficulty_id
IN (SELECT difficulty_id FROM main_content, lookup_difficulty WHERE lookup_difficulty.content_id = main_content.content_id)
INNER JOIN member ON member.member_id = main_content.member_id
以上工作正常,但我想知道,如果這是很好的做法。我實際上遵循了編排的結構Wikipedia's Database Normalization example。
當我使用EXPLAIN
運行上述查詢時,它說:'使用where;使用加入緩衝區',並且我正在使用2個依賴子查詢。我沒有看到任何方式不使用子查詢來實現相同的影響,但是再次我是一個noob,所以也許有更好的方法....
爲什麼'main_contant'有FK到困難和查找表?你能解釋一下你正在試圖構建什麼,所以我們可以評估你的設計? – Jacob
在我看來,你可以廢棄lookup_difficulty表,而只是在main_content上使用複合PK on(content_id,difficulty_id) –
對不起,我的意思是刪除該列。我沒有使用difficulty_id ..我將編輯帖子。我試圖將所有表基本上與main_content.content_id關聯。 – Justin