• pietlinden - Sunday, February 19, 2017 4:56 PM

    Ed,
    Do you have an example of creating constraints the way Gail does, but naming them?  The only examples I have seen require an ALTER TABLE ADD CONSTRAINT style... I searched around and didn't see any.
    Thanks!
    Pieter

    Absolutely.  Other than the name, the difference is that the constraint is defined separately from the column.
    Of course, the real difference is that Gail's example of Stations and Star Systems is much cooler than my boring  Employees table. 😛

    IF OBJECT_ID('dbo.Employees', 'u') IS NOT NULL DROP TABLE dbo.Employees;
    IF OBJECT_ID('dbo.Departments', 'u') IS NOT NULL DROP TABLE dbo.Departments;

    CREATE TABLE dbo.Departments (
    ID Integer not null identity (1, 1),
    constraint Employees_PK PRIMARY KEY (ID),
    Name Varchar(32) not null,
    EntryDate Datetime,
    UpdateDate Datetime);

    CREATE TABLE dbo.Employees (
    ID Integer not null identity (1, 1),
    constraint EmployeesNVC_PK PRIMARY KEY (ID),
    FirstName Nvarchar(32) not null,
    LastName Nvarchar(32) not null,
    DepartmentID Integer not null,
    CONSTRAINT Employees_Departments_FK
        FOREIGN KEY (DepartmentID)
        REFERENCES dbo.Departments (ID),
    HireDate Datetime not null);