Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Can a Unique Index be set on a secondary linked table field?

Can a Unique Index be set on a secondary linked table field?

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stryder_1
    wrote on last edited by
    #1

    Hi, For an example, using Sql Server - let's say I have three tables: Client: ID Name File: ID Name Bytes ClientID Data: ID DataField FileID Each record in Data can be linked back to each Client record through the File Record. I want the DataField value to be unique only per client - so two values of "42" in the DataField is ok as long as they link to different clients. I could put the ClientID in the Data table, but that would then mean I would need to be sure the ClientID in the related File record always matches. Can an index be set on the Data table using the DataField field and the Client ID field? If so, how would you do this? If not, is there an alternative? Thanks

    L 2 Replies Last reply
    0
    • S Stryder_1

      Hi, For an example, using Sql Server - let's say I have three tables: Client: ID Name File: ID Name Bytes ClientID Data: ID DataField FileID Each record in Data can be linked back to each Client record through the File Record. I want the DataField value to be unique only per client - so two values of "42" in the DataField is ok as long as they link to different clients. I could put the ClientID in the Data table, but that would then mean I would need to be sure the ClientID in the related File record always matches. Can an index be set on the Data table using the DataField field and the Client ID field? If so, how would you do this? If not, is there an alternative? Thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Stryder_1 wrote:

      I want the DataField value to be unique only per client

      How about this?

      Client
      ID
      Name
      DataField

      File
      ID
      Name
      Bytes
      ClientID

      That's what you asked literally, but not what you'd want;

      Client
      ID
      Name

      File
      ID
      Name
      Bytes
      DataField
      ClientID

      With a UNIQUE constraint on DataField and ClientId :)

      I are Troll :suss:

      modified on Monday, October 18, 2010 3:56 PM

      1 Reply Last reply
      0
      • S Stryder_1

        Hi, For an example, using Sql Server - let's say I have three tables: Client: ID Name File: ID Name Bytes ClientID Data: ID DataField FileID Each record in Data can be linked back to each Client record through the File Record. I want the DataField value to be unique only per client - so two values of "42" in the DataField is ok as long as they link to different clients. I could put the ClientID in the Data table, but that would then mean I would need to be sure the ClientID in the related File record always matches. Can an index be set on the Data table using the DataField field and the Client ID field? If so, how would you do this? If not, is there an alternative? Thanks

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I think you can add two unique constaint in File..ClientID and Data..FileID to achieve your goal. By your data design, if you want the DataField value to be unique only per client, the infomation in [File] should also unique match one [Client] record. Client: ID Name File: ID Name Bytes ClientID <<< Data: ID DataField FileID <<<

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups