Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How can I make a composite key where one key is an autonumber?

I want to make a table in my database where the composite key is made up of task_id and user_id. The task key i want the database to generate for each new task and then allow me to put multiple users who can use the task key. I dont want to put multiple fields like user1, user2,user3 in the same table because i want them to pay for the task separately. Is this possible?

1 Answer

Relevance
  • Frank
    Lv 7
    8 years ago
    Favorite Answer

    I may misunderstand what you are asking, but I think you are doing this wrong.

    I'll assume that user_id is a primary key in the user table.

    The task_purchase table should look like:

    task_purchase_id INT AUTO_INCREMENT ,

    task_id INT,

    purchased_by_user INT ,

    when_purchased DATETIME

    PRIMARY KEY (task_id),

    FOREIGN KEY (purchased_by_user) REFERENCES user(user_id)

    FOREIGN KEY (task_id) REFERENCES task(task_id)

    If there are 10 purchases, there are 10 rows in the task table. If some of those 10 rows reference the same user_id, that's just fine. If some of those rows reference the same task_id, that's fine.

Still have questions? Get your answers by asking now.