如何从sqlite(3.6.21)表中删除约束? [英] How do I DROP a constraint from a sqlite (3.6.21) table?

查看:689
本文介绍了如何从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约束命令。您需要创建一个没有约束的新表,传输数据,然后删除旧表。



我认为类似以下内容应该可以工作:

  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;

如果不想传输,也可以省略上述所有语句的parent_id。 / p>

I have the following table:

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

How do I drop the constraint?

解决方案

SQLite does not support the alter table drop constraint command. 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;

You could also leave out parent_id from all the statements above if you don't want it transferred.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