Making a Table be Self-Referencing : REFERENCES « Constraints « SQL Server / T-SQL Tutorial






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


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>    ALTER TABLE Employees
2>       ADD CONSTRAINT PK_EmployeeID
3>       PRIMARY KEY (EmployeeID)
4> GO
1>
2> EXEC sp_help Employees
3> GO
Name                                                                                                                             Owner
                                                          Type                            Created_datetime
-------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------
--------------------------------------------------------- ------------------------------- -----------------------
Employees                                                                                                                        dbo
                                                          user table                      2008-08-17 13:06:24.657


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

index_name                                                                                                                       index_description
                                                                                                                                            index_keys











-------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------
PK_EmployeeID                                                                                                                    clustered, unique, primary key located on PRIMARY
                                                                                                                                            EmployeeID












constraint_type                                                                                                                                    constraint_name
                                                                            delete_action update_action status_enabled status_for_replication constraint_keys











-------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------
--------------------------------------------------------------------------- ------------- ------------- -------------- ---------------------- ----------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------
PRIMARY KEY (clustered)                                                                                                                            PK_EmployeeID
                                                                            (n/a)         (n/a)         (n/a)          (n/a)                  EmployeeID












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








7.4.REFERENCES
7.4.1.Self-Referencing Tables
7.4.2.Primary key and foreign key
7.4.3.Making a Table be Self-Referencing
7.4.4.ON UPDATE CASCADE ON DELETE NO ACTION
7.4.5.REFERENCES customer(cust_id) (from another table)