Обсуждение: How to set a Default Value

Поиск
Список
Период
Сортировка

How to set a Default Value

От
Ângelo Marcos Rigo
Дата:
Hi

I have created a column item varchar(11) with default
value = 0

But when i do insert any record the value is set to
null.

How can i always get the defualt value if any other
value is not defined (sometimes it will have some
identification characters)

Thank´s in advance

Ângelo Marcos Rigo
AMR Informática
(51) 3348 0870
Rua Pe. Alois Kades 400/210
Porto Alegre /RS/Brasil
http://amr.freezope.org
angelo_rigo@yahoo.com.br









_______________________________________________________
Yahoo! doce lar. Faça do Yahoo! sua homepage.
http://br.yahoo.com/homepageset.html


Re: How to set a Default Value

От
Michael Glaesemann
Дата:
On Jan 13, 2006, at 22:10 , Ângelo Marcos Rigo wrote:

> I have created a column item varchar(11) with default
> value = 0

How did you set the default to 0? Can we see your table definition
statement? Also, note that 0 is an integer: you probably want
'0' (with the single quotes).

> But when i do insert any record the value is set to
> null.

How are your inserting records? Can we see your insert statement?

> How can i always get the defualt value if any other
> value is not defined (sometimes it will have some
> identification characters)

If you want to guarantee that there will always be *some* value, make
sure you set the column NOT NULL as well.

If this doesn't help, please provide a simple, complete example that
shows the behavior you describe so we may help you further.

Michael Glaesemann
grzm myrealbox com




Re: How to set a Default Value

От
Sean Davis
Дата:


On 1/13/06 8:10 AM, "Ângelo Marcos Rigo" <angelo_rigo@yahoo.com.br> wrote:

> Hi
>
> I have created a column item varchar(11) with default
> value = 0
> But when i do insert any record the value is set to
> null.

How are you defining your table and doing your insert?

Sean



Re: How to set a Default Value

От
Ângelo Marcos Rigo
Дата:
My table definition (i change the deafult to 'N') :

CREATE TABLE pautaitem
(
  cdpautaitem serial NOT NULL,
  arquivo varchar(255),
  item varchar(255),
  cdpauta varchar(255),
  cditem varchar(11) NOT NULL DEFAULT 'N',
  CONSTRAINT pauta_arquivos_pkey PRIMARY KEY
(cdpautaitem)
)

My insert query:

INSERT INTO PautaItem (
         CdPauta,
         Item,
                 CdItem,
         Arquivo) VALUES (
         '"+this.sCdPauta',
         '"+this.sItem',
                 '"+this.sCdItem',
         '"+this.sArqImagem')

Ângelo Marcos Rigo
AMR Informática
(51) 3348 0870
Rua Pe. Alois Kades 400/210
Porto Alegre /RS/Brasil
http://amr.freezope.org
angelo_rigo@yahoo.com.br










_______________________________________________________
Yahoo! doce lar. Faça do Yahoo! sua homepage.
http://br.yahoo.com/homepageset.html


Re: How to set a Default Value

От
Michael Fuhr
Дата:
On Fri, Jan 13, 2006 at 05:40:25PM +0000, ngelo Marcos Rigo wrote:
> My table definition (i change the deafult to 'N') :
>
> CREATE TABLE pautaitem
> (
>   cdpautaitem serial NOT NULL,
>   arquivo varchar(255),
>   item varchar(255),
>   cdpauta varchar(255),
>   cditem varchar(11) NOT NULL DEFAULT 'N',
>   CONSTRAINT pauta_arquivos_pkey PRIMARY KEY
> (cdpautaitem)
> )
>
> My insert query:
>
> INSERT INTO PautaItem (
>          CdPauta,
>          Item,
>                  CdItem,
>          Arquivo) VALUES (
>          '"+this.sCdPauta',
>          '"+this.sItem',
>                  '"+this.sCdItem',
>          '"+this.sArqImagem')

What was the result and how does it differ from what you were
expecting?  When I run the above statements I get

ERROR:  value too long for type character varying(11)

I assume the '"+this.sCdItem' values aren't literal but are going
to be interpolated into the INSERT statement by your application.
If that's the case, what does the final INSERT statement look like?
I'd guess you're inserting an empty string instead of NULL into the
cditem column, in which case the default expression won't be used.

--
Michael Fuhr