Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the primary key "column name" of a specific table in MySQL

Tags:

php

mysql

Our system creates a log of every table that was updated or inserted with new content, it saves the table name, the ID value of the updated row or the last id inserted and the timestamp of the event.

This is useful because we can check what is the latest table updated and refresh the information being displayed to the user as soon as a change occurs, but we don't have the column name of the ID saved on the log.

The problem is that we are programming case by case in php.

if($tableName == 'Clients'){ $idname = 'CID'; }

is there a way to just ask MySQL: give me the primary key column name of a specific table, something like:

SHOW COLUMN_NAME FROM CLEINTS WHERE KEY_NAME = 'PRIMARY KEY';

I remember I had used a query like this in the past, but I can't remember what it was, I have found some solutions for SQL but don't seem to work in MySQL or are way too complicated (using information_schema), the query that I am looking for is very simple, almost like the example I just gave.

Thanks

like image 744
multimediaxp Avatar asked Sep 12 '18 05:09

multimediaxp


People also ask

How can I get column names from a table in MySQL?

Get column names from a table using INFORMATION SCHEMA. The information schema is used to get information about the MySQL server like table name, database name column names, data types, etc.

How do I get the column names of a table?

We can verify the data in the table using the SELECT query as below. We will be using sys. columns to get the column names in a table. It is a system table and used for maintaining column information.

How can I get only column names from a table in SQL?

USE db_name; DESCRIBE table_name; it'll give you column names with the type.


3 Answers

You can get the details from information_schema database. Use the following query:

SELECT COLUMN_NAME 
FROM information_schema.KEY_COLUMN_USAGE 
WHERE TABLE_NAME = 'Your_table_name' 
  AND CONSTRAINT_NAME = 'PRIMARY'
like image 77
Vykintas Avatar answered Sep 17 '22 19:09

Vykintas


You can get KEYS by using

SHOW KEYS FROM CLEINTS WHERE Key_name = 'PRIMARY'

another alternative with SHOW to get INDEXES as @nick mentioned in comment

SHOW INDEXES FROM CLEINTS WHERE Key_name = 'PRIMARY'

syntax:

SHOW [EXTENDED] {INDEX | INDEXES | KEYS}
    {FROM | IN} tbl_name
    [{FROM | IN} db_name]
    [WHERE expr]

documentation link for more details.

like image 45
Jigar Shah Avatar answered Sep 19 '22 19:09

Jigar Shah


Hello @multimediaxp i think this may help you.

SELECT `COLUMN_NAME`
FROM `information_schema`.`COLUMNS`
WHERE (`TABLE_SCHEMA` = 'yourDBName')
  AND (`TABLE_NAME` = 'Clients')
  AND (`COLUMN_KEY` = 'PRI');

For more detail please see given link: http://mysql-0v34c10ck.blogspot.com/2011/05/better-way-to-get-primary-key-columns.html

like image 43
Yagnesh Makwana Avatar answered Sep 16 '22 19:09

Yagnesh Makwana