<tfoot id='Tozoh'></tfoot>
<legend id='Tozoh'><style id='Tozoh'><dir id='Tozoh'><q id='Tozoh'></q></dir></style></legend>
    <bdo id='Tozoh'></bdo><ul id='Tozoh'></ul>

    <i id='Tozoh'><tr id='Tozoh'><dt id='Tozoh'><q id='Tozoh'><span id='Tozoh'><b id='Tozoh'><form id='Tozoh'><ins id='Tozoh'></ins><ul id='Tozoh'></ul><sub id='Tozoh'></sub></form><legend id='Tozoh'></legend><bdo id='Tozoh'><pre id='Tozoh'><center id='Tozoh'></center></pre></bdo></b><th id='Tozoh'></th></span></q></dt></tr></i><div id='Tozoh'><tfoot id='Tozoh'></tfoot><dl id='Tozoh'><fieldset id='Tozoh'></fieldset></dl></div>
      1. <small id='Tozoh'></small><noframes id='Tozoh'>

        向现有 SQLite 表添加约束

        Add constraint to existing SQLite table(向现有 SQLite 表添加约束)

        <tfoot id='9xmqd'></tfoot>

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

              <bdo id='9xmqd'></bdo><ul id='9xmqd'></ul>

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

                  本文介绍了向现有 SQLite 表添加约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 SQLite,它不支持添加 约束到现有表.

                  I'm using SQLite, which doesn't support adding a constraint to an existing table.

                  所以我不能做这样的事情(仅作为示例):

                  So I can't do something like this (just as an example):

                  ALTER TABLE [Customer]
                  ADD CONSTRAINT specify_either_phone_or_email
                  CHECK (([Phone] IS NOT NULL) OR ([Email] IS NOT NULL));
                  

                  这种情况有什么解决方法吗?

                  我知道:

                  • 我可以为新表添加约束,但它不是新的(它是由我的 ORM、EF Core 生成的)
                  • 我可以进行表重建"(重命名表、创建新表、复制旧数据、删除临时表),但这看起来非常复杂

                  想法

                  • 我能否以某种方式将该表的副本复制到一个新表中,并对架构进行一些更改?
                  • 或者以某种方式获取"架构,然后在 SQL 脚本中对其进行编辑,然后添加具有该架构的表?

                  推荐答案

                  要复制一些架构更改的表,您必须手动创建和复制:

                  To make a copy of a table with some schema changes, you have to do the creation and the copying manually:

                  BEGIN;
                  CREATE TABLE Customer_new (
                      [...],
                      CHECK ([...])
                  );
                  INSERT INTO Customer_new SELECT * FROM Customer;
                  DROP TABLE Customer;
                  ALTER TABLE Customer_new RENAME TO Customer;
                  COMMIT;
                  

                  <小时>

                  要读取架构,请在 sqlite3 中执行 .schema Customer 命令行外壳.这为您提供了 CREATE TABLE 语句,您可以对其进行编辑和执行.


                  To read the schema, execute .schema Customer in the sqlite3 command-line shell. This gives you the CREATE TABLE statement, which you can edit and execute.

                  要就地更改表格,您可以使用后门.

                  To change the table in place, you can use a backdoor.

                  首先,阅读实际的表定义(这与您从 .schema 中得到的相同):

                  First, read the actual table definition (this is the same as what you would get from .schema):

                  SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'Customer';
                  

                  将您的 CHECK 约束添加到该字符串,然后使用 sqlite_master 的写访问">PRAGMA writable_schema=1; 并将您的新表定义写入其中:

                  Add your CHECK constraint to that string, then enable write access to sqlite_master with PRAGMA writable_schema=1; and write your new table definition into it:

                  UPDATE sqlite_master SET sql='...' WHERE type='table' AND name='Customer';
                  

                  然后重新打开数据库.

                  警告:这仅适用于不会更改表的磁盘格式的更改.如果您确实进行了更改记录格式的任何更改(例如添加/删除字段,或修改 rowid,或添加需要内部索引的约束),您的数据库将会爆炸.

                  WARNING: This works only for changes that do not change the on-disk format of the table. If you do make any change that changes the record format (such as adding/removing fields, or modifying the rowid, or adding a constraint that needs an internal index), your database will blow up horribly.

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

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

                  相关文档推荐

                  How do I rename a column in a SQLite database table?(如何重命名 SQLite 数据库表中的列?)
                  PCL vs .NET Standard library for SQLite in UWP(UWP 中 SQLite 的 PCL 与 .NET Standard 库)
                  How to use JsonObject of Pomelo.EntityFramework(如何使用 Pomelo.EntityFramework 的 JsonObject)
                  .NET 5 excludes some libraries from single file publication(.NET 5 从单个文件发布中排除了一些库)
                  how to configure entity framework 6 for sqlite in visual studio 2013(如何在 Visual Studio 2013 中为 sqlite 配置实体框架 6)
                  Will SQLite performance degrade if the database size is greater than 2 gigabytes?(如果数据库大小大于 2 GB,SQLite 性能会下降吗?)

                  1. <i id='MA0or'><tr id='MA0or'><dt id='MA0or'><q id='MA0or'><span id='MA0or'><b id='MA0or'><form id='MA0or'><ins id='MA0or'></ins><ul id='MA0or'></ul><sub id='MA0or'></sub></form><legend id='MA0or'></legend><bdo id='MA0or'><pre id='MA0or'><center id='MA0or'></center></pre></bdo></b><th id='MA0or'></th></span></q></dt></tr></i><div id='MA0or'><tfoot id='MA0or'></tfoot><dl id='MA0or'><fieldset id='MA0or'></fieldset></dl></div>
                      <tbody id='MA0or'></tbody>

                        <bdo id='MA0or'></bdo><ul id='MA0or'></ul>
                          <legend id='MA0or'><style id='MA0or'><dir id='MA0or'><q id='MA0or'></q></dir></style></legend>
                        • <small id='MA0or'></small><noframes id='MA0or'>

                            <tfoot id='MA0or'></tfoot>