SQLite basic grammar learning

SQLite is an in-process library that implements a self-sufficient, serverless, zero-configuration, transactional SQL database engine. It is a zero-configuration database, which means that unlike other databases, you do not need to configure it in the system.

Just like other databases, the SQLite engine is not an independent process, and can be statically or dynamically connected according to application requirements. SQLite directly accesses its storage files. It is said that the syntax of SQLite is the same as most of MySQL, and from the suffix of Lite, it can be seen that this should be like a Mini version of MySQL. Having said that, let's take a look at the differences between it and MySQL.

Enter the database command line

[root@master ~]# sqlite3
SQLite version 3.27.2 2019-02-25 16:06:06
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> 

Statement syntax

All SQLite statements can start with any keyword, such as SELECT, INSERT, UPDATE, DELETE, ALTER, DROP, etc., and all statements end with a semicolon ;.

Create database

[root@master ~]# sqlite3
SQLite version 3.27.2 2019-02-25 16:06:06
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .databases
main: 
sqlite> .open test.db
sqlite> .databases
main: /root/test.db
sqlite> .quit

Additional database

Somewhat similar to MySQL's use database; command

[root@master SQLite]# sqlite3
SQLite version 3.27.2 2019-02-25 16:06:06
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .open test.db
sqlite> .open testDB.db
sqlite> .database
main: /root/SQLite/testDB.db
sqlite> ATTACH DATABASE 'testDB.db' as 'TEST';
sqlite> .database
main: /root/SQLite/testDB.db
TEST: /root/SQLite/testDB.db

Separate database

sqlite> .database
main: /root/SQLite/testDB.db
TEST: /root/SQLite/testDB.db
sqlite> detach database TEST;
sqlite> .database
main: /root/SQLite/testDB.db

Create data table

CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL    
);
CREATE TABLE DEPARTMENT(
   ID INT PRIMARY KEY      NOT NULL,
   DEPT           CHAR(50) NOT NULL,
   EMP_ID         INT      NOT NULL
);
------------------------------------------------------------

sqlite> CREATE TABLE COMPANY(
   ...>    ID INT PRIMARY KEY     NOT NULL,
   ...>    NAME           TEXT    NOT NULL,
   ...>    AGE            INT     NOT NULL,
   ...>    ADDRESS        CHAR(50),
   ...>    SALARY         REAL
   ...> );
sqlite> CREATE TABLE DEPARTMENT(
   ...>    ID INT PRIMARY KEY      NOT NULL,
   ...>    DEPT           CHAR(50) NOT NULL,
   ...>    EMP_ID         INT      NOT NULL
   ...> );
sqlite> .tables
COMPANY     DEPARTMENT
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
);

Delete data table

sqlite> .tables
COMPANY     DEPARTMENT
sqlite> drop table company;
sqlite> .tables
DEPARTMENT

Insert data

sqlite> INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
   ...> VALUES (1, 'Paul', 32, 'California', 20000.00 );

sqlite> INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
   ...> VALUES (2, 'Allen', 25, 'Texas', 15000.00 );

sqlite> INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
   ...> VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );

sqlite> INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
   ...> VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );

sqlite> INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
   ...> VALUES (5, 'David', 27, 'Texas', 85000.00 );

sqlite> INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
   ...> VALUES (6, 'Kim', 22, 'South-Hall', 45000.00 );

sqlite> INSERT INTO COMPANY VALUES (7, 'James', 24, 'Houston', 10000.00 );

Query data table

sqlite> 
sqlite> .header on
sqlite> .mode column
sqlite> select * from company;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0   
2           Allen       25          Texas       15000.0   
3           Teddy       23          Norway      20000.0   
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       85000.0   
6           Kim         22          South-Hall  45000.0   
7           James       24          Houston     10000.0 

sqlite> select id, name, salary from company;
ID          NAME        SALARY    
----------  ----------  ----------
1           Paul        20000.0   
2           Allen       15000.0   
3           Teddy       20000.0   
4           Mark        65000.0   
5           David       85000.0   
6           Kim         45000.0   
7           James       10000.0 

# 可自己设置列宽
sqlite> .width 10, 20, 10
sqlite> select id, name, salary from company;
ID          NAME                  SALARY    
----------  --------------------  ----------
1           Paul                  20000.0   
2           Allen                 15000.0   
3           Teddy                 20000.0   
4           Mark                  65000.0   
5           David                 85000.0   
6           Kim                   45000.0   
7           James                 10000.0  

sqlite> .width 10, 20, 10
sqlite> SELECT sql FROM sqlite_master WHERE type = 'table' AND tbl_name = 'COMPANY';
sql       
----------
CREATE TAB
sqlite> .width on
sqlite> SELECT sql FROM sqlite_master WHERE type = 'table' AND tbl_name = 'COMPANY';
sql                                                                                                                                                                                       
------------------------------------------------------------------------------------                                                                   
CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL    
)

Operator

https://www.runoob.com/sqlite/sqlite-operators.html

When the .mode line is not used, it seems to be similar to the output of MySQL, but it seems that the line mode is really good.

sqlite> select 10 + 20;
10 + 20   
----------
30        
sqlite> .mode line
sqlite> select 10 + 20;
10 + 20 = 30

sqlite> select * from company;
     ID = 1
   NAME = Paul
    AGE = 32
ADDRESS = California
 SALARY = 20000.0

     ID = 2
   NAME = Allen
    AGE = 25
ADDRESS = Texas
 SALARY = 15000.0

...
sqlite> .mode column
sqlite> select * from company;
ID          NAME                  AGE         ADDRESS     SALARY    
----------  --------------------  ----------  ----------  ----------
1           Paul                  32          California  20000.0   
2           Allen                 25          Texas       15000.0   
3           Teddy                 23          Norway      20000.0   
4           Mark                  25          Rich-Mond   65000.0   
5           David                 27          Texas       85000.0   
6           Kim                   22          South-Hall  45000.0   
7           James                 24          Houston     10000.0   
sqlite> select * from company where salary=20000;
ID          NAME                  AGE         ADDRESS     SALARY    
----------  --------------------  ----------  ----------  ----------
1           Paul                  32          California  20000.0   
3           Teddy                 23          Norway      20000.0   
sqlite> select * from company where salary>50000;
ID          NAME                  AGE         ADDRESS     SALARY    
----------  --------------------  ----------  ----------  ----------
4           Mark                  25          Rich-Mond   65000.0   
5           David                 27          Texas       85000.0  

Update data sheet

sqlite> select * from company;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0   
2           Allen       25          Texas       15000.0   
3           Teddy       23          Norway      20000.0   
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       85000.0   
6           Kim         22          South-Hall  45000.0   
7           James       24          Houston     10000.0   
sqlite> UPDATE COMPANY SET ADDRESS = 'Texas' WHERE ID = 6;
sqlite> select * from company;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0   
2           Allen       25          Texas       15000.0   
3           Teddy       23          Norway      20000.0   
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       85000.0   
6           Kim         22          Texas       45000.0   
7           James       24          Houston     10000.0 

delete data

sqlite> select * from company;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0   
2           Allen       25          Texas       15000.0   
3           Teddy       23          Norway      20000.0   
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       85000.0   
6           Kim         22          Texas       45000.0   
7           James       24          Houston     10000.0   
sqlite> delete from company where id=7;
sqlite> select * from company;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0   
2           Allen       25          Texas       15000.0   
3           Teddy       23          Norway      20000.0   
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       85000.0   
6           Kim         22          Texas       45000.0  

like clause

This typical usage is the same as MySQL's "like", so I won't be shivering.

WHERE SALARY LIKE '200%' Find any value starting with 200
WHERE SALARY LIKE '%200%' Find any value containing 200 anywhere
WHERE SALARY LIKE '_00%' Find any value where the second and third digits are 00
WHERE SALARY LIKE '2_%_%' Find any value that starts with 2 and is at least 3 characters in length
WHERE SALARY LIKE '%2' Find any value ending in 2
WHERE SALARY LIKE '_2%3' Find any value whose second digit is 2 and ends in 3
WHERE SALARY LIKE '2___3' Find any value that is 5 digits in length and starts with 2 and ends with 3

glob clause

The glob I usually see is actually in the ruby ​​syntax (but having said that, the usage is actually pretty much the same):

返回一个数组,包含与指定的通配符模式 pat 匹配的文件名:

* - 匹配包含 null 字符串的任意字符串
** - 递归地匹配任意字符串
? - 匹配任意单个字符
[...] - 匹配封闭字符中的任意一个
{a,b...} - 匹配字符串中的任意一个
Dir["foo.*"] # 匹配 "foo.c"、 "foo.rb" 等等
Dir["foo.?"] # 匹配 "foo.c"、 "foo.h" 等等
Statement description
WHERE SALARY GLOB '200*' Find any value starting with 200
WHERE SALARY GLOB '*200*' Find any value containing 200 anywhere
WHERE SALARY GLOB '?00*' Find any value where the second and third digits are 00
WHERE SALARY GLOB '2??' Find any value that starts with 2 and is at least 3 characters in length
WHERE SALARY GLOB '*2' Find any value ending in 2
WHERE SALARY GLOB '?2*3' Find any value whose second digit is 2 and ends in 3
WHERE SALARY GLOB '2???3' Find any value that is 5 digits in length and starts with 2 and ends with 3

limit clause

sqlite> select * from company;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0   
2           Allen       25          Texas       15000.0   
3           Teddy       23          Norway      20000.0   
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       85000.0   
6           Kim         22          Texas       45000.0   

sqlite> select * from company limit 3;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0   
2           Allen       25          Texas       15000.0   
3           Teddy       23          Norway      20000.0   

sqlite> select * from company limit 3 offset 2;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
3           Teddy       23          Norway      20000.0   
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       85000.0 

order by

sqlite> select * from company;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0   
2           Allen       25          Texas       15000.0   
3           Teddy       23          Norway      20000.0   
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       85000.0   
6           Kim         22          Texas       45000.0   
sqlite> select * from company order by salary;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
2           Allen       25          Texas       15000.0   
1           Paul        32          California  20000.0   
3           Teddy       23          Norway      20000.0   
6           Kim         22          Texas       45000.0   
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       85000.0 

group by

INSERT INTO COMPANY VALUES (8, 'Paul', 24, 'Houston', 20000.00 );
INSERT INTO COMPANY VALUES (9, 'James', 44, 'Norway', 5000.00 );
INSERT INTO COMPANY VALUES (10, 'James', 45, 'Texas', 5000.00 );

sqlite> select * from company;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0   
2           Allen       25          Texas       15000.0   
3           Teddy       23          Norway      20000.0   
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       85000.0   
6           Kim         22          Texas       45000.0   
8           Paul        24          Houston     20000.0   
9           James       44          Norway      5000.0    
10          James       45          Texas       5000.0    
sqlite> SELECT NAME, SUM(SALARY) FROM COMPANY GROUP BY NAME;
NAME        SUM(SALARY)
----------  -----------
Allen       15000.0    
David       85000.0    
James       10000.0    
Kim         45000.0    
Mark        65000.0    
Paul        40000.0    
Teddy       20000.0  

having clause

In a query, the HAVING clause must be placed after the GROUP BY clause and must be placed before the ORDER BY clause.

sqlite> select * from company;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0   
2           Allen       25          Texas       15000.0   
3           Teddy       23          Norway      20000.0   
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       85000.0   
6           Kim         22          Texas       45000.0   
8           Paul        24          Houston     20000.0   
9           James       44          Norway      5000.0    
10          James       45          Texas       5000.0  
sqlite> SELECT NAME, SUM(SALARY) FROM COMPANY GROUP BY NAME;
NAME        SUM(SALARY)
----------  -----------
Allen       15000.0    
David       85000.0    
James       10000.0    
Kim         45000.0    
Mark        65000.0    
Paul        40000.0    
Teddy       20000.0    
sqlite> SELECT NAME, SUM(SALARY) as TOTAL FROM COMPANY GROUP BY NAME having TOTAL > 40000;
NAME        TOTAL     
----------  ----------
David       85000.0   
Kim         45000.0   
Mark        65000.0   
sqlite> SELECT NAME, SUM(SALARY) as TOTAL FROM COMPANY GROUP BY NAME HAVING TOTAL > 40000 ORDER BY TOTAL ASC;
NAME        TOTAL     
----------  ----------
Kim         45000.0   
Mark        65000.0   
David       85000.0 

distinct

sqlite> select name from company;
NAME      
----------
Paul      
Allen     
Teddy     
Mark      
David     
Kim       
Paul      
James     
James     
sqlite> select distinct name from company;
NAME      
----------
Paul      
Allen     
Teddy     
Mark      
David     
Kim       
James 

Field constraints

Constraints are rules enforced on the data columns of the table. These are used to limit the types of data that can be inserted into the table. This ensures the accuracy and reliability of the data in the database. Constraints can be column-level or table-level. Column-level constraints only apply to columns, and table-level constraints are applied to the entire table.

The following are commonly used constraints in SQLite.

  • NOT NULL constraint : ensure that a column cannot have NULL values.

  • DEFAULT constraint : When a column does not specify a value, provide a default value for the column.

  • UNIQUE constraint : to ensure that all values ​​in a column are different.

  • PRIMARY Key constraint : uniquely identify each row/record in the database table.

  • CHECK constraints : CHECK constraints ensure that all values ​​in a column meet certain conditions.

