console use db use error - mysql

I am working in the mysql console and trying to use a db that has a naming convention that is so: xxx-xxxx. When I try to use the db I get an error and it seems to be that the "-" is the cause of the issue. I run mysql use xxx-xxxx and get the error. How would I overcome this so I can use the db?

You can quote mysql names (tables, columns, databases) with backticks.
So the query
use `xxx-xxxx`;
should work for you.

Related

Query Mysql to find is a statement is valid (NOEXEC perhaps)

Is there any way in mysql to determine of a sql statement is valid before executing it? (In other word rather than execute the stamens and deal with errors I simply want to know if it is a valid statement)
I notice in Mysql workbench that then I type a query it checks it for validity, so I assume there is a way to do that?
In essence I am trying to "precheck" the sql at runtime to see if it is even valid with actually executing it.
Perhaps using the NOEXEC statement?
You can use something called 'SQL Fiddle', you have to build a schema first and then start running your sql queries, see link below:
http://sqlfiddle.com/

MYSQL to HSQLDB migration issue

I have an issue with HSQLDB, I have a MySql database that I'm dumping to an in memory HSQLDB i get the following error when I run the script: Error: unexpected token: ( which is on a create table script and the offending line is TINYINT(3)
if I remove the brackets and the number it works fine, this is a valid declaration on MYSQL and I have tried turning MYSQL compatibility on by changing my url to: jdbc:hsqldb:mem:dataSource;sql.syntax_mys=true but this still doesn't work. :(
just as additional info I'm using a Spring hibernate connection and using Liquibase to do the dumping from MySQL to HSQLDB and I'm running HSQLDB v2.3.2
SQL Syntax especially DDL is not very well portable between different databases. You will have to learn the correct syntax for create table in HSQLDB witch is somewhat different from MySQL.
You can not just export table definition from one flavor of database and import into another.
Would be great if this would be the case but SQL Standard is quite loose...
I assume you have a DDL-Script, you can add SET DATABASE SQL SYNTAX MYS TRUE; to the top of it, see also here (Table 13.29. MySQL Style Syntax).
You may use this only for tests though; if you want to fully migrate to HSQLDB, changing the scripts themselves is sure the long term solution.

Query to detect MySQL

I'm fixing a bug in a proprietary piece of software, where I have some kind of JDBC Connection (pooled or not, wrapped or not,...). I need to detect if it is a MySQL connection or not. All I can use is an SQL query.
What would be an SQL query that succeeds on MySQL each and every time (MySQL 5 and higher is enough) and fails (Syntax error) on every other database?
The preferred way, using JDBC Metadata...
If you have access to a JDBC Connection, you can retrieve the vendor of database server fairly easily without going through an SQL query.
Simply check the connection metadata:
string dbType = connection.getMetaData().getDatabaseProductName();
This will should give you a string that beings with "MySQL" if the database is in fact MySQL (the string can differ between the community and enterprise edition).
If your bug is caused by the lack of support for one particular type of statement which so happens that MySQL doesn't support, you really should in fact rely on the appropriate metadata method to verify support for that particular feature instead of hard coding a workaround specifically for MySQL. There are other MySQL-like databases out there (MariaDB for example).
If you really must pass through an SQL query, you can retrieve the same string using this query:
SELECT ##version_comment as 'DatabaseProductName';
However, the preferred way is by reading the DatabaseMetaData object JDBC provides you with.
Assuming your interesting preconditions (which other answers try to work around):
Do something like this:
SELECT SQL_NO_CACHE 1;
This gives you a single value in MySQL, and fails in other platforms because SQL_NO_CACHE is a MySQL instruction, not a column.
Alternatively, if your connection has the appropriate privileges:
SELECT * FROM mysql.db;
This is an information table in a database specific to MySQL, so will fail on other platforms.
The other ways are better, but if you really are constrained as you say in your question, this is the way to do it.
MySql may be the only db engine that uses backticks. That means something like this should work.
SELECT count(*)
FROM `INFORMATION_SCHEMA.CHARACTER_SETS`
where 1=3
I might not have the backticks in the right spot. Maybe they go like this:
FROM `INFORMATION_SCHEMA`.`CHARACTER_SETS`
Someone who works with MySql would know.

Deleting mysql db syntax?

Im trying to tidy up my local dbs which are left over duplicates and dev dbs from past projects.
To do it im viewing in Sequel pro, then im deleting them using the command DROP DATABASE testdatabase; that works fine.
But some of my dbs have names like lorem-blog-db (with dashes) when i try to run DROP DATABASE lorem-blog-db; it gives me back an error near 'lorem-blog-db'.
Any idea how to resolve this ? i guess its to do with the dashes.
In MySQL, you can use backticks to escape names. Like so:
DROP DATABASE `my-least-favorite-db`

Database context change

Pardon my noob question but was wondering how to change the database context within mysql? I know in SQL Server, it's:
USE [DBName];
so is there an equivalent in mysql?
In MySQL, if you want to change databases, you will also use use:
mysql> use DNNAME;
Same thing: use [database]; (the ; is optional here)
MySQL 5.0 Reference Manual :: 13.8.4 USE Syntax
The use command switches databases in MySQL. The following line
`use [database];`
will switch between databases, where you substitute the actual name of your database for [database], like use db1;.