Skip to content

Commit 7769689

Browse files
SQLite tutorial contribution (ronreiter#704)
* The union statement * The Case Statement * Altering Tables * Advanced Joins * Subqueries * Logic Fixes * Indexing * INSERT INTO SELECT * String Operations * Views
1 parent b76744b commit 7769689

10 files changed

+1101
-0
lines changed

Diff for: tutorials/learnsqlonline.org/en/Altering Tables.md

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
Tutorial
2+
--------
3+
4+
Unlike SQL-standard and other database systems, SQLite supports a very limited functionality of the ALTER TABLE statement.
5+
6+
By using an SQLite ALTER TABLE statement, you can perform three actions:
7+
8+
Rename a table.
9+
Add a new column to a table.
10+
Rename a column
11+
12+
To rename a table, you use the following ALTER TABLE RENAME TO statement:
13+
14+
.exec
15+
CREATE TABLE Customer ( name TEXT,customerID INTERGER, city TEXT, email TEXT);
16+
17+
INSERT INTO Customer ( name, customerID, city,email) VALUES
18+
("John", 1, "Huston", NULL), ("Eric", 2, "Paris", "[email protected]"), ("Jessica",3, "Kiev", "[email protected]"),
19+
("Mike", 4, "Paris", "[email protected]"), ("Jeff", 5, NULL, "[email protected]"),("Ben", 6, NULL, "[email protected]");
20+
21+
ALTER TABLE Customer
22+
RENAME TO Customer_Alter;
23+
24+
SELECT *
25+
FROM Customer_Alter;
26+
27+
Something important to consider is if a table is referenced by views or statements in triggers, you must manually change the definition of views and triggers
28+
29+
30+
You can use the SQLite ALTER TABLE statement to add a new column to an existing table. In this scenario, SQLite appends the new column at the end of the existing column list
31+
32+
There are some restrictions on the new column:
33+
34+
The new column cannot have a UNIQUE or PRIMARY KEY constraint.
35+
36+
If the new column has a NOT NULL constraint, you must specify a default value for the column other than a NULL value.
37+
38+
The new column cannot have a default of CURRENT_TIMESTAMP, CURRENT_DATE, and CURRENT_TIME, or an expression.
39+
40+
If the new column is a foreign key and the foreign key constraint check is enabled, the new column must accept a default value NULL.
41+
42+
43+
.exec
44+
CREATE TABLE Customer ( name TEXT,customerID INTERGER, city TEXT, email TEXT);
45+
46+
ALTER TABLE Customer
47+
ADD COLUMN PhoneNum text
48+
49+
INSERT INTO Customer ( name, customerID, city,email) VALUES
50+
("John", 1, "Huston", NULL,1111), ("Eric", 2, "Paris", "[email protected]",2222), ("Jessica",3, "Kiev", "[email protected]",3333),
51+
("Mike", 4, "Paris", "[email protected]",4444), ("Jeff", 5, NULL, "[email protected]",5555),("Ben", 6, NULL, "[email protected]",6666);
52+
53+
SELECT *
54+
FROM Customer;
55+
56+
57+
SQLite added the support for renaming a column using ALTER TABLE RENAME COLUMN statement in version 3.20.0. Syntax is as follows:
58+
59+
.exec
60+
CREATE TABLE Customer ( name TEXT,customerID INTERGER, city TEXT, email TEXT);
61+
62+
INSERT INTO Customer ( name, customerID, city,email) VALUES
63+
("John", 1, "Huston", NULL), ("Eric", 2, "Paris", "[email protected]"), ("Jessica",3, "Kiev", "[email protected]"),
64+
("Mike", 4, "Paris", "[email protected]"), ("Jeff", 5, NULL, "[email protected]"),("Ben", 6, NULL, "[email protected]");
65+
66+
ALTER TABLE Customer
67+
RENAME COLUMN city TO location;
68+
69+
SELECT *
70+
FROM Customer;
71+
72+
Exercise
73+
--------
74+
Rename the suppliers table and show its contents
75+
76+
Tutorial Code
77+
-------------
78+
CREATE TABLE Supplier ( name TEXT,customerID INTERGER, city TEXT, stock INTERGER);
79+
80+
INSERT INTO Supplier ( name, customerID, city, stock) VALUES
81+
("John", 1, "Huston"1000), ("Eric", 2, "Paris",200),
82+
("Mike", 3, "Paris", 20000),("Ben", 4, "Tokio",500);
83+
84+
85+
-- write your code here
86+
87+
Expected Output
88+
---------------
89+
"John", 1, "Huston", 1000
90+
"Mike", 2, "Paris", 200
91+
"Eric", 3, "Kiev", 20000
92+
"Ben", 4, "Tokio", 500
93+
94+
Solution
95+
--------
96+
CREATE TABLE Supplier ( name TEXT,customerID INTERGER, city TEXT, stock INTERGER);
97+
98+
INSERT INTO Supplier ( name, customerID, city, stock) VALUES
99+
("John", 1, "Huston"1000), ("Eric", 2, "Paris",200),
100+
("Mike", 3, "Paris", 20000),("Ben", 4, "Tokio",500);
101+
102+
-- write your code here
103+
104+
ALTER TABLE Customer
105+
RENAME TO Customer_Alter;
106+
107+
SELECT *
108+
FROM Customer_Alter;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
Tutorial
2+
--------
3+
4+
The INSERT INTO SELECT statement copies data from one table and inserts it into another table, the statement requires that the data types in source and target tables match.
5+
Note that the records in the target table are unaffected.
6+
7+
The following is an example of this statement. Note that is specially useful if you want to do backups of tables.
8+
.exec
9+
CREATE TABLE contacts (
10+
first_name text NOT NULL,
11+
last_name text NOT NULL,
12+
email text NOT NULL
13+
);
14+
15+
16+
INSERT INTO contacts (first_name, last_name, email)
17+
VALUES('David','Brown','[email protected]'),
18+
('Johny','Doe','[email protected]'),
19+
('Lisa','Smith','[email protected]');
20+
21+
CREATE TABLE contacts_bkp (
22+
first_name text NOT NULL,
23+
last_name text NOT NULL,
24+
email text NOT NULL
25+
);
26+
27+
INSERT INTO contacts_bkp
28+
select * from contacts
29+
30+
Additionally, you can add a WHERE clause to the query to filter the target dataset
31+
32+
.exec
33+
CREATE TABLE contacts (
34+
first_name text NOT NULL,
35+
last_name text NOT NULL,
36+
email text NOT NULL,
37+
gender text NOT NULL
38+
);
39+
40+
41+
INSERT INTO contacts (first_name, last_name, email, gender)
42+
VALUES('David','Brown','[email protected]','M'),
43+
('Johny','Doe','[email protected]','M'),
44+
('Lisa','Smith','[email protected]','F');
45+
46+
CREATE TABLE contacts_bkp (
47+
first_name text NOT NULL,
48+
last_name text NOT NULL,
49+
email text NOT NULL
50+
gender text NOT NULL
51+
);
52+
53+
INSERT INTO contacts_bkp
54+
select * from contacts
55+
Where gender like 'F'
56+
57+
58+
Exercise
59+
--------
60+
Create a table and fill it just with the customer's table records, which names starts with the letter J
61+
62+
Tutorial Code
63+
-------------
64+
CREATE TABLE Customer ( name TEXT,customerID INTERGER, city TEXT, email TEXT);
65+
66+
INSERT INTO Customer ( name, customerID, city,email) VALUES
67+
("John", 1, "Huston", [email protected]), ("Eric", 2, "Paris", "[email protected]"), ("Jessica",3, "Kiev", "[email protected]"), ("Mike", 4, "Paris", "[email protected]"), ("Jeff", 5, Antananarivo, "[email protected]"),("Ben", 6, Antananarivo, "[email protected]");
68+
69+
-- write your code here
70+
71+
Expected Output
72+
---------------
73+
"John", 1, "Huston", [email protected]"
74+
"Jessica",3, "Kiev", "[email protected]"
75+
"Jeff", 5, Antananarivo, "[email protected]"
76+
77+
Solution
78+
--------
79+
CREATE TABLE Customer ( name TEXT,customerID INTERGER, city TEXT, email TEXT);
80+
81+
INSERT INTO Customer ( name, customerID, city,email) VALUES
82+
("John", 1, "Huston", [email protected]), ("Eric", 2, "Paris", "[email protected]"), ("Jessica",3, "Kiev", "[email protected]"), ("Mike", 4, "Paris", "[email protected]"), ("Jeff", 5, Antananarivo, "[email protected]"),("Ben", 6, Antananarivo, "[email protected]");
83+
84+
-- write your code here
85+
86+
CREATE TABLE Customer_J ( name TEXT,customerID INTERGER, city TEXT, email TEXT);
87+
88+
INSERT INTO Customer_J
89+
Select * From Customer
90+
Where name like 'J%';
91+
92+
Select * from Customer_J;

Diff for: tutorials/learnsqlonline.org/en/Indexing.md

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
Tutorial
2+
--------
3+
### What is an index?
4+
5+
In relational databases, a table is a list of rows. In the same time, each row has the same column structure that consists of cells. Each row also has a consecutive row-id sequence number used to identify the row. Therefore, you can consider a table as a list of pairs: (rowid, row).
6+
7+
Unlike a table, an index has an opposite relationship: (row, rowid). An index is an additional data structure that helps improve the performance of a query.
8+
9+
SQLite uses B-tree for organizing indexes. Note that B stands for balanced, B-tree is a balanced tree, not a binary tree.
10+
11+
The B-tree keeps the amount of data at both sides of the tree balanced so that the number of levels that must be traversed to locate a row is always in the same approximate number. In addition, querying using equality (=) and ranges (>, >=, <,<=) on the B-tree indexes are very efficient.
12+
13+
### SQLite CREATE INDEX statement
14+
To create an index, you use the CREATE INDEX statement with the following syntax:
15+
16+
CREATE [UNIQUE] INDEX index_name
17+
ON table_name(column_list);
18+
19+
To create an index, you specify three important information:
20+
21+
- The name of the index after the CREATE INDEX keywords.
22+
- The name of the table to the index belongs.
23+
- A list of columns of the index.
24+
25+
In case you want to make sure that values in one or more columns are unique like email and phone, you use the UNIQUE option in the CREATE INDEX statement. The CREATE UNIQUE INDEX creates a new unique index.
26+
27+
Suppose, you want to enforce that the email is unique, you create a unique index as follows:
28+
29+
.exec
30+
CREATE TABLE contacts (
31+
first_name text NOT NULL,
32+
last_name text NOT NULL,
33+
email text NOT NULL
34+
);
35+
36+
CREATE UNIQUE INDEX idx_contacts_email
37+
ON contacts (email);
38+
39+
INSERT INTO contacts (first_name, last_name, email)
40+
VALUES('David','Brown','[email protected]'),
41+
('Johny','Doe','[email protected]'),
42+
('Lisa','Smith','[email protected]');
43+
44+
If you want to insert a duplicated row, an error message will appear, indicating that the unique index has been violated. Because when you inserted the second row, SQLite checked and made sure that the email is unique across of rows in email of the contacts table.
45+
46+
If you query data from the contacts table based on a specific email, SQLite will use the index to locate the data
47+
48+
.exec
49+
CREATE TABLE contacts (
50+
first_name text NOT NULL,
51+
last_name text NOT NULL,
52+
email text NOT NULL
53+
);
54+
55+
CREATE UNIQUE INDEX idx_contacts_email
56+
ON contacts (email);
57+
58+
INSERT INTO contacts (first_name, last_name, email)
59+
VALUES('David','Brown','[email protected]'),
60+
('Johny','Doe','[email protected]'),
61+
('Lisa','Smith','[email protected]');
62+
63+
SELECT
64+
first_name,
65+
last_name,
66+
email
67+
FROM
68+
contacts
69+
WHERE
70+
email = '[email protected]';
71+
72+
To check if SQLite uses the index or not, you use the EXPLAIN QUERY PLAN
73+
74+
.exec
75+
CREATE TABLE contacts (
76+
first_name text NOT NULL,
77+
last_name text NOT NULL,
78+
email text NOT NULL
79+
);
80+
81+
CREATE UNIQUE INDEX idx_contacts_email
82+
ON contacts (email);
83+
84+
INSERT INTO contacts (first_name, last_name, email)
85+
VALUES('David','Brown','[email protected]'),
86+
('Johny','Doe','[email protected]'),
87+
('Lisa','Smith','[email protected]');
88+
89+
EXPLAIN QUERY PLAN
90+
SELECT
91+
first_name,
92+
last_name,
93+
email
94+
FROM
95+
contacts
96+
WHERE
97+
email = '[email protected]';
98+
99+
### SQLite multicolumn index
100+
If you create an index that consists of one column, SQLite uses that column as the sort key. In case you create an index that has multiple columns, SQLite uses the additional columns as the second, third, … as the sort keys.
101+
102+
SQLite sorts the data on the multicolumn index by the first column specified in the CREATE INDEX statement. Then, it sorts the duplicate values by the second column, and so on.
103+
104+
Therefore, the column order is very important when you create a multicolumn index.
105+
106+
To utilize a multicolumn index, the query must contain the condition that has the same column order as defined in the index.
107+
108+
### SQLite DROP INDEX statement
109+
To remove an index from a database, you use the DROP INDEX statement as follows:
110+
111+
DROP INDEX [IF EXISTS] index_name;
112+
113+
In this syntax, you specify the name of the index that you want to drop after the DROP INDEX keywords. The IF EXISTS option removes an index only if it exists.
114+
115+
Exercise
116+
--------
117+
Create an index for the following table, index by customerID. Then select all.
118+
119+
Tutorial Code
120+
-------------
121+
CREATE TABLE Customer ( name TEXT,customerID INTERGER, city TEXT, email TEXT);
122+
123+
INSERT INTO Customer ( name, customerID, city,email) VALUES
124+
("John", 1, "Huston", [email protected]), ("Eric", 2, "Paris", "[email protected]"), ("Jessica",3, "Kiev", "[email protected]"), ("Mike", 4, "Paris", "[email protected]"), ("Jeff", 5, Antananarivo, "[email protected]"),("Ben", 6, Antananarivo, "[email protected]");
125+
126+
-- write your code here
127+
128+
Expected Output
129+
---------------
130+
"John", 1, "Huston", [email protected]"
131+
"Eric", 2, "Paris", "[email protected]"
132+
"Jessica",3, "Kiev", "[email protected]"
133+
"Mike", 4, "Paris", "[email protected]"
134+
"Jeff", 5, Antananarivo, "[email protected]"
135+
"Ben", 6, Antananarivo, "[email protected]"
136+
137+
Solution
138+
--------
139+
CREATE TABLE Customer ( name TEXT,customerID INTERGER, city TEXT, email TEXT);
140+
141+
INSERT INTO Customer ( name, customerID, city,email) VALUES
142+
("John", 1, "Huston", [email protected]), ("Eric", 2, "Paris", "[email protected]"), ("Jessica",3, "Kiev", "[email protected]"), ("Mike", 4, "Paris", "[email protected]"), ("Jeff", 5, Antananarivo, "[email protected]"),("Ben", 6, Antananarivo, "[email protected]");
143+
144+
-- write your code here
145+
146+
CREATE UNIQUE INDEX idx_Cus_id
147+
ON Customer (customerID);
148+
149+
Select * from Customer

0 commit comments

Comments
 (0)