A SQL statement is a single instruction to the database, written as a few keywords with names and values between them. This one reads every row and column of the Productions table from the example database:
SELECT * FROM Productions;How a statement reads
Section titled “How a statement reads”- Keywords such as
SELECT,FROMandWHEREsay what to do. They are not case sensitive, soselectworks as well asSELECT. Writing them in capitals is a convention that keeps them apart from table and column names. - Names pick out tables and columns, such as
ProductionsandTitle. Whether these are case sensitive depends on the database and how it is set up, so it is safest to write them exactly as they were created. - Values are the data you compare against or store. Text goes in single quotes, as in
'UK'; numbers do not. - Line breaks and spaces do not matter to the database. Splitting a long statement over several lines, one clause per line, makes it easier to read.
The semicolon
Section titled “The semicolon”A semicolon marks the end of a statement. Some tools need it only to separate several statements sent together; others expect it every time. Ending every statement with one works everywhere.
The commands you will use most
Section titled “The commands you will use most”| Command | What it does |
|---|---|
SELECT |
reads data |
INSERT INTO |
adds new rows |
UPDATE |
changes existing rows |
DELETE |
removes rows |
CREATE DATABASE |
creates a database |
CREATE TABLE |
creates a table |
ALTER TABLE |
changes a table’s structure |
DROP TABLE |
removes a table and all its data |
CREATE INDEX |
adds an index to speed up searches |
DROP INDEX |
removes an index |
The first four work on the data inside tables; the rest work on the tables and databases themselves.