sql - Using default constraint with null values? -


is there way insert default value if coming value null , constraint:

alter table student add constraint df_name default 'notavailable' [name] 

when run this:

insert student (name,classid)values (null,1) 

it still insert null instead of default value.

if understand you, want this:

insert student ( foo, bar, name ) values ( 'baz', 'qux', null ) 

to this:

insert student ( foo, bar, name ) values ( 'baz', 'qux', 'notavailable' ) 

(note think bad idea because null meant represent "notavailable" values - should avoid in-band values represent error conditions - , avoid string/text values represent status values)

(it's bad idea because checking "notavailabile" considerably more expensive checking null values - tables , indexes use more space , take longer seek , scan)

but if want this...

create trigger oninsertoverridenameifnull on student instead of insert      insert student ( foo, bar, name )     select foo, bar, isnull( [name], 'notavailable' ) [inserted]  end 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -