ALTER TABLE(修改表结构命令)
2016-07-22 18:55
369 查看
创建了sqlite表结构后,希望添加表字段,又不能修改原来的代码,所以用alter 修改表结构。
1、添加表字段
alter table 表名 add 字段名 类型(值)
示例:alter table member_table add test1 varchar(40);
2、删除表字段
alter table 表名 drop 字段名
示例: alter table member_table drop test1;
提醒:此方法在sqlite里不起作用,失败!!!
3、字段名更名
alter table 表名 rename 老字段名 to 新字段名
示例:alter table member_table rename test1 to test2;
提醒:sqlite里不支持此方法,失败!!!
4、更改字段类型
alter table 表名 alter 字段 类型;
alter table member_table alter test1 varchar(50);
提醒:sqlite里不支持此方法,失败!!!
查找资料才发现Sqlite不支持对列的改名和修改类型等操作,想要操作官方给出的方法是先备份原表数据到临时表,然后删除原表,再创建新的表结构,然后导入临时表的数据。
官方解释如下:(11) How do I add
or delete columns from an existing table in SQLite.
SQLite
has limited ALTER
TABLE support that you can use to add a column to the end of a table or to change the name of a table. If you want to make more complex changes in the structure of a table, you will have to recreate the table. You can save existing data to a temporary
table, drop the old table, create the new table, then copy the data back in from the temporary table.
For example, suppose you have a table named “t1″ with columns names “a”, “b”, and “c” and that you want to delete column “c” from this table. The following steps illustrate how this could be done:
比如说你要修改的表名是A,方法步骤如下:
1.新建一个临时表T,这个T和表A具有相同的列。
2.把A中所有的数据都通过insert语句插入到T中
3.删除表A
4.新建表A,这时表A的列名就是你想要的结果,以前想修改的列名是什么,这时候就定义成什么,以前要删除的某个列,那么在定义的时候就不定义它。
5.恢复数据,把数据通过insert语句插入A,结构是insert into A select ... from tablen T
6.删除临时表T。
1、添加表字段
alter table 表名 add 字段名 类型(值)
示例:alter table member_table add test1 varchar(40);
2、删除表字段
alter table 表名 drop 字段名
示例: alter table member_table drop test1;
提醒:此方法在sqlite里不起作用,失败!!!
3、字段名更名
alter table 表名 rename 老字段名 to 新字段名
示例:alter table member_table rename test1 to test2;
提醒:sqlite里不支持此方法,失败!!!
4、更改字段类型
alter table 表名 alter 字段 类型;
alter table member_table alter test1 varchar(50);
提醒:sqlite里不支持此方法,失败!!!
查找资料才发现Sqlite不支持对列的改名和修改类型等操作,想要操作官方给出的方法是先备份原表数据到临时表,然后删除原表,再创建新的表结构,然后导入临时表的数据。
官方解释如下:(11) How do I add
or delete columns from an existing table in SQLite.
SQLite
has limited ALTER
TABLE support that you can use to add a column to the end of a table or to change the name of a table. If you want to make more complex changes in the structure of a table, you will have to recreate the table. You can save existing data to a temporary
table, drop the old table, create the new table, then copy the data back in from the temporary table.
For example, suppose you have a table named “t1″ with columns names “a”, “b”, and “c” and that you want to delete column “c” from this table. The following steps illustrate how this could be done:
BEGIN TRANSACTION; CREATE TEMPORARY TABLE t1_backup(a,b); INSERT INTO t1_backup SELECT a,b FROM t1; DROP TABLE t1; CREATE TABLE t1(a,b); INSERT INTO t1 SELECT a,b FROM t1_backup; DROP TABLE t1_backup; COMMIT;
比如说你要修改的表名是A,方法步骤如下:
1.新建一个临时表T,这个T和表A具有相同的列。
2.把A中所有的数据都通过insert语句插入到T中
3.删除表A
4.新建表A,这时表A的列名就是你想要的结果,以前想修改的列名是什么,这时候就定义成什么,以前要删除的某个列,那么在定义的时候就不定义它。
5.恢复数据,把数据通过insert语句插入A,结构是insert into A select ... from tablen T
6.删除临时表T。
相关文章推荐
- php 实现 java com.sun.org.apache.xml.internal.security.utils.Base64 Byte数组加密
- Java 回调简单Demo
- 百练 拦截导弹
- Thinkphp 插入数据丢失
- HTTP协议
- Git的工作区与版本库的关系
- Java基本语法-----java数组(一维数组二维数组)
- Java基本语法-----java数组(一维数组二维数组)
- 卡特兰数
- mysysgit+gitblit搭建简易高效服务器
- git遇到过的错误
- Java 求前n项和
- List Leaves
- 基于MVC4+EasyUI的Web开发框架形成之旅--框架总体界面介绍
- BYTE WORD DWORD
- Codeforces 691 E Xor-sequences 矩阵快速幂
- 用UML建模需要注意的问题--强烈推荐的好文章
- 基于队列的Card Stacking(poj)
- 归并排序
- grunt自动化工具