假设我创建了一个这样的表:
CREATE TABLE table_1(column_1 UNIQUE, column_2 NOT NULL);
Run Code Online (Sandbox Code Playgroud)
在我插入大量数据之后,出于某种原因,我需要从第一列中删除UNIQUE约束,即column_1.可能吗?如果有,怎么样?
ALTER TABLE在sqlite中是非常有限的.
但是,您可以在有限的范围内使用CREATE INDEX和更改某些约束CREATE TRIGGER.
你可以,例如:
CREATE TABLE table_1(column_1, column_2);
-- No constrains in table_1
CREATE UNIQUE INDEX t1_c1_1 ON table_1 (column_1);
-- From now, column_1 must be UNIQUE
CREATE TRIGGER t1_c2_1i BEFORE INSERT ON table_1 WHEN column_2 IS NULL BEGIN
SELECT RAISE(ABORT, 'column_2 can not be NULL');
END;
CREATE TRIGGER t1_c2_1u BEFORE UPDATE ON table_1 WHEN column_2 IS NULL BEGIN
SELECT RAISE(ABORT, 'column_2 can not be NULL');
END;
-- From now, NULL column_2 update/insert will fail
DROP TRIGGER t1_c1_1;
-- Now column_1 doesn't need to be UNIQUE
DROP TRIGGER t1_c2_1i;
DROP TRIGGER t1_c2_1u;
-- Now column_2 may be NULL
Run Code Online (Sandbox Code Playgroud)
注意:
另一种解决方法是复制现有的表删除约束:
CREATE TEMP TABLE table_1 AS SELECT * FROM MAIN.table_1;
DROP TABLE table_1;
CREATE TABLE table_1 AS SELECT * FROM TEMP.table1;
-- table_1 is now a copy from initial table_1, but without constraints
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1179 次 |
| 最近记录: |