Get column index using column name mySql - mysql

I need a query that will give me the name and location (index of the column) of PRIMARY KEY
in different tables.
I can find the name of the PRIMARY KEY column using:
SELECT `COLUMN_NAME`FROM `information_schema`.`COLUMNS`WHERE
(`TABLE_SCHEMA` = 'dbName') AND (`TABLE_NAME` = 'tableName')
AND (`COLUMN_KEY` = 'PRI');
How do I get it's column position by that name or by another method?

try this
SELECT ORDINAL_POSITION FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA='your_db_name' AND TABLE_NAME ='your_table_name'
AND COLUMN_NAME = 'your_column_name'

Related

How can I select one column from an SQL query with the "SHOW" keyword?

How can I select one column from this SQL query:
SHOW KEYS FROM table WHERE Key_name = 'PRIMARY'
like this:
SELECT name FROM table WHERE id = 1
This code worked for me
SELECT k.column_name
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='YOURDATABASE'
AND t.table_name='YOURTABLE';
If your goal is to fetch the column name of primary keys, you can use this query which gets the information from the information_schema.statistics table. You can filter the information by schema / table name as well.
SELECT column_name
FROM information_schema.statistics
WHERE table_schema='schema_name'
AND table_name = 'table_name'
AND index_name='PRIMARY'
ORDER BY seq_in_index;

How do you get whether a column is a primary key of a table from with all columns?

I am getting all the columns of all tables listed down of my data base with following query in mySQL
SELECT * FROM information_schema.columns
WHERE table_schema = DATABASE()
ORDER BY table_name, ordinal_position
I want to get all columns whether it's a primary key or not? For primary key columns to appear as y and non primary key columns to appear as n in this query. How can I achieve that with this query?
You can use COLUMN_KEY column and CASE WHEN:
SELECT column_name, CASE WHEN COLUMN_KEY = 'PRI' THEN 'y' ELSE 'n' END AS result
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND `table_name` = 'tab' -- table name
ORDER BY `table_name`, ordinal_position
SqlFiddleDemo

How to find all primary keys which don't have an auto-increment

How to find all primary keys in all tables in a database, which don't have an auto-increment identifier to it. We have a large amount of tables and would like to identify all tables which don't have the auto increment identifier on the primary key.
You can extract this information from the information_schema.columns table
select distinct table_name
from information_schema.columns
where table_schema = 'DATABASENAME'
and table_name not in (select table_name
from information_schema.columns
where table_schema = 'DATABASENAME'
and column_key = 'PRI'
and data_type = 'int'
and extra = 'auto_increment')
This looks for all tables in one database having an auto_increment column and then returns the remaining tables. This also correctly detects tables with composite keys.
You can find that kind of information in the table information_schema.columns. The column column_key will be PRI and the column extra will contain auto_increment if it is auto incremented.
SELECT
table_schema,
table_name,
column_name
FROM
information_schema.columns
WHERE
column_key = 'PRI'
AND extra <> 'auto_increment'
AND data_type = 'int'
In this SQL Fiddle you can see that the sample table has "PRI" and "auto_increment" in the respective columns.

Can I find out the next auto_increment to be used?

Is it possible to find out what the next auto increment will be for my primary key without executing an INSERT INTO query? Some rows are deleted meaning that it's not as easy as just adding one to a SELECT MAX query on the PK. Many thanks.
If you really want to know next auto_increment value try SHOW TABLE STATUS returns next Auto_increment field, e.g.:
SHOW TABLE STATUS WHERE name = your_table_name;
or
SELECT Auto_increment
FROM information_schema.tables
WHERE table_schema = DATABASE() AND table_name = your_table_name
You can get the value by executing
SHOW TABLE STATUS WHERE Name = nameOfTableHere
and then retrieving the column 'Auto_Increment' from the result
Try the following:
SELECT Auto_increment
FROM information_schema.tables
WHERE table_name= 'tableName'
AND table_schema = DATABASE();
This is also possible:
(SELECT (SELECT your_primary_key FROM Your_Table ORDER BY your_primary_key DESC LIMIT 1)+1);

How to determine primary key of a specific SQL table?

I mean not mean by manually reading the output of show create table..., but by select ... so that the primary key name is output directly as the result?
The following query should give you the PKs - just plug in your table_schema and table_name at the bottom of the query.
SELECT k.`COLUMN_NAME`
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` = 'dbName'
AND t.`TABLE_NAME` = 'tableName';
show index from CC_CSR_USER where Key_name = 'PRIMARY';
CC_CSR_USER is a table name. Have a look on the column_name field on the result, you will get that PRIMARY KEY column of the table.