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


Instance: table column with default value


Instance: table column with default value

Author
Message
JoseACJr
JoseACJr
SSChasing Mays
SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)

Group: General Forum Members
Points: 630 Visits: 307
Hello,

During the installation of SQL Server or even after installed, it is possible to define some types of fields with value?

Is required to set the null value after the field type?
What can happen if null is not defined?

example:
create table Table1(
...
ExpirationDate datetime null,
ExpirationDate datetime, -- always have a value ?
...);

Thank you very much!

JoseAcJR
Keith Tate
Keith Tate
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2360 Visits: 979
Is required to set the null value after the field type?
It is not required.

What can happen if null is not defined?
If you don't set the NULL option then it will be nullable.

create table Test1 (
ExpirationDate datetime);
go





Microsoft Certified Master - SQL Server 2008
Follow me on twitter: @keith_tate

Forum Etiquette: How to post data/code on a forum to get the best help
JoseACJr
JoseACJr
SSChasing Mays
SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)SSChasing Mays (630 reputation)

Group: General Forum Members
Points: 630 Visits: 307
Ok, but let me explain better:

During the installation of SQL Server or even once installed, can we define default value for some types (eg DateTime) for all tables that pass to be created?

Thanks
Keith Tate
Keith Tate
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2360 Visits: 979
You can create User Defined Data Types that had the necessary Null values you want defaulted for that data type, but you would have to use that data type when creating tables. I don't believe that you can alter the existing data types.



Microsoft Certified Master - SQL Server 2008
Follow me on twitter: @keith_tate

Forum Etiquette: How to post data/code on a forum to get the best help
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63873 Visits: 17974
JoseACJr (7/30/2014)
Ok, but let me explain better:

During the installation of SQL Server or even once installed, can we define default value for some types (eg DateTime) for all tables that pass to be created?

Thanks


NO. As Keith said you would have to use a user defined datatype.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
ScottPletcher
ScottPletcher
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20028 Visits: 7418
Keith Tate (7/30/2014)

What can happen if null is not defined?
If you don't set the NULL option then it will be nullable.

create table Test1 (
ExpirationDate datetime);
go



That's not true. The default could be NULL or NOT NULL, and which one can be influenced by both database and/or session settings. Therefore, you should always explicitly specify "NULL" or "NOT NULL" on every column, since you won't know the database and/or session settings if/when the code is run again if the future.

SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63873 Visits: 17974
ScottPletcher (7/30/2014)
Keith Tate (7/30/2014)

What can happen if null is not defined?
If you don't set the NULL option then it will be nullable.

create table Test1 (
ExpirationDate datetime);
go



That's not true. The default could be NULL or NOT NULL, and which one can be influenced by both database and/or session settings. Therefore, you should always explicitly specify "NULL" or "NOT NULL" on every column, since you won't know the database and/or session settings if/when the code is run again if the future.


I agree we should always define NULL or NOT NULL. What settings can change or influence this? I am not familiar with that.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
ScottPletcher
ScottPletcher
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20028 Visits: 7418
Sean Lange (7/30/2014)
ScottPletcher (7/30/2014)
Keith Tate (7/30/2014)

What can happen if null is not defined?
If you don't set the NULL option then it will be nullable.

create table Test1 (
ExpirationDate datetime);
go



That's not true. The default could be NULL or NOT NULL, and which one can be influenced by both database and/or session settings. Therefore, you should always explicitly specify "NULL" or "NOT NULL" on every column, since you won't know the database and/or session settings if/when the code is run again if the future.


I agree we should always define NULL or NOT NULL. What settings can change or influence this? I am not familiar with that.


From Books Online.
"
If the system-supplied data type has only one option, it takes precedence. timestamp data types must be NOT NULL.

When any session settings are set ON by using SET:
ANSI_NULL_DFLT_ON = ON, NULL is assigned.
ANSI_NULL_DFLT_OFF = ON, NOT NULL is assigned.

