Finding the values of the foreign key referenced field - mysql

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.

Related

Show information about table columns and keys

In order to retrieve information about columns, e.g: their name, ordinal position, datatype, etc, I use the following query
SELECT *
FROM information_schema.`COLUMNS`
WHERE TABLE_SCHEMA = 'myDbName'
AND TABLE_NAME = 'myTable'
This returns all the information I need except information about which columns are PK and FK, especially FKs. I noticed that one of the columns returned by that query is COLUMN_KEY. For PKs, this column suffices because it has a value of PRI, but for FKs, this column either has a MUL or nothing. Is there a way to retrieve information about columns that includes key information reliably?
I don't need to know details about the PK or FK, I just want to get information about the columns AND to know if they are PK or FK.
This should give you an overview of columns, keys and FK constraints. I've arranged the keys to distinguish between those that reference another table and those that don't, which is somewhat against the natural order of the underlying views. You can add or remove columns to suit your requirement but you will get to a level of detail where you might as well just run show create table table_name.
N.B., this should work in mysql > 5.7.6 , if you are using an earlier version then you'll need to remove generation_expression from the SELECT.
SELECT c.`ordinal_position` AS '#',
c.`column_name` AS 'Name',
c.`column_type` AS 'Type',
c.`is_nullable` AS 'Allow NULL',
IFNULL(c.`column_default`,'') AS 'Default',
CONCAT(c.`extra`, ' ',c.`generation_expression`) AS 'Extra',
IFNULL((SELECT GROUP_CONCAT(CONCAT(IF(s.`non_unique` = 0 ,'*',''),s.`index_name`, '(', s.`seq_in_index`,')'))
FROM `information_schema`.`statistics` s
WHERE s.`table_schema` = c.`table_schema`
AND s.`table_name` = c.`table_name`
AND s.`column_name` = c.`column_name`
),'') as 'Key name(pos) *=unique',
IFNULL((SELECT GROUP_CONCAT(
CONCAT(k.`constraint_name`, ': ', k.`referenced_table_name`,' (', k.`referenced_column_name`,')'))
FROM `information_schema`.`key_column_usage` k
WHERE k.`table_schema` = c.`table_schema`
AND k.`table_name` = c.`table_name`
AND k.`column_name` = c.`column_name`
AND k.`referenced_table_name` IS NOT NULL
),'') AS 'FK name: table(column)'
FROM `information_schema`.`columns` c
WHERE c.`table_schema` = 'dbname'
AND c.`table_name` = 'table_name'
ORDER BY c.ordinal_position;
Assuming you're using INNODB for your tables, you can find out if a key is foreign key or primary key using the below script
SELECT * FROM information_schema.TABLE_CONSTRAINTS
WHERE information_schema.TABLE_CONSTRAINTS.CONSTRAINT_TYPE IN ('FOREIGN KEY', 'PRIMARY KEY')
AND information_schema.TABLE_CONSTRAINTS.TABLE_SCHEMA = 'myschema'
AND information_schema.TABLE_CONSTRAINTS.TABLE_NAME = 'mytable';

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;

How to get name parent table by name foreign field in child table?

I need to write a php script to work with tables in MySql database. I know table name and I need know name parent table by name foreign field in this child table. How can I do it with SQL query?
UPDATE
SELECT referenced_table_name, referenced_column_name
FROM information_schema.key_column_usage
WHERE table_name = '[child_table_name]'
AND column_name = '[foreign_key_field_name]'
select referenced_table_name
from information_schema.REFERENTIAL_CONSTRAINTS
where table_name ='[child_table_name]'
--and constraint_name ='[foreign_key_constraint_name]'
UPDATE
select referenced_table_name,
referenced_column_name
from information_schema.key_column_usage where table_name ='[child_table_name]'
and column_name='[foreign_key_constraint_name]';

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

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');

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.