Unknown table 'table_name' in information_schema - mysql

I want to show index from each table that has table_schema='foo' (database name).
mysql> show index from table_name from information_schema.tables where table_schema='foo';
ERROR 1109 (42S02): Unknown table 'table_name' in information_schema
From the error, I see that the query treats 'table_name' as a table in information_schema. How do I rewrite the query to treat 'table_name' as a column in information_schema.tables?

You're approaching this wrong, and you're making up syntax that doesn't exist.
I suggest the way you want to get the indexes is by reading the INFORMATION_SCHEMA.STATISTICS table, not the TABLES table.
The following query has the same columns as SHOW INDEXES:
SELECT table_name AS `Table`, Non_unique, index_name AS Key_name,
Seq_in_index, Column_name, Collation, Cardinality, Sub_part,
Packed, Nullable, Index_type, Comment, Index_comment
FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_schema = 'foo';
You might think there should be an I_S table called "INDEXES" but in fact the system table for index objects is named "STATISTICS". Go figure.

Related

MySQL get list of tables ending with specific name and it's (table's) comment

I have multiple tables in my multiple databases.
On different servers, i use MySQL / PostgreSQL / MS SQL.
I keep short table namesbut the comments given to the tables are with full explanation.
I want query that will show me tables ending with "com" and also the comment given to each table (table's comment).
In MySQL, I know:
SELECT table_name FROM information_schema.tables where table_name like "%com"
But this shows all tables from all databases.
For MySQL, check out following:
SELECT table_name FROM information_schema.tables;
will show all table names in all databases;
SELECT table_name,table_comment FROM information_schema.tables
will show all table names + comment in all databases;
interesting thing, you can fire
SELECT * FROM information_schema.tables;
to know what all info you can get of a table.
SELECT table_name,table_comment FROM information_schema.tables
where
table_schema = 'sifr_b';
will show all table names + comment in "sifr_b" database;
SELECT table_name,table_comment FROM information_schema.tables
where
table_schema = 'sifr_b' and
table_name like "%com";
will show those table names + comment in "sifr_b" database, that have table name ending with "com";

MySQL - How to alter column datatype in all databases?

On my remote server, I need to update a column in a table in all databases. How can I do this all at once? I have over than hundred databases with the same table.
ALTER TABLE tablename MODIFY columnname VARCHAR(255);
You can write a SQL query to emit a script that does this. The query looks at the information_schema table describing columns in your server, and generates a sequence of ALTER queries. You then run those queries.
SELECT CONCAT(' ALTER TABLE `', TABLE_SCHEMA, '`.`', TABLE_NAME,
'` MODIFY `',COLUMN_NAME,'` VARCHAR(255);') ddl
FROM information_schema.`COLUMNS`
WHERE TABLE_NAME = 'tablename'
AND COLUMN_NAME IN ('columnname')
There isn't any oneliner to do ALTER TABLE *.tablename or anything like that.

ALTER TABLE ALL TABLES ROW_FORMAT=Fixed;

How can I run a command for all tables in a database?
ALTER TABLE table_name ROW_FORMAT=Fixed;
I have more than 40000 tables, I want to change them all in one go.
You cannot alter table more than one table per ALTER statement; but you can use a query on information_schema.tables to generate the alter statements.
Something like
SELECT CONCAT('ALTER TABLE `', table_name, '` ROW_FORMAT=fixed;') AS aQuery
FROM information_schema.tables
WHERE table_schema = 'myschema'
Keep in mind FIXED is not supported for InnoDB tables, and I am not 100% sure if it can even be explicitly set for MyISAM ones (or is entirely dependent on the table's columns' data types.)

View the constraints enforced on a table- SQL

Since I am not much of a DB guy, I have a query.I use Mysql.
I am given a table which has around 12 columns and that table has a PRIMARY key,UNIQUE & a FOREIGN key defined.
Is there a way to find on which columns the constraints are defined?
I came across one query:
SHOW INDEX FROM tablt_name;
But It does not give a clear idea, only the primary key column is displayed by the above query.
If there is any other way to get the info, pls help
You can try like thisL
USE information_schema;
SELECT table_name,
column_name,
constraint_name,
referenced_table_name,
referenced_column_name
FROM key_column_usage
WHERE table_schema = ""
AND table_name = ""
AND referenced_column_name IS NOT NULL;
or
DESCRIBE table_name

Delete MySQL column index without knowing its name

I have an application which uses Hibernate to support Oracle and MySQL databases. After an update I have to manually delete some columns with indexes/constraints on it. These indexes have Hibernate generated random names.
In Oracle I can do this:
ALTER TABLE table_name DROP (column_name) CASCADE CONSTRAINTS;
Unfortunately this isn't possible for MySQL. Is there a possibility to do something like this
DROP INDEX (SELECT Key_name FROM (SHOW INDEX FROM table_name WHERE Column_name = 'column_name')) ON table_name;
before I drop the column?
EDIT: This should work without user interaction in a SQL script.
You can select indexes for a table form information_schema:
SELECT DISTINCT INDEX_NAME, TABLE_NAME, TABLE_SCHEMA FROM information_schema.STATISTICS;
There is no need to manually delete the indexes, MySQL 5.7 Reference Manual says:
If columns are dropped from a table, the columns are also removed from
any index of which they are a part. If all columns that make up an
index are dropped, the index is dropped as well. If you use CHANGE or
MODIFY to shorten a column for which an index exists on the column,
and the resulting column length is less than the index length, MySQL
shortens the index automatically.
To get all indexes for a particular database (replace <Database_Name> with your database name), use:
SELECT DISTINCT INDEX_NAME
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA LIKE '<Database_Name>';
To get all indexes for a table (replace <Table_Name> with your table name) of a particular database, use:
SELECT DISTINCT INDEX_NAME
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA LIKE '<Database_Name>' AND
TABLE_NAME LIKE '<Table_Name>';
To get all indexes of a specific column (replace <Column_Name> with your column name) of a table in a particular database, use:
SELECT DISTINCT INDEX_NAME
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA LIKE '<Database_Name>' AND
TABLE_NAME LIKE '<Table_Name>' AND
COLUMN_NAME LIKE '<Column_Name>';
In addition to that, you may also use any Wildcard character in the LIKE operator to get specific records, like:
SELECT DISTINCT INDEX_NAME
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA LIKE '<Database_Name>' AND
TABLE_NAME LIKE 'tbl_prefix_%';