将NotTime DateTime列添加到SQLite而不使用默认值? [英] Add not null DateTime column to SQLite without default value?

查看:219
本文介绍了将NotTime DateTime列添加到SQLite而不使用默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来我不能添加一个非空约束或删除一个默认约束。我想在表中添加一个datetime列,并将所有值设置为任何值(可能是1970或2000年),但是似乎我没有默认使用not null,我无法删除默认值。我可以添加这个列吗? (再次只是一个简单的datetime不为null)

It looks like i cant add a not null constraint or remove a default constraint. I would like to add a datetime column to a table and have all the values set to anything (perhaps 1970 or year 2000) but it seems like i cant use not null without a default and i cant remove a default once added in. So how can i add this column? (once again just a plain datetime not null)

推荐答案

而不是使用 ALTER TABLE ADD COLUMN ,创建一个具有额外列的新表,并复制旧数据。这将免除您 ALTER TABLE 的限制,并允许您具有不带默认值的 NOT NULL 约束。 / p>

Instead of using ALTER TABLE ADD COLUMN, create a new table that has the extra column, and copy your old data. This will free you from the restrictions of ALTER TABLE and let you have a NOT NULL constraint without a default value.

ALTER TABLE YourTable RENAME TO OldTable;
CREATE TABLE YourTable (/* old cols */, NewColumn DATETIME NOT NULL);
INSERT INTO YourTable SELECT *, '2000-01-01 00:00:00' FROM OldTable;
DROP TABLE OldTable;

这篇关于将NotTime DateTime列添加到SQLite而不使用默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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