INFORMATION_SCHEMA.COLUMNS : and must be a Primary Key : Mysql query - mysql

I've added a question on most of the solved problem with regards to COLUMN_NAME searching but didn't get any feedback yet.
How to do a query that goes like this:
I want to see all the tables that has a column name of 'Type_ID' and
must be a Primary key to the table.

SELECT table_schema, table_name
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE (table_schema, column_name, constraint_name)
= ('mydatabase', 'Type_ID', 'PRIMARY');

Related

Cannot Drop unique constraint [duplicate]

I created them, but I forgot which ones they are.
I just want to
show them.
remove all the constraints on a table.
select distinct CONSTRAINT_NAME
from information_schema.TABLE_CONSTRAINTS
where table_name = 'table_name' and constraint_type = 'UNIQUE';
This doesn't produce elegant output but is easy to remember:
SHOW CREATE TABLE table_name;
select distinct CONSTRAINT_NAME
from information_schema.TABLE_CONSTRAINTS
where CONSTRAINT_SCHEMA = 'mysql'
This query returns primay keys, unique keys and foreign ones :
show indexes from table_name;
The OP asked for a single table, which this will do.
In addition, removing the last where clause will show all columns for a database which are protected by unique constraints:
SELECT
CONSTRAINT_NAME,
TABLE_NAME,
COLUMN_NAME
FROM information_schema.KEY_COLUMN_USAGE
WHERE
CONSTRAINT_NAME LIKE 'UNIQ%'
AND TABLE_SCHEMA = 'your_database_name'
AND TABLE_NAME = 'your_table_name';
Unfortunately mysql doesn't facilitate the removal of indexes based on a query result. You could execute the output of the following query to drop all unique columns in 2 queries:
SELECT CONCAT(
'ALTER TABLE ',
TABLE_NAME,
' DROP INDEX ',
CONSTRAINT_NAME,
'; -- drops ',
COLUMN_NAME,
' constraint'
)
FROM information_schema.KEY_COLUMN_USAGE
WHERE
CONSTRAINT_NAME LIKE 'UNIQ%'
AND TABLE_SCHEMA = 'your_database_name';

MYSQL - Determine tables primary key in order

I am looking for a query which gives me the primary key of all tables from a database, at the same order that it was created, for example:
CREATE TABLE a_antennaport
...
PRIMARY KEY (dateday,neid,cn,srn,sn,pn)
I need as output the fields in the same order:
dateday,neid,cn,srn,sn,pn
I already tried:
SELECT GROUP_CONCAT(COLUMN_NAME), TABLE_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
TABLE_SCHEMA = schema()
AND CONSTRAINT_NAME='PRIMARY'
GROUP BY TABLE_NAME;
Your query should work already (in fact, it does. Tested, to be sure). Only thing you can improve to make sure, that the columns are in the right order, is to order by the ordinal position in the group_concat().
SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY ORDINAL_POSITION), TABLE_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
TABLE_SCHEMA = schema()
AND CONSTRAINT_NAME='PRIMARY'
GROUP BY TABLE_NAME;

Desc primary key of all tables in MYSQL

I want to get the result of Desc for only specific columns of each table in the information schema, let's say Primary Key.
Can this be done?
I tried things like
SELECT * FROM (DESC TABLENAME) WHERE ....;
but it did not work.
Also I want this to work for all tables.
DESC aka DESCRIBE aka SHOW COLUMNS will not work in a subquery.
But SELECT will.
You can for example replace
SHOW COLUMNS FROM t IN test LIKE '%2';
with
SELECT column_name AS `Field`, column_type AS `Type`,
is_nullable AS `Null`,
column_key as 'Key',
column_default AS `Default`, extra AS `Extra`
FROM information_schema.columns
WHERE table_schema = 'test'
AND table_name = 't'
AND column_name like '%2';
and you'll get the same thing.

Finding the values of the foreign key referenced field

I have here an SQL query that I got from this source. What it does is it finds all the primary keys and their references in the database.
select
concat(table_name, '.', column_name) as 'foreign key',
concat(referenced_table_name, '.', referenced_column_name) as 'references'
from
information_schema.key_column_usage
where
referenced_table_name is not null
and table_schema = 'my_database'
I modified it a little to become this
select
table_name as fk_table_name, column_name as 'foreign key',
referenced_table_name as ref_table_name, referenced_column_name as 'references'
from
information_schema.key_column_usage
inner join
information_schema.referenced_table_name
where
referenced_table_name is not null
and column_name = 'customer_number'
and referenced_table_name = 'accepted_orders'
Now it doesn't work. The error it returns is ' #1109 - Unknown table 'referenced_table_name' in information_schema'. My goal is instead of just displaying what the referenced column name is, it gives me all the values of that column instead.
So instead of telling me that the foreign key customer_number in accepted_orders references the primary key customer_number in customer_records, I want to get all the values of customer_number in customer_records instead.
I thought of using an inner join on the result of the query but apparently it won't let me. How do I do this? Do I have to use separate SQL statements?
You seem to be, as the error message says, using referenced_table_name in a context where the query parser wants a table name, not a column name. You wrote:
inner join
information_schema.referenced_table_name
That doesn't make any sense because you're trying to join to a column, not a table.
Try omitting the two lines above from your query.

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.