12

I have the following table:

CREATE TABLE `Foo` (
  `id`         int NOT NULL,
  `FirstName`  varchar(255) NULL,
  `LastName`   varchar(255) NOT NULL DEFAULT 'NONE',
  PRIMARY KEY (`id`)
);

When I run the following query it take the default value of 'NONE':

INSERT INTO Foo (`FirstName`) VALUES('FOO');

When I run the following query:

INSERT INTO Foo (`FirstName`, `LastName`) VALUES('FOO', NULL);

it gives an error:

[Err] 1048 - Column 'LastName' cannot be null


What I want to achieve is that if a value is NULL then MySQL should use the DEFAULT value.
Does anybody know the solution?

3
  • 2
    because you define last name as not null thats why Commented Jun 7, 2018 at 7:32
  • 1
    you define your table saying that the field cannot be null. The default value works on empty, not on null. So, if you don't pass a value for the field it will default to 'NONE'. If you pass any Mysql will check the value (and fail 'cause you said not null) Commented Jun 7, 2018 at 7:37
  • @LelioFaieta ok .. i know that ... but tell me the solution that if value is null then , db take default value .. I don't want to remove null check Commented Jun 7, 2018 at 7:43

3 Answers 3

3

try this

use NULL instead of NOT NULL

CREATE TABLE `Foo` (
`id`  int NOT NULL ,
`FirstName`  varchar(255) NULL ,
`LastName`  varchar(255) NULL DEFAULT  'NONE' , 
PRIMARY KEY (`id`)
);

and query like this

use DEFAULT instead of NULL

INSERT INTO Foo ( `FirstName`, `LastName` ) VALUES('FOO', DEFAULT);
Sign up to request clarification or add additional context in comments.

Comments

2

When you use NULL it means that you want to set NULL as a value, which can't happen for the NOT NULL column.

You can explicitly insert the default value by using DEFAULT:

INSERT INTO Foo ( `FirstName`, `LastName` ) VALUES('FOO', DEFAULT);

2 Comments

but at run time, its not possible to check which value is null or not .. so if db receive null, it should take default value
you have to check on the application side if value is null and in case pass DEFAULT
2

Try removing NOT NULL constraint.

INSERT INTO Foo ( `FirstName`) VALUES('FOO');

When you are not inserting any value manually to lastname you are literally inserting 'none'(default value) into your table hence NOT NULL constraint passes.


INSERT INTO Foo ( `FirstName`, `LastName` ) VALUES('FOO', NULL);

Here you are inserting NULL manually to lastname hence you are literally inserting NULL into your table hence NOT NULL constraint fails.

Solution:

use COALESCE() function while inserting into table, Which will return non null parameter.

INSERT INTO Foo ( FirstName, LastName ) VALUES('FOO', COALESCE(@value, default));

2 Comments

I want when value is null then it should take default value, that's I want to achieve
Use COALESCE() function while inserting into table, Which will return non null parameter. NSERT INTO Foo ( FirstName, LastName ) VALUES('FOO', COALESCE(@value, default));

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.