SQL 如何在 SQLite 中添加默认值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2254504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:22:59  来源:igfitidea点击:

How to add default value in SQLite?

sqldatabasesqlite

提问by Azlam

I had a table modified to add status column to it in this fashion

我修改了一个表,以这种方式向它添加状态列

ALTER TABLE ITEM ADD COLUMN STATUS VARCHAR DEFAULT 'N';

However SQLite doesnt seem to add N to the column for any new ITEM created. Is the syntax wrong or is there any issue with SQLite and its support for defaults.

然而,SQLite 似乎没有为任何新创建的 ITEM 列添加 N。语法错误还是 SQLite 及其对默认值的支持有任何问题。

I am using SQLite 3.6.22

我正在使用 SQLite 3.6.22

回答by databyte

Looks good to me. Here are the Docs.

在我看来很好。 这是文档

sqlite> create table t1 (id INTEGER PRIMARY KEY, name TEXT, created DATE);
sqlite> .table
t1
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE t1 (id INTEGER PRIMARY KEY, name TEXT, created DATE);
COMMIT;

sqlite> alter table t1 add column status varchar default 'N';
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE t1 (id INTEGER PRIMARY KEY, name TEXT, created DATE, status varchar default 'N');
COMMIT;

sqlite> insert into t1 (name) values ("test");
sqlite> select * from t1;
1|test||N

Dump your schema and verifythat your table structure is there after calling ALTER TABLE but before the INSERT. If it's in a transaction, make sure to COMMIT the transactionbefore the insert.

在调用 ALTER TABLE 之后但在 INSERT 之前转储您的架构并验证您的表结构是否存在。如果它在事务中,请确保在插入之前提交事务

$ sqlite3 test.db ".dump"