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 Unique/NonUnique Key

SQL Server Unique/NonUnique Key

Scheduled Pinned Locked Moved Database
databasesql-serversysadminxmlhelp
4 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.
  • D Offline
    D Offline
    Dirso
    wrote on last edited by
    #1

    Hi, To get the unique keys from a given table, i use a sql statement like this: Select [COLUMN_NAME] as FieldName, [CONSTRAINT_NAME] as KeyName, [ORDINAL_POSITION] as Ordinal FROM [INFORMATION_SCHEMA].[KEY_COLUMN_USAGE] WHERE TABLE_SCHEMA=@schema AND TABLE_NAME=@table AND CONSTRAINT_NAME LIKE "IX_%" ORDER BY [CONSTRAINT_NAME], [ORDINAL_POSITION] First of all, it waorks fine, but I don't like the idea of selecting it by the name, but it was the only solution I found... For now it works, so if it's the only way, I'll live with it ;) My main problem is that I didn't find any [INFORMATION_SCHEMA] table where the NonUnique keys are stored, but it has to be somewhere, right? Any ideas about it?

    Thanks, Dirso

    M D D 3 Replies Last reply
    0
    • D Dirso

      Hi, To get the unique keys from a given table, i use a sql statement like this: Select [COLUMN_NAME] as FieldName, [CONSTRAINT_NAME] as KeyName, [ORDINAL_POSITION] as Ordinal FROM [INFORMATION_SCHEMA].[KEY_COLUMN_USAGE] WHERE TABLE_SCHEMA=@schema AND TABLE_NAME=@table AND CONSTRAINT_NAME LIKE "IX_%" ORDER BY [CONSTRAINT_NAME], [ORDINAL_POSITION] First of all, it waorks fine, but I don't like the idea of selecting it by the name, but it was the only solution I found... For now it works, so if it's the only way, I'll live with it ;) My main problem is that I didn't find any [INFORMATION_SCHEMA] table where the NonUnique keys are stored, but it has to be somewhere, right? Any ideas about it?

      Thanks, Dirso

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

      I use the following against the sysobjects table (it predates the views) it is designed the get the IDENTITY column which for me is always the primary key for a table. It may help!

      select syscolumns.name from syscolumns INNER JOIN sysobjects on syscolumns.ID = sysobjects.ID
      where sysobjects.name = 'TableName' and colstat & 1 = 1

      Fixed that univote - this is a valid concern and you are right in not wanting to rely on a constrain name.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • D Dirso

        Hi, To get the unique keys from a given table, i use a sql statement like this: Select [COLUMN_NAME] as FieldName, [CONSTRAINT_NAME] as KeyName, [ORDINAL_POSITION] as Ordinal FROM [INFORMATION_SCHEMA].[KEY_COLUMN_USAGE] WHERE TABLE_SCHEMA=@schema AND TABLE_NAME=@table AND CONSTRAINT_NAME LIKE "IX_%" ORDER BY [CONSTRAINT_NAME], [ORDINAL_POSITION] First of all, it waorks fine, but I don't like the idea of selecting it by the name, but it was the only solution I found... For now it works, so if it's the only way, I'll live with it ;) My main problem is that I didn't find any [INFORMATION_SCHEMA] table where the NonUnique keys are stored, but it has to be somewhere, right? Any ideas about it?

        Thanks, Dirso

        D Offline
        D Offline
        David Skelly
        wrote on last edited by
        #3

        This blog post may help: http://blog.sqlauthority.com/2007/09/16/sql-server-2005-list-all-the-constraint-of-database-find-primary-key-and-foreign-key-constraint-in-database/[^]

        1 Reply Last reply
        0
        • D Dirso

          Hi, To get the unique keys from a given table, i use a sql statement like this: Select [COLUMN_NAME] as FieldName, [CONSTRAINT_NAME] as KeyName, [ORDINAL_POSITION] as Ordinal FROM [INFORMATION_SCHEMA].[KEY_COLUMN_USAGE] WHERE TABLE_SCHEMA=@schema AND TABLE_NAME=@table AND CONSTRAINT_NAME LIKE "IX_%" ORDER BY [CONSTRAINT_NAME], [ORDINAL_POSITION] First of all, it waorks fine, but I don't like the idea of selecting it by the name, but it was the only solution I found... For now it works, so if it's the only way, I'll live with it ;) My main problem is that I didn't find any [INFORMATION_SCHEMA] table where the NonUnique keys are stored, but it has to be somewhere, right? Any ideas about it?

          Thanks, Dirso

          D Offline
          D Offline
          Dirso
          wrote on last edited by
          #4

          Thank you both for your suggestions. I'll try it out in afew days and if I find a solution, I'll post it here.

          Thanks, Dirso

          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