Your schema is solid, good work! Your Users_and_Polls table is the key to your schema design. You realized the need for a table that contains the unique combination of one user, one poll, and one response. And you did so without compromising the poll creating association (one user can create many polls, but each poll can only be created by one user) and the response creating association (one poll can have many responses, but each response can only have one poll) and the voting association (one user can vote on many polls, and one poll can be voted on by many users). Do you realize what this means? It means you are really getting this database design stuff. CONGRATS!
Here are some tips to sharpen everything:
- The default names of your tables need to conform to naming convention. Table names should be plural, lowercase, with
_
between words, like this: users and users_polls and polls and responses. And you definitely do not want an&
in a table name! That would probably break a lot of stuff. - You forgot the question field on your polls table. (I'm guessing you were so focused on getting the relationships right that you kind of skipped over the easy stuff like adding fields.)
- You probably were trying to do this but couldn't figure it out: There is a way to show a uniqueness constraint on your schema design. (Remember: your users_polls table needs a constraint on it, so that any given user can vote on any given poll only once.) To accomplish this, you would need to create a uniqueness constraint for two fields on this table: the user_id field and the poll_id field. As in other words, before the database would save a record, it would check to see if there is already a record in the users_polls table with the same user_id and poll_id combination. If there is, the database would refuse to save the record (and thus enforce the desired constraint). To show this constraint with the schema designer, you would need to play with the "keys" button in the schema designer. Click on the table, then on the "keys" button, then on the "add key" button, and try to create a unique key for both of the desired fields. You can do it!
Good work!
Any questions let me know.
-Phil