NOT NULL constraint

By default, columns can hold NULL values. If you do not want a column to have NULL values, then you need to define this constraint on the column, specifying that NULL values ​​are not allowed on the column. NULL is not the same as no data, it represents unknown data.

CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);

 

DEFAULT constraint 

The DEFAULT constraint provides a default value for the column when the INSERT INTO statement does not provide a specific value.

CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL    DEFAULT 50000.00
);

UNIQUE constraint

The UNIQUE constraint prevents the existence of two records with the same value in a particular column. In the COMPANY table, for example, you might want to prevent two or more people from having the same age.

CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL UNIQUE,
   ADDRESS        CHAR(50),
   SALARY         REAL    DEFAULT 50000.00
);

PRIMARY KEY constraint

CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);

CHECK constraints

The CHECK constraint enables the condition to enter a record of the value to be checked. If the condition value is false, the record violates the constraint and cannot be entered into the table.

CREATE TABLE COMPANY3(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL    CHECK(SALARY > 0)
);

Delete constraint

SQLite supports a limited subset of ALTER TABLE. In SQLite, the ALTER TABLE command allows users to rename a table or add a new column to an existing table. It is impossible to rename a column, delete a column, or add or delete constraints from a table.

trigger

sqlite> CREATE TABLE AUDIT(
   ...>     EMP_ID INT NOT NULL,
   ...>     ENTRY_DATE TEXT NOT NULL
   ...> );
sqlite> CREATE TRIGGER audit_log AFTER INSERT 
   ...> ON COMPANY
   ...> BEGIN
   ...>    INSERT INTO AUDIT(EMP_ID, ENTRY_DATE) VALUES (new.ID, datetime('now'));
   ...> END;

sqlite> select * from audit;
sqlite> INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
   ...> VALUES (11, 'Paul', 32, 'California', 20000.00 );
sqlite> select * from audit;
11          2021-01-22 06:35:27
sqlite> SELECT name FROM sqlite_master
   ...> WHERE type = 'trigger' AND tbl_name = 'COMPANY';
audit_log
sqlite> DROP TRIGGER audit_log;

index

Create index

CREATE INDEX index_name ON table_name;

Single-column index

A single-column index is an index created on only one column of the table.

CREATE INDEX index_name ON table_name (column_name);

Unique index

The use of unique indexes is not only for performance, but also for data integrity. The unique index does not allow any duplicate values ​​to be inserted into the table.

CREATE UNIQUE INDEX index_name ON table_name (column_name);

Composite index

A composite index is based on an index created on two or more columns of a table.

CREATE INDEX index_name ON table_name (column1, column2);

Implicit index

An implicit index is an index automatically created by the database server when an object is created. Indexes are automatically created as primary key constraints and unique constraints.

Delete index

An index can be  deleted using SQLite's  DROP command. Pay special attention when deleting indexes, because performance may decrease or increase.

DROP INDEX index_name;

Under what circumstances should you avoid using indexes?

Although the purpose of indexing is to improve the performance of the database, there are several situations where you need to avoid using indexes. When using indexes, the following guidelines should be reconsidered:

  • Indexes should not be used on smaller tables.

  • Indexes should not be used on tables that are frequently updated or inserted in large batches.

  • Indexes should not be used on columns with a large number of NULL values.

  • Indexes should not be used on columns that are frequently operated on.

view

Create view

A view is nothing but a SQLite statement stored in the database with a related name. So when the original table changes, the result of the view will also change according to the corresponding result.

sqlite> select * from company;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0   
2           Allen       25          Texas       15000.0   
3           Teddy       23          Norway      20000.0   
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       85000.0   
6           Kim         22          Texas       45000.0   
8           Paul        24          Houston     20000.0   
9           James       44          Norway      5000.0    
10          James       45          Texas       5000.0    
11          Paul        32          California  20000.0   
sqlite> create view company_view as select id, name, age from company;
sqlite> select * from company_view;
ID          NAME        AGE       
----------  ----------  ----------
1           Paul        32        
2           Allen       25        
3           Teddy       23        
4           Mark        25        
5           David       27        
6           Kim         22        
8           Paul        24        
9           James       44        
10          James       45        
11          Paul        32        
sqlite> delete from company where id=11;
sqlite> select * from company_view;
ID          NAME        AGE       
----------  ----------  ----------
1           Paul        32        
2           Allen       25        
3           Teddy       23        
4           Mark        25        
5           David       27        
6           Kim         22        
8           Paul        24        
9           James       44        
10          James       45  

