SQL Server/T-SQL/Constraints/Default Value

Материал из SQL эксперт
Перейти к: навигация, поиск

Add a default value of 0 if no value specified

   <source lang="sql">

1> create table employee( 2> ID int, 3> name nvarchar (10), 4> salary int, 5> start_date datetime, 6> city nvarchar (10), 7> region char (1)) 8> GO 1> 2> insert into employee (ID, name, salary, start_date, city, region) 3> values (1, "Jason", 40420, "02/01/94", "New York", "W") 4> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (2, "Robert",14420, "01/02/95", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (4, "Linda", 40620, "11/04/97", "New York", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (5, "David", 80026, "10/05/98", "Vancouver","W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (6, "James", 70060, "09/06/99", "Toronto", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (7, "Alison",90620, "08/07/00", "New York", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W") 3> GO (1 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 New York   W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> -- This will add a default value of 0 if no value specified 3> ALTER TABLE Employee WITH NOCHECK 4> ADD CONSTRAINT SalaryDefaultValue DEFAULT (0) FOR Salary 5> GO 1> 2> INSERT employee (Name) VALUES ("Bedford") 3> GO (1 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 New York   W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W
      NULL Bedford              0                    NULL NULL       NULL

(10 rows affected) 1> 2> 3> drop table employee 4> GO 1>

      </source>
   
  


Add Default value constraint

   <source lang="sql">

7> CREATE TABLE Customers 8> ( 9> CustomerNo int IDENTITY NOT NULL 10> PRIMARY KEY, 11> CustomerName varchar(30) NOT NULL, 12> Address1 varchar(30) NOT NULL, 13> Address2 varchar(30) NOT NULL, 14> City varchar(20) NOT NULL, 15> State char(2) NOT NULL, 16> Zip varchar(10) NOT NULL, 17> Contact varchar(25) NOT NULL, 18> Phone char(15) NOT NULL, 19> FedIDNo varchar(9) NOT NULL, 20> DateInSystem smalldatetime NOT NULL 21> ) 22> GO 1> 2> ALTER TABLE Customers 3> ADD CONSTRAINT CN_CustomerAddress 4> DEFAULT "UNKNOWN" FOR Address1 5> GO 1> 2> drop table Customers; 3> GO 1> 2>

</source>
   
  


Set default value for column

   <source lang="sql">

1> 2> CREATE TABLE T ( 3> int1 int, 4> bit1 bit NOT NULL DEFAULT 0, 5> rvr1 timestamp, 6> usr1 nvarchar(28) DEFAULT USER, 7> createtime datetime DEFAULT CURRENT_TIMESTAMP 8> ) 9> GO 1> 2> INSERT T (int1) VALUES (1) WAITFOR DELAY "00:00:01" 3> GO 1> INSERT T (int1, bit1) VALUES (2, 0) WAITFOR DELAY "00:00:01" 2> GO 1> INSERT T (int1, bit1) VALUES (3, 1) 2> GO (1 rows affected) 1> SELECT int1, bit1, usr1, 2> CONVERT(int, rvr1) "Timestamp as int", 3> createtime 4> FROM T 5> GO int1 bit1 usr1 Timestamp as int createtime


---- ---------------------------- ---------------- -----------------------
         1    0 dbo                                      5007 2006-10-14 10:53:20.827
         2    0 dbo                                      5008 2006-10-14 10:53:21.827
         3    1 dbo                                      5009 2006-10-14 10:53:22.827

(3 rows affected) 1> 2> UPDATE T 3> set bit1 = 1 4> WHERE int1 = 2 5> GO (1 rows affected) 1> 2> SELECT int1, bit1, usr1, 3> CONVERT(int, rvr1) "Timestamp as int", 4> createtime 5> FROM T 6> GO int1 bit1 usr1 Timestamp as int createtime


---- ---------------------------- ---------------- -----------------------
         1    0 dbo                                      5007 2006-10-14 10:53:20.827
         2    1 dbo                                      5010 2006-10-14 10:53:21.827
         3    1 dbo                                      5009 2006-10-14 10:53:22.827

(3 rows affected) 1> 2> drop table t 3> GO 1>

      </source>