我正在使用Delphi 7创建一个带有一些DDL语句的Access DB。对于简单的股票发票系统,它将成为一个相对简单的关系数据库。我设法创建一个名为CUSTOMER的表没问题,但在尝试创建一个名为ORDER的表时,在ORDER中我有一个外键字段CustomerID,我收到以下错误消息:
"字段定义中的语法错误"。
CustomerID是CUSTOMER表中的Key字段,我只想将两者链接在一起。这是我的两个DDL语句。
cs:= 'CREATE TABLE tblCUSTOMER ('+
'CustomerID Number,' +
'FName Text(20),' +
'SName Text(20),' +
'AddressLine1 Text(35))';
ADOCommand1.CommandText:=cs;
ADOCommand1.Execute;
cs:='CREATE INDEX idxCustomerID ON tblCUSTOMER (CustomerID) WITH PRIMARY';
ADOCommand1.CommandText:=cs;
ADOCommand1.Execute;
cs:= 'CREATE TABLE tblORDER ('+
'OrderID Number,'+
//Here is the line!!
'CustomerID Number CONSTRAINT CustomerID REFERENCES tblCUSTOMER (CustomerID),'+
'OrderDate DateTime,'+
'CREATE INDEX idxPrimary ON tblORDER (OrderID) WITH PRIMARY)';
ADOCommand1.CommandText:=cs;
ADOCommand1.Execute;
我猜这个问题与上面的外键声明有关。我做错了什么?
我也试过这个:
cs:= 'CREATE TABLE tblORDER ('+
'OrderID Number CONSTRAINT PK_OrderID PRIMARY KEY,'+
'CustomerID Number CONSTRAINT FK_CustomerID REFERENCES tblCUSTOMER (CustomerID),'+
'OrderDate DateTime,'+
'CREATE INDEX idxPrimary ON tblORDER (OrderID) WITH PRIMARY';
仍然无法正常工作。
答案 0 :(得分:0)
我找到了解决方案。 CONSTRAINT子句应该如下:
cs:= 'CREATE TABLE tblORDER ('+
'OrderID Number,'+
'CustomerID Number CONSTRAINT FK_CustomerID References tblCUSTOMER (CustomerID),' +
'OrderDate DateTime)';
ADOCommand1.CommandText:=cs;
ADOCommand1.Execute;
CREATE INDEX子句应该是一个单独的DDL命令。
cs:='CREATE INDEX idxOrderID ON tblORDER (OrderID) WITH PRIMARY';
ADOCommand1.CommandText:=cs;
ADOCommand1.Execute;
非常感谢