Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of all column names in a table that do not have NULL as their default value?

Tags:

mysql

How do I get a list of all column names in a table that do not have NULL as their default value?

If I do "SHOW COLUMNS FROM table_name", I see there is a column in the results called "Default". I'd like my statement to return ONLY those columns that do not have NULL as the Default. I've tried using a WHERE clause, but I think it chokes because "Default" is a reserved word.

Thanks for any help!

like image 806
DivideByHero Avatar asked Nov 13 '09 00:11

DivideByHero


1 Answers

You can query a INFORMATION_SCHEMA.COLUMNS table:

SELECT * FROM INFORMATION_SCHEMA.COLUMNS
 WHERE table_name = 'my_table'
   AND column_default is [not] null;

Your title says "have null as their default" but post say "do not have null as their default". Pick one :-)

like image 186
ChssPly76 Avatar answered Sep 26 '22 12:09

ChssPly76