Skip to content

Commit

Permalink
feat: add check note in database
Browse files Browse the repository at this point in the history
  • Loading branch information
yilanboy committed Oct 27, 2024
1 parent 79e0718 commit 9720b23
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions database/06-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Check

在 PostgreSQL 中,可以使用 `CHECK` 來避免某些錯誤的資料被寫入。

以多階段留言系統為例,留言的 `parent_id` 不應該等於自己的 `id` (即自己的父留言不應該是留言自己本身)。我們可以在 PostgreSQL 中使用 `CHECK` 來確認這個規則。

```sql
ALTER TABLE comments
ADD CONSTRAINT parent_id_cannot_be_its_own_id CHECK (comments.id <> comments.parent_id);
```

如果想移除這個 `CONSTRAINT`,可以使用 `DROP` 來移除這個規則。

```sql
ALTER TABLE comments
DROP CONSTRAINT parent_id_cannot_be_its_own_id;
```

0 comments on commit 9720b23

Please sign in to comment.