CS50x threads to aide as a supplementary resource Forums CS50’s Introduction to Computer Science by Harvard University on Edx Week 7: SQL Naming convention for foreign key referencing users table in transactions Table

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
  • #2317

    Here is partial schema:
    1. users table:
    id, username, hash, cash (id primary key)\
    2. transactions table:
    id (will refer to transaction id and so primary key). Now it is also needed to add users id from users table. I understand this users id will be foreign key in transactions tabe.
    How to name id that is from the users table in transactions table?


    You can name the foreign key column in the transactions table to reference the id column from the users table. Common naming conventions would suggest something like user_id. This makes it clear that it’s a reference to the id column in the users table.

    So your transactions table schema could look like this:

    transactions table:
    id (primary key),
    user_id (foreign key referencing users.id),

    This way, when you query the transactions table, you can easily understand that user_id refers to the user associated with that transaction.

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
Scroll to Top