如何从 sqlite (3.6.21) 表中删除约束?

How do I DROP a constraint from a sqlite (3.6.21) table?(如何从 sqlite (3.6.21) 表中删除约束?)
本文介绍了如何从 sqlite (3.6.21) 表中删除约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表:

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

如何删除约束?

推荐答案

SQLite 不(截至本答案)支持 alter table drop constraint 命令.可以在此处查看允许的语法.您需要创建一个没有约束的新表,传输数据,然后删除旧表.

SQLite does not (as of this answer) support the alter table drop constraint command. The allowed syntax can be seen here. You will need to create a new table without a constraint, transfer the data, then delete the old table.

我认为类似以下内容应该有效:

I think something like the following should work:

CREATE TABLE child2 ( 
    id          INTEGER PRIMARY KEY, 
    parent_id   INTEGER,
    description TEXT
);
INSERT INTO child2 (id, parent_id, description)
   SELECT id, parent_id, description FROM CHILD;
DROP TABLE child;
ALTER TABLE child2 RENAME TO child;

请注意,insert into 可能会简化为 使用显式列名,但我保留了这种方式,以防您也想更改结构.

Note that the insert into could probably be simplified to not use explicit column names but I've left it that way in case you want to change the structure as well.

例如,如果您要删除parent_id 列上的约束,那么将其保留在那里的用途很可疑.在这种情况下,您可以将数据传输修改为:

For example, if you're removing the constraint on the parent_id column, it's of dubious use to keep it there at all. In that case, you could modify the data transfer to:

CREATE TABLE child2 (id INTEGER PRIMARY KEY, description TEXT);
INSERT INTO child2 (id, description) SELECT id, description FROM CHILD;

这篇关于如何从 sqlite (3.6.21) 表中删除约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

FastAPI + Tortoise ORM + FastAPI Users (Python) - Relationship - Many To Many(FastAPI+Tortoise ORM+FastAPI用户(Python)-关系-多对多)
Window functions not working in pd.read_sql; Its shows error(窗口函数在pd.read_sql中不起作用;它显示错误)
(Closed) Leaflet.js: How I can Do Editing Geometry On Specific Object I Select Only?((已关闭)Leaflet.js:如何仅在我选择的特定对象上编辑几何图形?)
in sqlite update trigger with multiple if/Case Conditions(在具有多个IF/CASE条件的SQLite UPDATE触发器中)
Android: Why is Room so slow?(Android:为什么Room这么慢?)
Remote Procedure call failed with sql server 2008 R2(使用 sql server 2008 R2 的远程过程调用失败)