I'm trying to figure out the foreign keys relationship within my MySQL databases.
I have many tables that has the "mydomain_id" column located in other tables.
Is there a command line I can use to list all tables that has that particular column?
SELECT
DISTINCT `table_name`
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
COLUMN_NAME IN ('name') AND
TABLE_SCHEMA='symfony_backend';
Gives: (all the tables listed below exist in my database symfony_backend)
table_name
'child'
'country'
'league'
'parent'
'player'
'schools'
'student'
'team'
'user'
You can feed more column names with: COLUMN_NAME IN ('name', 'another_column', 'and_another_one')
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'db_name'
AND column_name = 'mydomain_id'
Related
I have a web application that has multiple tables. These tables and the values in their columns are stores in mysql tables with each mysql table representing one table in the application. Let's say the following is what I in mysql database have:
table1 with column1_1, column1_2, column1_3
table2 with column2_1, column2_2
table3 with column3_1, column3_2, column3_3, column3_4
I have a new requirement wherein I should be able to allow a user to make comments on any of the columns of any of the tables. For this I am creating a mysql table named 'comments' with following fields:
id
table_name (name of table on which comment is being made)
column_name (name of the column on which comment is being made)
Currently, table_name and column_name are varchar. I am wondering if there is a way to restrict table_name column to taking values of the currently existing table names viz. table1, table2 and table3 and also be able to refer to those tables using the entry in table_name. Similarly, for column_name, I want to be able to refer to column present in the other tables. Is there a way to do this?
On MySQL, you can get the names of the tables in a schema by using the following query:
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'your_database_name';
You can also retrieve the names of the columns in a table with the query:
SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'your_database_name' AND table_name = 'your_table';
According to MySQL's documentation, CHECK constraints cannot contain user-defined functions or subqueries (among other things).
Instead of using a CHECK constraint, what you can do is write a trigger that checks whether or not the inserted table_name is valid, and, if so,
check if the column_name belongs to that table.
SELECT Count(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TableName' AND TABLE_SCHEMA = 'DatabaseName'
I tried this to pull count from a single table.
How to achieve this with multiple tables which is more than 10k
Remove your filter condition of Table name AS below
SELECT Count(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'DatabaseName'
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;
i want to query a database which has huge tables for the columns with specific name
ex: fetch all the tables which has column name like 'name'
Thanks.
Try
SELECT table_name -- you might want to add DISTINCT if you use pattern matching with LIKE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = SCHEMA()
AND column_name = 'name' -- or column_name LIKE '%name%'
Here is SQLFiddle demo
If I have a database some_database with multiples tables like:
table_one
table_two
table_three
where
table_one has CPV_one
table_two has CPV_two
table_three has CPV_three
I need to find all tables that have a column like '%CPV%'
This can be done by sql query? I want to avoid check all tables one by one.
You can query information_schema to get a list of each table in your database with a matching column name:
SELECT
TABLE_NAME, COLUMN_NAME
FROM
information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'SOME_DATABASE'
AND COLUMN_NAME LIKE '%CPV%'
EDIT (selecting only the TABLE_NAME column)
As pointed out in a comment, if you want to select only the name of the table (without the list of columns that match), you should also use the DISTINCT keyword - otherwise a duplicate row will be returned for the same table for each column that matches:
SELECT
DISTINCT TABLE_NAME
FROM
information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'SOME_DATABASE'
AND COLUMN_NAME LIKE '%CPV%'
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '%CPV%'
AND TABLE_SCHEMA='YourDatabase';
How about querying the MySQL information_schema?
SELECT TABLE_NAME
FROM information_schema.COLUMNS
WHERE COLUMN_NAME LIKE '%CPV%'
Here you have the code to get the table names with the needed condition:
SELECT DISTINCT TABLE_NAME
FROM information_schema.Columns
WHERE COLUMN_NAME LIKE '%CPV%';
For more details on the INFORMATION SCHEMA take a look here
And for COLUMNS table here