2013-04-11 184 views
1

當「關鍵字‘改變’附近的語法不正確」我有這些表:添加外鍵約束

Employee(Ename, SSN, Bdate, Address, Dnumber) 
Department (Dname,Dno,Dmgr_SSN(
Project(Pname, Pnumber, Plocation, Dnum) 
WorksON(SSN,Pnumber,Hours) 

當我嘗試使用ALTER

alter table Employee 
add foreign key (Dnumber) 
REFERENCES Department (Dno); 

我得到這個錯誤:

Msg 156, Level 15, State 1, Line 24
Incorrect syntax near the keyword 'alter'.

注意:我根據表查詢的定義寫了這個查詢

+1

你看了[文件](http://msdn.microsoft.com/en-us/library/ms190273 .aspx),包括例子? – Pondlife 2013-04-11 02:41:02

+0

只是問明顯。你是否執行了「創建表」和「更改表」? – Gayu 2013-04-11 03:00:46

回答

4

documentation on the ALTER TABLE command會很容易地顯示你,這是使用語法:

ALTER TABLE dbo.Employee 
    ADD CONSTRAINT FK_Employee_Department 
    FOREIGN KEY(Dnumber) REFERENCES dbo.Department(Dno);