Remix.run Logo
minkeymaniac a day ago

If you don't care for old data having null , you could add a check contraint with nocheck (this is sql server fwiw)

for example

create table foo(id int) insert foo values (1), (2), (3)

insert foo values (null)

select * from foo

id

1

2

3

NULL

ALTER TABLE foo with nocheck ADD CONSTRAINT CheckNotnull check (id IS NOT NULL)

insert foo values (null)

Msg 547, Level 16, State 0, Line 13 The INSERT statement conflicted with the CHECK constraint "CheckNotnull". The conflict occurred in database tempdb", table "dbo.foo", column 'id'. The statement has been terminated.

However be aware that if you update an existing value to NULL, you will still get the error

update foo set id = null where id = 2

Msg 547, Level 16, State 0, Line 20 The UPDATE statement conflicted with the CHECK constraint "CheckNotnull". The conflict occurred in database "tempdb", table "dbo.foo", column 'id'.