Results 1 to 5 of 5

Thread: sql query

  1. #1
    Join Date
    Jul 2011
    Posts
    4

    Unanswered: sql query

    can any one suggest how to write query in ms sql server to
    display column name of a table haveing primary key constraint..

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    I will not do it for you but I can tell you that it can be done by querying these 2 informaition schema views.

    INFORMATION_SCHEMA.TABLE_CONSTRAINTS
    INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Jul 2011
    Posts
    4
    thanks for your reply
    but my question is i need to get the name of a column haveing primary key or foreign key ...in a table ..
    so that i can come to know the relation between tables ...
    actaully need to handle this query in my java code jdbc.

  4. #4
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Pirated from some code I already had:
    Code:
    select	Objects.Object_ID,
    	Objects.Name as ObjectName,
    	Columns.column_id as ColumnID,
    	Columns.name as ColumnName
    from	sys.objects as Objects
    	inner join sys.columns Columns on Objects.object_id = Columns.object_id
    	inner join sys.index_columns IndexColumns
    		on Columns.object_id = IndexColumns.object_id
    		and Columns.column_id = IndexColumns.column_id
    	inner join sys.indexes Indexes
    		on IndexColumns.object_id = Indexes.object_id
    		and IndexColumns.index_id = Indexes.index_id
    		and Indexes.is_primary_key = 1
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  5. #5
    Join Date
    Jul 2011
    Posts
    4
    Thanks ....
    this query working fine ....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •