Gilvan André Gilvan André - 2 months ago 6
MySQL Question

Get colunms ia a table when it's defined how not null

I have a big table and I need get the name of the columns that is not null. For example, in this table:

id: long not null,
name: varchar(100) not null,
short_name: varchar(20) default null.


with an SQL SELECT, I would like that it will returned only the name of this columns:

id
name


Can I do this?

Answer

Check for IS_NULLABLE column in INFORMATION_SCHEMA.COLUMNS.

Query

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'your_table_name'
AND IS_NULLABLE = 'NO';
Comments