SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Wizard subnet_rx's Avatar
    Join Date
    Aug 2001
    Hattiesburg, MS
    0 Post(s)
    0 Thread(s)

    Null or Default value?

    What's the most optimized way to handle fields that aren't required to be filled in at record creation? NULL or give it a default value

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Toronto, Canada
    63 Post(s)
    3 Thread(s)
    NULL is better

    unless, of course, you know, a default is more appropriate

    example of NULL appropriate --
    create table employees
    ( id integer not null primary key auto_increment
    , name varchar(99) not null
    , hiredate  datetime not null
    , termdate  datetime null
    hiredate must be specified, because unless the employee is hired, we don't want her in the employees table

    termination date would be NULL until the employee terminates, then it gets a value

    a "default" value in termination date makes no sense whatsoever

    example of default appropriate --
    create table users
    ( id integer not null primary key auto_increment
    , username varchar(16) not null
    , password datetime not null
    , status char(1) default 'U'
    here the user's status defaults to U (ordinary user) so that we don't always have to specify it when adding a user --

    insert into users (username,password) values ('curly','woobwoob')
    insert into users (username,password) values ('larry','heyfellas')
    insert into users (username,password) values ('moe','whyioughta')

    insert into users (username,password,status) values ('superuser','iamgod','S') | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts