• <bdo id='9OHWh'></bdo><ul id='9OHWh'></ul>
    <i id='9OHWh'><tr id='9OHWh'><dt id='9OHWh'><q id='9OHWh'><span id='9OHWh'><b id='9OHWh'><form id='9OHWh'><ins id='9OHWh'></ins><ul id='9OHWh'></ul><sub id='9OHWh'></sub></form><legend id='9OHWh'></legend><bdo id='9OHWh'><pre id='9OHWh'><center id='9OHWh'></center></pre></bdo></b><th id='9OHWh'></th></span></q></dt></tr></i><div id='9OHWh'><tfoot id='9OHWh'></tfoot><dl id='9OHWh'><fieldset id='9OHWh'></fieldset></dl></div>
  • <legend id='9OHWh'><style id='9OHWh'><dir id='9OHWh'><q id='9OHWh'></q></dir></style></legend>

    1. <tfoot id='9OHWh'></tfoot>

      <small id='9OHWh'></small><noframes id='9OHWh'>

      1. 如何向现有 SQLite 表添加外键?

        How do I add a foreign key to an existing SQLite table?(如何向现有 SQLite 表添加外键?)

          <small id='6WBgT'></small><noframes id='6WBgT'>

            • <bdo id='6WBgT'></bdo><ul id='6WBgT'></ul>
                  <i id='6WBgT'><tr id='6WBgT'><dt id='6WBgT'><q id='6WBgT'><span id='6WBgT'><b id='6WBgT'><form id='6WBgT'><ins id='6WBgT'></ins><ul id='6WBgT'></ul><sub id='6WBgT'></sub></form><legend id='6WBgT'></legend><bdo id='6WBgT'><pre id='6WBgT'><center id='6WBgT'></center></pre></bdo></b><th id='6WBgT'></th></span></q></dt></tr></i><div id='6WBgT'><tfoot id='6WBgT'></tfoot><dl id='6WBgT'><fieldset id='6WBgT'></fieldset></dl></div>
                    <tbody id='6WBgT'></tbody>
                  <tfoot id='6WBgT'></tfoot>
                • <legend id='6WBgT'><style id='6WBgT'><dir id='6WBgT'><q id='6WBgT'></q></dir></style></legend>
                  本文介绍了如何向现有 SQLite 表添加外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有下表:

                  CREATE TABLE child( 
                    id INTEGER PRIMARY KEY, 
                    parent_id INTEGER, 
                    description TEXT);
                  

                  如何在 parent_id 上添加外键约束?假设启用了外键.

                  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.

                  推荐答案

                  你不能.

                  尽管向表中添加外键的 SQL-92 语法如下所示:

                  Although the SQL-92 syntax to add a foreign key to your table would be as follows:

                  ALTER TABLE child ADD CONSTRAINT fk_child_parent
                                    FOREIGN KEY (parent_id) 
                                    REFERENCES parent(id);
                  

                  SQLite 不支持 ALTER TABLE 命令的 ADD CONSTRAINT 变体 (sqlite.org:SQLite 未实现的 SQL 功能).

                  SQLite doesn't support the ADD CONSTRAINT variant of the ALTER TABLE command (sqlite.org: SQL Features That SQLite Does Not Implement).

                  因此,在sqlite 3.6.1中添加外键的唯一方法是在CREATE TABLE过程中,如下所示:

                  Therefore, the only way to add a foreign key in sqlite 3.6.1 is during CREATE TABLE as follows:

                  CREATE TABLE child ( 
                      id           INTEGER PRIMARY KEY, 
                      parent_id    INTEGER, 
                      description  TEXT,
                      FOREIGN KEY (parent_id) REFERENCES parent(id)
                  );
                  

                  不幸的是,您必须将现有数据保存到临时表中,删除旧表,使用 FK 约束创建新表,然后从临时表中复制数据.(sqlite.org - 常见问题解答:Q11)

                  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)

                  这篇关于如何向现有 SQLite 表添加外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Building a comma separated list?(建立一个逗号分隔的列表?)
                  Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                  How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                  Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                  Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                  SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)
                • <i id='RKkgl'><tr id='RKkgl'><dt id='RKkgl'><q id='RKkgl'><span id='RKkgl'><b id='RKkgl'><form id='RKkgl'><ins id='RKkgl'></ins><ul id='RKkgl'></ul><sub id='RKkgl'></sub></form><legend id='RKkgl'></legend><bdo id='RKkgl'><pre id='RKkgl'><center id='RKkgl'></center></pre></bdo></b><th id='RKkgl'></th></span></q></dt></tr></i><div id='RKkgl'><tfoot id='RKkgl'></tfoot><dl id='RKkgl'><fieldset id='RKkgl'></fieldset></dl></div>
                    <bdo id='RKkgl'></bdo><ul id='RKkgl'></ul>

                    <small id='RKkgl'></small><noframes id='RKkgl'>

                          • <tfoot id='RKkgl'></tfoot>

                          • <legend id='RKkgl'><style id='RKkgl'><dir id='RKkgl'><q id='RKkgl'></q></dir></style></legend>
                              <tbody id='RKkgl'></tbody>