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.
Related
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
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
(NOTE: I know this is an error that's commonly asked about, but most of the time, the issue is in an INSERT statement. I couldn't find a question on this website where this error happened during an UPDATE.)
I have a table in MySQL (InnoDB / v. 5.7.19) called RESULTS which has, among others, two columns called TYPE and STATUS. Both are of type ENUM, with PASS, FAIL and IGNORE being the supported values in both. I'm trying to run this UPDATE statement on that table, using Workbench (also tried the same directly on the DB machine, using the mysql command):
update `RESULTS` set `TYPE`='IGNORE' where `STATUS`='IGNORE';
I'm getting this error:
Error Code: 1136. Column count doesn't match value count at row 1
Changing the single quotes to double quotes didn't help. I'm able to run this query successfully:
select count(`TYPE`) from `RESULTS` where `STATUS`='IGNORE';
I'm probably making a silly mistake here, but can anyone point out what's wrong with the UPDATE statement?
As requested I am posting it as an answer.
The error basically is self-explanatory like performing an operation on set of attributes but the values provided in the query are not enough. But in your case, you are performing an update operation with all attributes and their values and still, this error appears it may be a case that there is some trigger is registered for this table probably on before/after the event, If that is the case you need to update or remove that trigger if no needed.
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.
I am new to building databases, i have created a database for a website i am creating, and i keep getting this message "Executing SQL script in server
ERROR: Error 1146: Table 'mydb.franch' doesn't exist", when trying to forward engineer my database. The table does exist but it is not being read. (See image below). Please any help will be appreciated.
I can't add a comment, but in your image the table is "Franch" not "franch". Maybe that is the problem?
I may be wrong but I believe you are trying to access your table as (something like)
select * from 'mydb.franch'
or
select * from `mydb.franch`
In either case it's wrong and should be
select * from `mydb`.`franch`
MYSQL is case sensitive when creating a table, which means that you cannot create a table or variable with lower/upper case, and call it with upper/lower cases.
As such you must call the same name you used to create the table. In this case since the table is called mydb.Franch you would do: select * frommydb.Franch;`