SHOW TABLES and MYSQL_NUM_ROWS - mysql

I have just noticed that where mysql_num_rows should return the number of rows returned for either SELECT or SHOW commands, returns 0 for SHOW TABLE command specifically.
Instead it shows the affected rows count instead of the num rows.
Can anyone please tell me if this is a bug or if am I missing anything here?

SHOW TABLE command is used to Show you table name in your database . On the other hand , mysql_num_rows is used to count how many result got from your query. This query is depend on your requirement basis ...

As stated on the PHP documentation page:
Retrieves the number of rows from a result set. This command is only
valid for statements like SELECT or SHOW that return an actual result
set.
My guess is that SHOW TABLES is not a technical query that would produce the type of result set that mysql_num_rows enumerates.

These "helper" functions (such as SHOW, EXPLAIN, DESCRIBE etc.) won't let you issue their results like you would in a regular table.
But if you're looking for how you can do this, for SHOW TABLES you can do
SELECT `table_name` FROM `information_schema`.`tables`
WHERE `table_schema`=DATABASE()
-- DATABASE() selects current database name
-- you can use the name of any database as a string instead
So basically you can use the information_schema database to get that information.

It was a bug in mysql, fixed it with the update.

Related

MySQL Query not giving expected result

In my XAMPP server's database, table named report exist which consist several column with there fixed set of values (Ok, Not Ok, Done, Not Done, Not Available). For some column like, column 'battery' , when I fire query in XAMPP sql as "SELECT * FROM Report WHERE battery='yes'"; it returns 0 set of result(no records) even when there are several records in which battery value is 'yes'. It's also not working for selecting value 'no';
I think my query is correct but the output is wrong. Please help with solution for this. Also I am not making any spelling mistake related to database, table and column name.
SELECT * FROM report WHERE battery='yes';
SELECT * FROM report WHERE battery='no';
output : MySQL returned an empty result set (i.e. zero rows). (Query took 0.0044 seconds.)

How to get the lenght property of a column in sql?

I have a database with tables and I need to work with inputs from java. For that, I need to know if the input from java can be inserted to the database by not overloading the length of a column. I've tried using SELECT COLUMNPROPERTY( OBJECT_ID('utilizador'),'U_NOME','PRECISION'); but it is giving me an error saying
SQL Error (1305):FUNCTION bd.COLUMNPROPERTY does not exist
Could some one please help me?
Once again, I'm trying to get the maximum input value that can be inserted to the column, not some that already exists. I just need to know which is the biggest size that I can insert. Per example, if I have a column named U_NOME and its a char with length 30, I want to get the 30, not the lenght of some data that already is in that column.
Thank you.
COLUMNPROPERTY is a SQL Server-specific function.
Based on your error code, it appears you're using MySQL, which doesn't implement COLUMNPROPERTY. Use the INFORMATION_SCHEMA database's COLUMN to query the length of the column in question:
SELECT CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = "u_nome"
AND TABLE_NAME = "utilizador"
AND TABLE_SCHEMA="<database_name>"
You can try DESCRIBE your_table_here. This will give the table column information.
Examples and details of other options from MySQL are:
Explain / Describe
SHOW COLUMNS
In SQL Server you can do the below
SELECT Top 1 DATALENGTH(U_NOME) FROM utilizador
In MySQl it would be
Select LENGTH(U_NOME) FROM utilizador LIMIT 1

What exactly does SELECT database(); mean?

So I'm really new to learning SQL and I'm interested about the command
SELECT DATABASE();
At my point in learning, I know that command is used to show what database I'm currently working with. I accidentally made the typo,
SELECT DATABASE() L; and the MySQL terminal displayed the letter L above the name of the database I was working with. Why does this happen? And what exactly does SELECT DATABASE() mean?
I tried this again and did the command
SELECT DATABASE() HI; and it did the same thing and showed "HI" above the name of my selected database. Using the correct syntax, it normally shows, "DATABASE()" above the db that I'm currently working with.
database() is an information function provided by MySQL to return the name of the current database. It can be used anywhere a string constant would be expected.
MySQL does not require a from clause, so a select is a complete query that returns one row -- the values calculated in the select.
Databases allow you to assign a name ("column alias") to an expression in the select. Normally, this would be written as:
select database() as database_name
However, the as is optional (although I strongly recommend using it for column aliases). And nothing enforces a reasonable name.
So:
select database() l
returns a result set with one row and one column. The column is called l and the value in the one row is the database name.
You are selecting a dataset with one column. The contents of the column is the current database. You are naming that column L.
Compare with
select 4 label;
I am not expert in database. But I am quite sure that show database(); will show you the selected database in other words the database which you have chosen.

Mysql table exists, but when shows 'It does not exist' when SELECT query is performed

I have a MySql table named options in the database. It is listed in the table list. When I perform the query show tables then it is being displayed in the table list. But when I perform select * from options I am getting an error that Table 'stillmanmvolvo.options' doesn't exist. Please suggest me what to do.
Thank you
Try this if options is still there:
SHOW TABLES FROM `stillmanmvolvo`;
If it's there, try this:
USE `stillmanmvolvo`;
Then:
SELECT * FROM `options`;
Don't forget the backticks!

MySQL store checksum of tables in another table

CONTEXT:
we have big databases with loads of tables. Most of them (99%) are using innodb.
we want to have a daily process that monitors which table has been modified. As they use innodb the value of Update_time from
SHOW table STATUS from information_schema;
is null.
For that reason we want to create a daily procedure that will store the checksum (and other stuffs for that matters) of each table somewhere (preferably another table). On that, we will do different checks.
PROBLEM:
I'm trying to use
checksum table from db_schema.table_name;
which returns a resultset-table with 2 columns: "table","checksum".
It gives me the value I want but I'm not able to use it in a select or insert statement.
I tried a lot of things like:
select `checksum` from (checksum table from db_schema.table_name);
or other similar queries. But I'm not able to extract the data from the resultset.
Is there a way I can do that?
Thanks in advance for your help.
EDIT: in the end what I want is to build a more complex resultset having different informations in it (table schema, table name, count, checksum, datetime:now()...)
Then I'll use this resultset to compare with the values of yesterday and draw my own statistics. That's why I want to get the checksum from that resultset.
There is no possibility to save the result of CHECKSUM TABLE directly using SQL. Neither can you use prepared statements or cursors in stored procedures to use the checksum result.
You best make a script around it, or download some popular tools doing it for you.
For MyISAM tables using the CHECKSUM=1 table argument, you can simply use INFORMATION_SCHEMA like this:
SELECT TABLE_NAME,CHECKSUM FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'test' AND ENGINE='MyISAM'
AND CHECKSUM IS NOT NULL;