SQL DELETE& .import effects / references两个表

时间:2017-07-12 23:40:10

标签: sql sqlite

你知道什么可能导致SQLite中两个表之间的引用/依赖吗?在sqlite3,Terminal,OS X 10.12.5中构建

概述:

我已经构建了一小组数据库表(底部是完整的模式)。 但是,其中两个表似乎是相互引用的。在.import到任何一个表的位置,两个表都受到影响。同样,在任何一个表的DELETE FROM上,两个表都会受到影响。

详情:

行动细节/步骤:

sqlite> SELECT * FROM Wine; -> NO DATA
sqlite> SELECT * FROM WineVarietal; -> NO DATA
sqlite> .import fileLocation/fileName.csv Wine
sqlite> SELECT * FROM Wine; -> NEW ROWS SHOW

SELECT * FROM Wine after initial .import to Wine

sqlite> SELECT * FROM WineVarietal; -> NEW ROWS FROM Wine TABLE SHOW

SELECT * FROM WineVarietal after initial .import to Wine

sqlite> .import fileLocation/fileName.csv WineVarietal
sqlite> SELECT * FROM Wine; -> NEW ROWS FROM WineVarietal TABLE SHOW

SELECT * FROM Wine after initial .import to WineVarietal

sqlite> SELECT * FROM WineVarietal; -> NEW ROWS SHOW

SELECT * FROM WineVarietal after initial .import to WineVarietal

sqlite> DELETE FROM WineVarietal WHERE VarietalID > 3;
sqlite> SELECT * FROM Wine; -> ROWS ARE MISSING FROM Wine TABLE

SELECT * FROM Wine after DELETE FROM WineVarietal

从以下步骤中可以看出,修改单个表会直接影响另一个表。子项的修改会影响父项,父项的修改会完全影响子项。我想必须有表设置创建这种依赖。

完整架构:

CREATE TABLE Appellation(AppellationID INTEGER PRIMARY KEY, AppellationName TEXT, AppellationState TEXT, AppelationCountry TEXT);
CREATE TABLE Style(StyleID INTEGER PRIMARY KEY, StyleName TEXT NOT NULL);
CREATE TABLE Vinter(VinterID INTEGER PRIMARY KEY, VinterName TEXT, VinterCountry TEXT, VinterState TEXT, VinterRegion TEXT, VinterCity TEXT);
CREATE TABLE Varietal(VarietalID INT PRIMARY KEY, VarietalName TEXT);
CREATE TABLE User(UserID INTEGER PRIMARY KEY, UserName TEXT, UserPhoto BLOB, UserSignupDate DATETIME DEFAULT CURRENT_TIMESTAMP);
CREATE TABLE Importer(ImporterID INTEGER PRIMARY KEY, Importer TEXT, ImporterCity TEXT, ImporterState TEXT, ImporterZip INTEGER, ImporterURL TEXT);
CREATE TABLE Retailer(RetailerID INTEGER PRIMARY KEY, RetailerName TEXT, RetailerStreet TEXT, RetailerCity TEXT, RetailerState TEXT, RetailerCountry TEXT);
CREATE TABLE Estate(EstateID INTEGER PRIMARY KEY, Estate TEXT, VinterID INTEGER, FOREIGN KEY (VinterID) REFERENCES Vinter(VinterID));
CREATE TABLE Wine(
WineID INTEGER PRIMARY KEY, 
VinterID INTEGER NOT NULL, 
StyleID INTEGER NOT NULL,
AppellationID INTEGER,
Vintage INTEGER NOT NULL,
WineName TEXT,
EstateID INTEGER,
BarcodeImage BLOB,
LabelImage BLOB,
ImporterID INTEGER,
FOREIGN KEY (VinterID) REFERENCES Vinter(VinterID),
FOREIGN KEY (StyleID) REFERENCES Style(StyleID),
FOREIGN KEY (AppellationID) REFERENCES Appellation(AppellationID),
FOREIGN KEY (EstateID) REFERENCES Estate(EstateID),
FOREIGN KEY (ImporterID) REFERENCES Importer(ImporterID));
CREATE TABLE "WineVarietal"(WineID INTEGER NOT NULL, VarietalID INTEGER NOT NULL, Percent INTEGER, FOREIGN KEY (WineID) REFERENCES Wine(WineID), FOREIGN KEY (VarietalID) REFERENCES Varietal(VarietalID));
CREATE TABLE Tasting(
TastingID INTEGER PRIMARY KEY, 
TastingTimeStamp DATETIME DEFAULT CURRENT_TIMESTAMP, 
UserID INTEGER NOT NULL,
WineID INTEGER NOT NULL,
RetailerID INTEGER,
Price INTEGER,
Rating INTEGER,
Review TEXT,
FOREIGN KEY (UserID) REFERENCES User(UserID),
FOREIGN KEY (WineID) REFERENCES Wine(WineID),
FOREIGN KEY (RetailerID) REFERENCES Retailer(RetailerID));
/* No STAT tables available */

1 个答案:

答案 0 :(得分:0)

由于您正在直接操作父表 Wine ,因此子表 WineVarietal 会受到影响,因为WineID正在引用键。