mysql IF EXISTS returning error [duplicate] - mysql

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
MySQL: How to add a column if it doesn't already exist?
A tool I use is running this query but it's failing. I'm trying to help debug but cannot figure out what's wrong:
IF EXISTS( SELECT NULL FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'TOWNY_RESIDENTS'
AND table_schema = 'minecraft'
AND column_name != 'town-ranks')
THEN
ALTER TABLE TOWNY_RESIDENTS (ADD
`town-ranks` mediumtext,
`nation-ranks` mediumtext
);
The inner select query works fine. It seems the if exists syntax is wrong but I can't figure out how. Examples on website like SO show similar ideas...
The error is:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS( SELECT NULL FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'TOW' at line 1

ALTER TABLE does not support IF EXISTS in MySQL. You can run your ALTER TABLE statement and ignore the resulting errors.
Another option is to do a SELECT INTO OUTFILE on information_schema to generate the ALTER TABLE statement if necessary, and then source that file to execute the ALTER TABLE statement.

Related

MySQL select command with a column named "set" [duplicate]

This question already has answers here:
Select a column with a keyword name
(6 answers)
Closed 1 year ago.
I would like to select some parameters in a column of my database. The issue that I have is that the column where I want to select the data is named "set".
SELECT * FROM database.table where set=5130;
"Set" is also a keyword in MySQL which leads to an error :
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set=5130' at line 1
How should I bypass this issue ?
I guess changing the column name is not an option for you. Then you need to use backticks (`).
SELECT * FROM database.table where `set`=5130;
The best approach would probably be to rename the column. If this is not an option, you can escape it by surrounding it with backticks:
SELECT * FROM database.table WHERE `set` = 5130;
-- Here ---------------------------^---^

Check if a column exists in mysql [duplicate]

This question already has answers here:
Check if a column exists in a table with MySQL
(11 answers)
Closed 2 years ago.
How do I check if a column exists in a table and add one if it doesn't exist?(I am using mysql)
IF EXISTS(SELECT * FROM sys.columns
WHERE Name = N'columnName' AND OBJECT_ID = OBJECT_ID(N'tableName'))
BEGIN
PRINT 'Your Column Exists'
END
This is a sample one.The results I got one from web are the older versions of mysql. I need the answer in the latest version (mysql server 2019 and above) .How do I solve it?
You can access this kind of information from the information_schema database. It contains a columns table.
The information_schema database is part of ISO SQL, and implemented on all SQL servers :
MySQL (COLUMNS table)
SQL Server (COLUMNS table)
PostgreSQL (COLUMNS table)
Here is a portable query :
SELECT count(*) FROM information_schema.columns
WHERE table_schema = 'thedatabase'
AND table_name = 'thetable'
AND column_name = 'thecolumn';
You can use below query
SHOW COLUMNS FROM table_name LIKE '%column_name%'

Making a backup copy of SQL table throws me this error: "#1064 - You have an error in your SQL syntax; "

I'm trying to make a backup of my table in MySql but I get this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table `zbackup_oc_t_city` from `oc_t_city` LIMIT 0, 30' at line 1
This is the code that I'm using to backup
SELECT * INTO TABLE `zbackup_oc_t_city` FROM `oc_t_city`
Here is my oc_t_city table:
Here is zbackup_oc_t_city
I have tried it on numerous tables and it keeps throwing me the same error... any ideas?
Thanks
If you want to create your backup table and do the backup in just one statement use
CREATE TABLE `zbackup_oc_t_city` SELECT * FROM `oc_t_city`;
CREATE TABLE ... SELECT Syntax
You can create one table from another by adding a SELECT statement at
the end of the CREATE TABLE statement:
CREATE TABLE new_tbl [AS] SELECT * FROM orig_tbl;
With MySQL you can't use SELECT ... INTO to select into a new table:
SELECT ... INTO Syntax
The SELECT ... INTO form of SELECT enables a
query result to be stored in variables or written to a file:
SELECT ... INTO var_list selects column values and stores them into
variables.
SELECT ... INTO OUTFILE writes the selected rows to a file. Column and
line terminators can be specified to produce a specific output format.
SELECT ... INTO DUMPFILE writes a single row to a file without any
formatting.
I do remember having similar troubles while working with SQL myself. One cause of error I found was the use of citation marks... try removing the citation marks like this:
SELECT * INTO zbackup_oc_t_city FROM oc_t_city;
I'm not sure this fixes your problem (but I can't see anything else wrong with your query). I hope it does though. :)

How to check if a column exist inside a table, and preform action based on the result

I use MySQL 5.5.16.
I'm trying to check if a certain column exists in one of my tables. If it is I want to update its type & collation and if it isn't I want to add it to the table.
I've read similar posts Here and Here about IF EXISTS statement. But I'm getting an error with my query:
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'e4s_account_details' AND column_name = 'accountID') THEN
ALTER TABLE 'e4s_account_details' CHANGE 'accountID' 'accountID' INT(10) NOT NULL AUTO_INCREMENT;
ELSE
ALTER TABLE 'e4s_account_details' ADD COLUMN 'accountID' INT(10) NOT NULL AUTO_INCREMENT;
END IF;
The error I get is:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS (SELECT * FROM information_schema.columns WHERE table_name = 'e4s_acco' at line 1
BTW, If I only query this:
(SELECT * FROM information_schema.columns WHERE table_name = 'e4s_account_details' AND column_name = 'accountID');
I get no error and the table+column information is returned.
You can't use this syntax directly with select query instead you can make stored procedure or query with information schema table for doing same.

Add column if not exist

How could I want to add a column if not exist.
I tried this code:
IF EXISTS( SELECT NULL
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'forwind.measuringdata_fino_10_00000000'
AND table_schema = 'forwind'
AND column_name != 'F1_USA(40)_u') THEN
ALTER TABLE `forwind.measuringdata_fino_10_00000000` ADD `F1_USA(40)_u` FLOAT NOT NULL default '0';
END IF;
but I get the following error:
Error Code: 1064 You have an error in
your SQL syntax; check the manual that
corresponds to your MySQL server
version for the right syntax to use
near 'IF EXISTS( SELECT NULL
FROM INFORMATION_SCHEMA.COLUMNS
Good for me if somebody know an other solution!
MySQL does not support anonymous IF statements like that.
You have at least 3 options to address this:
1) Just run the ALTER TABLE statement and ignore the Duplicate column name error if the column already exists. If you're executing a bunch of DDL in a script you can use mysql -f to ignore the errors and keep going.
2) Use a scripting language such as bash, python, perl etc to check for the existence of the column and then add it if it does not already exist.
3) Create a stored procedure in MySQL to check for the existence of the column and then add it if it does not already exist.
P.S. As an aside, I recommend against putting parentheses in column names, because that forces you to quote the column name every time you reference it.
------Add an IpAddress column in ActionsTracking table if -----
DECLARE #tableName varchar(MAX) ='ActionsTracking'
DECLARE #columnName varchar(MAX) ='IpAddress'
IF EXISTS(SELECT 1 FROM sys.columns
WHERE Name = #columnName
AND Object_ID = Object_ID(#tableName))
PRINT 'Column Already Exists'
ELSE
BEGIN
EXEC('ALTER TABLE '+#tableName+'
ADD '+ #columnName +' varchar(15) null')
PRINT 'Column Added Sucessfully'
END