Check if a column exists in mysql [duplicate] - mysql

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%'

Related

how can we check if a column exists on Table in MySQL without using Stored Procedure

how we can check if a column exists on Table in MySQL without using Stored Procedure. MySQL v3.23 which won't support writing Store Procedure.
v3.23 ?? If You know the table name and column name then try describe tablename or show create tablename if you know only column name select * from information schema.columns where column_name = columnname. Show tables should show all tables then manually select column name from the listed tables.
But this version is so ancient I have no idea if any of these will work
Try this, counting the columns in your table using the information_schema.COLUMNS.
SELECT COUNT(*) FROM information_schema.`COLUMNS`
WHERE table_schema = 'your_database_name'
AND table_name='your_table_name'
AND column_name='your_column_name';
The INFORMATION_SCHEMA COLUMNS Tabletable provides information about columns in tables.
Link

Search in all tables for a field with given value or name [duplicate]

This question already has answers here:
Search text in fields in every table of a MySQL database
(27 answers)
Closed 4 years ago.
Is there a pure sql solution to search in all tables for a specific field with a given value or name. In pseudocode the query im looking for would be
SELECT * FROM * WHERE field = "value"
or
SELECT tablename, field FROM *
I already know how I could use the mysql information schema to search for all tables with the column, but I'm unsure if (and how) I could combine this inside a query (or stored procedure).
You can write a script wich will generate all queries:
SELECT concat('SELECT * FROM ',TABLE_NAME,' WHERE ',COLUMN_NAME,' =\'value\'')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'columnname'
;
Then, you can execute each rows of the result to get the needed data.

Add a column if not exists in table [duplicate]

This question already has answers here:
add column to mysql table if it does not exist
(16 answers)
Closed 9 years ago.
I want to add a column which will be added in table only if column with same name is not exists.
Alter Table tablename
add col varchar(250)
I think you cant just execute your SQL and retrieve the error if the column already exists.
On the other hand, if you don't want to reach a possible error situation at the MySQL side you could try something like (dynamic SQL):
SET #query = (SELECT
IF((SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=database() AND
TABLE_NAME = 'YOUR_TABLE' AND COLUMN_NAME = 'COL')=0,
CONCAT('ALTER TABLE YOUR_TABLE ADD COLUMN COL varchar(255)'),
'SELECT ''ALREADY EXISTS'''
));
PREPARE st FROM #query;
EXECUTE st;
First, you will generate a SQL containing the column addition SQL or a dummy SQL depending on whether you already have the target column at the INFORMATION_SCHEMA.
PREPARE
and
EXECUTE
statements are used to execute that query.
use INFORMATION_SCHEMA database
SELECT
count(*)
FROM
information_schema.COLUMNS
WHERE
information_schema.TABLE_SCHEMA=DB name
AND information_schema.TABLE_NAME=table name
AND information_schema.COLUMN_NAME=col name
if count is more than 0 create the column else don't

MySQL query where column may be missing

I have a Microsoft stored procedure that queries two MySQL databases using OpenQuery. The two MySQL databases should be have the same schemas, so I can run the same query on both.
However, we will soon alter the MySQL schemas, and add a column to a table. But the two MySQL databases won't happen at the same time, and I don't know the exact date of the releases.
I therefore want to write the query so that if the new column exists, then I use it in my select. If not, then I use a default value.
Is this possible? (That is have a query that handles differences in the table schema?)
(Not to be confused with 'coelesce' where the field definitely exists, but is simply null.)
You can use the following SELECT statement:
SELECT *
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'database name' AND TABLE_NAME = 'your table name'
AND COLUMN_NAME = 'the column name you want to check for'
If the above returns a value, your column is there. If not, then run your alternative SELECT statement
Updated statement:
IF EXISTS (SELECT *
FROM OPENQUERY(servername, 'SELECT *
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = ''database name''
AND TABLE_NAME = ''your table name''
AND COLUMN_NAME = ''the column name you want to check for'' ))

mysql IF EXISTS returning error [duplicate]

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.