MySQL Tables and row count - mysql

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.

Related

MySQL: Is it possible to list all tables with a modification date?

Is it possible to list all tables in a given MySQL database with the modification date in the right side?
I am looking for something like
SHOW TABLES FROM MY_DATABASE;
You can query the tables with information_schema.tables
select table_name,
coalesce(update_time, create_time) as last_modified
from information_schema.tables

MySQL Auto-Increment query is returning two rows

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

Deleting records in a non-relational configuration

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

count of the column names in phpmyadmin (mysql)

this seems stupid, but I really don't know this thing.
Today, I am in need to get number of properties in a mysql table (in phpmyadmin).
Can I get the count of the column names in phpmyadmin (mysql)?
For that You can write sql query in phpmyadmin.
SELECT count(*)
FROM information_schema.columns
WHERE table_name = '<table_name>'

MYSQL - count number of rows in each table

I would like to know how many rows are in each table in my database. I've come so far as to having
select count(*) _tablename_;
However i would need to do that on each and every table - and there are a lot. What would me the best way to get a print-out with the table name and it's row count?
SELECT table_name, table_rows
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '<your db>';
I also hope you realise there's an error in your query: it's missing a FROM.
This following query will return number of rows in each table but it doesn't seem to return exact value all the time
SELECT table_name, table_rows
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '<your db>';
TABLE_ROWS: The number of table rows in the partition. For partitioned InnoDB tables, the row count given in the TABLE_ROWS column is only an estimated value used in SQL optimization, and may not always be exact...
for more https://dev.mysql.com/doc/mysql-infoschema-excerpt/5.5/en/partitions-table.html
In addition to SQL queries by others, one can also use Workbench GUI to get the row-counts of each table. To do this, Launch Workbench -> Connect to Db -> right click Db and select "Schema Inspector" (as in Screenshot below - I have highlighted the "Rows" column):
HTH.