Insert with default value : Insert « Insert Delete Update « SQL Server / T-SQL






Insert with default value

 


2> CREATE TABLE publishers2
3> (
4>     pub_id        int            NOT NULL PRIMARY KEY IDENTITY,
5>     pub_name      varchar(40)    NULL DEFAULT ('Anonymous'),
6>     city          varchar(20)    NULL,
7>     state         char(2)        NULL,
8>     country       varchar(30)    NOT NULL DEFAULT('USA')
9> )
10> GO
1>
2>
3> INSERT publishers2 VALUES ('AAA Publishing', 'Vancouver', 'BC',
4>     'Canada')
5>
6> INSERT INTO publishers2 VALUES ('Best Publishing', 'Mexico City',
7>     NULL, 'Mexico')
8>
9> INSERT INTO publishers2 (pub_name, city, state, country)
10> VALUES ('Complete Publishing', 'Washington', 'DC', 'United States')
11>
12> INSERT publishers2 (state, city) VALUES ('WA', 'Poulsbo')
13>
14> INSERT publishers2 VALUES (NULL, NULL, NULL, DEFAULT)
15>
16> INSERT publishers2 VALUES (DEFAULT, NULL, 'WA', DEFAULT)
17>
18> INSERT publishers2 VALUES (NULL, DEFAULT, DEFAULT, DEFAULT)
19>
20> INSERT publishers2 DEFAULT VALUES
21> GO

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)
1>
2> drop table publishers2;
3> GO
1>

 








Related examples in the same category

1.The order of the columns in the insert statement
2.Inserts one or more rows selected with a subquery
3.Leave column as NULL
4.Inserting Several Records in a Query Batch