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. SQL Server 2012 - forming relationship between tables

SQL Server 2012 - forming relationship between tables

Scheduled Pinned Locked Moved Database
databasequestionsql-serversysadmin
6 Posts 4 Posters 4 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.
  • R Offline
    R Offline
    RickBStewart
    wrote on last edited by
    #1

    This is my first database project and I am having difficulties forming the correct relationship between two tables. The database is storing inventory records for approximately 20 buildings, and the room numbers in many of the building are numbered the same. One table has columns like Asset Number, PO Number, Equipment Type, Manufacturer, Model Number, Serial Number. The PK is the Asset Number. The second table has Building Name, Room Number, Room Type, Staff Name. The key for this table is a combination of Building Name and Room Number. I need both to uniquely identify the location. My question is, how do I go about creating the relationship between the tables. The relationship would be described as each Asset having a unique Building/Room Number, but each Building/Room Number can have many Assets. When I try to create the one-to-many relationship it tells me "Both sides of a relationship must have the same number of columns"

    Richard DeemingR 1 Reply Last reply
    0
    • R RickBStewart

      This is my first database project and I am having difficulties forming the correct relationship between two tables. The database is storing inventory records for approximately 20 buildings, and the room numbers in many of the building are numbered the same. One table has columns like Asset Number, PO Number, Equipment Type, Manufacturer, Model Number, Serial Number. The PK is the Asset Number. The second table has Building Name, Room Number, Room Type, Staff Name. The key for this table is a combination of Building Name and Room Number. I need both to uniquely identify the location. My question is, how do I go about creating the relationship between the tables. The relationship would be described as each Asset having a unique Building/Room Number, but each Building/Room Number can have many Assets. When I try to create the one-to-many relationship it tells me "Both sides of a relationship must have the same number of columns"

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      All of the primary key columns from the table on the "one" side need to be present in the table on the "many" side. In this case, you would add both building name and room number to your assets table, and specify both columns in the FOREIGN KEY relationship. I'd be inclined to move the buildings out to a separate table with a surrogate key, since the value is likely to be repeated across many rows in the rooms table. Similarly, room type and staff name might be better in their own lookup tables, depending on your data.

      Buildings

      BuildingId (PK)
      BuildingName

      Rooms

      BuildingId (PK, FK->Buildings)
      RoomNumber (PK)
      ...

      Assets

      AssetNumber (PK)
      BuildingId (FK->Rooms)
      RoomNumber (FK->Rooms)
      ...


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      R 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        All of the primary key columns from the table on the "one" side need to be present in the table on the "many" side. In this case, you would add both building name and room number to your assets table, and specify both columns in the FOREIGN KEY relationship. I'd be inclined to move the buildings out to a separate table with a surrogate key, since the value is likely to be repeated across many rows in the rooms table. Similarly, room type and staff name might be better in their own lookup tables, depending on your data.

        Buildings

        BuildingId (PK)
        BuildingName

        Rooms

        BuildingId (PK, FK->Buildings)
        RoomNumber (PK)
        ...

        Assets

        AssetNumber (PK)
        BuildingId (FK->Rooms)
        RoomNumber (FK->Rooms)
        ...


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        R Offline
        R Offline
        RickBStewart
        wrote on last edited by
        #3

        Thank You Richard, I will take your suggestion and do a bit of experimentation. It's clearly the case that a few hours of video at an online training site has not fully educated me. LOL... But it may be enough to get me through this project. I will try and find ( and study ) some documentation on database theory when I get a chance. But until then... Thanks

        M R 2 Replies Last reply
        0
        • R RickBStewart

          Thank You Richard, I will take your suggestion and do a bit of experimentation. It's clearly the case that a few hours of video at an online training site has not fully educated me. LOL... But it may be enough to get me through this project. I will try and find ( and study ) some documentation on database theory when I get a chance. But until then... Thanks

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          As a STRONG suggestion you should devote a couple of days to a basic database design book. The consequences of getting your data structure wrong will cost you many multiples of that investment down the track. Depending on the criticality of the project I would even consider getting a professional to do the basic DB design. There is also a site with sample schemas (I have lost the link) that will give you some excellent pointers.

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          • R RickBStewart

            Thank You Richard, I will take your suggestion and do a bit of experimentation. It's clearly the case that a few hours of video at an online training site has not fully educated me. LOL... But it may be enough to get me through this project. I will try and find ( and study ) some documentation on database theory when I get a chance. But until then... Thanks

            R Offline
            R Offline
            Ralph D Wilson II
            wrote on last edited by
            #5

            To somewhat piggy-back on the previous suggestions, I would recommend that you consider using an Identity column for your Primary Key and then using the PK to accomplish the links beween tables. For Example: Table: Buildings BuildingID [int] Primary Key Identity (1-1) BuildingName [VarChar] (25) ... Table: BuildingRooms BuildingRoomID [int] Primary Key Identity (1-1) BuildingID INT RoomName [Varchar] (10) ... Table: Assets AssetID [int] Primary Key Identity (1-1) AssetNumber [varchar] (25) AssetName [VarChar] (50) BuildingRoomID [int] ... By using using the Identity columns for the PK's of the tables, you can make your joins much more easily. You may also want to add some Unique Indexes on, for instance, the BuildingName, RoomName, and AssetNumber columns just to make sure someone doesn't accidentally add those items multiple times. ;-) Check out the following link regarding what is termed "normalization" . . . which is what we have suggested with regard to your tables. ;-) http://www.dbnormalization.com/rules-of-normalization-i

            R 1 Reply Last reply
            0
            • R Ralph D Wilson II

              To somewhat piggy-back on the previous suggestions, I would recommend that you consider using an Identity column for your Primary Key and then using the PK to accomplish the links beween tables. For Example: Table: Buildings BuildingID [int] Primary Key Identity (1-1) BuildingName [VarChar] (25) ... Table: BuildingRooms BuildingRoomID [int] Primary Key Identity (1-1) BuildingID INT RoomName [Varchar] (10) ... Table: Assets AssetID [int] Primary Key Identity (1-1) AssetNumber [varchar] (25) AssetName [VarChar] (50) BuildingRoomID [int] ... By using using the Identity columns for the PK's of the tables, you can make your joins much more easily. You may also want to add some Unique Indexes on, for instance, the BuildingName, RoomName, and AssetNumber columns just to make sure someone doesn't accidentally add those items multiple times. ;-) Check out the following link regarding what is termed "normalization" . . . which is what we have suggested with regard to your tables. ;-) http://www.dbnormalization.com/rules-of-normalization-i

              R Offline
              R Offline
              RickBStewart
              wrote on last edited by
              #6

              I've spent some time looking into normalization and currently my tables should be in 3rd Normal Form. I also have my application talking to the remote SQL server via the Entity Framework, so I'm getting there. Thanks all for your suggestions, they were most helpful. Rick...

              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