I'm running a quick query to make sure that all the foreign keys in my table are referencing a field that is indexed.
My code is as follows:
select table_name, column_name, index_name from statistics where table_name in (select referenced_table_name from key_column_usage where table_name='table' and table_schema='schema') and column_name in (select referenced_column_name from key_column_usage where table_name='table' and table_schema='schema') order by table_name;
I'm running this on multiple environments, but for some reason I'm receiving duplicate results on one particular environment. The tables are all set up the same, however, so I'm not sure what is causing this behavior.
Any suggestions?
In that mysql server you probably have 2 databases (schemas) with the same structure or you have multi-column foreign keys. Include the table_schema field in the select list of the outer query to confirm.
Use a multi-column in operator or inner join instead of multiple single column in operators in the where clause of your query.
Related
I want to get the detailed list of non-indexed columns of entire tables of a DB.
Please help me how to get the list in MySQL?
Thanks.
For a single table use:
show columns from tablename where `Key` is not null and `Key` != '';
For a whole schema/database use this:
select distinct
table_name,
index_name
from information_schema.statistics
where table_schema = 'your_database_name';
For all databases remove the where clause.
Described in the doc of show columns.
The Key field indicates whether the column is indexed:
If Key is empty, the column either is not indexed or is indexed only
as a secondary column in a multiple-column, nonunique index.
I going to list all columns of a table except PRIMARY key in mysql.
Note : operation is automatically so I don't know the names of fields.
Can you help me for making this query?
This is something worth doing some research on, if you are going to be working with databases in any length.
All DBMS I have worked with thus far have a means of looking at the constraints, columns, and table information. The ones for MySQL that will help you do what you want are likely in the INFORMATION_SCHEMA:
TABLE_CONSTRAINTS The MySQL Reference for this is here.
SELECT table_name, constraint_name, constraint_type FROM INFORMATION_SCHEMA.table_constraints;
COLUMNS The MySQL reference for this is here.
SELECT column_name FROM INFORMATION_SCHEMA.columns;
KEY_COLUMN_USAGE
You should be able to do something like this to get what you want:
SELECT INFORMATION_SCHEMA.key_column_usage.column_name
FROM INFORMATION_SCHEMA.key_column_usage
JOIN INFORMATION_SCHEMA.table_constraints
ON INFORMATION_SCHEMA.key_column_usage.column_name = INFORMATION_SCHEMA.table_constraints.column_name
WHERE INFORMATION_SCHEMA.table_constraints.constraint_type <> 'PRIMARY KEY'
The should be essentially what you need. Views/tables like these can be your best friend when needing to get information about your schema.
I hope that this information helps.
I have an inno-db table with one primary key which is the auto-increment column. I am using the following query to find the next auto-increment id:
SELECT Auto_increment FROM information_schema.tables WHERE table_name='mytable';
However, when I view the results, I get two rows returned with two values (1352, 123841). I've tried resetting the auto-increment value and even deleted the rows below 2000. How can reset the table to return a single result?
You just might have two databases in the MySQL Instance with the same table name.
SELECT table_schema, Auto_increment
FROM information_schema.tables WHERE table_name='mytable';
If you are asking about the database you are standing in, run this
SELECT Auto_increment
FROM information_schema.tables
WHERE table_name='mytable'
AND table_schema=DATABASE();
Give it a Try !!!
You probably have two tables in your db. Try to check the schema and you will find the reason:-
SELECT table_schema, Auto_increment
FROM information_schema.tables
WHERE table_name='mytable';
I am using MySQL and it is not a relational database (foreign keys constraints) so when I perform a delete on a record I want to check whether it is used in any other table, if so, don't delete.
I assume I would have to perform a database-wide search on all tables except it's own. I keep each records id uniform throughout the database.
Example:
Assets
id | date_created | type_id
History
asset_id | date_recorded | store_id
I found a script to find all the table that have the records id:
SELECT
DISTINCT TABLE_NAME,
TABLE_NAME.COLUMN_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
COLUMN_NAME IN ('desander_id')
AND
TABLE_SCHEMA='emp'
But I get an error on the TABLE_NAME.COLUMN_NAME part where it says COLUMN_NAME is unknown. Is there a way I can do this? Am I doing this the right way?
your from table is INFORMATION_SCHEMA.COLUMNS and you are selecting TABLE_NAME.COLUMN_NAME which not possible in MySQL. It should be like table_name.column_name. try this:
SELECT DISTINCT COLUMNS.TABLE_NAME,
COLUMNS.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('desander_id') AND
TABLE_SCHEMA='emp';
I am trying to get a list of tables and their number of rows. I have been using this query:
SELECT TABLE_NAME, TABLE_ROWS
FROM INFORMATION_SCHEMA WHERE TABLE_SCHEMA = 'myDatabase'
I am finding this sometimes returns null. What I would like to do is catch this probably doing something similar to
IFNULL ( TABLE_ROWS, SELECT COUNT(*) FROM ????)
Only I'm not sure what I should enter for ????
how to make it dynamic based on the TABLE_NAME column?.
Edit: Additional information: I found that the 'tables' not displaying are actually views.
Might as well do
SHOW TABLE STATUS FROM YOUR_DATABASE;
It always return a column named as Rows which is number of rows in that table.
From the documentation:
The TABLE_ROWS column is NULL if the table is in the INFORMATION_SCHEMA database.
So, you will propably not need it.