-
Notifications
You must be signed in to change notification settings - Fork 6
Shadi A. #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Shadi A. #5
Changes from all commits
e56923d
aa539ce
fadfb8a
387de35
d771f69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,69 @@ | ||
| -- Write here your DDL statements to create the tables for the database. | ||
| CREATE TABLE member ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(255) NOT NULL, | ||
| email VARCHAR(255) NOT NULL UNIQUE, | ||
| phone_number VARCHAR(255) NOT NULL, | ||
| address VARCHAR(255) NOT NULL | ||
| ); | ||
|
|
||
| create table card ( | ||
| id SERIAL PRIMARY KEY, | ||
| member_id INT NOT NULL UNIQUE REFERENCES member(id) ON DELETE CASCADE, | ||
| number VARCHAR(255) NOT NULL UNIQUE, | ||
| issued_on DATE NOT NULL, | ||
| expires_on DATE NOT NULL | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For data integrity need to validate that expiry date is not before issued_on date. You could add a constraint like |
||
| ); | ||
|
|
||
| CREATE TABLE author ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(255) NOT NULL | ||
| ); | ||
| CREATE TABLE publisher ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(255) NOT NULL | ||
| ); | ||
|
|
||
| CREATE TABLE book ( | ||
| id SERIAL PRIMARY KEY, | ||
| title VARCHAR(255) NOT NULL, | ||
| isbn VARCHAR(255) NOT NULL UNIQUE , | ||
| publisher_id INT NOT NULL REFERENCES publisher(id) ON DELETE CASCADE | ||
|
|
||
| ); | ||
|
|
||
| CREATE TABLE book_author ( | ||
| book_id INT NOT NULL REFERENCES book(id) ON DELETE CASCADE, | ||
| author_id INT NOT NULL REFERENCES author(id) ON DELETE CASCADE, | ||
| PRIMARY KEY (book_id, author_id) | ||
| ); | ||
|
|
||
| CREATE TABLE tag ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(255) NOT NULL UNIQUE | ||
| ); | ||
|
|
||
| CREATE TABLE book_tag ( | ||
| book_id INT NOT NULL REFERENCES book(id) ON DELETE CASCADE, | ||
| tag_id INT NOT NULL REFERENCES tag(id) ON DELETE CASCADE, | ||
| PRIMARY KEY (book_id, tag_id) | ||
| ); | ||
|
|
||
| CREATE TABLE book_copy ( | ||
| id SERIAL PRIMARY KEY, | ||
| book_id INT NOT NULL REFERENCES book(id) ON DELETE CASCADE, | ||
| barcode VARCHAR(255) NOT NULL UNIQUE, | ||
| shelf_code VARCHAR(255) NOT NULL | ||
| ); | ||
| CREATE TABLE borrowing ( | ||
| id SERIAL PRIMARY KEY, | ||
| member_id INT NOT NULL REFERENCES member(id) ON DELETE CASCADE, | ||
| book_copy_id INT NOT NULL REFERENCES book_copy(id) ON DELETE CASCADE, | ||
| borrowed_at TIMESTAMP NOT NULL, | ||
| due_date TIMESTAMP NOT NULL, | ||
| returned_at TIMESTAMP , | ||
| fine_eur NUMERIC(8, 2) NOT NULL DEFAULT 0, | ||
|
|
||
| CHECK (fine_eur >= 0), | ||
| CHECK (returned_at IS NULL OR returned_at >= borrowed_at), | ||
| CHECK (due_date >= borrowed_at::DATE) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because Use the full timestamp comparison instead: CHECK (due_date >= borrowed_at) |
||
| ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, clean schema overall. Good normalization, sensible bridge tables, and the CHECK constraints are spot on! One thing to note though: you've used ON DELETE CASCADE on every foreign key, and in several places that's dangerous. Take the borrowing table: Borrowings are the library's financial/bookkeeping records (loans, due dates, fines). With CASCADE, deleting a member or even a single damaged book copy, silently wipes out the entire loan history attached to it, including unpaid fines. The same applies to book.publisher_id: Deleting one publisher would delete all of its books, which cascades further into
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to enforce a basic shape at the database level, you could add a small sanity CHECK constraint like below, but don’t try to fully validate every possible email format in SQL. The important database constraint here is UNIQUE, because each member email should identify one member.