正确使用 FOREIGN KEY 函数
correct usage of the FOREIGN KEY function
在我的数据库中,我有两个 table 名为艺术家和曲目。在创建曲目 table 时,我将 FOREIGN KEY 设置为艺术家。但是当我 运行 这个显示的代码时,插入一个不存在的艺术家的曲目行是没有问题的。
那绝对不是目的...
在 http://homepages.ecs.vuw.ac.nz/~adam/scie201/lec_R_SQLite.html 上,我发现我必须使用 PRAGMA foreign_keys=ON
之类的东西打开此功能 - 但我不知道我应该如何编写此代码...
这是我的问题:如何正确实现 FOREIGN KEY 功能?
非常感谢您的帮助!
现在我的代码:
# needed packages for this script
# install.packages("sqldf") # install this package if necessary
library(sqldf)
# connection to the database TestDB.sqlite
db=dbConnect(SQLite(), dbname="TestDB.sqlite")
# create the first table of the database
dbSendQuery(conn = db,
"CREATE TABLE IF NOT EXISTS artists
(ID INTEGER,
name TEXT,
PRIMARY KEY (ID))")
# create the second table
dbSendQuery(conn = db,
"CREATE TABLE IF NOT EXISTS tracks
(track_ID INTEGER,
title TEXT,
artist INTEGER,
FOREIGN KEY(artist) REFERENCES artists(ID),
PRIMARY KEY (track_ID))")
# filling the artist table with two rows
dbSendQuery(conn = db,
paste0("INSERT INTO artists
VALUES (1,'Tom Chapin')"))
dbSendQuery(conn = db,
paste0("INSERT INTO artists
VALUES (2,'Harry Chapin')"))
# filling the tracks table
dbSendQuery(conn = db,
paste0("INSERT INTO tracks
VALUES (1,'Cats in the Cradle',1)"))
# with the following tracks filling order there must occur an error
### but how to switch on the 'FOREIGN KEY'
dbSendQuery(conn = db,
paste0("INSERT INTO tracks
VALUES (2,'Cats in the Cradle',3)"))
# list the tables of the database
print(dbListTables(db))
# list the columns of a specific table
print(dbListFields(db,"artists")) # of artists
print(dbListFields(db,"tracks")) # of tracks
# show the data ...
print(dbReadTable(db,"artists")) # of artists
print(dbReadTable(db,"tracks")) # of tracks
# close the connection
dbDisconnect(db)
打开连接后需要向数据库发送PRAGMA语句:
dbExecute(conn = db, "PRAGMA foreign_keys=ON")
请注意,文档指出:
Foreign key constraints are disabled by default (for backwards compatibility), so must be enabled separately for each database connection.
因此每次连接到数据库时都必须这样做。
在我的数据库中,我有两个 table 名为艺术家和曲目。在创建曲目 table 时,我将 FOREIGN KEY 设置为艺术家。但是当我 运行 这个显示的代码时,插入一个不存在的艺术家的曲目行是没有问题的。
那绝对不是目的...
在 http://homepages.ecs.vuw.ac.nz/~adam/scie201/lec_R_SQLite.html 上,我发现我必须使用 PRAGMA foreign_keys=ON
之类的东西打开此功能 - 但我不知道我应该如何编写此代码...
这是我的问题:如何正确实现 FOREIGN KEY 功能?
非常感谢您的帮助!
现在我的代码:
# needed packages for this script
# install.packages("sqldf") # install this package if necessary
library(sqldf)
# connection to the database TestDB.sqlite
db=dbConnect(SQLite(), dbname="TestDB.sqlite")
# create the first table of the database
dbSendQuery(conn = db,
"CREATE TABLE IF NOT EXISTS artists
(ID INTEGER,
name TEXT,
PRIMARY KEY (ID))")
# create the second table
dbSendQuery(conn = db,
"CREATE TABLE IF NOT EXISTS tracks
(track_ID INTEGER,
title TEXT,
artist INTEGER,
FOREIGN KEY(artist) REFERENCES artists(ID),
PRIMARY KEY (track_ID))")
# filling the artist table with two rows
dbSendQuery(conn = db,
paste0("INSERT INTO artists
VALUES (1,'Tom Chapin')"))
dbSendQuery(conn = db,
paste0("INSERT INTO artists
VALUES (2,'Harry Chapin')"))
# filling the tracks table
dbSendQuery(conn = db,
paste0("INSERT INTO tracks
VALUES (1,'Cats in the Cradle',1)"))
# with the following tracks filling order there must occur an error
### but how to switch on the 'FOREIGN KEY'
dbSendQuery(conn = db,
paste0("INSERT INTO tracks
VALUES (2,'Cats in the Cradle',3)"))
# list the tables of the database
print(dbListTables(db))
# list the columns of a specific table
print(dbListFields(db,"artists")) # of artists
print(dbListFields(db,"tracks")) # of tracks
# show the data ...
print(dbReadTable(db,"artists")) # of artists
print(dbReadTable(db,"tracks")) # of tracks
# close the connection
dbDisconnect(db)
打开连接后需要向数据库发送PRAGMA语句:
dbExecute(conn = db, "PRAGMA foreign_keys=ON")
请注意,文档指出:
Foreign key constraints are disabled by default (for backwards compatibility), so must be enabled separately for each database connection.
因此每次连接到数据库时都必须这样做。