HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

A function to check if a column allows NULL

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
allowscolumnnullfunctioncheck

Problem

Is there a way to write an insert/update query that checks if a column allows NULLs: If it does set the column to NULL, and '' (empty string) otherwise?

I would be something like:

UPDATE mytable 
SET field = IF(A_FUNCTION_TO_CHECK_IF_ALLOWS_NULL(), NULL, '');

Solution

You can query the INFORMATION_SCHEMA:

http://dev.mysql.com/doc/refman/5.0/en/columns-table.html

In the INFORMATION_SCHEMA.COLUMNS table there is a IS_NULLABLE column.

You could turn it into a function, I imagine, but I would probably put this logic in an outer part.

Context

StackExchange Database Administrators Q#29618, answer score: 10

Revisions (0)

No revisions yet.