Delete view

To delete a view, just use  the DROP VIEW statement with  view_name (similar to the syntax for deleting a data table).

sqlite> DROP VIEW view_name;

Subquery

Subquery is also called inner query or nested query, which refers to the embedded query statement in the WHERE clause of SQLite query. The query result of a SELECT statement can be used as the input value of another statement.

Subqueries can be used with SELECT, INSERT, UPDATE, and DELETE statements, and can be accompanied by operators such as =, <, >, >=, <=, IN, BETWEEN, etc.

The following are the rules that subqueries must follow:

  • Subqueries must be enclosed in parentheses.

  • The subquery can only have one column in the SELECT clause, unless there are multiple columns in the main query to compare with the selected columns of the subquery.

  • ORDER BY cannot be used in subqueries, although the main query can use ORDER BY. You can use GROUP BY in a subquery, the function is the same as ORDER BY.

  • Subqueries return more than one row and can only be used with multi-value operators, such as the IN operator.

  • The BETWEEN operator cannot be used with subqueries, but BETWEEN can be used within subqueries.

sqlite> select * from company;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0   
2           Allen       25          Texas       15000.0   
3           Teddy       23          Norway      20000.0   
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       85000.0   
6           Kim         22          Texas       45000.0   
8           Paul        24          Houston     20000.0   
9           James       44          Norway      5000.0    
10          James       45          Texas       5000.0    

sqlite> select * from company where id in (select id from company where salary > 45000);
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       85000.0   

sqlite> select * from company where salary > 45000;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       85000.0 
sqlite> CREATE TABLE COMPANY_BKP(
   ...>    ID INT PRIMARY KEY     NOT NULL,
   ...>    NAME           TEXT    NOT NULL,
   ...>    AGE            INT     NOT NULL,
   ...>    ADDRESS        CHAR(50),
   ...>    SALARY         REAL    
   ...> );
sqlite> select * from company;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0   
2           Allen       25          Texas       15000.0   
3           Teddy       23          Norway      20000.0   
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       85000.0   
6           Kim         22          Texas       45000.0   
8           Paul        24          Houston     20000.0   
9           James       44          Norway      5000.0    
10          James       45          Texas       5000.0    
sqlite> INSERT INTO COMPANY_BKP SELECT * FROM COMPANY WHERE ID IN (SELECT ID FROM COMPANY) ;
sqlite> UPDATE COMPANY SET SALARY = SALARY * 0.50 WHERE AGE IN (SELECT AGE FROM COMPANY_BKP WHERE AGE >= 27 );
sqlite> select * from company;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  10000.0   
2           Allen       25          Texas       15000.0   
3           Teddy       23          Norway      20000.0   
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       42500.0   
6           Kim         22          Texas       45000.0   
8           Paul        24          Houston     20000.0   
9           James       44          Norway      2500.0    
10          James       45          Texas       2500.0 
sqlite> DELETE FROM COMPANY WHERE AGE IN (SELECT AGE FROM COMPANY_BKP WHERE AGE > 27 );
sqlite> select * from company;
ID          NAME        AGE         ADDRESS     SALARY    
----------  ----------  ----------  ----------  ----------
2           Allen       25          Texas       15000.0   
3           Teddy       23          Norway      20000.0   
4           Mark        25          Rich-Mond   65000.0   
5           David       27          Texas       42500.0   
6           Kim         22          Texas       45000.0   
8           Paul        24          Houston     20000.0 

Aggregate function

Serial number Function & description
1 COUNT function
COUNT aggregate function is used to count the number of rows in a database table.
2 MAX function
MAX aggregate function allows us to select the maximum value of a column.
3 MIN function The
MIN aggregate function allows us to select the minimum value of a column.
4 AVG function
AVG aggregate function calculates the average value of a column.
5 SUM function The
SUM aggregate function allows you to calculate the sum for a numeric column.
6 RANDOM function The
RANDOM function returns a pseudo-random integer between -9223372036854775808 and +9223372036854775807.
7 ABS function The
ABS function returns the absolute value of a numeric parameter.
8 UPPER function The
UPPER function converts a string to uppercase letters.
9 LOWER function The
LOWER function converts a string to lowercase letters.
10 LENGTH function The
LENGTH function returns the length of the string.
11 sqlite_version function The
sqlite_version function returns the version of the SQLite library.

Guess you like

Origin blog.csdn.net/TomorrowAndTuture/article/details/112861213