← Back to Constraints
Question 71
Library Loans Follow the Student
Library Loans Follow the Student
Create a table calledlibrary_loanswith four columns:id(anINTEGER PRIMARY KEY),student_id(INTEGER, required, a foreign key tostudents(id)),book(TEXT, required) anddue_on(TEXT, required). A loan for a student id that does not exist must be rejected. When a student is deleted, their loans must disappear with them: declare the foreign key withON DELETE CASCADE, so that deleting a student who still has a loan succeeds instead of failing.
Expected result:
Table library_loans ✓ id INTEGER PRIMARY KEY ✓ student_id INTEGER NOT NULL ✓ book TEXT NOT NULL ✓ due_on TEXT NOT NULL ✓ student_id REFERENCES students(id) ✓ Insert a loan for student 999, who does not exist: rejected (FOREIGN KEY) ✓ Insert a loan for student 28: accepted ✓ Insert a loan for student 30, then delete student 30: accepted
Hints3 available
Sign in to use hintsTopics:
Constraints
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (sel, ins, desc)
Results
Run your SQL to see every statement's output here, then how it compares with the expected answer.
Not sure what is in a table? SHOW TABLES lists them and DESCRIBE students shows the columns — or open the database browser on the left.