问题描述
我有一个包含 name
、qty
、rate
列的表格.现在我需要在 name
和 qty
列之间添加一个新列 COLNew
.如何在两列之间添加新列?
I have a table with columns name
, qty
, rate
. Now I need to add a new column COLNew
in between the name
and qty
columns. How do I add a new column in between two columns?
推荐答案
您有两个选择.首先,您可以简单地添加一个具有以下内容的新列:
You have two options.First, you could simply add a new column with the following:
ALTER TABLE {tableName} ADD COLUMN COLNew {type};
第二,更复杂,但实际上将列放在您想要的位置,将重命名表:
Second, and more complicatedly, but would actually put the column where you want it, would be to rename the table:
ALTER TABLE {tableName} RENAME TO TempOldTable;
然后用缺失的列创建新表:
Then create the new table with the missing column:
CREATE TABLE {tableName} (name TEXT, COLNew {type} DEFAULT {defaultValue}, qty INTEGER, rate REAL);
并用旧数据填充它:
INSERT INTO {tableName} (name, qty, rate) SELECT name, qty, rate FROM TempOldTable;
然后删除旧表:
DROP TABLE TempOldTable;
我更喜欢第二个选项,因为它允许您在需要时完全重命名所有内容.
I'd much prefer the second option, as it will allow you to completely rename everything if need be.
这篇关于在sqlite的表中插入新列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!