What is the schema for Show Fields MySQL resultset? - mysql

I find myself in a peculiar situation while building a query builder in .net of needing to know this information so I can setup the appropriate structured classes.
What is the schema information for
SHOW FIELDS FROM `foo`;
I have tried
SHOW FIELDS FROM (SHOW FIELDS FROM `foo`) tbl1
... however that yields a sql error
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SHOW FIELDS FROM foo) tbl1' at line 1
I need to know the information about the columns returned. I could cheat and make them all strings, but I would much rather to use the ACTUAL structure (and if it truly is all strings, then damnnnnnnnn).
Thanks in advance.

You can use:
SHOW COLUMNS FROM tablename;
This will give you back structured information about fields.
More info on that:
http://dev.mysql.com/doc/refman/5.5/en/show-columns.html
For schema information you need to run:
DESCRIBE information_schema.columns
More about schema can be found here: http://dev.mysql.com/doc/refman/5.0/en/information-schema.html

Related

"EXPLAIN PARTITIONS" in MySQL 8

In an old project that we have not changed for some time, the following request is used for partitioning:
EXPLAIN PARTITIONS SELECT * FROM table
Then, the list of partitions is retrieved from the column "partitions" of this request result, and the actions to be taken over the partitions are deduced from this list.
This project is deployed and running on multiple servers that use MySQL 5.7.xx.
Now, we are trying to deploy it on our new AWS servers, using RDS instances that run MySQL 8.0.21, but the "EXPLAIN PARTITIONS" request is not recognized, the error is :
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'SELECT * FROM table' at line 1
My questions are:
Is there a MySQL page in which this regression is documented? I read all their pages about the EXPLAIN keyword but could not find any information about it.
Has it been replaced by another syntax that gives the exact same result?
From Features Removed in MySQL 8.0
The EXTENDED and PARTITIONS keywords for the EXPLAIN statement have been removed. These keywords are unnecessary because their effect is always enabled.
So the "new syntax" is simply EXPLAIN SELECT * FROM table.

Escaping database name in prepared statement with Node

I am trying to use mysql library to request my database.
I need to use the syntax with query placeholders, so I tried a simple request:
connection.query('DROP DATABASE IF EXISTS ?;', ['mydb']);
But this leads to :
'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'\'mydb\'\' at line 1'
The query is indeed:
'DROP DATABASE IF EXISTS \'mydb\';'
So how is this supposed to work actually ?
The MySQL notation for this is:
DROP DATABASE IF EXISTS `mydb`
Since you're escaping it as a string, that's not a database reference it can drop.
Some drivers support alternate placeholders for this very reason:
DROP DATABASE IF EXISTS ??
Normally you can't use placeholder values for things like databases, columns or tables as these are treated differently. This is a limitation of the driver.
You just need to be careful on user-supplied values and names with irregular characters in them. These need to be escaped according to MySQL schema identifier rules.

Issue with MySQL and phpAdmin

I am using MySQL 5.1.5 on a Yahoo Sight Server, with phpMyAdmin as the databse interface.
I use the following Query
UPDATE table_family SET last_name='Smith' WHERE id=1;
Then I get the following error:
Error There seems to be an error in your SQL query. The MySQL server
error output below, if there is any, may also help you in diagnosing
the problem
ERROR: Unknown Punctuation String # 34 STR: =\ SQL: UPDATE
table_family SET last_name=\'Smith\' WHERE id=1;
SQL query:
UPDATE table_family SET last_name=\'Smith\' WHERE id=1;
MySQL said: Documentation
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near '\'Smith\' WHERE id=1' at line 1
Ideas? I feel like its an issue with the database, not with my code. the last_name field is a varchar(50). I actually opened a ticket with support on this issue, but it happened to me on two different domains I have with Yahoo, so that makes me think there is more than I know is going on. I have done this with integer fields that don't require the (') single quote and have had no issues. I have also run my syntax through a local access database I created just to make sure it wan't a syntax issue. Worked perfectly first time. Then I had 2 database guys I know look at it. They think its good too. So now I am lost.
Thanks for any help you can provide.
Andy

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

I am using Sybase Power Designer to create a database from a physical data model (Sybase creates an SQL file) . When i import the SQL file with phpMyAdmin I have the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if exists(select 1 from sys.sysforeignkey where role='FK_ARTWORK_CREATES_ARTIST'' at line 7 .
Any ideas? Could this error appear due to errors on the physical model or there is another problem?
This is the code :
if exists(select 1 from sys.sysforeignkey where role='FK_ARTWORK_HAS_BUY') then
alter table artwork
delete foreign key FK_ARTWORK_HAS_BUY
end if;
The error you are getting is from MySQL. Regardless of the tool used to generate SQL, the database seems to be MySQL (or something is terribly wrong with your systems if they are confused and think they are MySQL).
The MySQL if statement (documented here) has to be inside a stored program. That means that this code only compiles inside a stored procedure, user defined function, or trigger. It doesn't "just work" on its own.
In addition, MySQL doesn't have sys tables. It uses information_schema tables. My strongest suggestion is to use tools appropriate for your actual database. If you are using a tool to generate Sybase, then use Sybase as the destination database. If you are using MySQL, then use a tool to generate MySQL code. Or, better yet, learn how to write the commands yourself.
Finally, if you intend to use Sybase, then connect to the correct database and your problem should be fixed.
As I can't post here, and sqlfiddle.com temporary unavailable, I decided to post code with correct syntax for Sybase on the PasteBin

Is there any way to retrieve more accurate MySQL error messages?

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near [random snippet of query code]
I am very rarely able to deduce something of value from MySQL errors like that one, is there any way to get some more specific data?
I've seen people dismiss this question saying that it's impossible to get exact error data because of how MySQL's syntax works. Is that really so?
It's not only MySQL that gives syntax errors like that, MS SQL Server gives very similar messages.
The error message is very accurate in the sense that the code shown in the error message is the exact position where the parser determined that it could no longer go on with parsing the query.
However, the actual error in the query is often somewhat earlier in the query. If you for example misspelled "from" in a query as "fom", the parser will go on thinking that "fom" is an alias for the last field that came before that, and give you a syntax error when it finds a table name instead of the expected comma or "from" keyword. It will point to the table name as the position of the error instead of the misspelled keyword.
Sometimes it helps to break down your query into several lines instead of just one long one. This will still show you only the approximate position of your error, but it might help a bit.