python alter table_SQLite3的ALTER TABLE语句

SQLite3数据库只支持有线的alter table语句,即只能做rename和add column。

rename table

sqlite> .table

jsond pnum ssd timed

sqlite> .mode column

sqlite> select * from pnum;

pid pnum tid

--- ------------ ---

1 GM2020090501 1

2 GM2020090401 2

3 GM2020090601 3

4 GM2020090801 7

5 GM2020090901 8

6 GM2020091101 9

7 GM2020091001 10

8 GM2020090701 11

9 GM2020091301 12

10 GM2020091701 13

11 GM2020091601 14

12 GM2020091901 15

13 GM2020091201 16

14 GM2020091401 18

15 GM2020091801 19

16 GM2020092001 20

17 GM2020091501 21

sqlite> alter table pnum rename to abcd;

sqlite> .table

abcd jsond ssd timed

sqlite> select * from abcd;

pid pnum tid

--- ------------ ---

1 GM2020090501 1

2 GM2020090401 2

3 GM2020090601 3

4 GM2020090801 7

5 GM2020090901 8

6 GM2020091101 9

7 GM2020091001 10

8 GM2020090701 11

9 GM2020091301 12

10 GM2020091701 13

11 GM2020091601 14

12 GM2020091901 15

13 GM2020091201 16

14 GM2020091401 18

15 GM2020091801 19

16 GM2020092001 20

17 GM2020091501 21

以上测试,将表pnum修改为abcd,里面的数据保持不变。

add column

sqlite> .table

abcd jsond ssd timed

sqlite> select * from abcd;

pid pnum tid

--- ------------ ---

1 GM2020090501 1

2 GM2020090401 2

3 GM2020090601 3

4 GM2020090801 7

5 GM2020090901 8

6 GM2020091101 9

7 GM2020091001 10

8 GM2020090701 11

9 GM2020091301 12

10 GM2020091701 13

11 GM2020091601 14

12 GM2020091901 15

13 GM2020091201 16

14 GM2020091401 18

15 GM2020091801 19

16 GM2020092001 20

17 GM2020091501 21

sqlite> alter table abcd add a7 text;

sqlite> select * from abcd;

pid pnum tid a7

--- ------------ --- --

1 GM2020090501 1

2 GM2020090401 2

3 GM2020090601 3

4 GM2020090801 7

5 GM2020090901 8

6 GM2020091101 9

7 GM2020091001 10

8 GM2020090701 11

9 GM2020091301 12

10 GM2020091701 13

11 GM2020091601 14

12 GM2020091901 15

13 GM2020091201 16

14 GM2020091401 18

15 GM2020091801 19

16 GM2020092001 20

17 GM2020091501 21

sqlite> alter table abcd add a text unique;

Error: Cannot add a UNIQUE column

以上测试,给表abcd增加a7列,增加后,这一列默认全部为null。增加a8列的时候出错,SQLite3不允许直接增加有unique属性的列。

-- EOF --

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值