简体   繁体   中英

Possible to assign a default value on a NULL against table column in sql

I am wondering if there is a way that when creating a table that you can assign a default value to a column if the value is null. I understand that you can use the syntax DEFAULT however this is only for when the value is absent. Is there a way similar to this that you can say when NULL it will add the default without using a trigger.

CREATE TABLE DBO.TESTS
(
TEST VARCHAR(100) DEFAULT(ISNULL('test',NULL)),
NUM INT
)

This is a test and the kind of thing i was looking at?

UPDATE: Example input

INSERT INTO TESTS (TEST,NUM)
VALUES (NULL,1)

Where the "NULL" is i would like that to enter the value "test". But also if i was to do the following

INSERT INTO TESTS (NUM)
VALUES (1)

This would also enter the value of "test" into the column "TEST".

I hope this helps.

You can add a default constraint to your table which will automatically add a set value for the column if the insert does not have a value for it:

CREATE TABLE DBO.TESTS
(
TEST VARCHAR(100),
NUM INT
)

ALTER TABLE [DBO].[TESTS] ADD  CONSTRAINT [DF_TESTS_TEST] DEFAULT (N'default_value_goes_here') FOR [TEST]

INSERT INTO [DBO].[TESTS] VALUES (NULL, 1)
INSERT INTO [DBO].[TESTS] (num) VALUES (2)

Results

NULL                      1
'default_value_goes_here' 2

If you want to check during an insert you can use COALESCE

DECLARE @insertValue VARCHAR(100)
SET @insertValue = NULL
INSERT INTO DBO.TESTS VALUES (COALESCE (@insertValue, 'defaultValue'), 1);

The bottom line is that the column should be non-nullable with a default value.
It's possible to replace null values with default values in a trigger for insert/update, but that doesn't make any sense - first because it means every update/insert will have to do that extra work, and second, because that would make it impossible to insert null to the column (unless the triggers are disabled) so why allow nulls in the first place? It's a mistake that will only be confusing for anyone attempting to use that table.

Think about it from the user side - when you send null to a nullable column, you expect it to be null , you don't expect it to contain a value.

If you run this insert statement:

INSERT INTO TESTS (TEST,NUM)
VALUES (NULL,1)

You expect the table to contain a row where Test is null and num = 1.
You do not expect the Test column to contain the default value.

Yes, there is a way to do what you want. It is called a trigger. You would have to define a trigger that sets the value when a NULL is inserted into the column.

If you use an INSTEAD OF trigger, then you can still declare the column as NOT NULL . The trigger will take care of assigning a value so the constraint is not violated.

So, you can do this. Why you would want to do it is another question. Perhaps you are not familiar with the DEFAULT keyword that allows default values to be inserted using a VALUES() clause. This is explained in the documentation for INSERT .

When providing a value for a column, including NULL , that value will be used. NULL is still a value, just an unknown value. A DEFAULT value will only be used if no value is passed (which, as I just said, NULL is a value so doesn't count).

If you don't want a NULL in your table, then instead stop people supplying them by setting your column as NOT NULL :

CREATE TABLE dbo.TestTable (ID int, String varchar(100) NOT NULL DEFAULT 'test')
GO
--INSERT is successful, String has a value of 'test'
INSERT INTO dbo.TestTable (ID)
VALUES(1);
GO
--INSERT fails, String cannot have a value of NULL
INSERT INTO dbo.TestTable (ID,
                           String)
VALUES(2,NULL);
GO
SELECT *
FROM dbo.TestTable;
GO
DROP TABLE dbo.TestTable;
GO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM