You are on page 1of 5

Table: Crooks

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:

SELECT name, DoB --what to return


FROM crooks --where are you returning it from

This would return the following:

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):

SELECT ID, name, DoB


FROM crooks
WHERE town = 'Snape' AND gender = 'male' --Criteria

This would return the following:

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:

SELECT name, town, scars


FROM crooks
WHERE numScars >= 4 AND gender = 'female' --Criteria
This would return:

name town numScars


Kelly East Ham 10
Marea Wythenshawe 6

However, the police want to quickly sort through and see who is the most heavily scarred. We are
going to use an ORDER command:

SELECT name, town


FROM crooks
WHERE numScars >= 4 AND gender = 'female' --Criteria
ORDER BY numScars DESC --sorts the numScars values in big to small order

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!

ID name gender DoB town numScars


1 Geoff male 12/05/1982 Hull 8
2 Jane female 05/08/1956 York 8
3 Keith male 07/02/1999 Snape 8
4 Oliver male 22/08/1976 Blaxhall 8
5 Kelly female 11/11/1911 East Ham 8
Mare
6 female 14/07/1940 Wythenshawe 8
a

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:

INSERT INTO crooks


VALUES (1234,'Julie', 'female','12/12/1994','Little Maplestead',67)

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')

ID name gender DoB town numScars


1 Geoff male 12/05/1982 Hull 0
2 Jane female 05/08/1956 York 1
3 Keith male 07/02/1999 Snape 6
4 Oliver male 22/08/1976 Blaxhall 2
5 Kelly female 11/11/1911 East Ham 10
6 Marea female 14/07/1940 Wythenshawe 6
123
Julie female 12/12/1994 Little Maplestead 67
4
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:

DELETE FROM crooks


WHERE name = 'Geoff'

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:

 Create tables: CREATE TABLE

 Change the structure of a table: ALTER

 Delete tables: DROP


CREATE
You need to know what they all do (as listed above), though you only need to know how to
implement the CREATE TABLE command. Let's look at how we could have made the crooks table
above:

CREATE TABLE crooks


(
ID INTEGER PRIMARY KEY,
NAME VARCHAR(16),
GENDER VARCHAR(6),
DOB DATE,
TOWN VARCHAR(20),
NUMSCARS INTEGER
)

ALTER
An ALTER statement in SQL changes the properties of a table in a relational database without the
need to access the table manually.

ALTER TABLE crooks ADD convictions INTEGER


ALTER TABLE crooks DROP COLUMN convictions

DROP
Dropping a table is like dropping a nuclear bomb. It is irreversible and is frowned upon in modern
society.

DROP TABLE crooks

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.

Setting Primary Keys


Primary keys can be set after table creation via the alter statement.

ALTER TABLE Persons


ADD PRIMARY KEY (id)

Primary keys can also be set during table creation

CREATE TABLE users


(
user_id int NOT NULL,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
Address varchar(255),
PRIMARY KEY (user_id)
)

Setting Composite Keys


To set a primary key made up of two columns during table creation you could do something such as
this

CREATE TABLE users


(
user_id int NOT NULL,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
Address varchar(255),
CONSTRAINT pk_UserId PRIMARY KEY (user_id,username)
)

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:

ALTER TABLE users


ADD CONSTRAINT pk_UserID PRIMARY KEY (user_id,username)

You might also like