table with a column be primary key and foreign key at same time

  • hi everybody

    I have two table that have many to many relationship so i add table between them.and this table column is Id of each table.my question is i want this table info be unique(not duplicated) and it is better this two id in this table be unique or be primary key with same time that are foreign key????

  • vahid.arr (2/5/2014)


    hi everybody

    I have two table that have many to many relationship so i add table between them.and this table column is Id of each table.my question is i want this table info be unique(not duplicated) and it is better this two id in this table be unique or be primary key with same time that are foreign key????

    I am missing something in translation but if I understand you correctly you have a bridge table to prevent a many to many relationship?

    An example might be Employees and Departments where a given employee can belong to more than 1 department. You could create a EmployeeDepartment table with two columns (EmployeeID and DepartmentID). Each of those columns have a foreign key reference. Additionally the primary key for EmployeeDepartment would be a composite key.

    Is that the question, and if so does that make sense?

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Assuming I understand the question, yes, that's not only possible, it's the right way to get the job done.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • my question is as you say in example.it is better EmployeeID and DepartmentID be both primary key as they are foreign key or i unique them to prevent duplicate info???

  • In the sample the EmployeeID is the primary key in the Employee table. The DepartmentID is the primary key in the Department table. The primary key in the EmployeeDepartment table (the bridge table) is a composite of both the EmployeeID en the DepartmentID column.

    Because this bridge table has a one-to-many relationship to both the Employee and the Department table, you have in effect created a many-to-many relation between these two tables. The records in the EmployeeDepartment table (the bridge table) are unique.

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply