← Back to CREATE TABLE
Question 95
Clubs and Memberships
Clubs and Memberships
Build two related tables in one script.clubs:idINTEGER PRIMARY KEY;nameTEXT NOT NULL UNIQUE;founded_yearINTEGERwith aCHECKthat it is at least 1900;advisor_idINTEGER(optional) thatREFERENCES instructors(id).memberships:idINTEGER PRIMARY KEY;club_idINTEGER NOT NULLthatREFERENCES clubs(id)ON DELETE CASCADE;student_idINTEGER NOT NULLthatREFERENCES students(id)ON DELETE CASCADE;roleTEXT NOT NULL DEFAULT 'member'; andUNIQUE (club_id, student_id). Createclubsfirst, sincemembershipspoints at it; deleting a club must take its memberships with it, and the same student joining the same club twice must be refused.
Expected result:
Table clubs ✓ id INTEGER PRIMARY KEY ✓ name TEXT NOT NULL ✓ founded_year INTEGER ✓ advisor_id INTEGER ✓ advisor_id REFERENCES instructors(id) ✓ UNIQUE (name) ✓ A club founded in 1800: rejected (CHECK) ✓ A club advised by instructor 99: rejected (FOREIGN KEY) ✓ A membership of club 99: rejected (FOREIGN KEY) ✓ Student 1 joining the same club twice: rejected (UNIQUE) ✓ A membership with no role given: accepted ✓ Deleting a club that has a member: accepted Table memberships ✓ id INTEGER PRIMARY KEY ✓ club_id INTEGER NOT NULL ✓ student_id INTEGER NOT NULL ✓ role TEXT NOT NULL DEFAULT 'member' ✓ student_id REFERENCES students(id) ✓ club_id REFERENCES clubs(id) ✓ UNIQUE (club_id, student_id) ✓ A club founded in 1800: rejected (CHECK) ✓ A club advised by instructor 99: rejected (FOREIGN KEY) ✓ A membership of club 99: rejected (FOREIGN KEY) ✓ Student 1 joining the same club twice: rejected (UNIQUE) ✓ A membership with no role given: accepted ✓ Deleting a club that has a member: accepted
Hints4 available
Sign in to use hintsTopics:
CREATE TABLE
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.