Add a nullable column : Nullable « Constraints « SQL Server / T-SQL






Add a nullable column

 


4>
5>    CREATE TABLE Employees
6>    (
7>       EmployeeID       int           IDENTITY  NOT NULL,
8>       FirstName        varchar(25)             NOT NULL,
9>       MiddleInitial    char(1)                 NULL,
10>       LastName         varchar(25)             NOT NULL,
11>       Title            varchar(25)             NOT NULL,
12>       SSN              varchar(11)             NOT NULL,
13>       Salary           money                   NOT NULL,
14>       PriorSalary      money                   NOT NULL,
15>       LastRaise AS Salary - PriorSalary,
16>       HireDate         smalldatetime           NOT NULL,
17>       TerminationDate  smalldatetime           NULL,
18>       ManagerEmpID     int                     NOT NULL,
19>       Department       varchar(25)             NOT NULL
20>    )
21>    GO
1>
2>
3> EXEC sp_help Employees
4> GO
Name                                                                                                                             Owner
                                                          Type                            Created_datetime
-------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------
--------------------------------------------------------- ------------------------------- -----------------------
Employees                                                                                                                        dbo
                                                          user table                      2008-08-17 13:06:12.460


Column_name                                                                                                                      Type
                                                          Computed                            Length      Prec  Scale Nullable                            TrimTrailingBlanks                  FixedLenNu
llInSource                Collation
-------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------
--------------------------------------------------------- ----------------------------------- ----------- ----- ----- ----------------------------------- ----------------------------------- ----------
------------------------- --------------------------------------------------------------------------------------------------------------------------------
EmployeeID                                                                                                                       int
                                                          no                                            4 10    0     no                                  (n/a)                               (n/a)
                          NULL
FirstName                                                                                                                        varchar
                                                          no                                           25             no                                  no                                  no
                          SQL_Latin1_General_CP1_CI_AS
MiddleInitial                                                                                                                    char
                                                          no                                            1             yes                                 no                                  yes
                          SQL_Latin1_General_CP1_CI_AS
LastName                                                                                                                         varchar
                                                          no                                           25             no                                  no                                  no
                          SQL_Latin1_General_CP1_CI_AS
Title                                                                                                                            varchar
                                                          no                                           25             no                                  no                                  no
                          SQL_Latin1_General_CP1_CI_AS
SSN                                                                                                                              varchar
                                                          no                                           11             no                                  no                                  no
                          SQL_Latin1_General_CP1_CI_AS
Salary                                                                                                                           money
                                                          no                                            8 19    4     no                                  (n/a)                               (n/a)
                          NULL
PriorSalary                                                                                                                      money
                                                          no                                            8 19    4     no                                  (n/a)                               (n/a)
                          NULL
LastRaise                                                                                                                        money
                                                          yes                                           8 19    4     yes                                 (n/a)                               (n/a)
                          NULL
HireDate                                                                                                                         smalldatetime
                                                          no                                            4             no                                  (n/a)                               (n/a)
                          NULL
TerminationDate                                                                                                                  smalldatetime
                                                          no                                            4             yes                                 (n/a)                               (n/a)
                          NULL
ManagerEmpID                                                                                                                     int
                                                          no                                            4 10    0     no                                  (n/a)                               (n/a)
                          NULL
Department                                                                                                                       varchar
                                                          no                                           25             no                                  no                                  no
                          SQL_Latin1_General_CP1_CI_AS

Identity                                                                                                                         Seed                                     Increment
           Not For Replication
-------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------- ------------------------------
---------- -------------------
EmployeeID                                                                                                                                                              1
         1                   0

RowGuidCol
--------------------------------------------------------------------------------------------------------------------------------
No rowguidcol column defined.

Data_located_on_filegroup
--------------------------------------------------------------------------------------------------------------------------------
PRIMARY

The object 'Employees' does not have any indexes, or you do not have permissions.

