0

I have a DB that does not allow null values in a certain table. But I need to work some validation from the code side for rows that may or may not have certain data.

Question: Is there a way to force nulls in SQL? For example if I had 13 columns in a table (none of which allow for null values) and I only wanted to INSERT data into one of them while leaving the others empty. Can this be done?

Tony D.
  • 551
  • 1
  • 10
  • 26
  • 2
    No- if a column does not allow NULL values then you can't put in a null value. You can define a DEFAULT value for the column and only insert into some of them but you can;t have NULL as a default if the column does not allow nulls. – D Stanley Jul 16 '15 at 16:31
  • 2
    The answer is no. But there are some hacky workarounds. Like storing the string "NULL" or other sentinel values that your application will recognize as NULL, but I wouldn't recommend that approach (get messy real quick). If the schema does not allow NULLs there should be a good reason. If not, then allow NULLS – Dan Jul 16 '15 at 16:31
  • 1
    Cannot you use empty strings for texts, 0 for numbers and false for booleans? – Mindastic Jul 16 '15 at 16:32
  • Ok great I got it working. I was able to pass in empty strings. Thanks for the help all of you. – Tony D. Jul 16 '15 at 16:36
  • You might find http://stackoverflow.com/questions/3579079/how-can-you-represent-inheritance-in-a-database helpful. – Micha Wiedenmann Jul 17 '15 at 07:38

0 Answers0