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. Primary Indexed?

Primary Indexed?

Scheduled Pinned Locked Moved Database
databasequestion
6 Posts 3 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.
  • M Offline
    M Offline
    Mike654321
    wrote on last edited by
    #1

    CREATE TABLE [PreferenceType] ( [PreferenceTypeId] [BIGINT] IDENTITY (1,1) NOT NULL, [LongName] [NVARCHAR](500), [ShortName] [NVARCHAR](250) ); CREATE TABLE [Table2] ( [Table2Id] [BIGINT] IDENTITY (1,1) NOT NULL, [PreferenceTypeId] [INT] ) Given the Preference Lookup table above, if I do an Inner Join to Table2 on the PreferenceTypeId field, I should index like this: CREATE INDEX [PreferenceTypeId_Idx] ON [Table2]([PreferenceTypeId]); <--- good? But I shouldn't index like this: CREATE INDEX [PreferenceTypeId2_Idx] ON [PreferenceType]([PreferenceTypeId]); <--- bad? because Primary Key fields should already be indexed, correct?

    I 1 Reply Last reply
    0
    • M Mike654321

      CREATE TABLE [PreferenceType] ( [PreferenceTypeId] [BIGINT] IDENTITY (1,1) NOT NULL, [LongName] [NVARCHAR](500), [ShortName] [NVARCHAR](250) ); CREATE TABLE [Table2] ( [Table2Id] [BIGINT] IDENTITY (1,1) NOT NULL, [PreferenceTypeId] [INT] ) Given the Preference Lookup table above, if I do an Inner Join to Table2 on the PreferenceTypeId field, I should index like this: CREATE INDEX [PreferenceTypeId_Idx] ON [Table2]([PreferenceTypeId]); <--- good? But I shouldn't index like this: CREATE INDEX [PreferenceTypeId2_Idx] ON [PreferenceType]([PreferenceTypeId]); <--- bad? because Primary Key fields should already be indexed, correct?

      I Offline
      I Offline
      i j russell
      wrote on last edited by
      #2

      You should make PreferenceType.PreferenceTypeId a PrimaryKey (Yes it will be automatically indexed) and you should make Table2.PreferenceTypeId a ForeignKey constraint referencing PreferenceType.PreferenceTypeId (this will also be automatically indexed). [Edit] Foreign keys are NOT automatically indexed.

      modified on Wednesday, March 17, 2010 5:06 PM

      M 1 Reply Last reply
      0
      • I i j russell

        You should make PreferenceType.PreferenceTypeId a PrimaryKey (Yes it will be automatically indexed) and you should make Table2.PreferenceTypeId a ForeignKey constraint referencing PreferenceType.PreferenceTypeId (this will also be automatically indexed). [Edit] Foreign keys are NOT automatically indexed.

        modified on Wednesday, March 17, 2010 5:06 PM

        M Offline
        M Offline
        Mike654321
        wrote on last edited by
        #3

        KEWL, i learned something new, that foreign keys automatically get indexed for me.

        J 1 Reply Last reply
        0
        • M Mike654321

          KEWL, i learned something new, that foreign keys automatically get indexed for me.

          J Offline
          J Offline
          Jorgen Andersson
          wrote on last edited by
          #4

          I believe that's incorrect. Creating an index on a foreign key is usually recommended, but not done automatically[^].

          "When did ignorance become a point of view" - Dilbert

          I 1 Reply Last reply
          0
          • J Jorgen Andersson

            I believe that's incorrect. Creating an index on a foreign key is usually recommended, but not done automatically[^].

            "When did ignorance become a point of view" - Dilbert

            I Offline
            I Offline
            i j russell
            wrote on last edited by
            #5

            Thank you for the correction and the links.

            J 1 Reply Last reply
            0
            • I i j russell

              Thank you for the correction and the links.

              J Offline
              J Offline
              Jorgen Andersson
              wrote on last edited by
              #6

              You're welcome.

              "When did ignorance become a point of view" - Dilbert

              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