mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
commit
ba4fe1c601
@ -1,272 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (amwps290)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (A hands-on tutorial of SQLite3)
|
||||
[#]: via: (https://opensource.com/article/21/2/sqlite3-cheat-sheet)
|
||||
[#]: author: (Klaatu https://opensource.com/users/klaatu)
|
||||
|
||||
A hands-on tutorial of SQLite3
|
||||
======
|
||||
Get started with this incredibly powerful and common database. Download
|
||||
the SQLite cheat sheet.
|
||||
![Cheat Sheet cover image][1]
|
||||
|
||||
Applications very often save data. Whether your users create simple text documents, complex graphic layouts, game progress, or an intricate list of customers and order numbers, software usually implies that data is being generated. There are many ways to store data for repeated use. You can dump text to configuration formats such as INI, [YAML][2], XML, or JSON, you can write out raw binary data, or you can store data in a structured database. SQLite is a self-contained, lightweight database that makes it easy to create, parse, query, modify, and transport data.
|
||||
|
||||
**[Download our [SQLite3 cheat sheet][3]]**
|
||||
|
||||
SQLite has been dedicated to the [public domain][4], which [technically means it is not copyrighted and therefore requires no license][5]. Should you require a license, you can [purchase a Warranty of Title][6]. SQLite is immensely common, with an estimated 1 _trillion_ SQLite databases in active use. That's counting multiple databases on every Android and iOS device, every macOS and Windows 10 computer, most Linux systems, within every Webkit-based web browser, modern TV sets, automotive multimedia systems, and countless other software applications.
|
||||
|
||||
In summary, it's a reliable and simple system to use for storing and organizing data.
|
||||
|
||||
### Installing
|
||||
|
||||
You probably already have SQLite libraries on your system, but you need its command-line tools installed to use it directly. On Linux, you probably already have these tools installed. The command provided by the tools is **sqlite3** (not just **sqlite**).
|
||||
|
||||
If you don't have SQLite installed on Linux or BSD, you can install it from your software repository or ports tree, or [download and install it][7] from source code or as a compiled binary.
|
||||
|
||||
On macOS or Windows, you can download and install SQLite tools from [sqlite.org][7].
|
||||
|
||||
### Using SQLite
|
||||
|
||||
It's common to interact with a database through a programming language. For this reason, there are SQLite interfaces (or "bindings") for Java, Python, Lua, PHP, Ruby, C++, and many many others. However, before using these libraries, it helps to understand what's actually happening with the database engine and why your choice of a database is significant. This article introduces you to SQLite and the **sqlite3** command so you can get familiar with the basics of how this database handles data.
|
||||
|
||||
### Interacting with SQLite
|
||||
|
||||
You can interact with SQLite using the **sqlite3** command. This command provides an interactive shell so you can view and update your databases.
|
||||
|
||||
|
||||
```
|
||||
$ sqlite3
|
||||
SQLite version 3.34.0 2020-12-01 16:14:00
|
||||
Enter ".help" for usage hints.
|
||||
Connected to a transient in-memory database.
|
||||
Use ".open FILENAME" to reopen on a persistent database.
|
||||
sqlite>
|
||||
```
|
||||
|
||||
The command places you in an SQLite subshell, and so your prompt is now an SQLite prompt. Your usual Bash commands don't work here. You must use SQLite commands. To see a list of SQLite commands, type **.help**:
|
||||
|
||||
|
||||
```
|
||||
sqlite> .help
|
||||
.archive ... Manage SQL archives
|
||||
.auth ON|OFF SHOW authorizer callbacks
|
||||
.backup ?DB? FILE Backup DB (DEFAULT "main") TO FILE
|
||||
.bail ON|off Stop after hitting an error. DEFAULT OFF
|
||||
.binary ON|off Turn BINARY output ON OR off. DEFAULT OFF
|
||||
.cd DIRECTORY CHANGE the working directory TO DIRECTORY
|
||||
[...]
|
||||
```
|
||||
|
||||
Some of these commands are binary, while others require unique arguments (like filenames, paths, etc.). These are administrative commands for your SQLite shell and are not database queries. Databases take queries in Structured Query Language (SQL), and many SQLite queries are the same as what you may already know from the [MySQL][8] and [MariaDB][9] databases. However, data types and functions differ, so pay close attention to minor differences if you're familiar with another database.
|
||||
|
||||
### Creating a database
|
||||
|
||||
When launching SQLite, you can either open a prompt in memory, or you can select a database to open:
|
||||
|
||||
|
||||
```
|
||||
`$ sqlite3 mydatabase.db`
|
||||
```
|
||||
|
||||
If you have no database yet, you can create one at the SQLite prompt:
|
||||
|
||||
|
||||
```
|
||||
`sqlite> .open mydatabase.db`
|
||||
```
|
||||
|
||||
You now have an empty file on your hard drive, ready to be used as an SQLite database. The file extension **.db** is arbitrary. You can also use **.sqlite**, or whatever you want.
|
||||
|
||||
### Creating a table
|
||||
|
||||
Databases contain _tables_, which can be visualized as a spreadsheet. There's a series of rows (called _records_ in a database) and columns. The intersection of a row and a column is called a _field_.
|
||||
|
||||
The Structured Query Language (SQL) is named after what it provides: A method to inquire about the contents of a database in a predictable and consistent syntax to receive useful results. SQL reads a lot like an ordinary English sentence, if a little robotic. Currently, your database is empty, devoid of any tables.
|
||||
|
||||
You can create a table with the **CREATE** query. It's useful to combine this with the **IF NOT EXISTS** statement, which prevents SQLite from clobbering an existing table.
|
||||
|
||||
You can't create an empty table in SQLite, so before trying a **CREATE** statement, you must think about what kind of data you anticipate the table will store. In this example, I'll create a table called _member_ with these columns:
|
||||
|
||||
* A unique identifier
|
||||
* A person's name
|
||||
* The date and time of data entry
|
||||
|
||||
|
||||
|
||||
#### Unique ID
|
||||
|
||||
It's always good to refer to a record by a unique number, and luckily SQLite recognizes this and does it automatically for you in a column called **rowid**.
|
||||
|
||||
No SQL statement is required to create this field.
|
||||
|
||||
#### Data types
|
||||
|
||||
For my example table, I'm creating a _name_ column to hold **TEXT** data. To prevent a record from being created without data in a specified field, you can add the **NOT NULL** directive.
|
||||
|
||||
The SQL to create this field is: **name TEXT NOT NULL**
|
||||
|
||||
There are five data types (actually _storage classes_) in SQLite:
|
||||
|
||||
* TEXT: a text string
|
||||
* INTEGER: a whole number
|
||||
* REAL: a floating point (unlimited decimal places) number
|
||||
* BLOB: binary data (for instance, a .jpeg or .webp image)
|
||||
* NULL: a null value
|
||||
|
||||
|
||||
|
||||
#### Date and time stamp
|
||||
|
||||
SQLite includes a convenient date and timestamp function. It is not a data type itself but a function in SQLite that generates either a string or integer, depending on your desired format. In this example, I left it as the default.
|
||||
|
||||
The SQL to create this field is: **datestamp DATETIME DEFAULT CURRENT_TIMESTAMP**
|
||||
|
||||
### Table creation SQL
|
||||
|
||||
The full SQL for creating this example table in SQLite:
|
||||
|
||||
|
||||
```
|
||||
sqlite> CREATE TABLE
|
||||
...> IF NOT EXISTS
|
||||
...> member (name TEXT NOT NULL,
|
||||
...> datestamp DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
```
|
||||
|
||||
In this code sample, I pressed **Return** after the logical clauses of the statement to make it easier to read. SQLite won't run your command unless it terminates with a semi-colon (**;**).
|
||||
|
||||
You can verify that the table has been created with the SQLite command **.tables**:
|
||||
|
||||
|
||||
```
|
||||
sqlite> .tables
|
||||
member
|
||||
```
|
||||
|
||||
### View all columns in a table
|
||||
|
||||
You can verify what columns and rows a table contains with the **PRAGMA** statement:
|
||||
|
||||
|
||||
```
|
||||
sqlite> PRAGMA table_info(member);
|
||||
0|name|TEXT|1||0
|
||||
1|datestamp|CURRENT_TIMESTAMP|0||0
|
||||
```
|
||||
|
||||
### Data entry
|
||||
|
||||
You can populate your new table with some sample data by using the **INSERT** SQL keyword:
|
||||
|
||||
|
||||
```
|
||||
> INSERT INTO member (name) VALUES ('Alice');
|
||||
> INSERT INTO member (name) VALUES ('Bob');
|
||||
> INSERT INTO member (name) VALUES ('Carol');
|
||||
> INSERT INTO member (name) VALUES ('David');
|
||||
```
|
||||
|
||||
Verify the data in the table:
|
||||
|
||||
|
||||
```
|
||||
> SELECT * FROM member;
|
||||
Alice|2020-12-15 22:39:00
|
||||
Bob|2020-12-15 22:39:02
|
||||
Carol|2020-12-15 22:39:05
|
||||
David|2020-12-15 22:39:07
|
||||
```
|
||||
|
||||
#### Adding multiple rows at once
|
||||
|
||||
Now create a second table:
|
||||
|
||||
|
||||
```
|
||||
> CREATE TABLE IF NOT EXISTS linux (
|
||||
...> distro TEXT NOT NULL)
|
||||
```
|
||||
|
||||
Populate it with some sample data, this time using a little **VALUES** shortcut so you can add multiple rows in just one command. The **VALUES** keyword expects a list in parentheses but can take multiple lists separated by commas:
|
||||
|
||||
|
||||
```
|
||||
> INSERT INTO linux (distro)
|
||||
...> VALUES ('Slackware'), ('RHEL'),
|
||||
...> ('Fedora'),('Debian');
|
||||
```
|
||||
|
||||
### Altering a table
|
||||
|
||||
You now have two tables, but as yet, there's no relationship between the two. They each contain independent data, but possibly you might need to associate a member of the first table to a specific item listed in the second.
|
||||
|
||||
To do that, you can create a new column for the first table that corresponds to something in the second. Because both tables were designed with unique identifiers (automatically, thanks to SQLite), the easiest way to connect them is to use the **rowid** field of one as a selector for the other.
|
||||
|
||||
Create a new column in the first table to represent a value in the second table:
|
||||
|
||||
|
||||
```
|
||||
`> ALTER TABLE member ADD os INT;`
|
||||
```
|
||||
|
||||
Using the unique IDs of the **linux** table, assign a distribution to each member. Because the records already exist, you use the **UPDATE** SQL keyword rather than **INSERT**. Specifically, you want to select one row and then update the value of one column. Syntactically, this is expressed a little in reverse, with the update happening first and the selection matching last:
|
||||
|
||||
|
||||
```
|
||||
`> UPDATE member SET os=1 WHERE name='Alice';`
|
||||
```
|
||||
|
||||
Repeat this process for the other names in the **member** table, just to populate it with data. For variety, assign three different distributions across the four rows (doubling up on one).
|
||||
|
||||
### Joining tables
|
||||
|
||||
Now that these two tables relate to one another, you can use SQL to display the associated data. There are many kinds of _joins_ in databases, but you can try them all once you know the basics. Here's a basic join to correlate the values found in the **os** field of the **member** table to the **id** field of the **linux** table:
|
||||
|
||||
|
||||
```
|
||||
> SELECT * FROM member INNER JOIN linux ON member.os=linux.rowid;
|
||||
Alice|2020-12-15 22:39:00|1|Slackware
|
||||
Bob|2020-12-15 22:39:02|3|Fedora
|
||||
Carol|2020-12-15 22:39:05|3|Fedora
|
||||
David|2020-12-15 22:39:07|4|Debian
|
||||
```
|
||||
|
||||
The **os** and **id** fields form the join.
|
||||
|
||||
In a graphical application, you can imagine that the **os** field might be set by a drop-down menu, the values for which are drawn from the contents of the **distro** field of the **linux** table. By using separate tables for unique but related sets of data, you ensure the consistency and validity of data, and thanks to SQL, you can associate them dynamically later.
|
||||
|
||||
### Learning more
|
||||
|
||||
SQLite is an infinitely useful self-contained, portable, open source database. Learning to use it interactively is a great first step toward managing it for web applications or using it through programming language libraries.
|
||||
|
||||
If you enjoy SQLite, you might also try [Fossil][10] by the same author, Dr. Richard Hipp.
|
||||
|
||||
As you learn and use SQLite, it may help to have a list of common commands nearby, so download our **[SQLite3 cheat sheet][3]** today!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/21/2/sqlite3-cheat-sheet
|
||||
|
||||
作者:[Klaatu][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/klaatu
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/coverimage_cheat_sheet.png?itok=lYkNKieP (Cheat Sheet cover image)
|
||||
[2]: https://www.redhat.com/sysadmin/yaml-beginners
|
||||
[3]: https://opensource.com/downloads/sqlite-cheat-sheet
|
||||
[4]: https://sqlite.org/copyright.html
|
||||
[5]: https://directory.fsf.org/wiki/License:PublicDomain
|
||||
[6]: https://www.sqlite.org/purchase/license?
|
||||
[7]: https://www.sqlite.org/download.html
|
||||
[8]: https://www.mysql.com/
|
||||
[9]: https://mariadb.org/
|
||||
[10]: https://opensource.com/article/20/11/fossil
|
282
translated/tech/20210204 A hands-on tutorial of SQLite3.md
Normal file
282
translated/tech/20210204 A hands-on tutorial of SQLite3.md
Normal file
@ -0,0 +1,282 @@
|
||||
[#]: collector: "lujun9972"
|
||||
[#]: translator: " "
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
[#]: subject: "A hands-on tutorial of SQLite3"
|
||||
[#]: via: "https://opensource.com/article/21/2/sqlite3-cheat-sheet"
|
||||
[#]: author: "Klaatu https://opensource.com/users/klaatu"
|
||||
|
||||
|
||||
SQLite3 实践教程
|
||||
======
|
||||
|
||||
|
||||
|
||||
开始使用这个功能强大且通用的数据库吧。你可以先下载 SQLite 备忘单。
|
||||
|
||||
![Cheat Sheet cover image][1]
|
||||
|
||||
应用程序经常需要保存数据。 无论您的用户是创建简单的文本文档,复杂的图形布局,游戏进度还是错综复杂的客户和订单号列表,通常都暗示着正在生成数据。 有很多方法可以存储数据以供重复使用。 您可以将文本转储为 INI,[YAML][2],XML 或 JSON 等格式,可以输出原始的二进制数据,也可以将数据存储在结构化数据库中。 SQLite 是一个自包含的轻量级数据库,可轻松创建,解析,查询,修改和传输数据。
|
||||
|
||||
**下载 [SQLite3 备忘录][3]**
|
||||
|
||||
SQLite 一直致力于[公共领域][4],[从技术上讲,它没有版权,因此不需要许可证][5]。 如果您需要许可证,则可以[购买所有权担保][6]。 SQLite 非常常见,大约有 1 万亿个 SQLite 数据库正在使用中。 在每个基于 Webkit 的 Web 浏览器,现代电视机,汽车多媒体系统以及无数其他软件应用程序中, Android 和 iOS 设备, macOS 和 Windows 10 计算机,大多数 Linux 系统上都包含多个数据库。
|
||||
|
||||
|
||||
总而言之,它是用于存储和组织数据的一个可靠而简单的系统。
|
||||
|
||||
|
||||
**安装**
|
||||
|
||||
您的系统上可能已经有 SQLite 库,但是您需要安装其命令行工具才能直接使用它。 在 Linux上,您可能已经安装了这些工具。 工具提供的命令是 **sqlite3** (而不仅仅是 **sqlite**)。
|
||||
|
||||
如果没有在您的 Linux 或 BSD 上安装 SQLite,您则可以从软件仓库中或 ports tree 中安装 SQLite,也可以从源代码或已编译的二进制文件进行[下载并安装][7]。
|
||||
|
||||
在 macOS 或 Windows 上,您可以从 [sqlite.org][7] 下载并安装 SQLite 工具。
|
||||
|
||||
### 使用 SQLite
|
||||
|
||||
通过编程语言与数据库进行交互是很常见的。 因此,像 Java,Python,Lua,PHP,Ruby,C ++ 以及其他编程语言都提供了 SQLite 的接口(或“绑定”)。 但是,在使用这些库之前,使用基本的命令有助于了解数据库引擎实际发生的情况以及选择数据库的重要性。 本文向您介绍 SQLite 和 **sqlite3** 命令,以便您熟悉该数据库如何处理数据的基础知识。
|
||||
|
||||
### 与 SQLite 交互
|
||||
|
||||
您可以使用 **sqlite3** 命令与 SQLite 进行交互。 该命令提供了一个交互式的 shell 程序,以便您可以查看和更新数据库。
|
||||
|
||||
|
||||
```
|
||||
$ sqlite3
|
||||
SQLite version 3.34.0 2020-12-01 16:14:00
|
||||
Enter ".help" for usage hints.
|
||||
Connected to a transient in-memory database.
|
||||
Use ".open FILENAME" to reopen on a persistent database.
|
||||
sqlite>
|
||||
```
|
||||
|
||||
该命令将您使您处于 SQLite 的子 shell 中,因此现在的提示是 SQLite 的提示。 您以前使用的 Bash 命令在这里将不再适用。 您必须使用 SQLite 命令。 要查看 SQLite 命令列表,请输入 **. help **:
|
||||
|
||||
|
||||
```
|
||||
sqlite> .help
|
||||
.archive ... Manage SQL archives
|
||||
.auth ON|OFF SHOW authorizer callbacks
|
||||
.backup ?DB? FILE Backup DB (DEFAULT "main") TO FILE
|
||||
.bail ON|off Stop after hitting an error. DEFAULT OFF
|
||||
.binary ON|off Turn BINARY output ON OR off. DEFAULT OFF
|
||||
.cd DIRECTORY CHANGE the working directory TO DIRECTORY
|
||||
[...]
|
||||
```
|
||||
|
||||
这些命令中的一些是二进制的,而其他一些则需要唯一的参数(如文件名,路径等)。 这些是 SQLite Shell 的管理命令,不是用于数据库查询。 数据库以结构化查询语言(SQL)进行查询,许多 SQLite 查询与您从 [MySQL][8] 和 [MariaDB][9] 数据库中已经知道的查询相同。 但是,数据类型和功能不同,因此,如果您熟悉另一个数据库,请特别注意细微的差异。
|
||||
|
||||
### 创建数据库
|
||||
|
||||
启动 SQLite 时,可以打开内存中的提示,也可以选择要打开的数据库:
|
||||
|
||||
|
||||
```
|
||||
`$ sqlite3 mydatabase.db`
|
||||
```
|
||||
|
||||
如果还没有数据库,则可以在 SQLite 提示符下创建一个数据库:
|
||||
|
||||
|
||||
```
|
||||
`sqlite> .open mydatabase.db`
|
||||
```
|
||||
现在,您的硬盘驱动器上有一个空文件,可以用作 SQLite 数据库。 文件扩展名 **.db** 是任意的。 您也可以使用 **.sqlite** 或任何您想要的后缀。
|
||||
|
||||
### 创建一个表
|
||||
|
||||
数据库包含一些 _表_,可以将其可视化为电子表格。 有许多的行(在数据库中称为 _记录_ )和列。 行和列的交集称为 _域_。
|
||||
|
||||
结构化查询语言(SQL)以其提供的名称命名:是一种以可预测且一致的语法查询数据库内容以接收有用的结果的方法。 SQL 读起来很像普通的英语句子,即使有点像机器人一样。 当前,您的数据库是一个没有任何表的空数据库。
|
||||
|
||||
你可以使用 **CREATE** 来创建一个新表,你可以和 **IF NOT EXISTS** 结合使用。以便不会破坏现在已有的同名的表。
|
||||
|
||||
您无法在 SQLite 中创建一个没有任何字段的空表,因此在尝试 **CREATE** 语句之前,必须考虑预期表将存储的数据类型。 在此示例中,我将使用以下列创建一个名为 _member_ 的表:
|
||||
|
||||
* 唯一标识符
|
||||
* 人名
|
||||
* 记录创建的时间和日期
|
||||
|
||||
#### 唯一标识符
|
||||
|
||||
|
||||
最好用唯一的编号来引用记录,幸运的是,SQLite 认识到这一点,创建一个名叫 **rowid** 的列来自动实现这一点。
|
||||
|
||||
|
||||
无需 SQL 语句即可创建此字段。
|
||||
|
||||
|
||||
#### 数据类型
|
||||
|
||||
|
||||
对于我的示例表中,我正在创建一个 _name_ 列来保存 **TEXT** 类型的数据。 为了防止在没有指定字段数据的情况下创建记录,可以添加 **NOT NULL** 指令。
|
||||
|
||||
用 **name TEXT NOT NULL** 语句来创建。
|
||||
|
||||
SQLite 中有五种数据类型(实际上是 _储存类别_):
|
||||
|
||||
* TEXT: 文本字符串
|
||||
* INTEGER: 一个数字
|
||||
* REAL: 一个浮点数(小数位数无限制)
|
||||
* BLOB: 二进制数据(例如,.jpeg或.webp图像)
|
||||
* NULL: 空值
|
||||
|
||||
|
||||
#### 日期和时间戳
|
||||
|
||||
SQLite 有一个方便的日期和时间戳功能。 它本身不是数据类型,而是 SQLite 中的一个函数,它根据所需的格式生成字符串或整数。 在此示例中,我将其保留为默认值。
|
||||
|
||||
|
||||
创建此字段的 SQL 语句是:**datestamp DATETIME DEFAULT CURRENT_TIMESTAMP**
|
||||
|
||||
### 创表语句
|
||||
|
||||
|
||||
在 SQLite 中创建此示例表的完整 SQL:
|
||||
|
||||
|
||||
```
|
||||
sqlite> CREATE TABLE
|
||||
...> IF NOT EXISTS
|
||||
...> member (name TEXT NOT NULL,
|
||||
...> datestamp DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
```
|
||||
|
||||
在此代码示例中,我在语句的分句后按了回车键。以使其更易于阅读。 SQLite 除非以分号(**;**)终止,否则不会运行您的命令。
|
||||
|
||||
您可以使用 SQLite 命令 **.tables** 验证表是否已创建:
|
||||
|
||||
|
||||
```
|
||||
sqlite> .tables
|
||||
member
|
||||
```
|
||||
|
||||
### 查看表中的所有列
|
||||
|
||||
您可以使用 **PRAGMA** 语句验证表包含哪些列和行:
|
||||
|
||||
|
||||
```
|
||||
sqlite> PRAGMA table_info(member);
|
||||
0|name|TEXT|1||0
|
||||
1|datestamp|CURRENT_TIMESTAMP|0||0
|
||||
```
|
||||
|
||||
### 数据输入
|
||||
|
||||
您可以使用 **INSERT** 语句将一些示例数据填充到表中:
|
||||
|
||||
|
||||
```
|
||||
> INSERT INTO member (name) VALUES ('Alice');
|
||||
> INSERT INTO member (name) VALUES ('Bob');
|
||||
> INSERT INTO member (name) VALUES ('Carol');
|
||||
> INSERT INTO member (name) VALUES ('David');
|
||||
```
|
||||
|
||||
查看表中的数据
|
||||
|
||||
|
||||
```
|
||||
> SELECT * FROM member;
|
||||
Alice|2020-12-15 22:39:00
|
||||
Bob|2020-12-15 22:39:02
|
||||
Carol|2020-12-15 22:39:05
|
||||
David|2020-12-15 22:39:07
|
||||
```
|
||||
|
||||
#### 添加多行数据
|
||||
|
||||
现在创建第二个表:
|
||||
|
||||
|
||||
```
|
||||
> CREATE TABLE IF NOT EXISTS linux (
|
||||
...> distro TEXT NOT NULL)
|
||||
```
|
||||
|
||||
填充一些示例数据,这一次使用多个 **VALUES** 的快捷方式,因此您可以在一个命令中添加多行。 关键字 **VALUES** 期望在括号中列出一个列表,但可以采用多个用逗号分隔列表:
|
||||
|
||||
|
||||
```
|
||||
> INSERT INTO linux (distro)
|
||||
...> VALUES ('Slackware'), ('RHEL'),
|
||||
...> ('Fedora'),('Debian');
|
||||
```
|
||||
|
||||
### 修改表
|
||||
|
||||
您现在有两个表,但是到目前为止,两者之间没有任何关系。 它们每个都包含独立的数据,但是可能您可能需要将第一个表的成员与第二个表中列出的特定项相关联。
|
||||
|
||||
为此,您可以为第一个表创建一个新列,该列对应于第二个表。 由于两个表都设计有唯一标识符(这要归功于 SQLite 的自动创建),所以连接它们的最简单方法是将其中一个的 **rowid** 字段用作另一个的选择器。
|
||||
|
||||
在第一个表中创建一个新列,以存储第二个表中的值:
|
||||
|
||||
|
||||
```
|
||||
`> ALTER TABLE member ADD os INT;`
|
||||
```
|
||||
|
||||
使用 **linux** 表中的唯一标识符,作为 member 表中每一条记录中 os 字段的值。因为记录已经存在。因此你可以使用 **UPDATE** 语句而不是使用 **INSERT** 语句来更新数据。需要特别注意的是,你首先需要选中特定的一行来然后才能更新其中的某个字段。从句法上讲,这有点相反,更新首先发生,选择匹配最后发生:
|
||||
|
||||
|
||||
```
|
||||
`> UPDATE member SET os=1 WHERE name='Alice';`
|
||||
```
|
||||
|
||||
|
||||
对 **member** 表中的其他行重复相同的过程。更新 os 字段,为了数据多样性,在四行记录上分配三种不同的发行版。
|
||||
|
||||
### 联接表
|
||||
|
||||
现在,这两个表相互关联,您可以使用 SQL 显示关联的数据。 数据库中有多种 _joins_ ,但是一旦掌握了基础知识,就可以尝试全部使用。 这是一个基本联接,用于将 member 表的 os 字段中的值与 linux 表的 id 字段相关联:
|
||||
|
||||
|
||||
```
|
||||
> SELECT * FROM member INNER JOIN linux ON member.os=linux.rowid;
|
||||
Alice|2020-12-15 22:39:00|1|Slackware
|
||||
Bob|2020-12-15 22:39:02|3|Fedora
|
||||
Carol|2020-12-15 22:39:05|3|Fedora
|
||||
David|2020-12-15 22:39:07|4|Debian
|
||||
```
|
||||
|
||||
**os** 和 **id** 字段形成了关联
|
||||
|
||||
在一个图形应用程序中,你可以想象 os 字段是一个下拉选项按钮,其中的值是 **linux** 表中 **distro** 字段中的数据。通过将相关的数据集通过唯一的字段相关联,可以确保数据的一致性和有效性,并且借助 SQL,您可以在以后动态地关联它们。
|
||||
|
||||
|
||||
### 了解更多
|
||||
|
||||
SQLite 是一个非常有用的独立,可移植的开源数据库。 学习以交互方式使用它是迈向针对 Web 应用程序进行管理或通过编程语言库使用它的重要的第一步。
|
||||
|
||||
如果您喜欢 SQLite,也可以尝试由同一位作者 Richard Hipp 博士的 [Fossil][10]。
|
||||
|
||||
在学习和使用 SQLite 时,有一些常用命令可能会有所帮助,所以请立即下载我们的 **[SQLite3备忘单][3] **!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/21/2/sqlite3-cheat-sheet
|
||||
|
||||
作者:[Klaatu][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[amwps290](https://github.com/amwps290)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/klaatu
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/coverimage_cheat_sheet.png?itok=lYkNKieP "Cheat Sheet cover image"
|
||||
[2]: https://www.redhat.com/sysadmin/yaml-beginners
|
||||
[3]: https://opensource.com/downloads/sqlite-cheat-sheet
|
||||
[4]: https://sqlite.org/copyright.html
|
||||
[5]: https://directory.fsf.org/wiki/License:PublicDomain
|
||||
[6]: https://www.sqlite.org/purchase/license?
|
||||
[7]: https://www.sqlite.org/download.html
|
||||
[8]: https://www.mysql.com/
|
||||
[9]: https://mariadb.org/
|
||||
[10]: https://opensource.com/article/20/11/fossil
|
Loading…
Reference in New Issue
Block a user