SQLite 的 DELETE 查询用于删除表中已有的记录。可以使用带有 WHERE 子句的 DELETE 查询来删除选定行,否则所有的记录都会被删除。 语法 带有 WHERE 子句的 DELETE 查询的基本语法如下:
1 2 |
<span class="pln">DELETE FROM table_name WHERE </span><span class="pun">[</span><span class="pln">condition</span><span class="pun">];</span> |
您可以使用 AND 或 OR 运算符来结合 N 个数量的条件。 实例 假设 COMPANY 表有以下记录:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
下面是一个实例,它会删除 ID 为 7 的客户:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> DELETE FROM COMPANY WHERE ID </span><span class="pun">=</span> <span class="lit">7</span><span class="pun">;</span> |
现在,COMPANY 表有以下记录:
1 2 3 4 5 6 7 8 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> |
如果您想要从 COMPANY 表中删除所有记录,则不需要使用 WHERE 子句,DELETE 查询如下:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> DELETE FROM COMPANY</span><span class="pun">;</span> |
现在,COMPANY 表中没有任何的记录,因为所有的记录已经通过 DELETE 语句删除。 from:http://www.runoob.com/sqlite/sqlite-delete.html
View DetailsSQLite 的 UPDATE 查询用于修改表中已有的记录。可以使用带有 WHERE 子句的 UPDATE 查询来更新选定行,否则所有的行都会被更新。 语法 带有 WHERE 子句的 UPDATE 查询的基本语法如下:
1 2 3 |
<span class="pln">UPDATE table_name SET column1 </span><span class="pun">=</span><span class="pln"> value1</span><span class="pun">,</span><span class="pln"> column2 </span><span class="pun">=</span><span class="pln"> value2</span><span class="pun">....,</span><span class="pln"> columnN </span><span class="pun">=</span><span class="pln"> valueN WHERE </span><span class="pun">[</span><span class="pln">condition</span><span class="pun">];</span> |
您可以使用 AND 或 OR 运算符来结合 N 个数量的条件。 实例 假设 COMPANY 表有以下记录:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
下面是一个实例,它会更新 ID 为 6 的客户地址:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> UPDATE COMPANY SET ADDRESS </span><span class="pun">=</span> <span class="str">'Texas'</span><span class="pln"> WHERE ID </span><span class="pun">=</span> <span class="lit">6</span><span class="pun">;</span> |
现在,COMPANY 表有以下记录:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">Texas</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
如果您想修改 COMPANY 表中 ADDRESS 和 SALARY 列的所有值,则不需要使用 WHERE 子句,UPDATE 查询如下:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> UPDATE COMPANY SET ADDRESS </span><span class="pun">=</span> <span class="str">'Texas'</span><span class="pun">,</span><span class="pln"> SALARY </span><span class="pun">=</span> <span class="lit">20000.00</span><span class="pun">;</span> |
现在,COMPANY 表有以下记录:
1 2 3 4 5 6 7 8 9 10 11 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">Texas</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">20000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Texas</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">20000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">20000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">Texas</span> <span class="lit">20000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Texas</span> <span class="lit">20000.0 from:<a href="http://www.runoob.com/sqlite/sqlite-update.html">http://www.runoob.com/sqlite/sqlite-update.html</a></span> |
SQLite 的 AND 和 OR 运算符用于编译多个条件来缩小在 SQLite 语句中所选的数据。这两个运算符被称为连接运算符。 这些运算符为同一个 SQLite 语句中不同的运算符之间的多个比较提供了可能。 AND 运算符 AND 运算符允许在一个 SQL 语句的 WHERE 子句中的多个条件的存在。使用 AND 运算符时,只有当所有条件都为真(true)时,整个条件为真(true)。例如,只有当 condition1 和 condition2 都为真(true)时,[condition1] AND [condition2] 为真(true)。 语法 带有 WHERE 子句的 AND 运算符的基本语法如下:
1 2 3 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,</span><span class="pln"> columnN FROM table_name WHERE </span><span class="pun">[</span><span class="pln">condition1</span><span class="pun">]</span><span class="pln"> AND </span><span class="pun">[</span><span class="pln">condition2</span><span class="pun">]...</span><span class="pln">AND </span><span class="pun">[</span><span class="pln">conditionN</span><span class="pun">];</span> |
您可以使用 AND 运算符来结合 N 个数量的条件。SQLite 语句需要执行的动作是,无论是事务或查询,所有由 AND 分隔的条件都必须为真(TRUE)。 实例 假设 COMPANY 表有以下记录:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
下面的 SELECT 语句列出了 AGE 大于等于 25 且工资大于等于 65000.00 的所有记录:
1 2 3 4 5 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY WHERE AGE </span><span class="pun">>=</span> <span class="lit">25</span><span class="pln"> AND SALARY </span><span class="pun">>=</span> <span class="lit">65000</span><span class="pun">;</span><span class="pln"> ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> |
OR 运算符 OR 运算符也用于结合一个 SQL 语句的 WHERE 子句中的多个条件。使用 OR 运算符时,只要当条件中任何一个为真(true)时,整个条件为真(true)。例如,只要当 condition1 或 condition2 有一个为真(true)时,[condition1] OR [condition2] 为真(true)。 语法 带有 WHERE 子句的 OR 运算符的基本语法如下:
1 2 3 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,</span><span class="pln"> columnN FROM table_name WHERE </span><span class="pun">[</span><span class="pln">condition1</span><span class="pun">]</span><span class="pln"> OR </span><span class="pun">[</span><span class="pln">condition2</span><span class="pun">]...</span><span class="pln">OR </span><span class="pun">[</span><span class="pln">conditionN</span><span class="pun">]</span> |
您可以使用 OR 运算符来结合 N 个数量的条件。SQLite 语句需要执行的动作是,无论是事务或查询,只要任何一个由 […]
View DetailsSQLite的 WHERE 子句用于指定从一个表或多个表中获取数据的条件。 如果满足给定的条件,即为真(true)时,则从表中返回特定的值。您可以使用 WHERE 子句来过滤记录,只获取需要的记录。 WHERE 子句不仅可用在 SELECT 语句中,它也可用在 UPDATE、DELETE 语句中,等等,这些我们将在随后的章节中学习到。 语法 SQLite 的带有 WHERE 子句的 SELECT 语句的基本语法如下:
1 2 3 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,</span><span class="pln"> columnN FROM table_name WHERE </span><span class="pun">[</span><span class="pln">condition</span><span class="pun">]</span> |
实例 您还可以使用比较或逻辑运算符指定条件,比如 >、<、=、LIKE、NOT,等等。假设 COMPANY 表有以下记录:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
下面的实例演示了 SQLite 逻辑运算符的用法。下面的 SELECT 语句列出了 AGE 大于等于 25 且工资大于等于 65000.00 的所有记录:
1 2 3 4 5 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY WHERE AGE </span><span class="pun">>=</span> <span class="lit">25</span><span class="pln"> AND SALARY </span><span class="pun">>=</span> <span class="lit">65000</span><span class="pun">;</span><span class="pln"> ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> |
下面的 SELECT 语句列出了 AGE 大于等于 25 或工资大于等于 65000.00 的所有记录:
1 2 3 4 5 6 7 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY WHERE AGE </span><span class="pun">>=</span> <span class="lit">25</span><span class="pln"> OR SALARY </span><span class="pun">>=</span> <span class="lit">65000</span><span class="pun">;</span><span class="pln"> ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> |
下面的 SELECT 语句列出了 AGE 不为 NULL 的所有记录,结果显示所有的记录,意味着没有一个记录的 AGE 等于 NULL:
1 2 3 4 5 6 7 8 9 10 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY WHERE AGE IS NOT NULL</span><span class="pun">;</span><span class="pln"> ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
下面的 SELECT 语句列出了 NAME 以 'Ki' 开始的所有记录,’Ki' 之后的字符不做限制:
1 2 3 4 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY WHERE NAME LIKE </span><span class="str">'Ki%'</span><span class="pun">;</span><span class="pln"> ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> |
下面的 SELECT 语句列出了 NAME 以 'Ki' 开始的所有记录,’Ki' 之后的字符不做限制:
1 2 3 4 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY WHERE NAME GLOB </span><span class="str">'Ki*'</span><span class="pun">;</span><span class="pln"> ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> |
下面的 SELECT 语句列出了 AGE 的值为 25 或 27 的所有记录:
1 2 3 4 5 6 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY WHERE AGE IN </span><span class="pun">(</span> <span class="lit">25</span><span class="pun">,</span> <span class="lit">27</span> <span class="pun">);</span><span class="pln"> ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> |
下面的 […]
View Details表达式是一个或多个值、运算符和计算值的SQL函数的组合。 SQL 表达式与公式类似,都写在查询语言中。您还可以使用特定的数据集来查询数据库。 语法 假设 SELECT 语句的基本语法如下:
1 2 3 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,</span><span class="pln"> columnN FROM table_name WHERE </span><span class="pun">[</span><span class="pln">CONTION </span><span class="pun">|</span><span class="pln"> EXPRESSION</span><span class="pun">];</span> |
有不同类型的 SQLite 表达式,具体讲解如下: SQLite – 布尔表达式 SQLite 的布尔表达式在匹配单个值的基础上获取数据。语法如下:
1 2 3 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,</span><span class="pln"> columnN FROM table_name WHERE SINGLE VALUE MATCHTING EXPRESSION</span><span class="pun">;</span> |
假设 COMPANY 表有以下记录:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
下面的实例演示了 SQLite 布尔表达式的用法:
1 2 3 4 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY WHERE SALARY </span><span class="pun">=</span> <span class="lit">10000</span><span class="pun">;</span><span class="pln"> ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">4</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
SQLite – 数值表达式 这些表达式用来执行查询中的任何数学运算。语法如下:
1 2 |
<span class="pln">SELECT numerical_expression </span><span class="kwd">as</span><span class="pln"> OPERATION_NAME </span><span class="pun">[</span><span class="pln">FROM table_name WHERE CONDITION</span><span class="pun">]</span> <span class="pun">;</span> |
在这里,numerical_expression 用于数学表达式或任何公式。下面的实例演示了 SQLite 数值表达式的用法:
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">(</span><span class="lit">15</span> <span class="pun">+</span> <span class="lit">6</span><span class="pun">)</span><span class="pln"> AS ADDITION ADDITION </span><span class="pun">=</span> <span class="lit">21</span> |
有几个内置的函数,比如 avg()、sum()、count(),等等,执行被称为对一个表或一个特定的表列的汇总数据计算。
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT COUNT</span><span class="pun">(*)</span><span class="pln"> AS </span><span class="str">"RECORDS"</span><span class="pln"> FROM COMPANY</span><span class="pun">;</span><span class="pln"> RECORDS </span><span class="pun">=</span> <span class="lit">7</span> |
SQLite – 日期表达式 日期表达式返回当前系统日期和时间值,这些表达式将被用于各种数据操作。
1 2 3 4 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT CURRENT_TIMESTAMP</span><span class="pun">;</span><span class="pln"> CURRENT_TIMESTAMP </span><span class="pun">=</span> <span class="lit">2013</span><span class="pun">-</span><span class="lit">03</span><span class="pun">-</span><span class="lit">17</span> <span class="lit">10</span><span class="pun">:</span><span class="lit">43</span><span class="pun">:</span><span class="lit">35 from:http://www.runoob.com/sqlite/sqlite-expressions.html</span> |
SQLite 运算符是什么? 运算符是一个保留字或字符,主要用于 SQLite 语句的 WHERE 子句中执行操作,如比较和算术运算。 运算符用于指定 SQLite 语句中的条件,并在语句中连接多个条件。 算术运算符 比较运算符 逻辑运算符 位运算符 SQLite 算术运算符 假设变量 a=10,变量 b=20,则: 运算符 描述 实例 + 加法 – 把运算符两边的值相加 a + b 将得到 30 – 减法 – 左操作数减去右操作数 a – b 将得到 -10 * 乘法 – 把运算符两边的值相乘 a * b 将得到 200 / 除法 – 左操作数除以右操作数 b / a 将得到 2 % 取模 – 左操作数除以右操作数后得到的余数 b % a will give 0 实例 下面是 SQLite 算术运算符的简单实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<span class="pln">sqlite</span><span class="pun">></span> <span class="pun">.</span><span class="pln">mode line sqlite</span><span class="pun">></span> <span class="kwd">select</span> <span class="lit">10</span> <span class="pun">+</span> <span class="lit">20</span><span class="pun">;</span> <span class="lit">10</span> <span class="pun">+</span> <span class="lit">20</span> <span class="pun">=</span> <span class="lit">30</span><span class="pln"> sqlite</span><span class="pun">></span> <span class="kwd">select</span> <span class="lit">10</span> <span class="pun">-</span> <span class="lit">20</span><span class="pun">;</span> <span class="lit">10</span> <span class="pun">-</span> <span class="lit">20</span> <span class="pun">=</span> <span class="pun">-</span><span class="lit">10</span><span class="pln"> sqlite</span><span class="pun">></span> <span class="kwd">select</span> <span class="lit">10</span> <span class="pun">*</span> <span class="lit">20</span><span class="pun">;</span> <span class="lit">10</span> <span class="pun">*</span> <span class="lit">20</span> <span class="pun">=</span> <span class="lit">200</span><span class="pln"> sqlite</span><span class="pun">></span> <span class="kwd">select</span> <span class="lit">10</span> <span class="pun">/</span> <span class="lit">5</span><span class="pun">;</span> <span class="lit">10</span> <span class="pun">/</span> <span class="lit">5</span> <span class="pun">=</span> <span class="lit">2</span><span class="pln"> sqlite</span><span class="pun">></span> <span class="kwd">select</span> <span class="lit">12</span> <span class="pun">%</span> <span class="lit">5</span><span class="pun">;</span> <span class="lit">12</span> <span class="pun">%</span> <span class="lit">5</span> <span class="pun">=</span> <span class="lit">2</span> |
SQLite 比较运算符 假设变量 a=10,变量 b=20,则: 运算符 描述 实例 == 检查两个操作数的值是否相等,如果相等则条件为真。 (a == b) 不为真。 = […]
View DetailsSQLite 的 SELECT 语句用于从 SQLite 数据库表中获取数据,以结果表的形式返回数据。这些结果表也被称为结果集。 语法 SQLite 的 SELECT 语句的基本语法如下:
1 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,</span><span class="pln"> columnN FROM table_name</span><span class="pun">;</span> |
在这里,column1, column2…是表的字段,他们的值即是您要获取的。如果您想获取所有可用的字段,那么可以使用下面的语法:
1 |
<span class="pln">SELECT </span><span class="pun">*</span><span class="pln"> FROM table_name</span><span class="pun">;</span> |
实例 假设 COMPANY 表有以下记录:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
下面是一个实例,使用 SELECT 语句获取并显示所有这些记录。在这里,前三个命令被用来设置正确格式化的输出。
1 2 3 |
<span class="pln">sqlite</span><span class="pun">>.</span><span class="pln">header on sqlite</span><span class="pun">>.</span><span class="pln">mode column sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY</span><span class="pun">;</span> |
最后,将得到以下的结果:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
如果只想获取 COMPANY 表中指定的字段,则使用下面的查询:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT ID</span><span class="pun">,</span><span class="pln"> NAME</span><span class="pun">,</span><span class="pln"> SALARY FROM COMPANY</span><span class="pun">;</span> |
上面的查询会产生以下结果:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">10000.0</span> |
设置输出列的宽度 有时,由于要显示的列的默认宽度导致 .mode column,这种情况下,输出被截断。此时,您可以使用 .width num, num…. 命令设置显示列的宽度,如下所示:
1 2 |
<span class="pln">sqlite</span><span class="pun">>.</span><span class="pln">width </span><span class="lit">10</span><span class="pun">,</span> <span class="lit">20</span><span class="pun">,</span> <span class="lit">10</span><span class="pln"> sqlite</span><span class="pun">></span><span class="pln">SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY</span><span class="pun">;</span> |
上面的 .width 命令设置第一列的宽度为 10,第二列的宽度为 20,第三列的宽度为 10。因此上述 SELECT 语句将得到以下结果:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">--------------------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
Schema 信息 因为所有的点命令只在 SQLite 提示符中可用,所以当您进行带有 SQLite 的编程时,您要使用下面的带有 sqlite_master 表的 SELECT 语句来列出所有在数据库中创建的表:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT tbl_name FROM sqlite_master WHERE type </span><span class="pun">=</span> <span class="str">'table'</span><span class="pun">;</span> |
假设在 testDB.db 中已经存在唯一的 COMPANY 表,则将产生以下结果:
1 2 3 |
<span class="pln">tbl_name </span><span class="pun">----------</span><span class="pln"> COMPANY</span> |
您可以列出关于 COMPANY 表的完整信息,如下所示:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT sql FROM sqlite_master WHERE type </span><span class="pun">=</span> <span class="str">'table'</span><span class="pln"> AND tbl_name </span><span class="pun">=</span> <span class="str">'COMPANY'</span><span class="pun">;</span> |
假设在 testDB.db 中已经存在唯一的 COMPANY 表,则将产生以下结果:
1 2 3 4 5 6 7 8 9 |
<span class="pln">CREATE TABLE COMPANY</span><span class="pun">(</span><span class="pln"> ID INT PRIMARY KEY NOT NULL</span><span class="pun">,</span><span class="pln"> NAME TEXT NOT NULL</span><span class="pun">,</span><span class="pln"> AGE INT NOT NULL</span><span class="pun">,</span><span class="pln"> ADDRESS CHAR</span><span class="pun">(</span><span class="lit">50</span><span class="pun">),</span><span class="pln"> SALARY REAL </span><span class="pun">) from:<a href="http://www.runoob.com/sqlite/sqlite-select.html">http://www.runoob.com/sqlite/sqlite-select.html</a></span> |
SQLite 的 INSERT INTO 语句用于向数据库的某个表中添加新的数据行。 语法 INSERT INTO 语句有两种基本语法,如下所示:
1 2 |
<span class="pln">INSERT INTO TABLE_NAME </span><span class="pun">(</span><span class="pln">column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,</span><span class="pln"> column3</span><span class="pun">,...</span><span class="pln">columnN</span><span class="pun">)]</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="pln">value1</span><span class="pun">,</span><span class="pln"> value2</span><span class="pun">,</span><span class="pln"> value3</span><span class="pun">,...</span><span class="pln">valueN</span><span class="pun">);</span> |
在这里,column1, column2,…columnN 是要插入数据的表中的列的名称。 如果要为表中的所有列添加值,您也可以不需要在 SQLite 查询中指定列名称。但要确保值的顺序与列在表中的顺序一致。SQLite 的 INSERT INTO 语法如下:
1 |
<span class="pln">INSERT INTO TABLE_NAME VALUES </span><span class="pun">(</span><span class="pln">value1</span><span class="pun">,</span><span class="pln">value2</span><span class="pun">,</span><span class="pln">value3</span><span class="pun">,...</span><span class="pln">valueN</span><span class="pun">);</span> |
实例 假设您已经在 testDB.db 中创建了 COMPANY表,如下所示:
1 2 3 4 5 6 7 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> CREATE TABLE COMPANY</span><span class="pun">(</span><span class="pln"> ID INT PRIMARY KEY NOT NULL</span><span class="pun">,</span><span class="pln"> NAME TEXT NOT NULL</span><span class="pun">,</span><span class="pln"> AGE INT NOT NULL</span><span class="pun">,</span><span class="pln"> ADDRESS CHAR</span><span class="pun">(</span><span class="lit">50</span><span class="pun">),</span><span class="pln"> SALARY REAL </span><span class="pun">);</span> |
现在,下面的语句将在 COMPANY 表中创建六个记录:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span class="pln">INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span> <span class="str">'Paul'</span><span class="pun">,</span> <span class="lit">32</span><span class="pun">,</span> <span class="str">'California'</span><span class="pun">,</span> <span class="lit">20000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">2</span><span class="pun">,</span> <span class="str">'Allen'</span><span class="pun">,</span> <span class="lit">25</span><span class="pun">,</span> <span class="str">'Texas'</span><span class="pun">,</span> <span class="lit">15000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">3</span><span class="pun">,</span> <span class="str">'Teddy'</span><span class="pun">,</span> <span class="lit">23</span><span class="pun">,</span> <span class="str">'Norway'</span><span class="pun">,</span> <span class="lit">20000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">4</span><span class="pun">,</span> <span class="str">'Mark'</span><span class="pun">,</span> <span class="lit">25</span><span class="pun">,</span> <span class="str">'Rich-Mond '</span><span class="pun">,</span> <span class="lit">65000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">5</span><span class="pun">,</span> <span class="str">'David'</span><span class="pun">,</span> <span class="lit">27</span><span class="pun">,</span> <span class="str">'Texas'</span><span class="pun">,</span> <span class="lit">85000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">6</span><span class="pun">,</span> <span class="str">'Kim'</span><span class="pun">,</span> <span class="lit">22</span><span class="pun">,</span> <span class="str">'South-Hall'</span><span class="pun">,</span> <span class="lit">45000.00</span> <span class="pun">);</span> |
您也可以使用第二种语法在 COMPANY 表中创建一个记录,如下所示:
1 |
<span class="pln">INSERT INTO COMPANY VALUES </span><span class="pun">(</span><span class="lit">7</span><span class="pun">,</span> <span class="str">'James'</span><span class="pun">,</span> <span class="lit">24</span><span class="pun">,</span> <span class="str">'Houston'</span><span class="pun">,</span> <span class="lit">10000.00</span> <span class="pun">);</span> |
上面的所有语句将在 COMPANY 表中创建下列记录。下一章会教您如何从一个表中显示所有这些记录。
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
使用一个表来填充另一个表 您可以通过在一个有一组字段的表上使用 select 语句,填充数据到另一个表中。下面是语法:
1 2 3 4 |
<span class="pln">INSERT INTO first_table_name </span><span class="pun">[(</span><span class="pln">column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,</span> <span class="pun">...</span><span class="pln"> columnN</span><span class="pun">)]</span><span class="pln"> SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,</span> <span class="pun">...</span><span class="pln">columnN FROM second_table_name </span><span class="pun">[</span><span class="pln">WHERE condition</span><span class="pun">];</span> |
您暂时可以先跳过上面的语句,可以先学习后面章节中介绍的 SELECT 和 WHERE 子句。 from:http://www.runoob.com/sqlite/sqlite-insert.html
View DetailsSQLite 的 DROP TABLE 语句用来删除表定义及其所有相关数据、索引、触发器、约束和该表的权限规范。 使用此命令时要特别注意,因为一旦一个表被删除,表中所有信息也将永远丢失。 语法 DROP TABLE 语句的基本语法如下。您可以选择指定带有表名的数据库名称,如下所示:
1 |
<span class="pln">DROP TABLE database_name</span><span class="pun">.</span><span class="pln">table_name</span><span class="pun">;</span> |
实例 让我们先确认 COMPANY 表已经存在,然后我们将其从数据库中删除。
1 2 |
<span class="pln">sqlite</span><span class="pun">>.</span><span class="pln">tables COMPANY test</span><span class="pun">.</span><span class="pln">COMPANY</span> |
这意味着 COMPANY 表已存在数据库中,接下来让我们把它从数据库中删除,如下:
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln">DROP TABLE COMPANY</span><span class="pun">;</span><span class="pln"> sqlite</span><span class="pun">></span> |
现在,如果尝试 .TABLES 命令,那么将无法找到 COMPANY 表了:
1 2 |
<span class="pln">sqlite</span><span class="pun">>.</span><span class="pln">tables sqlite</span><span class="pun">></span> |
显示结果为空,意味着已经成功从数据库删除表。 from:http://www.runoob.com/sqlite/sqlite-drop-table.html
View DetailsSQLite 的 CREATE TABLE 语句用于在任何给定的数据库创建一个新表。创建基本表,涉及到命名表、定义列及每一列的数据类型。 语法 CREATE TABLE 语句的基本语法如下:
1 2 3 4 5 6 7 |
CREATE TABLE database_name.table_name( column1 datatype PRIMARY KEY(one or more columns), column2 datatype, column3 datatype, ..... columnN datatype, ); |
CREATE TABLE 是告诉数据库系统创建一个新表的关键字。CREATE TABLE 语句后跟着表的唯一的名称或标识。您也可以选择指定带有 table_name 的 database_name。 实例 下面是一个实例,它创建了一个 COMPANY 表,ID 作为主键,NOT NULL 的约束表示在表中创建纪录时这些字段不能为 NULL:
1 2 3 4 5 6 7 |
sqlite> CREATE TABLE COMPANY( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL ); |
让我们再创建一个表,我们将在随后章节的练习中使用:
1 2 3 4 5 |
sqlite> CREATE TABLE DEPARTMENT( ID INT PRIMARY KEY NOT NULL, DEPT CHAR(50) NOT NULL, EMP_ID INT NOT NULL ); |
您可以使用 SQLIte 命令中的 .tables 命令来验证表是否已成功创建,该命令用于列出附加数据库中的所有表。
1 2 |
sqlite>.tables COMPANY DEPARTMENT |
在这里,可以看到我们刚创建的两张表 COMPANY、 DEPARTMENT。 您可以使用 SQLite .schema 命令得到表的完整信息,如下所示:
1 2 3 4 5 6 7 8 |
sqlite>.schema COMPANY CREATE TABLE COMPANY( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL ); |
from:http://www.runoob.com/sqlite/sqlite-create-table.html
View Details