Fetch DEFAULT value from MySQL table or MySQL get DEFAULT value for Column or How to SELECT DEFAULT value of a field or How Can I get back DEFAULT values for Column names in a MySQL Table.
We can fetch the column names in a MySQL Table fairly easily by the simple command:
[bash]
DESC tbl_admin_modules;
[/bash]
But is there a way to retrieve the default data value for each column in a MySQL Table? As per the Stackoverflow link there is a little complex way but I figured out two simpler ways to do the same.
Method 1:
Syntax
[bash]
SHOW FULL COLUMNS FROM tablename;
[/bash]
Example
[bash]
SHOW FULL COLUMNS FROM tbl_admin_modules;
[/bash]
Method 2:
Syntax
[bash]
SELECT COLUMN_DEFAULT
FROM information_schema.COLUMNS
WHERE
table_name = ‘tablename’
AND column_name = ‘columnname’;
[/bash]
Example
[bash]
SELECT COLUMN_DEFAULT
FROM information_schema.COLUMNS
WHERE
table_name = ‘tbl_admin_modules’
AND column_name = ‘active’;
[/bash]
Leave a Reply