Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Default constraints and values

 
   Database Help (Home) -> Programming RSS
Next:  Placing a field in one particular monthly date re..  
Author Message
bpdee

External


Since: Mar 25, 2008
Posts: 8



(Msg. 1) Posted: Tue Jun 17, 2008 1:56 pm
Post subject: Default constraints and values
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hi,

Does anyone know why I am able to create a table such as follows?

create table test
(col1 nvarchar(1) default 'AA',
col2 nvarchar(1) null)
go

Notice that I have a default value of 'AA' for a column that is defined as
an nvarchar(1). SQL Server created the table without any errors.

Of course, when I try to do an insert without specifying the column so that
the default value would kick in, it gives me an error.

insert into test
(col2)
values
('A')
go

Server: Msg 8152, Level 16, State 4, Line 1
String or binary data would be truncated.
The statement has been terminated.

Thanks,
Dee

 >> Stay informed about: Default constraints and values 
Back to top
Login to vote
--CELKO--

External


Since: Jan 11, 2008
Posts: 1089



(Msg. 2) Posted: Tue Jun 17, 2008 2:04 pm
Post subject: Re: Default constraints and values [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>> Does anyone know why I am able to create a table such as follows?

CREATE TABLE Test
(col1 NVARCHAR(1) DEFAULT 'AA',
col2 NVARCHAR(1));

Notice that I have a DEFAULT of 'AA' for a column that is defined as
an NVARCHAR(1). SQL Server created the table without any errors. f
course, when I try to do an insert without specifying the column so
that the default value would kick in, it gives me an error. <<

There is no validation on a DEFAULT until run time. This is just SQL
Server; other SQL products handle it differently. I am not sure about
2008, but you used to be able to give a DEFAULT of a different but
CAST-able data type -- say INTEGER value to a REAL -- and it would
force the engine to constantly CAST the value on insertion.

 >> Stay informed about: Default constraints and values 
Back to top
Login to vote
Gert-Jan Strik

External


Since: Sep 09, 2003
Posts: 255



(Msg. 3) Posted: Wed Jun 18, 2008 10:13 pm
Post subject: Re: Default constraints and values [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

One possible reason is that it would lead to problems when a dynamic
default is used. The following example will only allow inserts 10
seconds of every minute:

create table test
(col1 nvarchar(1) default (datepart(second,current_timestamp)),
col2 nvarchar(1) null)

--
Gert-Jan
SQL Server MVP


bpdee wrote:
>
> Hi,
>
> Does anyone know why I am able to create a table such as follows?
>
> create table test
> (col1 nvarchar(1) default 'AA',
> col2 nvarchar(1) null)
> go
>
> Notice that I have a default value of 'AA' for a column that is defined as
> an nvarchar(1). SQL Server created the table without any errors.
>
> Of course, when I try to do an insert without specifying the column so that
> the default value would kick in, it gives me an error.
>
> insert into test
> (col2)
> values
> ('A')
> go
>
> Server: Msg 8152, Level 16, State 4, Line 1
> String or binary data would be truncated.
> The statement has been terminated.
>
> Thanks,
> Dee
 >> Stay informed about: Default constraints and values 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Finding columns in tables with specific default constraints - How can I display some specific defaults on some tables? For example some tables may have a default on a CreatingUser_ID column that default the value to suser_sid(). How could I display: 1. table name 2. column name 3. (default) constraint..

Column default values - Hello everyone, I have the following problem I am not sure how to solve: I am using sql 2005. I have a database that has a product table (already with around 2000 products). The product table has a price column. I need to add another column called..

IDENTITY as Default values - I need some feedback on a tricky subject: Given: CREATE TABLE [TEST] ( [id] [bigint] IDENTITY (1, 1) NOT NULL , [id2] [bigint] NULL CONSTRAINT [DF__TEST__id2__7C104AB9] DEFAULT (dbo.IDENT('test')) [field] varchar NULL ) ON [PRIMARY] ....

create table, default values - Hello, I'm trying recreate tables using the script that can be generated using enterprise manager. However the default values of columns are not generated. Can anyone advise how this can be remedied? Thanks Soc.

Obtaining default values after an INSERT - I am inserting a row into a database, and some columns insert default values (such as an @@identity column and a (getdate()) column). Though I can get the identity through @@identity, is there a way to get the other default values without having to..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]