Syntax error when defining table with ON DELETE CASCADE

爷,独闯天下 提交于 2019-12-10 20:16:46

问题


I'm trying to use ON DELETE CASCADE in a FK constraint in MS Access 2007, but I'm getting an error on table definition:

SQL Error: Syntax error in CONSTRAINT clause.

Here's the code for creating the table:

CREATE TABLE Area (
    Id AUTOINCREMENT PRIMARY KEY, 
    AreaType__Id int NOT NULL, 
    Tbl1 text(31) NOT NULL, 
    Tbl2__Id int NOT NULL, 
    CONSTRAINT UK_Area_1 UNIQUE (Tbl1, Container__Id), 
    CONSTRAINT FK_Area_1 FOREIGN KEY (AreaType__Id) REFERENCES AreaType (Id), 
    CONSTRAINT FK_Area_2 FOREIGN KEY (Tbl2__Id) REFERENCES Tbl2 (Id) ON UPDATE CASCADE ON DELETE CASCADE
);

What am I doing wrong? I looked at the Access Help, and my syntax seems correct. I've tried removing the ON UPDATE CASCADE portion, but got the same error. I've also tried using the default PK field for the referenced table (REFERENCES Container instead of REFERENCES Container (Id)), but again got the same error. I also searched SO, but didn't find much useful info for my situation. It's got to be something simple, but I'm not seeing it at the moment.

EDIT

It's worth mentioning, that the table definition was working properly exactly as it is, except without the ON UPDATE CASCADE ON DELETE CASCADE part. Only after adding the CASCADE parts did the error appear.

EDIT 2

In an attempt to pinpoint the problem, here is new test code that demonstrates the error:

THIS WORKS:

CREATE TABLE T1 (Id AUTOINCREMENT PRIMARY KEY);

CREATE TABLE T2 (
    Id AUTOINCREMENT PRIMARY KEY, 
    T1__Id int NOT NULL, 
    CONSTRAINT FK_T2_1 FOREIGN KEY (T1__Id) REFERENCES T1 (Id)
);

THIS GIVES ERROR:

CREATE TABLE T1 (Id AUTOINCREMENT PRIMARY KEY);

CREATE TABLE T2 (
    Id AUTOINCREMENT PRIMARY KEY, 
    T1__Id int NOT NULL, 
    CONSTRAINT FK_T2_1 FOREIGN KEY (T1__Id) REFERENCES T1 (Id) ON DELETE CASCADE
);

Can anyone replicate the error?


回答1:


Your CREATE statement is valid Access DDL, but must be executed with ADO.

Here is an Immediate window session which demonstrates the problem ...

strSql = "CREATE TABLE T2 (" & vbCrLf & _
"    Id AUTOINCREMENT PRIMARY KEY, " & vbCrLf & _
"    T1__Id int NOT NULL, " & vbCrLf & _
"    CONSTRAINT FK_T2_1 FOREIGN KEY (T1__Id) REFERENCES T1 (Id) ON DELETE CASCADE" & vbCrLf & _
");"

' executing that statement with DAO triggers error 3289,
' "Syntax error in CONSTRAINT clause."
' (CurrentDb.Execute is a DAO Method)
CurrentDb.Execute strSql ' DAO -> fail

' CurrentProject.Connection.Execute is an ADO method,
' so this attempt executes without error
CurrentProject.Connection.Execute strSql ' ADO -> OK

Note if you were trying to execute the statement from the Access query designer, that also uses DAO so would trigger error 3289 too.




回答2:


The OP's problem has been solved a long time ago (see HansUp's answer); however, this answer might be useful to someone else getting the same error message.

I got the same error because I tried to reference a table named "User". Since user is a reserved word (apparently), this failed. The resolution is to enclose reserved names in brackets ([User]).

ALTER TABLE UserSetting
    ADD CONSTRAINT FK_UserSetting_User
    FOREIGN KEY (UserID) REFERENCES [User] (UserID) ON DELETE CASCADE



回答3:


The referential integrity syntax that works for me is the following:

create table Area (
  ID autoincrement primary key
, AreaTypeID long not null
  constraint Area_AreaTypeID
  references AreaType (ID)
, Tbl1 varchar(31) not null
, Tbl2ID long not null
  constraint Area_Tbl2ID
  references Tbl2 (ID)
  on update cascade
  on delete cascade
, constraint Area_UK unique (
    Tbl1
  , ContainerID
  )
)

The names themselves aren't very important, but as you can see I avoid giving columns the same name as other tables to avoid confusing the database engine.

Also I would say that if the Tbl2 ID column is an autoincrement column, the on update clause is redundant because obviously the numerical ID is never going to change.



来源:https://stackoverflow.com/questions/23028884/syntax-error-when-defining-table-with-on-delete-cascade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!