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'

 

嗯,,,中規中矩的一篇記錄文

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章