MySql Two Tables Same Name Different Casing - mysql

I'm starting to freak out because I seem to have a phantom table... it's not showing up in Navicat however if I run these two queries:
SELECT count(*) from messages;
SELECT count(*) from Messages;
I get two different sets of results!
However, the weird thing is if i run show tables I only see one table called messages
This freaks me out because i have no clue if data is going to mistakenly get throwing into the incorrect table Messages
Has anyone ever seen this before?
I'm not sure what to do.
Per Request
After running show table status like 'messages';
messages InnoDB 10 Compact 224163 222 49889280 0 53608448 8388608 208683 2014-08-23 20:16:11 latin1_swedish_ci
One more update
I've ran both:
SELECT * FROM information_schema.tables WHERE table_name = 'Messages';
SELECT * FROM information_schema.tables WHERE table_name = 'messages';
It's showing multiple records with different record counts
The scary part is I ran the same query for other tables in the database and all the other tables i tested with the same technique had the same problem.
It's as if I have two copies of each table, one with a capital first letter, the other with a lowercase, and it seems that the lowercase is the "freshest" of the two.

I'd recommend you check for the table using a query of information_schema.tables.
SELECT * FROM information_schema.tables WHERE table_name = 'Messages';
Also consider the possibility that someone created a view.
SELECT * FROM information_schema.views WHERE table_name = 'Messages';
There's a MySQL variable lower_case_tables_names that has an effect; the default value for this variable depends on the OS (Linux, Windows, OS X). (We have that explicitly set to 1 on our MySQL systems.) According to the reference manual:
"If you are using InnoDB tables, you should set this variable to 1 on all platforms to force names to be converted to lowercase."
(This section of the manual is does not describe the behavior you'd observe with InnoDB tables if this variable were set to something other than 1.)
Ref: http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_lower_case_table_names

Related

SQL injection query for enumerating available tables in databases

We have an exercise and are struggling to come up with a sql injection to find more data from the database in mySQL.
Here is how far we have got:
mySQL webpage result
In the above picture you can see we have managed to get the database to divulge the userid, user and passwd values.
To achieve this we have typed:
admin' --
in the login box
Then:
' union select table_name from information_schema.tables --
in the password box
However, this is not the entire goal of the exercise. We must discover the databases and tables that are available.
We are unsure why the response is not taking into account our query union select table_name from information_schema.tables.
Here is an example of what the response is if nothing is typed in either login or password box:
default mySQL webpage
Our tasks:
enumerate available tables in the database
find username with userid of 3 (done - right?)
find a table containing md5 hashes
Could someone point us in the right direction?
Why isnt our select table_name from information_schema.tables working?
UPDATE: we managed to get 238 rows returned after restructuring our initial query in the first login box to:
admin’ union select table_name,2,3 from information_schema.tables -- -
The fix: the amount of columns have to match between first select query and union select query.
You'll probably have to put the entire injection in the username box. At the moment the -- after the admin' in the login input is commenting the rest of the query.
i.e. The login box should contain admin' UNION SELECT table_name FROM information_schema.tables --
You may need to select padding columns from information_schema.tables as there is no way to tell how many columns the users table has.

Count query from different databases with same schema

Is there any simple way to get count data of single table from different databases(db names) with same schema.
I tried like this..
SELECT COUNT(id) AS db1users,
(SELECT COUNT(id) FROM DB2.users) AS db2users,(
SELECT COUNT(id) FROM DB3.users) AS db3users,
(SELECT COUNT(id) FROM DB4.users) AS db4users,
..........
..........
..........
FROM DB1.users;
I got the exact result but query becoming very large. Is there any simple way to get this..
Please help
Another option, that avoids the need for dynamic sql (and is far less expensive, hence much more scalable), would be to use MySQL the INFORMATION_SCHEMA.TABLES table. It has a column named TABLE_ROWS, whose specification is as follows:
TABLE_ROWS
The number of rows. Some storage engines, such as MyISAM, store the exact count. For other storage engines, such as InnoDB, this value is an approximation, and may vary from the actual value by as much as 40% to 50%. In such cases, use SELECT COUNT(*) to obtain an accurate count.
If this matches your requirement, then you can use a simple query loke:
SELECT table_schema, table_rows FROM information_schema.tables WHERE table_name = 'users'
The best way to do this would be via a scripting language (e.g. Python, Ruby, PHP); you'd execute a database query to get all the database names from your database, then create a SQL statement with all your select count(id) from...; once you've built the SQL statement, you'd execute it.
You can also do this in dynamic SQL inside MySQL; dynamic SQL is hard to write and debug, so I'm not a huge fan....

Why there is a difference in no. of rows when fetched from schema & phpmyadmin

I am using MySQL as database to one of my PHP project. It is very big database with more than 11,000,000 records across various tables. For some analysis, I tried to list the tables with total no. of records between 100000 AND 110000 records by using the following query,
SELECT TABLE_NAME, TABLE_ROWS FROM `information_schema`.`tables`
WHERE `table_schema` = 'my_db' AND TABLE_ROWS BETWEEN 100000 AND 110000
ORDER BY TABLE_ROWS DESC;
In the result for one of the tables, it showed '101556' as shown in the image below,
But when I visited phpmyadmin, it shows higher value as shown in the below image,
Please note that this is the only database available in my PC & I am not connected to Internet when I was checking this.
Can anyone please let me know why there is a difference in the rows count in the same table as explained above.
If this is a table under the InnoDB storage engine, an approximate row count is returned. Have a look at https://dba.stackexchange.com/questions/17926/why-doesnt-innodb-store-the-row-count. The comments in this reference give various explanations.

select count(*) on a big table mysql

I have a big table (approx. 150 M rows), and if I try to run a simple select count(*) on it, then mysql works for about an hour and then throws an error.
I'm assuming this is not due to a limitation of mysql, rather a problem on my side, but I have no idea where to start looking. any ideas?
the table is innodb
mysql 5.5 on linux
you can check it with table status like this
SHOW TABLE STATUS FROM db_name LIKE 'table_name';
you see the rows column....
You can use count(1) instead of count(*)
Try ::
Select count(1) from my_table
The easiest way to speed up queries like this is with a covering index. This will allow you to scan through the rows you want, but require fewer bytes of I/O per row (since you're only scanning a portion of each row's data, not the entire row). Furthermore, if your index is sorted in the same way that your query is, you can avoid the cost of a sort and can scan vastly fewer rows.
You can use Information Schema.
SELECT TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WHERE table_schema =
YOUR_DB_NAME AND table_name = YOUR_TABLE_NAME

MySQL get the number of rows in an innodb table

I have a table using innodb. I know the table has roughly 89 million rows. Using
SELECT COUNT(*) FROM table;
takes about five minutes to run. I know that innodb is not optimized for unconditional COUNT(*) queries. How can I restructure the query to give me a count faster? Would just adding WHERE 1=1 work, or do I need to query a specific field?
I know I can get the approximate number of rows using SHOW TABLE STATUS, but I want to get the value in a PHP script, and it seems like there is a lot to dig through using that method.
If you are OK with the estimated number and just don't want to mess with running SHOW TABLE STATUS from PHP, you can use the information_schema DB:
SELECT TABLE_ROWS FROM information_schema.tables
WHERE TABLE_SCHEMA = 'my_db_name'
AND TABLE_NAME = 'my_table_name';
If you are ok with approximate number of records, you can use output of "explain".
Simplified verion of the code is
$result = mysql_query('explain SELECT count(*) from TABLE_NAME');
$row = mysql_fetch_assoc($result);
echo $row['rows'];
If the table is read frequently and updated infrequently, you may want to consider creating a statistics table that is updated via triggers when making changes to the table.
mysql_num_rows may be useful to you.