回顧與練習
關鍵要點
- 大多數應用只需要三種儲存形態 — 關係型、文件型、鍵值型 — 中的一種,而關係型是安全的預設選擇。
- 從託管資料庫開始:資料和模式決策由你掌握,伺服器的執行由平臺負責。
- 好的模式設計意味著誠實的型別、外部索引鍵與約束、合理的索引,以及沒有重複的資料。
- 把每一次遷移都當作一道單向門:在 drop 或重新命名任何東西之前先備份,並仔細審查 AI 寫的遷移。
- 始終要求引數化查詢,使使用者輸入永遠不會被拼接進 。
動手試試
挑一個小小的應用點子,先用平實的中文把它的實體和關係寫出來。然後把這段描述交給 AI,請它給出一份模式加一個前向遷移 — 在執行任何東西之前,用本章的清單審查結果。
Design a relational schema for a personal bookmarks app.
Entities and relationships:
- A user has many bookmarks.
- A bookmark belongs to one user, has a URL, a title, and many tags.
- A tag can apply to many bookmarks.
Requirements:
- Use UUID primary keys and created_at timestamps.
- Enforce relationships with foreign keys.
- Add indexes for the columns we'll filter on.
- Give me the schema as SQL plus a forward migration file.
- Flag any migration step that drops or renames so I can back up first.