mysql details about tables - mysql

I know about show tables and describe <table_name> commends from mysql, but I'll like to know if thee is any solution which will show me all the tables from a database togheter with the number of columns from each table.
It that possible?
The thing is that i need to compare two databases (with 52 tables each), which seems to have the same structure, but I'm not very sure.

If your user has the permissions, you can query the database "information_schema", table "COLUMNS".

It is a very easy thing to do, MySQL keeps information about its databases in the information_schema database, this acts as the metadata for MySQL so you can find pretty much any information you need provided you have the right privileges to access that database.
I just tested this on MySQL 5.1.54, run it in any database to get the tables with their associated number of columns
SELECT table_name, COUNT(column_name) AS num_columns
FROM information_schema.statistics S
WHERE table_schema = DATABASE()
GROUP BY table_name
ORDER BY table_name

Depending on your version of MySQL you can take a look at the INFORMATION SCHEMA: INFORMATION_SCHEMA Tables

If you are using PHPMyadmin, check for Data Dictionary option at the bottom on database structure tab.

You can compare your databases and view differences with Schema Comparison tool in dbForge Studio for MySQL.
Stand-alone tool - dbForge Schema Compare

Related

Get tables name with the same query in Postgres-MySql-SQLite

i'm working on a school project and i'm building a little server in NodeJS for our project, one of my tasks is to find a way to get the tables name of all "user"tables in the selected Database.
Since i need to connect to different SQL databases, MySQL - PostgreSQL - SQLite, i was looking to get the same result by exectuing the same query, i'll explain my self.
I've start working on MySQL and i've "found" this query:
`SELECT table_name FROM information_schema.tables WHERE table_schema ='${config.DB_Name}'`
And it correctly return all the table name from the selected database. After that i moved to work with PostgreSQL and that query (succesfully run) returns 0 result.
So i've start looking for another query that can return the same result in Postgres, and i found this:
SELECT table_name
FROM information_schema.tables
WHERE table_schema='public'
AND table_type='BASE TABLE';
I was wondering, is there any Query which, run on MySQL-PostgreSQL-SQLite, give me the same kind of result?
Your PostgreSQL query will work fine on all databases that support SQL standard feature F021. If it doesn't work in MySQL, then either it doesn't support that feature, or it is a bug.
Note that what PostgreSQL and the SQL standard calls a schema is called "database" in MySQL.

how to list mysql databases and it respective engines

How do I find out the engines for all databases in a given MySQL instance in a single query? I could query engine for each database individually but would require it in a single go.
as Barmar correctly points out, there can be multiple engines in each database
To get all database names-tables-engines you can run:
SELECT TABLE_SCHEMA, TABLE_NAME, ENGINE FROM INFORMATION_SCHEMA.TABLES;

Is there a way to know what are all the tables changed

I am debugging an web application that uses MySQL. I am seeing lot of tables getting modified between different flows ( enrollment , edit etc .. ) . And also , it is very difficult to go through the code base to understand what are all the tables got modified between different invocations .
Is there a better wat to know what are all the tables got modiifed between two times ( two invocations ).
Have general query log enabled to know what all queries are getting executed.
I came across this- How can I tell when a MySQL table was last updated?
In later versions of MySQL you can use the information_schema database
to tell you when another table was updated:
SELECT UPDATE_TIME FROM information_schema.tables WHERE
TABLE_SCHEMA = 'dbname' AND TABLE_NAME = 'tabname'
This does of course mean opening a connection to the database.
See show-table-status
SHOW TABLE STATUS FROM db_name WHERE Update_time BETWEEN (<date1> AND <date2>)
The following will return you different timestamps about your database. See if this helps you.
SELECT *
FROM information_schema.tables
WHERE TABLE_SCHEMA = 'dbname'
You may also consider using a mysql profiler like http://www.jetprofiler.com/
Enable binlog on your server, which is better for your purpose than the querylog since it only contains changes (updates, inserts, deletes).
Hoevery, you have to use mysqlbinlog commandline tool to transform it into a plain text file:
mysqlbinlog server-bin.00001 > binlog.txt

Can I query MYSQL for a specific datatype?

Can I query a MYSQL database for what tables use a specific datatype?
For example: We are wanting to use CakeDC migrations (For CakePHP) which do not support database specific features such as enums. How would I get a comprehensive list of which tables use enums so we can get an idea of how our system might be affected?
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE = 'enum';

Retrieve Comments from tables and columns in a Mysql database

i have to build an application to manage an existing MySQL database. This database was created with MySQLworkbench and some useful comments were added to its tables and columns.
I think it would be great to somehow, query that comments and show them to the user to explain "what that field is". The problem is i don't know if its possible to retrieve that comments (they are only visible from the workbench).
EDIT:
in the MySQL existing database i have to work with there is no INFORMATION_SCHEMA table. I think is something usual to find it but in my model there is no :S
Try with:
SELECT column_comment FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='table_name';
You can parse the output of
show create table `YOURTABLE`;