When any database settings are configured by using ALTER DATABASE:
ANSI_NULL_DEFAULT_ON = ON, NULL is assigned.
ANSI_NULL_DEFAULT_OFF = ON, NOT NULL is assigned.

When neither of the ANSI_NULL_DFLT options is set for the session and the database is set to the default (ANSI_NULL_DEFAULTis OFF), the default of NOT NULL is assigned.
"

Hmm, yeah, I'm not gonna try to guess the current state of all that, I'm just gonna code "NULL" or "NOT NULL" and be done with it :-D.

SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63873 Visits: 17974
ScottPletcher (7/30/2014)
Sean Lange (7/30/2014)
ScottPletcher (7/30/2014)
Keith Tate (7/30/2014)

What can happen if null is not defined?
If you don't set the NULL option then it will be nullable.

create table Test1 (
ExpirationDate datetime);
go



That's not true. The default could be NULL or NOT NULL, and which one can be influenced by both database and/or session settings. Therefore, you should always explicitly specify "NULL" or "NOT NULL" on every column, since you won't know the database and/or session settings if/when the code is run again if the future.


I agree we should always define NULL or NOT NULL. What settings can change or influence this? I am not familiar with that.


From Books Online.
"
If the system-supplied data type has only one option, it takes precedence. timestamp data types must be NOT NULL.

When any session settings are set ON by using SET:
ANSI_NULL_DFLT_ON = ON, NULL is assigned.
ANSI_NULL_DFLT_OFF = ON, NOT NULL is assigned.

When any database settings are configured by using ALTER DATABASE:
ANSI_NULL_DEFAULT_ON = ON, NULL is assigned.
ANSI_NULL_DEFAULT_OFF = ON, NOT NULL is assigned.

When neither of the ANSI_NULL_DFLT options is set for the session and the database is set to the default (ANSI_NULL_DEFAULTis OFF), the default of NOT NULL is assigned.
"

Hmm, yeah, I'm not gonna try to guess the current state of all that, I'm just gonna code "NULL" or "NOT NULL" and be done with it :-D.


Agreed about not guessing. Was curious about what settings as I didn't know about them so had no idea where to start. Thanks for the info. Good to know.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Keith Tate
Keith Tate
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2360 Visits: 979
ScottPletcher (7/30/2014)
Sean Lange (7/30/2014)
ScottPletcher (7/30/2014)
Keith Tate (7/30/2014)

What can happen if null is not defined?
If you don't set the NULL option then it will be nullable.

create table Test1 (
ExpirationDate datetime);
go



That's not true. The default could be NULL or NOT NULL, and which one can be influenced by both database and/or session settings. Therefore, you should always explicitly specify "NULL" or "NOT NULL" on every column, since you won't know the database and/or session settings if/when the code is run again if the future.


I agree we should always define NULL or NOT NULL. What settings can change or influence this? I am not familiar with that.


From Books Online.
"
If the system-supplied data type has only one option, it takes precedence. timestamp data types must be NOT NULL.

When any session settings are set ON by using SET:
ANSI_NULL_DFLT_ON = ON, NULL is assigned.
ANSI_NULL_DFLT_OFF = ON, NOT NULL is assigned.

When any database settings are configured by using ALTER DATABASE:
ANSI_NULL_DEFAULT_ON = ON, NULL is assigned.
ANSI_NULL_DEFAULT_OFF = ON, NOT NULL is assigned.

When neither of the ANSI_NULL_DFLT options is set for the session and the database is set to the default (ANSI_NULL_DEFAULTis OFF), the default of NOT NULL is assigned.
"

Hmm, yeah, I'm not gonna try to guess the current state of all that, I'm just gonna code "NULL" or "NOT NULL" and be done with it :-D.
Thanks for the info Scott!



Microsoft Certified Master - SQL Server 2008
Follow me on twitter: @keith_tate

Forum Etiquette: How to post data/code on a forum to get the best help
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