如何在SQLite中添加默认值? [英] How to add default value in SQLite?

查看:1667
本文介绍了如何在SQLite中添加默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

但是SQLite似乎没有添加N到创建的任何新ITEM的列。



我使用SQLite 3.6.22

解决方案

看起来不错。 这里是文档

  sqlite>创建表t1(id INTEGER PRIMARY KEY,name TEXT,created DATE); 
sqlite> .table
t1
sqlite> .dump
PRAGMA foreign_keys = OFF;
BEGIN交易;
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交易;
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

转储模式并验证表明您的表结构在调用ALTER TABLE之后但在INSERT之前。如果是在事务中,请在插入之前>



$ sqlite3 test < .db.dump


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.

I am using SQLite 3.6.22

解决方案

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 verify that your table structure is there after calling ALTER TABLE but before the INSERT. If it's in a transaction, make sure to COMMIT the transaction before the insert.

$ sqlite3 test.db ".dump"

这篇关于如何在SQLite中添加默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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