Unable to access column names from information_schema (MySQL/MariaDB) - mysql

I'm trying to get column names from a specific table.
EDIT:
I forgot to mention it initially, but the python script i.e. gets the table names without any issues (different query), but the one selecting column names runs into an error.
When I write this query (see below) inside of DBeaver's SQL editor I get the expected result. But when I run the query inside of my python script, I get an error that COLUMN_NAME doesn't exist.
SELECT column_name FROM information_schema.columns WHERE table_schema=<database-name> AND table_name=<table-name>;
(db name and table name are correctly written in the real query)
I also need to mention, that when I previously used MySQL Workbench, this query written in python has worked as expected. It was when I switched to DBeaver for database management that the issue arose.
Are there possibly some further steps I need to do inside of DBeaver in order to get the information_schema to work as expected?
mysql --version >> mysql Ver 15.1 Distrib 10.4.13-MariaDB, for Linux (x86_64) using EditLine wrapper

WHERE table_schema=<database-name> AND table_name=<table-name>;
If there are no quotes around those two names, and if there is punctuation or spacing in the names, any of several error can occur.
Don't simply stick the names in; quote them and escape certain characters. Or use some "bind" technique. Show us the actual Python code that builds the string and the built SQL string.

Say this to your server
SHOW VARIABLES LIKE 'lower_case_table_names';
If you get the value 0 your table names are case sensitive. (1 means case-insensitive). See this for a bit more explanation.
If your names are case-sensitive you may need to try a query with this casing.
SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=<database-name> AND TABLE_NAME=<table-name>;
Be sure to give <database-name> and <table-name> in the case you used to create them.

Related

Using special characters in mysql table or column name

I have seen other questions related to using special characters in column name but I didn't get my point. I want to use column name like
`first_name#abc|xyz|exy#`
Is it legal does it cause any security issue or any error while querying them?
MySQL 5.7 sets the encoding of the column names to utf-8 by default.
If you want to see the configuration in your system, execute the following command:
SHOW VARIABLES LIKE 'character_set_results';
Source: https://dev.mysql.com/doc/refman/5.7/en/charset-metadata.html

Is it important to write database name before table name?

I have a database named DELTASTORE in mysql in my cpanel. There are tables like ADMIN,CATAGORY,PRODUCT,ORDER. I have inserted some values in each table.
If I run sql
SELECT * FROM ADMIN
it works nicely.
But if I run sql
SELECT * FROM ORDER
it doesn't work! Instead of that, if I run sql
SELECT * FROM DELTASTORE.ORDER
then it works correctly.
Why does that occur?
Is it important to write database name before table name and give a dot between them all the time?
To leave out the database prefix, you have to set a default database, with
USE databasename
When writing programs to access the database, the API provides a way to do this. For instance, in PHP PDO you specify the default database in the DSN:
mysql:host=hostname;dbname=defaultDB
In MySQLi it's an argument to mysqli_connect(). In the obsolete mysql extension you use mysql_use_database(). There are similar methods in other programming languages.
Additionally, since ORDER is a MySQL keyword, you either have to put it in backticks:
SELECT * FROM `ORDER`
or prefix it with a database:
SELECT * FROM DELTASTORE.ORDER
It's usually best to avoid using MySQL reserved words as table or column names, to prevent problems like this. See Syntax error due to using a reserved word as a table or column name in MySQL
The reason is, that ORDER is a SQL keyword (for ORDER BY, which you use to sort the result lines). So with the database name (or schematic name) you mean the table ORDER.
It's better to not use keywords for the table.
Just need to put brackets.
Select * from `ORDER`

Replace instances of newlines in entire database

In MySQL Workbench I want to replace all instances of newlines with the explicit string "\\n" in my entire database, except where the string already exists. How would I form the query? Thanks.
[edit]
It just occurred to me that it might be easier to dump the database and use Notepad++ to replace the strings. But I was hoping there were a way to do this from within MySQL Workbench itself.
You will need to do this for each table and each column that you want to do the replacement. For example:
update table set column = REPLACE(column,'\n','\\n')

What is the collation of a MySQL 4.0.20 database/table/column/string?

I want to find out which collation the mySQL database has. phpMyAdmin 2.8.0.3 doesn't show me that. Is there a command like mentioned in this thread? I would need it for the database, table, column and perhaps the string literal.
SHOW COLLATION appeared in mysql 4.1, actually, there wasn't much information about collations and character sets in 4.0. See the release notes for 4.1.0. My guess is that you can't get this information. I also think you can assume it's latin-1 as that was the default in 4.0.
SHOW CREATE TABLE table_name should include the character set and collation for the table.
To get the default for the database, try this:
show variables like 'character_set_database';
show variables like 'collation_database';
Not sure if these are available in your MySQL version however.
On my version 4.0.15 DB, I have been able to do:
SHOW VARIABLES LIKE "%char%";
And get:

SELECTing an enum in MySQL not working after database upgrade, why?

I'm currently moving a large PHP-based webapp from one server to another. The old server is running MySQL 5.0.51a (on SuSE Linux), the new server is running MySQL 5.5.15 (on ArchLinux).
Now, the new MySQL is complaining about the following statement (Syntax Error):
SELECT DISTINCT(field) FROM config WHERE range='global' ORDER BY field
I did a bit of testing and found out that the following shortened statement still fails (with Syntax Error):
SELECT * FROM config WHERE range='global'
Unfortunately, this looks like perfectly legal SQL to me. My guess is that it has to do with the type of column range, which is enum('global', 'user'). Selecting from other columns, e.g. columns of type int, works perfectly fine.
I know how to fix this issue locally, i.e. adding the column name to the where-clause, like this:
SELECT * FROM config WHERE config.range='global'
But I don't want to go through the whole code, adding column names to SQL statements where appropriate.
Hence my question:
How do I tell MySQL 5.5.15 to accept the somewhat sloppy 5.0.51a syntax?
RANGE is a reserved word in mysql 5.5 see list
Protect your field, it sould work:
SELECT * FROM `config` WHERE `range`='global'