简体   繁体   中英

Access > SQL > Create Table . Constraints > Foreign Key

The below statement is being used to create a table CAMPUS. I am getting the Error:

"Syntax error in CONSTRAINT clause"
"UPDATE" is highlighted signifying source of error.

By removing "ON UPDATE CASCADE ON DELETE NO ACTION" I am able to create the table with no issues.

What is the proper syntax or procedure? (the MS Access "Help" was of no help)

SQL Code:

CREATE TABLE CAMPUS(                
    CampusID    Counter(1,5)    NOT NULL,    
    UnivID    Long    NOT NULL,    
    CampusName    Text(50)    NOT NULL,    
    Address    Text(50)    NULL,    
    Zip    Number    NULL,    
    Phone    Number    NULL,               
    CONSTRAINT    CampusPK    PRIMARY KEY    (CampusID,UnivID),    
    CONSTRAINT    CampusFK    FOREIGN KEY    (UnivID)
    REFERENCES UNIVERSITY(UnivID)
    ON UPDATE CASCADE 
    ON DELETE NO ACTION
    CONSTRAINT    CampusAK1    UNIQUE    (CampusName)    
    );

Access (ACE, Jet, whatever) has supported referential actions in its SQL DLL since Jet 4.0 (Access2000). However, they are only available in ANSI-92 Query Mode .

With effect from Access2003, the Access UI can be placed in ANSI-92 Query Mode, allowing the newer, richer SQL DDL to be executed from the SQL View of a Query. Note that ADO (OLE DB) always uses ANSI-92 Query Mode and DAO uses "traditional" ANSI-89 Query Mode (however IIRC DAO's object model has been enhanced to include all referential actions including the post-89 SET NULL action).

Therefore, I speculate that you are getting a syntax error because your are trying to execute ANSI-92 Query Mode SQL DDL while in ANSI-89 Query Mode.

It's been more then 10 years since I last used MS Access, but it seems you can only write either CASCADE or SET NULL after ON UPDATE and ON DELETE in a referential constraint.

So basically you have to omit this part

ON DELETE NO ACTION

Link http://www.sqlexamples.info/SQL/bsc_sqlddl1.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM