SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Default Constraint Not Reflected on Table


Default Constraint Not Reflected on Table

Author
Message
sqlusers
sqlusers
Mr or Mrs. 500
Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)

Group: General Forum Members
Points: 539 Visits: 512
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?
laurie-789651
laurie-789651
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1380 Visits: 1272
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.
sqlusers
sqlusers
Mr or Mrs. 500
Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)

Group: General Forum Members
Points: 539 Visits: 512

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]


laurie-789651
laurie-789651
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1380 Visits: 1272
I can't see ImportDate there?
sqlusers
sqlusers
Mr or Mrs. 500
Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)

Group: General Forum Members
Points: 539 Visits: 512
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.
laurie-789651
laurie-789651
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1380 Visits: 1272
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)
*/


dwain.c
dwain.c
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16781 Visits: 6431
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
laurie-789651
laurie-789651
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1380 Visits: 1272
But you couldn't apply a NOT NULL constraint to a column containing nulls, could you?
laurie-789651
laurie-789651
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1380 Visits: 1272
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
dwain.c
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16781 Visits: 6431
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search