Database SQL
Database SQL
The SELECT statement allows you to ask the database a question (Query it), and specify what data
it returns. We might want to ask something like Tell me the name and ages of all the crooks. Of
course this wouldn't work, so we need to put this into a language that a computer can understand:
Structured Query Language or SQL for short:
name DoB
Geoff 12/05/1982
Jane 05/08/1956
Keith 07/02/1999
Oliver 22/08/1976
Kelly 11/11/1911
Marea 14/07/1940
But suppose we wanted to filter these results, for instance: Tell me the ID, name and ages of all the
crooks who are male and come from Snape. We need to use another statement, the WHERE
clause, allowing us to give the query some criteria (or options):
I
name DoB
D
3 Keith 07/02/1999
Say the police knew that a crime had been committed by a heavily scarred woman (4+ scars), they
want a list of all the scarred women:
However, the police want to quickly sort through and see who is the most heavily scarred. We are
going to use an ORDER command:
ORDER BY numScars sorts your returned data into DESCending (big to small) or ASCending (small
to big) order
Database aren't always perfect and there may be times that we want to change the data inside our
database. For example in Facebook if someone is annoying you and you limit their access to your
profile, you'd update the access field from 'normal' to 'restricted'. Or if one of our crooks gets an
additional scar you'd have to update the numScars field. Let's take a look at that example, where our
crook Peter gains a scar on his right cheek. This was his initial state:
name: Peter
numScars: 7
UPDATE crooks
SET numScars = 8
But we have a problem here, this statement updates all records to numScars = 8. This means that
every crook will now have 8 scars!
We need to specify which crooks we want to update by using a WHERE clause, you saw it earlier in
the SELECT example.
UPDATE crooks
SET numScars = 8
WHERE name = "Peter" --only updates those people who are called Peter
We might also want to add new things to our database, for example when we are adding new
Criminal records or a new friendship link on Facebook. To add new records we use the INSERT
command with values of the fields we want to insert:
Sometimes we might not want to insert all the fields, some of them might not be compulsory:
INSERT INTO crooks (ID, name, town) --specific fields to insert into
VALUES (999, 'Frederick', 'Shotley')
Sometimes you might fall out with friends on Facebook so that you don't even want them to see your
restricted page. You'd want to delete your relationship (it's actually likely that Facebook retains the
connection but flags it as 'deleted', that isn't important here). The police might also find that a crook
is in fact innocent and then want to delete their criminal record. We need a DELETE command to do
all of these things, to permanently delete records from databases. Imagine that we find out that
Geoff was framed and he is completely innocent:
DDL
Data Definition Language (DDL) is a way to adjust the structure of a database. You might have
created databases in the past using a GUI such as Access or even MySQL. DDL allows you to
create databases from pure code including the ability to:
ALTER
An ALTER statement in SQL changes the properties of a table in a relational database without the
need to access the table manually.
DROP
Dropping a table is like dropping a nuclear bomb. It is irreversible and is frowned upon in modern
society.
By running this line of code, the table "crooks" will be removed from the database with no chance of
it being recovered unless backups have been previously made.
Where the constraint name would be UserId and the table's primary key would be made up of the
user_id and the username columns.
This could also be done after table creation: