sqlite--创建表,销毁表,修改表结构

来源:互联网 发布:websocket服务器 java 编辑:程序博客网 时间:2024/05/01 05:43

创建表:
creat table <table_name>(field type,field type..);
销毁表:
drop <table_name>;
修改表结构:
(添加列,专业一点说就是,往表中添加新字段)
alter table <table_name> add column <field>;

-------------------------------------------------------------
注意:(来自网络)
今天在做数据库升级时,碰到要对原来数据库中一张表的一个字段名进行修改,但是用:
alter table tablename rename column oldColumnName to newColumnName;
始终不成功,后面查阅相关信息:
SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table. It is not possible to rename a column, remove a column, or add or remove constraints from a table.
sqlite支持一个更改表内容的有限子集,就是说在sqlite更改表的命令中,只允许用户重命名表名或者增加多一个列到一个的表中。而重命名一个字段名和删除一个字段、或者增加和删除系统规定的参数这些操作是不可能的。

解决办法:
例如:在上面的操作过程中,我们在people表中新添加了一个字段addr,要删除这个字段,直接用sqlite的语句时无法完成的。
我们可以这样干:
A.将people表重命名为temp;
B.重新创建people表;
C.将temp表中的相应字段内容复制到people表中。
D.删除temp表

操作如下:
A.alter table people rename to temp;
B.create table people(id,name,age);
C.insert  into  people  select  id,name,age  from temp;
0 0