How to check if column exists in mysql?

Member

by zita , in category: SQL , 2 years ago

How to check if column exists in mysql?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by nicola , 2 years ago

@zita You can use the query below to check if column exist in database:

1
2
3
4
5
6
SELECT * 
FROM information_schema.COLUMNS 
WHERE 
    TABLE_SCHEMA = 'db_name' 
AND TABLE_NAME = 'table_name' 
AND COLUMN_NAME = 'column_name';


Member

by uriah , a year ago

@zita To check if a column exists in a MySQL table, you can use the SHOW COLUMNS command or the DESCRIBE command.


Here's an example of how you can use the SHOW COLUMNS command to check if a column exists in a table:

1
SHOW COLUMNS FROM table_name WHERE Field = 'column_name';


If the column exists, this command will return information about the column. If the column does not exist, it will return an empty set.


You can also use the DESCRIBE command to check if a column exists in a table. Here's an example of how to use the DESCRIBE command:

1
DESCRIBE table_name column_name;


If the column exists, this command will return information about the column. If the column does not exist, it will return an error.


You can also use the SHOW COLUMNS command to get a list of all columns in a table, and then search that list for the column you want to check. For example:

1
SHOW COLUMNS FROM table_name;


This will return a list of all columns in the table. You can then search the list for the column you want to check.


Alternatively, you can also use the INFORMATION_SCHEMA.COLUMNS table to check if a column exists in a table. Here's an example of how to use the INFORMATION_SCHEMA.COLUMNS table:

1
2
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'table_name' AND COLUMN_NAME = 'column_name';


If the column exists, this query will return a row with information about the column. If the column does not exist, it will return an empty set.