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

SQL Server / T-SQL Tutorial
1. Query
2. Insert Delete Update
3. Table
4. Table Join
5. Data Types
6. Set Operations
7. Constraints
8. Subquery
9. Aggregate Functions
10. Date Functions
11. Math Functions
12. String Functions
13. Data Convert Functions
14. Analytical Functions
15. Sequence Indentity
16. View
17. Index
18. Cursor
19. Database
20. Transact SQL
21. Procedure Function
22. Trigger
23. Transaction
24. XML
25. System Functions
26. System Settings
27. System Tables Views
28. User Role
29. CLR
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
SQL Server / T-SQL Tutorial » Constraints » REFERENCES 
7. 4. 3. Making a Table be Self-Referencing
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)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.