Default Constraint Not Reflected on Table

  • Hi Guys,

    I have created a Default Constraint for my table. But this is not reflected on my table.

    EX: Column ImportDate has a Datetime Datatype with NOT NULL Constraint.

    I have created a Default Constranit for this as GETDATE(). But while checking the query this Constraint not getting reflected on my table.

    Every time it shows NULL.

    Can any one help on this?

  • Can you post the definition of the table? This will make it clear.

    Select the table, 'Script table as' > 'Create to' > 'new query editor window' & post the SQL.

  • SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    CREATE TABLE [dbo].[tblGen](

    [PROCESS_ID] [bigint] NOT NULL,

    [Gen_FLAG] [bit] NOT NULL CONSTRAINT [DF_FLAG] DEFAULT ((0)),

    [Gen_DATE] [datetime] NOT NULL CONSTRAINT [DF_DATE] DEFAULT (getdate())

    ) ON [PRIMARY]

  • I can't see ImportDate there?

  • laurie-789651 (10/1/2012)


    I can't see ImportDate there?

    You could see Gen_DATE has the import date. Please let me know if any.

    Thanks.

  • It looks OK to me:

    You couldn't get NULL in Gen_DATE when it's defined as NOT NULL.

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    IF OBJECT_ID('dbo.tblGen') IS NOT NULL

    DROP TABLE dbo.tblGen;

    CREATE TABLE [dbo].[tblGen](

    [PROCESS_ID] [bigint] NOT NULL,

    [Gen_FLAG] [bit] NOT NULL CONSTRAINT [DF_FLAG] DEFAULT ((0)),

    [Gen_DATE] [datetime] NOT NULL CONSTRAINT [DF_DATE] DEFAULT (getdate())

    ) ON [PRIMARY]

    insert into dbo.tblGen ( PROCESS_ID ) values ( 6703 ) ;

    select * from dbo.tblGen;

    /*

    PROCESS_ID Gen_FLAG Gen_DATE

    -------------------- -------- -----------------------

    6703 0 2012-10-01 10:52:51.937

    (1 row(s) affected)

    */

  • sqlusers (10/1/2012)


    Hi Guys,

    I have created a Default Constraint for my table. But this is not reflected on my table.

    EX: Column ImportDate has a Datetime Datatype with NOT NULL Constraint.

    I have created a Default Constranit for this as GETDATE(). But while checking the query this Constraint not getting reflected on my table.

    Every time it shows NULL.

    Can any one help on this?

    Could it be that you are looking at records that were created before you added the DEFAULT constraint?


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • But you couldn't apply a NOT NULL constraint to a column containing nulls, could you?

  • dwain.c (10/1/2012)


    sqlusers (10/1/2012)


    Hi Guys,

    I have created a Default Constraint for my table. But this is not reflected on my table.

    EX: Column ImportDate has a Datetime Datatype with NOT NULL Constraint.

    I have created a Default Constranit for this as GETDATE(). But while checking the query this Constraint not getting reflected on my table.

    Every time it shows NULL.

    Can any one help on this?

    Could it be that you are looking at records that were created before you added the DEFAULT constraint?

    Looks like you may be right there...

  • dwain.c (10/1/2012)


    sqlusers (10/1/2012)


    Hi Guys,

    I have created a Default Constraint for my table. But this is not reflected on my table.

    EX: Column ImportDate has a Datetime Datatype with NOT NULL Constraint.

    I have created a Default Constranit for this as GETDATE(). But while checking the query this Constraint not getting reflected on my table.

    Every time it shows NULL.

    Can any one help on this?

    Could it be that you are looking at records that were created before you added the DEFAULT constraint?

    That is true. I was suggesting that maybe the DEFAULT constraint was added after some INSERTs were made, and that wouldn't of course affect data already saved. I didn't notice that the column the OP was referring to was NOT NULL.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • dwain.c (10/1/2012)


    dwain.c (10/1/2012)


    sqlusers (10/1/2012)


    Hi Guys,

    I have created a Default Constraint for my table. But this is not reflected on my table.

    EX: Column ImportDate has a Datetime Datatype with NOT NULL Constraint.

    I have created a Default Constranit for this as GETDATE(). But while checking the query this Constraint not getting reflected on my table.

    Every time it shows NULL.

    Can any one help on this?

    Could it be that you are looking at records that were created before you added the DEFAULT constraint?

    That is true. I was suggesting that maybe the DEFAULT constraint was added after some INSERTs were made, and that wouldn't of course affect data already saved. I didn't notice that the column the OP was referring to was NOT NULL.

    I still reckon you're right though.

    Note to OP: If you add a default constraint to an existing column that allows nulls, SQL Server does not backfill the values to existing rows - you have to do that yourself!:-)

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply