Results 1 to 3 of 3
  1. #1
    Join Date
    May 2012
    Posts
    2

    Unanswered: How to check if an atribute is a primary Key?

    Hello! Here is my problem:

    How can I check if an attribute is a primary key of table x?

    For example:

    I have the attribute name "student" and the table name "College", I need to know if "student" is the primary key of the table "College".

    Other solution would be:

    What's the primary key of table "College"? I just need the name...

    That's all, I was taking a look at the information_schema but since I'm still learning SQL I'm totally lost.

    Any help would be appreciated..


    Thanks in advance.

  2. #2
    Join Date
    Nov 2003
    Posts
    2,933
    Provided Answers: 12
    Quote Originally Posted by Mr.Anything View Post
    Hello! Here is my problem:

    How can I check if an attribute is a primary key of table x?

    For example:

    I have the attribute name "student" and the table name "College", I need to know if "student" is the primary key of the table "College".
    Code:
    select column_name
    from information_schema.key_column_usage
    where table_name = 'college'
    order by ordinal_position
    Returns all primary key columns of the table. If you have the same table in multiple schemas, you will need to add a condition on table_schema to the where clause.

    What's the primary key of table "College"? I just need the name...
    Code:
    select constraint_name
    from information_schema.table_constraints
    where table_name = 'College'
    and constraint_type = 'PRIMARY KEY'
    Note that the table name is case-sensitive. So depending on how you created your table you might need to pass 'college' or 'college'.

  3. #3
    Join Date
    May 2012
    Posts
    2
    Thank you Very much! You help me a lot!

Posting Permissions

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