我需要重命名SQLite数据库中一些表中的一些列。 我知道以前有人在stackoverflow上问过类似的问题,但这是针对一般的SQL,没有提到SQLite的情况。
从ALTER TABLE的SQLite文档中,我收集到不可能“轻松”地做这样的事情(即一个ALTER TABLE语句)。
我想知道有人知道一个通用的SQL方法做这样的事情与SQLite。
我需要重命名SQLite数据库中一些表中的一些列。 我知道以前有人在stackoverflow上问过类似的问题,但这是针对一般的SQL,没有提到SQLite的情况。
从ALTER TABLE的SQLite文档中,我收集到不可能“轻松”地做这样的事情(即一个ALTER TABLE语句)。
我想知道有人知道一个通用的SQL方法做这样的事情与SQLite。
当前回答
来自官方文件
对于某些不以任何方式影响磁盘上内容的更改,可以选择使用更简单、更快的过程。以下更简单的过程适用于删除CHECK或FOREIGN KEY或NOT NULL约束,重命名列,或添加、删除或更改列上的默认值。
Start a transaction. Run PRAGMA schema_version to determine the current schema version number. This number will be needed for step 6 below. Activate schema editing using PRAGMA writable_schema=ON. Run an UPDATE statement to change the definition of table X in the sqlite_master table: UPDATE sqlite_master SET sql=... WHERE type='table' AND name='X'; Caution: Making a change to the sqlite_master table like this will render the database corrupt and unreadable if the change contains a syntax error. It is suggested that careful testing of the UPDATE statement be done on a separate blank database prior to using it on a database containing important data. If the change to table X also affects other tables or indexes or triggers are views within schema, then run UPDATE statements to modify those other tables indexes and views too. For example, if the name of a column changes, all FOREIGN KEY constraints, triggers, indexes, and views that refer to that column must be modified. Caution: Once again, making changes to the sqlite_master table like this will render the database corrupt and unreadable if the change contains an error. Carefully test of this entire procedure on a separate test database prior to using it on a database containing important data and/or make backup copies of important databases prior to running this procedure. Increment the schema version number using PRAGMA schema_version=X where X is one more than the old schema version number found in step 2 above. Disable schema editing using PRAGMA writable_schema=OFF. (Optional) Run PRAGMA integrity_check to verify that the schema changes did not damage the database. Commit the transaction started on step 1 above.
其他回答
一种选择是,如果你需要在紧要时刻完成它,如果你的初始列是用默认创建的,那就创建你想要的新列,将内容复制到它,然后基本上“放弃”旧列(它仍然存在,但你只是不使用/更新它,等等)。
ex:
alter table TABLE_NAME ADD COLUMN new_column_name TYPE NOT NULL DEFAULT '';
update TABLE_NAME set new_column_name = old_column_name;
update TABLE_NAME set old_column_name = ''; -- abandon old column, basically
这将留下一个列(如果它是用NOT NULL创建的,但没有默认值,那么未来的插入可能会忽略它),但如果它只是一个丢弃的表,那么折衷可能是可以接受的。否则,请使用这里提到的其他答案之一,或者使用允许重命名列的不同数据库。
引用sqlite文档:
SQLite支持的有限子集 ALTER TABLE。ALTER TABLE命令 在SQLite中允许用户重命名 表或添加新列到 现有的表。不能重命名列、删除列或从表中添加或删除约束。
当然,您可以用新的布局创建一个新表,SELECT * FROM old_table,并用您将收到的值填充新表。
在挖掘中,我发现了这个多平台(Linux | Mac | Windows)图形化工具,称为DB Browser for SQLite,它实际上允许用户以一种非常用户友好的方式重命名列!
Edit | Modify Table |选中“Table | Edit Field”。点击点击!瞧!
然而,如果有人想分享一种程序化的方式来做这件事,我很乐意知道!
请注意,从2018年9月发布的3.25.0版本开始,您现在可以使用ALTER TABLE重命名列。
原来“创建新表和删除旧表”的答案如下。
假设你有一个表,需要将“colb”重命名为“col_b”:
首先创建一个临时名称的新表,该名称基于旧表定义,但使用了更新的列名:
CREATE TABLE tmp_table_name (
col_a INT
, col_b INT
);
然后从原始表复制内容。
INSERT INTO tmp_table_name(col_a, col_b)
SELECT col_a, colb
FROM orig_table_name;
放下旧桌子。
DROP TABLE orig_table_name;
最后将临时表重命名为原来的表:
ALTER TABLE tmp_table_name RENAME TO orig_table_name;
不要忘记重新创建索引、触发器等。文档更全面地描述了这些陷阱和注意事项。
在BEGIN事务中封装所有这些;和提交;也可能是个好主意。
首先,这是那些让我吃惊的事情之一:重命名一个列需要创建一个全新的表,并将数据从旧表复制到新表……
我用来执行SQLite操作的GUI是Base。它有一个漂亮的日志窗口,显示所有已执行的命令。通过Base重命名列会用必要的命令填充日志窗口:
然后可以很容易地复制和粘贴到您可能需要的地方。对我来说,这是一个ActiveAndroid迁移文件。还有一个不错的地方,复制的数据只包括SQLite命令,不包括时间戳等。
希望这能节省一些人的时间。