No constraints are defined on object 'Employees', or you do not have permissions.

No foreign keys reference table 'Employees', or you do not have permissions on referencing tables.
Table is referenced by views
--------------------------------------------------------------------------------------------------------------------------------
1>
2>       ALTER TABLE Employees
3>       ADD
4>          PreviousEmployer   varchar(30)   NULL
5> GO
1>
2> EXEC sp_help Employees
3> GO
Name                                                                                                                             Owner
                                                          Type                            Created_datetime
-------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------
--------------------------------------------------------- ------------------------------- -----------------------
Employees                                                                                                                        dbo
                                                          user table                      2008-08-17 13:06:12.460


Column_name                                                                                                                      Type
                                                          Computed                            Length      Prec  Scale Nullable                            TrimTrailingBlanks                  FixedLenNu
llInSource                Collation
-------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------
--------------------------------------------------------- ----------------------------------- ----------- ----- ----- ----------------------------------- ----------------------------------- ----------
------------------------- --------------------------------------------------------------------------------------------------------------------------------
EmployeeID                                                                                                                       int
                                                          no                                            4 10    0     no                                  (n/a)                               (n/a)
                          NULL
FirstName                                                                                                                        varchar
                                                          no                                           25             no                                  no                                  no
                          SQL_Latin1_General_CP1_CI_AS
MiddleInitial                                                                                                                    char
                                                          no                                            1             yes                                 no                                  yes
                          SQL_Latin1_General_CP1_CI_AS
LastName                                                                                                                         varchar
                                                          no                                           25             no                                  no                                  no
                          SQL_Latin1_General_CP1_CI_AS
Title                                                                                                                            varchar
                                                          no                                           25             no                                  no                                  no
                          SQL_Latin1_General_CP1_CI_AS
SSN                                                                                                                              varchar
                                                          no                                           11             no                                  no                                  no
                          SQL_Latin1_General_CP1_CI_AS
Salary                                                                                                                           money
                                                          no                                            8 19    4     no                                  (n/a)                               (n/a)
                          NULL
PriorSalary                                                                                                                      money
                                                          no                                            8 19    4     no                                  (n/a)                               (n/a)
                          NULL
LastRaise                                                                                                                        money
                                                          yes                                           8 19    4     yes                                 (n/a)                               (n/a)
                          NULL
HireDate                                                                                                                         smalldatetime
                                                          no                                            4             no                                  (n/a)                               (n/a)
                          NULL
TerminationDate                                                                                                                  smalldatetime
                                                          no                                            4             yes                                 (n/a)                               (n/a)
                          NULL
ManagerEmpID                                                                                                                     int
                                                          no                                            4 10    0     no                                  (n/a)                               (n/a)
                          NULL
Department                                                                                                                       varchar
                                                          no                                           25             no                                  no                                  no
                          SQL_Latin1_General_CP1_CI_AS
PreviousEmployer                                                                                                                 varchar
                                                          no                                           30             yes                                 no                                  yes
                          SQL_Latin1_General_CP1_CI_AS

Identity                                                                                                                         Seed                                     Increment
           Not For Replication
-------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------- ------------------------------
---------- -------------------
EmployeeID                                                                                                                                                              1
         1                   0

RowGuidCol
--------------------------------------------------------------------------------------------------------------------------------
No rowguidcol column defined.

Data_located_on_filegroup
--------------------------------------------------------------------------------------------------------------------------------
PRIMARY

The object 'Employees' does not have any indexes, or you do not have permissions.

No constraints are defined on object 'Employees', or you do not have permissions.

No foreign keys reference table 'Employees', or you do not have permissions on referencing tables.
Table is referenced by views
--------------------------------------------------------------------------------------------------------------------------------
1>
2>    drop table Employees;
3>    GO
1>

 








Related examples in the same category

1.Not Allowing Null Values in a Column
2.Changing a Column Definition: Make it Nullable