如何向现有 SQLite 表添加外键?
我有下表:
CREATE TABLE child(
id INTEGER PRIMARY KEY,
parent_id INTEGER,
description TEXT);
如何在 parent_id
上添加外键约束?假设启用了外键。
大多数示例假设您正在创建表 - 我想将约束添加到现有表中。
I have the following table:
CREATE TABLE child(
id INTEGER PRIMARY KEY,
parent_id INTEGER,
description TEXT);
How do I add a foreign key constraint on parent_id
? Assume foreign keys are enabled.
Most examples assume you're creating the table - I'd like to add the constraint to an existing one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
你不能。
尽管向表添加外键的 SQL-92 语法如下所示:
SQLite 不支持
ADD CONSTRAINT<
ALTER TABLE
命令的 /code> 变体(sqlite.org:SQLite 的 SQL 功能未实现)。因此,在 sqlite 3.6.1 中添加外键的唯一方法是在
CREATE TABLE
期间,如下所示:不幸的是,您必须将现有数据保存到临时表,删除旧表,创建新表。带有 FK 约束的新表,然后将数据从临时表复制回。 (sqlite.org - 常见问题解答:Q11)
You can't.
Although the SQL-92 syntax to add a foreign key to your table would be as follows:
SQLite doesn't support the
ADD CONSTRAINT
variant of theALTER TABLE
command (sqlite.org: SQL Features That SQLite Does Not Implement).Therefore, the only way to add a foreign key in sqlite 3.6.1 is during
CREATE TABLE
as follows:Unfortunately you will have to save the existing data to a temporary table, drop the old table, create the new table with the FK constraint, then copy the data back in from the temporary table. (sqlite.org - FAQ: Q11)
如果更改表并添加使用约束的列,则可以添加约束。
首先,创建没有parent_id的表:
然后,更改表:
You can add the constraint if you alter table and add the column that uses the constraint.
First, create table without the parent_id:
Then, alter table:
是的,您可以,无需添加新列。您必须小心正确地执行此操作,以避免损坏数据库,因此在尝试此操作之前应该完全备份数据库。
对于您的具体示例:
或者更一般地说:
无论哪种方式,您可能希望在进行任何更改之前首先查看 SQL 定义是什么:
如果您使用 Replace() 方法,您可能会发现在执行之前它很有帮助首先通过运行以下命令来测试你的replace()命令:
Yes, you can, without adding a new column. You have to be careful to do it correctly in order to avoid corrupting the database, so you should completely back up your database before trying this.
for your specific example:
or more generally:
Either way, you'll probably want to first see what the SQL definition is before you make any changes:
If you use the replace() approach, you may find it helpful, before executing, to first test your replace() command by running:
请检查https://www.sqlite.org/lang_altertable.html#otheralter
Please check https://www.sqlite.org/lang_altertable.html#otheralter
正如 @Daniel Vassallo 所说,你做不到。您必须使用的代码如下所示:
给定表:
我假设您要添加以下外键:
因此我将基于该表创建一个临时表,然后我将创建一个新表作为第一个一个,但使用外键,最后我将临时表的数据添加到其中:
As @Daniel Vassallo has said, you can't do it. The code that you have to use is something like this:
Given the table:
I assume that you want to add the following Foreignk Key:
So I would create a temporary table based on that table, then I would create a new table as the first one but with the Foreign Key and finally I would add the data of the temporary table to it:
你可以试试这个:
You can try this:
如果您使用的是 Firefox 插件 sqlite-manager,您可以执行以下操作:
无需删除并再次创建表,只需像这样修改它即可。
在“列”文本框中,右键单击列出的最后一个列名称以显示上下文菜单,然后选择“编辑列”。
请注意,如果表定义中的最后一列是主键,则需要首先添加新列,然后编辑新列的列类型,以便添加外键定义。
在“列类型”框中,
在数据类型后面附加逗号和定义。
单击“更改”按钮,然后单击“危险操作”对话框中的“是”按钮。
参考:
Sqlite 管理器
If you are using the Firefox add-on sqlite-manager you can do the following:
Instead of dropping and creating the table again one can just modify it like this.
In the Columns text box, right click on the last column name listed to bring up the context menu and select Edit Column.
Note that if the last column in the TABLE definition is the PRIMARY KEY then it will be necessary to first add a new column and then edit the column type of the new column in order to add the FOREIGN KEY definition.
Within the Column Type box , append a comma and the
definition after data type.
Click on the Change button and then click the Yes button on the Dangerous Operation dialog box.
Reference:
Sqlite Manager
如果您使用 SQLite 的 Db Browser,那么修改表将很容易。您可以在现有表中添加外键,而无需编写查询。
就是这样。您已成功在现有表中添加外键。
If you use Db Browser for sqlite ,then it will be easy for you to modify the table. you can add foreign key in existing table without writing a query.
that's it. You successfully added foreign key in existing table.
创建现有 SQLLite 表的外键:
对于 SQL LITE 没有直接的方法可以做到这一点。运行以下查询以使用外键重新创建 STUDENTS 表。
创建初始 STUDENTS 表并向该表插入数据后运行查询。
将数据插入 STUDENTS 表。
然后添加外键:将 BADGE_NO 作为同一 STUDENTS 表的外键,
我们也可以添加任何其他表中的外键。
Create a foreign key to the existing SQLLite table:
There is no direct way to do that for SQL LITE. Run the below query to recreate STUDENTS table with foreign keys.
Run the query after creating initial STUDENTS table and inserting data into the table.
Insert data into STUDENTS table.
Then Add FOREIGN KEY : making BADGE_NO as the foreign key of same STUDENTS table
we can add the foreign key from any other table as well.
如果其他人需要有关 SQLiteStudio 的信息,您可以通过它的 GUI 轻松完成。
双击该列,然后双击外键行,然后勾选外键并单击配置。您可以添加参考列,然后在每个窗口中单击“确定”。
最后单击绿色勾号以提交结构中的更改。
请注意,这些步骤会创建删除表并重新创建它的 SQL 脚本!
从数据库备份您的数据。
In case somebody else needs info on SQLiteStudio, you can easily do it form it's GUI.
Double-click on the column and double-click foreign key row, then tick foreign key and click configure. You can add the reference column, then click OK in every window.
Finally click on the green tick to commit changes in the structure.
BE AWARE THAT THESE STEPS CREATE SQL SCRIPTS THAT DELETES THE TABLE AND RECREATES IT!!
Backup your data from the database.
只是为了直观地完成 @Gaurav Ganani 的解释,这对我来说效果很好(我希望它适用于那些使用 DB Browser for SQLite 的人),让我将这张图片粘贴到这里:
Just to visually complete @Gaurav Ganani explanation, which worked fine for me (and i hope it works for those using DB Browser for SQLite), let me paste this image here:
打开数据库,右键单击表,选择修改数据库。
在打开的窗口中,选择您想要创建外键的列,然后滚动到右侧,您将找到一个写有外键的列,然后轻松选择您引用的表以及该列。
Open your database, right click your table, select modify database.
In the window which will open select the column you want to make the foreign key and scroll to your right you will find a column written foreign key and easily select the table yo are referencing to plus the column.
基本上你不能,但你可以绕过这种情况。
将外键约束添加到现有表的正确方法是以下命令。
然后将 parent_Id 数据复制到 newCol 中,然后删除 Parent_Id 列。
因此,不需要临时表。
Basically you cannot but you can bypass the situation.
The correct way to add the foreign key constraint to an existing table is the following command.
Then copy the parent_Id data to the newCol and then delete the Parent_Id column.
Hence, no need for temporary table.
首先在子表
Cid
中添加一列作为int
,然后使用以下代码alter table
。这样,您可以将外键 Cid 添加为父表的主键,并将其用作子表中的外键...希望它会对您有所帮助,因为这对我有好处:First add a column in child table
Cid
asint
thenalter table
with the code below. This way you can add the foreign keyCid
as the primary key of parent table and use it as the foreign key in child table ... hope it will help you as it is good for me: