Using constraints in SQL Server 2005?

Hello!

I'm new to using SQL Server 2005 and I'm having problems with a SQL Query. The query is:

CREATE TABLE Deliveries(

S CHAR(2) NOT NULL,

P CHAR(2) NOT NULL,

QTY SMALLINT NOT NULL,

PRIMARY KEY (S,P)

CONSTRAINT FKSuppliers

FOREIGN KEY (S) REFERENCES Suppliers(S),

CONSTRAINT FKProducts

FOREIGN KEY (P) REFERENCES Products(P)

)


When I run it I get the message:

Incorrect syntax near the keyword 'CONSTRAINT'.

I'm not sure if a column can be both Primary- and Foreign Key And I'm not sure if SQL SERVER supports all the SQL I'm using (of if I'm using it the right way).

Thanks in advance!



Answer this question

Using constraints in SQL Server 2005?

  • Galen Murdock

    Hello again!

    Thanks! Now it all works great ;)


  • minton

    You missed keywords  CONSTRAINT PK_Deliveries ..This compiled fine in my system.

    CREATE
    TABLE Deliveries(

    S CHAR(2) NOT NULL,

    P CHAR(2) NOT NULL,

    QTY SMALLINT NOT NULL,

    CONSTRAINT PK_Deliveries PRIMARY KEY (S,P),

    CONSTRAINT FKSuppliers

    FOREIGN KEY (S) REFERENCES Suppliers(S),

    CONSTRAINT FKProducts

    FOREIGN KEY (P) REFERENCES Products(P)

    )



  • Using constraints in SQL Server 2005?