SQLite-更改表的列类型? [英] SQLite - alter a table's column type?
问题描述
我正在开始一个涉及小型数据库的项目,并且正在考虑使用SQLite. 如果我创建一个表并且将其中一列定义为文本,但是存储的所有值都是整数-是否可以更改列的数据类型?我正在使用SQLite Manager,但找不到允许我执行此操作的函数.我可以使用SQL命令执行此操作吗,还是它是SQLite的限制? 即使有此限制,也可能可以找到一些方法,用所需的列类型创建一个新表,然后将数据导入其中.
I am starting a project involving a small database and I am considering to use SQLite. If I create a table and I defined one of the columns as text, but all the values stored are integers - is there a way to change the data type of a column? I am using SQLite Manager and I can't find a function that allows me to do that. Can I do it using a SQL command, or is it a restriction of SQLite? Probably, even with this restriction, one can find some way around and create a new table with the required column types and then import data into it.
关于, 尼克
推荐答案
SQLite不完全支持ALTER TABLE语句.您唯一可以做的就是重命名表和/或添加列.如果要重命名列,最好的选择是使用新列的数据类型/名称创建一个新表,并删除旧表以重命名新表.
SQLite does not fully support ALTER TABLE statements. The only thing you can do is rename a table and/or add columns. If you want to rename a column, your best option is to create a new table with the new columns datatype/names, and to drop the old table in order to rename the new one.
让我们说,您有一个表,需要将"field-1"重命名为"field-2": 首先== >>重命名旧表:
Lets say, you have a table and need to rename "field-1" to "field-2": First ==>> rename the old table:
ALTER TABLE original RENAME TO tmp;
现在基于旧表创建新表,但使用更新的列名:
== >>用更新的列创建一个表
Now create the new table based on the old table but with the updated column name:
==>> create a table with the updated columns
CREATE TABLE original(
field_a INT
, field_b INT
);
然后从原始表中复制内容.
Then copy the contents across from the original table.
INSERT INTO origignal(field_a, field_b)
SELECT field_a, field_b
FROM tmp;
最后,放下旧桌子.
DROP TABLE tmp;
这篇关于SQLite-更改表的列类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!