web sql database foreign key support

允我心安 提交于 2019-12-06 10:31:32

问题


I'm testing this statement in Safari 5.0.5, but I get an error before FOREIGN:

CREATE TABLE IF NOT EXISTS Idea (
    id INTEGER PRIMARY KEY,
    title TEXT NOT NULL,
    content TEXT NOT NULL,
    created TIMESTAMP NOT NULL,
    sketchID INTEGER,
    categoryID INTEGER NOT NULL, 
    FOREIGN KEY (sketchID) REFERENCES (Sketch),
    FOREIGN KEY (categoryID) REFERENCES (Category));

I get the following error message:

SQLStatementError 1 [DATABASE] near "(": syntax error

Where is the error in this SQL statement?


回答1:


(Adding my comment as an answer)

As Neil pointed out, you are closing the bracket at the wrong position.

Additionally the syntax for the foreign key is wrong, the following should work (provided the HTML5 SQL dialect is standard compliant)

CREATE TABLE IF NOT EXISTS Idea 
(    
   id INTEGER PRIMARY KEY,    
   title TEXT NOT NULL,     
   content TEXT NOT NULL,    
   created TIMESTAMP NOT NULL,    
   sketchID INTEGER,    
   categoryID INTEGER NOT NULL,
   FOREIGN KEY (sketchID) REFERENCES Sketch (sketchId),    
   FOREIGN KEY (categoryID) REFERENCES Category (categoryId)
);



回答2:


keeping in mind that even with the correct syntax, foreign key feature is not enabled and could not in web database. because foreign key feature is disabled by default in sqlite3, you have to manually enable it via statement "PRAGMA foreign_keys = ON". Unfortunately, PRAGMA statement is disabled in web database. good luck!




回答3:


You need to replace the ) with a , after categoryID INTEGER NOT NULL) so your statement will become:

CREATE TABLE IF NOT EXISTS Idea (    
           id INTEGER PRIMARY KEY,    
           title TEXT NOT NULL,     
           content TEXT NOT NULL,    
           created TIMESTAMP NOT NULL,    
           sketchID INTEGER,    
           categoryID INTEGER NOT NULL,
           FOREIGN KEY (sketchID) REFERENCES Sketch (sketchID),    
           FOREIGN KEY (categoryID) REFERENCES Category (categoryID));

Note the additional bracket at the end of the statement.



来源:https://stackoverflow.com/questions/6647144/web-sql-database-foreign-key-support

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