SQL - delete row if another table exists - mysql

I'm trying to delete a row from a table if another table doesn't exist. I've tried using the following statement
IF (NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'user3project3'))
BEGIN
DELETE FROM user1table WHERE id=3
END
However I get the following error:
Unrecognized statement type. (near "IF" at position 0)
I'm using phpMyAdmin with XAMPP, if that matters.
Thanks a lot in advance!

The IF statement is only allowed in programming blocks, which in practice means in stored procedures, functions, and triggers.
You could express this logic in a single query:
DELETE FROM user1table
WHERE id = 3 AND
NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'user3project3');
That said, you have a very questionable data model if you are storing a separate table for each user.

Related

Using MYSQL, How do I query a table only if it exists already?

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.

How to use IF EXISTS to check whether table exists before removing data in that table

I wanted to check whether a table exists before deleting the values inside the table. In SQL Server we can do as simple as so :
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'table_to_be_edited')
BEGIN
DELETE FROM table_to_be_edited;
END;
but how do we do it in MySQL ?
I am using MySQL Workbench V8.0.
When delete an option is to ignore the table not found error. This eliminates race conditions where a table is created between the test and the truncate. Always consider this when doing SQL operations.

mysql insert into table if exists

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;

To avoid typos how can I make MySql sql compiler check for table definitions when creating a new procedure?

Having the following DDL in MySql 5.5.x.
create table if not exists t1 (col1 int);
and
CREATE PROCEDURE my_proc()
BEGIN
select inexistent_column from t1;
END;
Why the procedure is created without errors?
I expect it to throw that inexistent_column is not known or does not exists.
How can I make MySql sql compiler check for table definitions?
UPDATE: Rephrase
I just want a way to avoid a typo in a column while developing, meaning, creating a brand new procedure. Is there any way to do this? Maybe that should've been the question to ask from the start.
The server will only do syntax checking, but you can check for the existence of columns yourself. All existing tables and columns are defined in the information_schema.
You could use a query like this to check if your column exists:
SELECT *
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'your_database_name'
AND TABLE_NAME = 'your_table_name'
AND COLUMN_NAME = 'some_column'

How can i check if the table mysql.proc exists

I am working on a WPF application which installs if MySQL is installed,
so before installation I want to check whether mysql.proc table exists or not.
I googled about it and ended up with a query
select * from information_schema.Tables
where Table_schema = Schema() and Table_Name = 'mysql.proc'
This query returns an empty row.
I also tried simple select statement
select * from mysql.proc,
and this returned a table with the names of all the stored procedures, but if this table didn't exists then it throws an exception in the c# code.
So is there any way that I can fire a query from c# and get a boolean value depending on whether mysql.proc table exists or not?
Try SHOW TABLES FROM mysql LIKE 'proc'. If there are no result rows, the table doesn't exist. If there is one row, the table exists. Note that this approach isn't portable across RDBMSs, though that doesn't seem to be a concern of yours.
As for your first query, SCHEMA() returns the default database, so if it's not "mysql", the query will fail. Likewise, data in the Table_Name column doesn't include the database name, so comparing to 'mysql.proc' will always fail.