Sql server - default

1

Good evening, I created a table to test the sql server default:

I made a select to see if the default values of the default were entered but you can see in the image that this did not happen. When does it enter these default values? I found that these values were entered when null or empty values were passed. Another problem is that the date entered was from 1900 and this only happens in that case, because if I pass the getDate () on insert the date to save will be the current one.

    
asked by anonymous 15.03.2016 / 06:23

2 answers

2

When you enter '' in the database, it NOT understands this as valor não informado and yes as string vazia . So in the varchar field it stores the empty string and in the field date converts to 1900-01-01.

Since you did not enter the order of the fields and which fields will be inserted in your insert it follows the order of your table. Example:

If you do insert into [testes5] (nome) values ('josé') the date field will get getdate() .

If you do insert into [testes5] (data) values ('2015-01-01') the name field will get padrão .

or if you want to do the insert without entering the order and inserted fields as you tried you can do

insert into [testes5] values (default,default)
    
15.03.2016 / 08:02
0
CREATE TABLE [testes5] (
  'id' integer PRIMARY KEY AUTOINCREMENT,
  'nome' varchar(255) DEFAULT 'Padrão',
  'data' date DEFAULT getdate()
)

INSERT INTO [testes5] (nome, data) VALUES (DEFAULT, DEFAULT)

SELECT * FROM [testes5]

If you specify a value without a string or NULL, it will be recognized as blank or null instead of unspecified, so that the default value is not entered.

    
15.03.2016 / 07:08