​​sqlite熟悉笔记​​

2022-12-31 15:48  ​​轩脉刃​​ 

阅读(15)  评论(0)  ​​编辑​​ 

​​收藏​​ 

​​举报​


sqlite在mac中是不需要安装的,只需要命令sqlite3就行了。

所有数据内容都存放在一个文件中,非常方便。

sqlite的一个教程:​​https://www.runoob.com/sqlite/sqlite-tutorial.html​

数据库安装

非常简单

sqlite3 demo.db

这个命令就能在当前目录下创建一个demo.db 的数据库文件

创建表

CREATE TABLE database_name.table_name(
column1 datatype PRIMARY KEY(one or more columns),
column2 datatype,
column3 datatype,
.....
columnN datatype,
);

比如

CREATE TABLE DEPARTMENT(
ID INT PRIMARY KEY NOT NULL,
DEPT CHAR(50) NOT NULL,
EMP_ID INT NOT NULL
);

这篇文章说了sqlite的数据类型:

​https://www.runoob.com/sqlite/sqlite-data-types.html​

这个sqlite有个动态类型(Affinity 亲和类型)比较有意思。

sqlite于大部分传统的SQL数据库不同,大部分传统的SQL数据库采用的书静态数据类型,而SQLite采用的是动态数据类型。即存储的值的数据类型,由值本身决定,而非存储容器决定。

create test table (c1 int);
insert into test values ("abc");
select typeof(c1) from test;
-- 输出:text

SQLite的字段创建中的类型都是Affinity 建议类型,如果传递进入的值不是对应的预设类型,则会根据传递的值转换为对应的建议类型。

我们可以为同一个字段插入不同的类型。这样每一行都有可能是不同的建议类型。

sqlite> create  table test (c1 int);
sqlite> insert into test values (12);
sqlite> insert into test values("abc");
sqlite> insert into test values(1.21);
sqlite> insert into test values(1.212);


sqlite> select typeof(c1) from test;
integer
text
real
real

sqlite> select * from test;
12
abc
1.21
1.212

这篇也可以看看:​​https://www.51cto.com/article/112817.html​

唯一键

我们也可以对一个表设置主键和唯一键

​https://www.runoob.com/sqlite/sqlite-constraints.html​

实时了解作者更多技术文章,技术心得,请关注微信公众号“轩脉刃的刀光剑影”