SQLite SQL
What is SQL?
SQL stands for Structured Query Language. It is the language you use to talk to a database.
With SQL, you can get data from a database, add new data, change data and delete data.
Tables, Columns and Records
A database contains one or more tables. A table has columns, and each row in the table is called a record.
Below is a selection from the "Customers" table in the Northwind sample database:
| CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
|---|---|---|---|---|---|---|
| 1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
| 2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
| 3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
| 4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
| 5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |
The table above has seven columns, and each record is one customer.
SQL Statements
Everything you do with a database is done with SQL statements. This statement selects all the records in the Customers table:
- SQL keywords are not case sensitive:
selectis the same asSELECT. In this tutorial, we write keywords in upper case - End each statement with a semicolon. Then you can run more than one statement at a time
Some of the Most Important SQL Statements
| Statement | What it does |
|---|---|
SELECT | Gets data from a database |
INSERT INTO | Adds new data to a database |
UPDATE | Changes data in a database |
DELETE | Deletes data from a database |
CREATE TABLE | Creates a new table |
ALTER TABLE | Changes a table |
DROP TABLE | Deletes a table |
CREATE INDEX | Creates an index, to make searches faster |
DROP INDEX | Deletes an index |
Note: SQLite has no CREATE DATABASE statement. A new database is created when you open a file that does not exist. Read more in the SQLite Create DB chapter.
Text in Single Quotes
In SQLite, text values are written in single quotes, like 'Mexico'. Double quotes are used for the names of tables and columns.
This example selects the customers from Mexico. You will learn about WHERE in a later chapter: