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

前端之家收集整理的这篇文章主要介绍了sqlite--创建表,销毁表,修改表结构前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

@H_502_2@
------------------------------------------------------------- @H_502_2@
注意:(来自网络) @H_502_2@
今天在做数据库升级时,碰到要对原来数据库中一张表的一个字段名进行修改,但是用: @H_502_2@
alter table tablename rename column oldColumnName to newColumnName; @H_502_2@
始终不成功,后面查阅相关信息: @H_502_2@
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. @H_502_2@
sqlite支持一个更改表内容的有限子集,就是说在sqlite更改表的命令中,只允许用户重命名表名或者增加多一个列到一个的表中。而重命名一个字段名和删除一个字段、或者增加删除系统规定的参数这些操作是不可能的。 @H_502_2@

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

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

猜你在找的Sqlite相关文章