I already read multiples post about drop a column if exists on the forum as
if exists
(select * from INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='tablename' AND COLUMN_NAME='columname' )
ALTER TABLE table_name DROP COLUMN column_name
But, unfortunately, none give a results on MySQL 5.7. It's because i try this:
if (select count(*) from information_schema.columns
where table_name = 'XXXX'
and column_name = 'XXXX')=1
then
ALTER TABLE `XXXX`.`XXXX` DROP COLUMN `XXXX`;
end if;
Please can someone explain me how to solve this issues?
Thanks in advance!
Related
I am trying to delete rows if new_id column value is equal to 2 from multiple mysql tables, but I don't know if all those tables have column new_id.
I try the following statement, but it gives a syntax error:
DELETE FROM table_name WHERE new_id =2 IF EXISTS new_id int(11)
How to do this?
you can get column name by using below query
SHOW COLUMNS FROM `table_name` LIKE 'new_id';
Then from frontend you can take the decision to execute delete query
You can check in information schema:
IF EXISTS ( SELECT *
FROM information_schema.columns
WHERE table_name = 'table_name'
AND column_name = 'new_id'
AND table_schema = DATABASE () ) THEN
DELETE FROM table_name WHERE new_id = 2;
END IF;
I know how to use queries but I've never had to use one for this particular tasks .. I know about the SHOW TABLES; command .. How can I write a query to check if a particular table exists in a MYSQL database .. For example , a query that checks if table MEMBERS exists in database called USERS ??
You can use INFORMATION_SCHEMA.TABLES
USE USERS;
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'MEMBERS'
Searching Table:
select * from
information_schema.tables
where table_name like '%MEMBERS%'
Searching Column:
select * from
information_schema.columns
where table_name like '%COLUMN%'
Is there a way to search the database if a column name / field name exists in a table in mysql?
use INFORMATION_SCHEMA database and its tables.
eg :
SELECT *
FROM information_schema.columns
WHERE table_schema = 'MY_DATABASE'
AND column_name IN ( 'MY_COLUMN_NAME' );
If you want to search in the whole database then you should try
SELECT *
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'db_name'
AND COLUMN_NAME = 'column_name'
And if you want to search in the particular table then you should try
SELECT *
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'db_name'
AND TABLE_NAME = 'table_name'
AND COLUMN_NAME = 'column_name'
SHOW COLUMNS FROM tablename LIKE 'columnname'
have fun ! :-)
UPDATE:
As mentioned in the comments, this searches only one table, not the whole database (every table). In that case, please refer to DhruvPathak's answer.
If you want search two or more columns use following below metioned.
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('columnA','ColumnB')
AND TABLE_SCHEMA='YourDatabase';
I have a script that drops a load of tables using DROP TABLE IF EXISTS, this works.
There is also a delete in this script to DELETE a row from another table that I do not manage. This table may or may not exist.Is there any to check the table exists before attempting to delete a row?
this needs to work for MYSQL and SQLServer
thanks
Alex
To check in SQL SERVER,
IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'TheSchema' AND TABLE_NAME = 'TheTable'))
BEGIN
--Do Stuff
END
To check in mysql:
You simply count:
SELECT COUNT(*)
FROM information_schema.tables
WHERE table_schema = '[database name]'
AND table_name = '[table name]';
This one deletes the row and does not complain if it can't.
DELETE IGNORE FROM table WHERE id=1
source here.
For SQL Server: You could use:
IF OBJECT_ID('tablename','U') IS NOT NULL
I dont think you'll find a common syntax between SQL server and my SQL. I mean, you can check if the table exsits on SQL Server using something like:
if exists(select * from sys.objects where name like 'table_name')
but mySql would have its own catalog.
Unless you write a script like:
if (sql_server) then
if exists(select * from sys.objects where name like 'table_name')
else --mySQl
--execute the mysql script
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TABLE_NAME]') AND type in (N'U'))
It seems to me right the first item in the "Related" column on the right side answers your question.... Check if table exists in SQL Server
For MySQL
show tables like "test1";
For SQL Server
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'testSchema' AND TABLE_NAME = 'test1'
A question you want to ask yourself (in terms of database design): Why are you trying to delete rows from a table you are not sure exists? If it doesn't, but you expect it does, wouldn't you rather create the table than not delete it?
Anyway, Chris Gesslers answer does exactly what you are asking in SQL Server, but there is some smell here.
The construct in MySQL you can use is
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'databasename'
AND table_name = 'tablename'
and check for results
you can use bellow code:
DECLARE #TABLENAME VARCHAR(20)='TableName';
IF (OBJECT_ID(#TABLENAME) IS NOT NULL )
BEGIN
execute(N'TRUNCATE TABLE ' + #TABLENAME + '' );
END
ELSE
BEGIN
PRINT 'Table NOT Exists'
END
I am trying to add a column if it doesn't exist and populate it. My query is the following
IF NOT EXISTS (
SELECT *
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'table_name'
AND TABLE_NAME = 'adapter'
AND COLUMN_NAME = 'adapter_ip'
)
BEGIN
ALTER TABLE `adapter` ADD `adapter_ip` varchar(15) NOT NULL DEFAULT '192.168.194.57';
UPDATE `adapter` SET `adapter_ip` = '192.168.194.57';
END;
Yet every time I get an error. What exactly am I doing wrong? I tested and if I run
SELECT *
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'table_name'
AND TABLE_NAME = 'adapter'
AND COLUMN_NAME = 'adapter_ip'
by itself then it works. As soon as I put it into the if statement it gives me error at line 1 saying syntax is wrong. Because of that I even tried replacing BEGIN with THEN, and that didn't work either. Any idea what might be causing this? Thanks to anyone for their help.
I think you're missing a THEN, and an END IF after your IF <condition>
Also, this link looks good: http://www.cryer.co.uk/brian/mysql/howto_add_column_unless_exists.htm
The syntax for IF is if(test-expr,then-expr,else-expr), as detailed here: http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html