sqlite修改表、表字段等与sql server的不同之处

sqlite中只支持 ALTER TABLE 命令的 RENAME TABLE 和 ADD COLUMN。 其他类型的 ALTER TABLE 操作如 DROP COLUMN,ALTER COLUMN,ADD CONSTRAINT 等等均被忽略。

重命名表名:

  alter table tableName rename to newTableName

添加列

  alter table tableName add columnName columnType

这两个和sql server基本一致,用起来挺方便,但是后面就有点蓝瘦了

删除列

  create table copyTableName (fields)

  insert into copyTableName (fields)  select fields from tableName

  dorp table tableName

  alter table copyTableName rename to tableName

  首先创建个新表,然后将原表数据转移到新表,当然这时候新表的fields只保留了不删除的字段,然后将原表删除,再将新表的名字改回原来的表名

修改列

  实现方法和删除列思路相同,只是fields里面不止要删除原来的列,还要添加新的列名和类型

其他的暂时没有遇到,下面记录一下,中间用到的关于sqlite的一些知识

获取表的创建sql

  select sql from sqlite_master where name='tableName' and type='table'

获取表的所有字段

  PRAGMA table_info('tableName')

判断表是否存在

  select count(*)  from sqlite_master where type='table' and name = 'tableName'

 

嗯,,,中规中矩的一篇记录文

posted @ 2019-11-17 16:31  伊禾棵  阅读(558)  评论(1编辑  收藏  举报