← Back to Constraints
Question 73
Clubs and Their Members
Clubs and Their Members
Write twoCREATE TABLEstatements in one script. Firstclubs:id(anINTEGER PRIMARY KEY),name(TEXT, required, and no two clubs may share a name) andfounded_year(INTEGER, optional). Thenclub_members, which links students to clubs:student_id(INTEGER, required, referencesstudents(id)withON DELETE CASCADE),club_id(INTEGER, required, referencesclubs(id)withON DELETE CASCADE) andjoined_on(TEXT, required). A student can join a club only once, so the primary key ofclub_membersis the pair(student_id, club_id)and there is no separateidcolumn. Deleting a club or a student must remove their memberships.
Expected result:
Table clubs ✓ id INTEGER PRIMARY KEY ✓ name TEXT NOT NULL ✓ founded_year INTEGER ✓ UNIQUE (name) ✓ Add student 1 to club 1 twice: rejected (UNIQUE) ✓ Add student 1 to two different clubs: accepted ✓ Add student 1 to club 99, which does not exist: rejected (FOREIGN KEY) ✓ Create two clubs called Chess Club: rejected (UNIQUE) ✓ Add a member to club 1, then delete club 1: accepted ✓ Add student 28 to club 1, then delete student 28: accepted Table club_members ✓ student_id INTEGER PRIMARY KEY ✓ club_id INTEGER PRIMARY KEY ✓ joined_on TEXT NOT NULL ✓ club_id REFERENCES clubs(id) ✓ student_id REFERENCES students(id) ✓ Add student 1 to club 1 twice: rejected (UNIQUE) ✓ Add student 1 to two different clubs: accepted ✓ Add student 1 to club 99, which does not exist: rejected (FOREIGN KEY) ✓ Create two clubs called Chess Club: rejected (UNIQUE) ✓ Add a member to club 1, then delete club 1: accepted ✓ Add student 28 to club 1, then delete student 28: accepted
Hints4 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.