Bypass mysql error if column does not exist - mysql

I am working on a mysql procedure that I can run on all of my client environments however I am running into one issue. Some of my clients have a particular column on the database and others do not.
I am trying to use update ignore but I still get an error if the column doesnt exist.
I know I can query information schema for a count where that column exists, but I'm wondering if there is a more simplistic way to achieve this.
update ignore table1 set columnA = null;
ERROR 1054 (42S22): Unknown column 'columnA' in 'field list'

You have two options:
First solution: Check first if the column exists.
SELECT t.table_name, (c.column_name IS NULL) AS columnA_present
FROM INFORMATION_SCHEMA.TABLES AS t
LEFT OUTER JOIN INFORMATION_SCHEMA.COLUMNS AS c
ON t.table_name = c.table_name and c.column_name = 'columnA'
WHERE t.table_name = 'table1';
Second solution: Run the UPDATE and catch the error. Check if the error is 1054. If so, then skip it. If not, then it's some other error so report it.

Related

Unknown column in 'where clause' failing in procedure

I'm trying to run the following code in a procedure. It is the very first line of code in the procedure after the BEGIN
update `deleteRequestsInitial`
SET `mamaID` = (SELECT `id` FROM `love_users`
WHERE `love_users`.`email` = `deleteRequestsInitial`.`Email`);
When I run it in the phpmyadmin SQL environment, it works fine. But when I call the procedure, I get the error:
Unknown column 'deleteRequestsInitial.Email' in 'where clause'
I've looked through a lot of the posts on the 'Unknown column' error, but none of the suggested workarounds seem to be working for me.
You are probably really running this:
update `deleteRequestsInitial`
SET `mamaID` = (SELECT `id` FROM `love_users`
WHERE `love_users`.`email` = `deleteRequestsInitial.Email`);
Or possibly, the column is email, not Email.
I would suggest just dropping all the back ticks:
UPDATE deleteRequestsInitial dri
SET mamaID = (SELECT id
FROM love_users lu
WHERE lu.email = dri.Email
);

Check if a field exists and it's not null

I need to fetch a value from a table with three possibilities: 1) The field exists and it's not null, so it returns the value of the field 2) the field exists but it's null, so it returns is-null-string 3) The field doesn't exists, so it returns not-existing-string
I am trying to run this query but I get this error message #1054 - Unknown column 'm.my_field' in 'field list'
SELECT if (exists(SELECT *
FROM information_schema.COLUMNS
WHERE TABLE_NAME = 'my_table' AND COLUMN_NAME = 'my_field'
), IFNULL(m.my_field, 'is-null-string'), 'not-existing-string'
) AS my_field,
m.*
FROM my_table m
Any idea how can I get it done in mysql 5.6?
What you are looking for is DESCRIBE:
DESCRIBE my_table;
Look at the result to see if your field exists. Note, you don't want this code to be in your application. You need to know your table fields to run queries. You might only use this to generate some boilerplate code.

How to alter a db column attribute to `NULL` from `NOT-NULL` state using SQL query?

I want to write a SQL script that alters a column's attribute to null from the not-null state.
Tried:
SELECT COLUMNPROPERTY('prefetching_rules', 'date_delete','AllowsNull') AS 'AllowsNull';
But getting following error:
Error Code: 1054. Unknown column 'COLUMNPROPERTY' in 'field list'
For this you should use alter table (assuming you column is a varchar of 255) just apply modify.
Columns are nullable by default and so just not assign other constrains as NOT NULL
ALTER TABLE mytable MODIFY your_column_name VARCHAR(255);
This query will give you detailed info about all 'not nullable' columns
in all tables in 'your_database_name_here'.
SELECT *
FROM information_schema.columns
WHERE TABLE_SCHEMA = 'your_database_name_here'
AND IS_NULLABLE = 'NO';
Then you have to decide which of them you want to modify.
Just FYI u can't use this query as sub query in ALTER sql because it doesn't accept subqueries

How to check column exist or not before select that column from table

Below is the query I used to select multiple columns from one of my tables.
I get the column names from user to select from the table.
If the user gives a wrong column name it shows Unknown column error. How can I check if that column exists in the table before going to select?
SELECT `address_id`,`address_firstname`,` afserfw`
FROM `patsm_addresstable`
WHERE `address_id`='28'
LIMIT 0, 25
This would give the following error:
#1054 - Unknown column ' afserfw' in 'field list'
This query will give you a list of columns in a particular table with their datatypes.
SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'tablename'
You can, when constructing a query against any table, use this resultset to determine whether a given column exists.
But be careful: a web application allowing end-users to give you column names is hard to make secure against intrusion by cybercrooks.

Column not found when trying a cross database update in mysql

I'm trying to copy the contents of a column in one mysql database to an identical table in another mysql database.
I'm using:
UPDATE db1.table
SET db1.table.name = db2.table.name,
db1.table.address = db2.table.address
WHERE db1.table.id = db2.table.id;
I'm getting the error 1054: Unknown column 'db2.table.id' in 'where clause'.
Both tables have an id column, so I'm not sure why it won't work. I'm logged in as admin, and have full rights to both databases.
UPDATE db1.table
JOIN db2.table
ON db1.table.id = db2.table.id
SET db1.table.name = db2.table.name,
db1.table.address = db2.table.address