Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to display primary key of a table and database?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.77k
    Comment on it

    Hello friends,
    Today we learn how we can display the primary key of a table or database.

    1. To display the primary key in a table we can execute any of the following command:

    SHOW COLUMNS FROM <database_name>.<table_name> WHERE `Key` = "PRI";
    SHOW { KEYS | INDEXES | INDEX } FROM <database_name>.<table_name> WHERE Key_name = 'PRIMARY';
    SELECT k.column_name, t.table_name, t.table_schema, t.constraint_type FROM information_schema.table_constraints t JOIN information_schema.key_column_usage k USING ( constraint_name, table_schema, table_name ) WHERE t.constraint_type = 'PRIMARY KEY' AND t.table_schema = '<database_name>' AND t.table_name='<table_name>';

    2. To display the primary key of a database we need to execute the following command:

    SELECT k.column_name, t.table_name, t.table_schema, t.constraint_type
    FROM information_schema.table_constraints t
    JOIN information_schema.key_column_usage k
    USING ( constraint_name, table_schema, table_name )
    WHERE t.constraint_type = 'PRIMARY KEY'
    AND t.table_schema = '<database_name>'

    In all above MySQL statement in place of <database_name> we will write our database name and in place of <table_name> will write our table name.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: