SQLite 补操作复制表以及修改表结构 | 学习笔记

简介: 快速学习 SQLite 补操作复制表以及修改表结构

开发者学堂课程【嵌入式之 RFID 开发与应用2020版:SQLite 补操作复制表以及修改表结构】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/665/detail/11227


SQLite 补操作复制表以及修改表结构

内容介绍:

复制表以及修改表结构

补充介绍组合操作,首先查看表的内容:

sqlite> .schema

CREATE TABLEtbl(id interger ,name text, addr text);

CREATE TABLE tbl2(id interger primary key,name text , addr text) ;

sqlite> select*from tbl;

id     name    addr

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

101     zs     bj

102     ls       tj

103     ww     sh

104     zs       cd

104     xw      cq

sqlite> select*from tbl2;

sqlite>

有两张表,tbl 内容如上,tbl2 内容为空。

Tbl2 的 id 类型已经设置成主键,如果要将 tbl 内容复制到 tbl2 中是不可能的,只有重新创建 tbl2,在创建的同时复制 tbl 内容。

第一种情况,复制表:

/删除 tbl2,创建新表 tbl2,内容来源于 tbl

sqlite> drop table tbl2;

sqlite> .schema

CREATE TABLE tbl(id interger,name textJaddr text);

sqlite- create table tbl2 as select *from tbl;

注:as 是指定复制来源

sqlite> .schema

CREATE TABLE tbl(id interger,name text , addr text) ;

CREATE TABLE tbl2(id INT, name TEXT , addr TEXT);

/再次查看,此时 tbl2 和 tbl 内容一致

sqlite> select*from tbl2;

id     name    addr

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

101     zs     bj

102    ls       tj

103     ww   sh

104     zs       cd

104     xw      cq

如果是复制一部分内容:

sqlite> .schema

CREATE TABLE tbl(id interger,name text, addr text) ;

sqlite> create table tbl2 as select *from tbl where id=104;

sqlite> select*from tbl2;

id     name    addr

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

104     zs       cd

104     xw      cq

sqlite>

第二种情况,修改表结构

/创建新的表,指定信息

sqlite>create table ntbl(id interger primary key , name text ,addr text);.

sqlite> .schema

CREATE TABLE tbl(id interger,name text, addr text);

CREATETABLE tbl2(id INT, name TEXT , addr TEXT);

CREATE TABLE ntbl(id interger primary key,name text, addr text) ;

/导入数据

sqlite> insert into ntbl(id ,name , addr) select id ,name , addr from tbl;

Error: UNIQUE constraint failed: ntbl.id

sqlite>

注意:主键要求不能重复,但是 tbl 中有两个 104

如果有重复,可以不复制重复的内容或者将其删除。

sqlite>delete from tbl where addr='cq';

sqlite> insert into ntbl(id, name , addr) select id,name , addr from tbl;

sqlite> select* from ntbl;

id     name    addr

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

101     zs      bj

102     ls       tj

103     ww     sh

104     zs       cd

/修改表名

之前的 tbl 是没有主键的,现在要加上主键要将tbl删除

sqlite> drop table tbl;

sqlite> .schema

CREATETABLE tbl2(id INT,nameTEXT , addr TEXT);

CREATE TABLE ntbl(id interger primary key,name text , addr text);

sqlite>

sqlite> alter table ntbl rename to tbl;

sqlite> .schema

CREATE TABLE tbl2(id INT, name TEXT , addr TEXT);

CREATE TABLE IF NOT EXISTS"tbl"(id interger primary key ,name text,addr text;

sqlite>select * from tbl;

id     name    addr

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

101     zs      bj

102     ls       tj

103     ww    sh

104     zs       cd

此时,tbl 的 id 就带有主键。

插入数据,执行全值插入:

sqlite> insert into tbl values(105,'xw','cq');

sqlite> select * from tbl;

id     name    addr

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

101     zs      bj

102     ls       tj

103     ww     sh

104     zs       cd

105    sw      cq

现在要求系统自动生成主键,如果只输入内容,不输入主键,可以在主键位置输入( null 大小写都可),之后系统会自动生成:

sqlite> irsert into tbl values (NULL, 'xl' , 'hb' );sqlite> select*from tbl;

id     name    addr

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

101     zs      bj

102     ls       tj

103     ww     sh

104     zs       cd

105     sw      cq

xl       hb

理论上输入 null,作为主键的一列会在已有基础上递增的,但不同平台的效果不一样,这个平台不能递增,需要手动修改。

sqlite> update tbl set id=106 where name='xl';

sqlite> select*from tbl;

id     name    addr

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

101     zs      bj

102     ls       tj

103     ww     sh

104     zs       cd

105     sw      cq

106      xl       hb

正常情况下,主键会自动修改。

以上就是本节课需要补充的内容。

笔记:

1、复制一张表:

create table tbl2 as select * from tbl;

2、复制一张表的部分内容:

create table tbl2 as select * from tbl where id=104;

3、修改表的结构:

第一步,创建新表:

create table ntbl(id interger primary key,name text,addr text);第二步,导入数据(如果有主键,要注意数据不要重复):

insert into ntbl(id, name , addr) select id,name , addr from tbl第三步,修改表名:

drop table tbl;

alter table ntbl rename to tbl;

相关文章
|
4天前
|
SQL 存储 数据挖掘
深入了解SQLite3命令:小巧强大的数据库工具
SQLite3是轻量级数据库工具,适用于嵌入式设备和数据分析。它提供交互式shell,无需服务器,易于部署。常用命令如`.schema`显示表结构,`.mode`设置输出格式。示例包括创建数据库`mydatabase.db`,创建表`users`,插入数据并查询。注意动态类型系统、性能限制及SQL注入安全。适合轻量级数据存储和管理。
|
25天前
|
数据库 Android开发 数据安全/隐私保护
在 Android Studio 中结合使用 SQLite 数据库实现简单的注册和登录功能
在 Android Studio 中结合使用 SQLite 数据库实现简单的注册和登录功能
65 2
|
1月前
|
SQL 关系型数据库 MySQL
mysqldiff - Golang 针对 MySQL 数据库表结构的差异 SQL 工具
Golang 针对 MySQL 数据库表结构的差异 SQL 工具。https://github.com/camry/mysqldiff
59 7
|
28天前
|
数据库 数据安全/隐私保护 数据库管理
QT中sqlite数据库数据加密/混淆---MD5/SHA1/SHA2/SHA3
QT中sqlite数据库数据加密/混淆---MD5/SHA1/SHA2/SHA3
|
12天前
|
存储 Java Linux
SQLite3数据库的安装与使用教程
SQLite3数据库的安装与使用教程
|
17天前
|
编译器 API 数据库
技术好文共享:(xxxx)十一:SQLite3的db数据库解密(三)数据库在线备份
技术好文共享:(xxxx)十一:SQLite3的db数据库解密(三)数据库在线备份
19 0
|
1月前
|
SQL 关系型数据库 数据库
17. Python 数据库操作之MySQL和SQLite实例
17. Python 数据库操作之MySQL和SQLite实例
79 2
|
1月前
|
SQL 存储 数据库
48. 【Android教程】数据库:SQLite 的使用
48. 【Android教程】数据库:SQLite 的使用
22 1
|
21天前
|
存储 数据库 Android开发
数据库SQLite3总结
数据库SQLite3总结
|
21天前
|
存储 缓存 数据库
Android之SQLite数据库使用详解
Android之SQLite数据库使用详解