1

I can't get a table to accept "" or '' and use the default value. It is inserting NULL instead.

I am trying these commands in the direct input sql window.

INSERT INTO test01 VALUES ("", now(), "");
INSERT INTO test01 VALUES ('', now(), '');

But both just give NULL in the 3rd column. The structure is set to non-null with a default value of "yes". (Without quotation marks).

Here is a screenshot of the structure. You can see NULL is not checked. http://garryjones.se/extras/so3.png enter image description here

1
  • I don't think those insert would put null into the 3rd column, since the 3rd column cannot be set to null. Commented Jan 23, 2016 at 20:18

3 Answers 3

3

Default values only work if no value is inserted/updated. If you explicitly set it to an empty string (which is NOT the same as a NULL value) then it will end up with an empty string in the column. Instead of the code above you should eliminate the column from the INSERT statement at all:

INSERT INTO test01 (t1, t2) VALUES ('', now())
Sign up to request clarification or add additional context in comments.

1 Comment

I think second column also can skip becuase of update current time stamp
0

Other is already explain the reason here I am adding one more point you are also using current time stamp on update so do not need to use this column as well.

INSERT INTO test01 (t1) VALUES ('')

8 Comments

Thanks. What if I have 4 columns and I want to insert a value in the 4th and a default value in the 3rd.
@garry-jones if u need custom value in fourth column than u just write insert into table (col4) values ('val4'); no need to use other columns in this case.
If u want to insert second column than add col2 ... col1 and col3 automatic update @garry-jones
Thanks. What if I have 4 columns and I want to insert a value in the 4th and a default value in the 3rd. I tried this INSERT INTO test01 VALUES (", now(), ,1); but it did not work - In the live table there are 25 columns and some values will not be input by the user so I need those as "yes" or "no". I thought I could control this on the insert statement by using default values.
Redesign the database so that you don't have a table with a bunch of unused columns. Short of that, you need to name the columns that you're inserting into. If you want the database to use the default value then you'll need to exclude them from the column list. Stop trying to use INSERT INTO TABLE VALUES and use INSERT INTO TABLE (<column names>) VALUES (...)
|
0

You could use the DEFAULT keyword: INSERT INTO test01 VALUES ("", now(), DEFAULT);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.