So, primarily I work in oracle and have gotten spoiled by working with dual. For a user with select privileges on a given database how could you reproduce the dual functionality?
IE:
Select 'foo' from dual
Probably a very simple answer that I'm just missing.
I remember using Select 1 from dual in oracle and just select 1; in mysql. So I think you just try without a table reference.
This was to verify connectivity so not sure if it applies in your case.
You are permitted to specify DUAL as a dummy table name in situations where no tables are referenced:
SELECT 1+1;
OR
SELECT 1 + 1 FROM DUAL;
In most cases you can just omit FROM DUAL in MySQL and MariaDB.
But if you need a WHERE to verify things, you still need the FROM DUAL.
Example (usage: one way to avoid re-inserting with different id):
INSERT INTO `t1`(`id`, `name`, `description`)
SELECT NULL, 'LopsusNonsus', 'my description'
FROM DUAL
WHERE NOT EXISTS (SELECT id FROM `t1` WHERE `name`='LopsusNonsus' LIMIT 1) ;
You can use information_schema.schemata table for the current session database as it has one row for each database.
select
1
from
information_schema.schemata
where
schema_name = database()
Related
Im writing a migration and Im trying to query a table only if it exists in the db. In some envs it will exist and in others it wont which is why i want to check if it exists. The table of interest is called OLD_PASSWORD. I do the following.
SELECT ID, PASSWORD
FROM OLD_PASSWORD;
WHERE EXISTS
(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'OLD_PASSWORD);
This fails '.dbcSQLSyntaxErrorException: Table "USER_OLD_PASSWORDS" not found;' which makes sense because it wont exist in some envs.
I also tried something like
IF (EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'OLD_PASSWORD'))
BEGIN
SELECT ID,
PASSWORD
FROM OLD_PASSWORD
END
but it's not compatible with mysql.
Best approach was to query INFORMATION_SCHEMA.TABLES from Java, and then do the query from OLD_PASSWORD if it returns something.
You forgot the ' sign at the end of query before the ).
Nonetheless, this is cartesian select. Use in instead. i.e
password in (select password ...) etc.
Does anyone know how to retrieve UUID via HSQLDB.
For example when i try
SELECT UUID();
via MYSQL it works fine. But the same statement doesn't work with HSQLDB.
The following methods achieve the corresponding purpose
VALUES (UUID())
CALL(UUID())
SELECT UUID() FROM (VALUES(0)) t;
Is there a way which is same for mysql and hsqldb?
HSQL doc says that UUID has been activated. http://hsqldb.org/doc/guide/builtinfunctions-chapt.html
Thanks.
Turn on the MySQL compatibility mode in HSQLDB and it will allow your SELECT statement:
http://hsqldb.org/doc/2.0/guide/compatibility-chapt.html#coc_compatibility_mysql
Is there a way which is same for mysql and hsqldb?
Only way i can think off.
Create a table DUAL in HSQLDB
CREATE TABLE DUAL (
id INT
);
So you can use
SELECT UUID() FROM DUAL LIMIT 1;
Then the query should work the same in both MySQL and HSQLDB.
DUAL in MySQL is a non existing dummy table.
In my project I have two code paths that interact with MySQL during first time setup. The first step is the database structure creation, in here, a user has the ability to pick and choose the features they want - and some tables may not end up being created in the database depending on what the user selects.
In the second part, I need to preload the tables that did get created with some basic data - how could I go about inserting rows into these tables, only if the table exists?
I know of IF NOT EXISTS but as far as I know, that only works with creating tables, I am trying to do something like this
INSERT INTO table_a ( `key`, `value` ) VALUES ( "", "" ) IF EXISTS table_a;
This is loaded through a file that contains a lot of entries, so letting it throw an error when the table does not exist is not an option.
IF (SELECT count(*)FROM information_schema.tables WHERE table_schema ='databasename'AND table_name ='tablename') > 0
THEN
INSERT statement
END IF
Use information schema to check existence if table
If you know that a particular table does exist with at least 1 record (or you can create a dummy table with just a single record) then you can do a conditional insert this way without a stored procedure.
INSERT INTO table_a (`key`, `value`)
SELECT "", "" FROM known_table
WHERE EXISTS (SELECT *
FROM information_schema.TABLES
WHERE (TABLE_SCHEMA = 'your_db_name') AND (TABLE_NAME = 'table_a')) LIMIT 1;
I've a problem with MySql, here is the details :
I created a new schema/database, executed (only) this queries :
create table mytable (
id varchar(50) not null,
name varchar(50) null default '',
primary key (id));
create view myview as
select id,name from mytable;
insert into mytable values ('1','aaa');
insert into mytable values ('2','bbb');
insert into mytable values ('3','ccc');
and then, if I run these queries :
select * from mytable;
select * from myview;
prepare cmd from 'select id,name from mytable where id=?';
set #param1 = '2';
execute cmd using #param1;
the queries give the correct result (3 rows,3 rows,1 row).
but, the problem exists if I run this query:
prepare cmd from 'select id,name from myview where id=?';
set #param1 = '2';
execute cmd using #param1;
ERROR: #1615 - Prepared statement needs to be re-prepared
I've done some research and found that the increment of configurations below "may" solve the problem :
increase table_open_cache_instances value
increase table_open_cache value
increase table_definition_cache value
As far as I know, the queries above are the common and standard MySql queries, so I think there is no problem with the syntax.
I'm on a shared webhosting and using MySql version is 5.6.22
But the things that make me confused is, it only contain 1 schema/database, with 1 table with 3 short records and 1 view,
and I executed a common and standard MySql select query,
does the increment of values above really needed?
is there anyone with the same problem had increase the values and really solve the problem?
or, perhaps do you have any other solution which you think may or will works to solve this problem?
ps: it does not happen once or twice in a day (which assumed caused by some backup or related), but in all day (24 hours).
Thank you.
Do you do this after each execute?
deallocate prepare cmd;
The closest guess until now is some other shared members on the server dont write their code quite well (because it is a shared webhosting), either doing large alter while doing the large select, or dont deallocate the prepared statement after using it, like Rick James said. (want to make the post usefull, but I dont have the reputation, sorry Rick)
I can not make sure if the increment of "table_definition_cache" will works because the system administrator still wont change the value until now, but incase you having the same problem and you can modify it, it worth to try.
My current solution is I change all my views in my query strings into non-view or subqueries, it works for me, but the problem is still in the air.
eg. from
select myview.id, myview.name
from myview
inner join other_table on ...
where myview.id=?
into
select x.id, x.name
from (select id,name from mytable) x
inner join other_table on ...
where x.id=?
I don't want to commit a SQL request. I just want to known whether a specific table exists in data base or not, similar to the following pseudo statement:
IF EXISTS TABLE mytablename RETURN TRUE ELSE FALSE
How can I do this? I found several examples on how to modify a table if it doesn't exist.
This depends highly on the database, but this will work in many:
if exists (select 1 from information_schema.tables where table_name = 'mytablename')
(You might want to add additional conditions for the database/schema name.)
Although information_schema is supported in several databases, other databases might use all_tabs (Oracle) or some other table/view.
And, if you want a query that returns this value, use:
select max(case when table_name = 'mytablename' then 1 else 0 end) as TableExists
from information_schema.tables;
Try this Proc
CREATE PROCEDURE dbo.DoesTableExist (#TableName NVARCHAR(100))
AS
BEGIN
IF EXISTS (SELECT * FROM sys.tables WHERE Name = #TableName)
SELECT 1
ELSE
SELECT 0
END