Puzzled by MySQL information_schema.tables info - mysql

For some unknown reason, the info of 'information_schema.tables' is not match to real tables:
First, find table-name from information_schema.tables, there is one result;
Then, select * from table-name, it shows table not exist! /(愒o愒)/~~
I try to create a new table(MyISAM engine) with same name, it create OK!
Then, find table-name from information_schema.tables, there are two result!
And select * from table-name shows empty set, it's normally.
Until now, table can be use, but can not be drop completely.
If drop table, it return the "First".
As the following image:
all the step
I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this......

The image shows nothing wrong to me. When you run this:
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE
TABLE_NAME = 'report_instance_performance_20160614
and get a result, you are probably getting a result for a table of that name in a different schema from where you ran that query. It would be more helpful to run this to see if something were wrong:
SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE
TABLE_NAME = 'report_instance_performance_20160614'
My assertion that that table exists in another schema but not that one that you're running in the query from is that you're getting the doesn't exist error when you try and SELECT from it.
Next, when you create a new instance of that table and then run your INFORMATION_SCHEMA.TABLES query, you are getting two results because you've now created a table with that name in your current schema. If that table had existed in your current schema, you would've gotten an error that a table of that name already existed in the schema. This further supports my assertion that report_instance_performance_20160614 exists in a different schema.
Lastly, since the table is newly created and you haven't INSERTed anything into the table before SELECTing from it, it's totally normal that the table would be empty.

Related

Selecting using `database_name`.`viewname`.* syntax in MySQL throws "Unknown table 'database_name.viewname'"

I'm having a strange issue with MySQL and database views.
I have a view defined like this:
CREATE VIEW circuits AS
(SELECT Id, Id AS Old_Id, Name FROM circuits_1)
UNION
(SELECT Id + 1000 AS Id, Id AS Old_Id, Name FROM circuits_2)
I have to join this view with a table that is in another database.
To do so, I usually prefix the table name with its database name, like db_name.table_name.
I've mapped this view using an ORM, specifying its prefix, and the resulting query is this one:
SELECT `webapp`.`circuits`.* FROM `webapp`.`circuits`
But this query returns this error:
#1051 - Unknown table 'webapp.circuits'
However, I've tried to manually run the query and remove the webapp. prefix from the SELECT statement, and it works as expected, throwing no error at all
SELECT `circuits`.* FROM `webapp`.`circuits`
Any idea why this happens?
Is it related to the way the view is defined?
EDIT
Another strange thing:
Even if this query fails:
SELECT `webapp`.`circuits`.* FROM `webapp`.`circuits`
This doesn't:
SELECT `webapp`.`circuits`.Id FROM `webapp`.`circuits`
I was hesitant to answer, as i am not familiar enough with mysql to give a full answer. I did some testing on rextester.com though, and found the following:
If I create a table test(id int), I can query it using its fully qualified object name:
SELECT rextester.test.*
FROM rextester.test
Works, no problem.
If I create a view so_test as (Select 1 id from dual)
I cannot do the same:
SELECT rextester.so_test.*
FROM rextester.so_test
Returns the same error you get.
I cannot conclude too much from this, as i don't know mysql well enough. However, it seems a general issue with views, not the way you created it.
MySQL does not seam to support * rewrite to the matching table columns with in the VIEW.
MySQL 5.6.39
http://sqlfiddle.com/#!9/68f2d3/4
MySQL 5.7
https://www.db-fiddle.com/f/taRV6FLAP6Mf8oMeuniZP3/2
MySQL 8.0.11
https://www.db-fiddle.com/f/taRV6FLAP6Mf8oMeuniZP3/3

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!

Check for column existence in mySQL

I know this question has been asked before, but for some reason the suggested solutions do not work in my setup.
I am calling a mySQL database from within matlab on windows, and need to check whether a given table has a specific column before my script commences calculating values for that column.
From previous answers, I get that the following should help me determine if col1 exists:
select exists (select * from table1 where col1=val1)
And this works fine if col1 exists. However, when it doesn't I get the following:
>> fetch(conn,'select exists (select * from model12B where col1=.5)')
Error using database/fetch (line 37)
[MySQL][ODBC 5.3(a) Driver][mysqld-5.5.37-log]Unknown column 'col1' in 'where clause'
This looks more like a MySQL error than a matlab error, hence why I'm framing it as a MySQL question. I can of course wrap this in a try-catch block, but that feels wrong. Can anyone suggest a good way to ask for existence without generating an error?
You can use the information_schema to tell you if the column exists in specified database and specified table :
select * from information_schema.columns where table_name='tablename' and table_schema='databasename' and and column_name='columnname'
Or you can use "show columns " command:
show columns from databasename.tablename where like 'columnname';
Of course, you can use the try - catch to test if a column exists of not. I don't think there is any side effect if you analyze the error message carefully.

mysql tell difference between table and view

I'm surprised I didn't find this answer out there.
I know very well what the difference between a table and a view is. BUT..how do I DETERMINE whether a db object is a table or view? Since
show tables;
will show both tables and views - and there is no "show views" command.
to determine in my coding (which has to read multiple objects and may not "know" better), I do this:
show create view my_table_or_view
and if I get an error, which I prevent from dying, it's a table. Pretty clumsy, is there a better way?
try this variation instead ...
show full tables;
the Table_type column will give the info you require :)
You can use the following query and if it returns a record it's a table
SELECT *
FROM information_schema.tables
WHERE 'TABLE_TYPE' = 'BASE TABLE'
AND table_name = 'your table name'

how to retrieve views creation time?

Is it possible to know when a view has been created or updated?
If I run a query on information_schema.tables I get these infos only for "base table" while for view everything is equals to null. Thanks
No, it's not possible because view is not a table and not contain any physical data. MySQL creates view on the current data which exist in other tables, so you only can get update time for tables.
View is only a definition, and that definition are stored in INFORMATION_SHEMA. You can only get information about definition:
SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME = 'v';
But, you can't get update or created time.