Aims:
Sudbury Bird Sanctuary operates a Fund-A-Fowl scheme whereby members of the public can help pay for the birds' upkeep.
birds | ||
---|---|---|
num | name | species |
1 | Stevie | swan |
2 | Mandy | moorhen |
3 | Derek | duck |
4 | Dervla | duck |
sponsors | ||
---|---|---|
num | name | address |
1 | Hugh Jeegoh | Dublin |
2 | Donna Kebab | Cork |
3 | Ann O Domini | Dublin |
donations | ||
---|---|---|
snum | bnum | donation |
1 | 1 | 256 |
1 | 4 | 123 |
3 | 4 | 562 |
CREATE DATABASE database_name;
(We've done this for you!)
DROP DATABASE database_name;
(Don't do this to the one we created for you!)
CREATE
, DROP
, etc.) are not case-sensitive, but your
table and column names are
CREATE TABLE table_name (
column_name1 data_type,
column_name2 data_type,
...
PRIMARY KEY (column_names)
);
int
and varchar(n)
(strings of
max. length n)
DROP TABLE table_name;
CREATE TABLE birds ( num int AUTO_INCREMENT, name varchar(30), species varchar(25), PRIMARY KEY (num) ); CREATE TABLE donations ( snum int, bnum int, donation int, PRIMARY KEY(snum, bnum) );
INSERT INTO table_name (column_name1, column_name2, ...)
VALUES (value1, value2, ...);
DELETE FROM table_name
WHERE column_name = some_value;
INSERT INTO birds (name, species) VALUES ('Stevie', 'swan'); INSERT INTO birds (name, species) VALUES ('Mandy', 'moorhen'); INSERT INTO donations (snum, bnum, donation) VALUES (100, 1, 256);
UPDATE
command for changing values in rows
SELECT column_names
FROM table_names
WHERE conditions
These are called queries
SELECT * FROM...
to retrieve all columnsSELECT DISTINCT column_names FROM ...
to return
only distinct values=, <>, <, <=, >, >=,
...AND
and OR
Paraphrase into English the following SQL queries:
SELECT * FROM sponsors WHERE address = 'Dublin'; SELECT snum FROM donations WHERE donation < 300; SELECT bnum FOM donations WHERE donation < 300;
Write SQL queries to retrieve the following:
sponsors
with each row from donations
(in that order):
SELECT * FROM sponsors, donations;
SELECT * FROM sponsors, donations;
WHERE sponsors.num = donations.snum;
Paraphrase into English the following SQL queries:
SELECT name FROM sponsors, donations WHERE sponsors.num = donations.snum; SELECT DISTINCT name FROM sponsors, donations WHERE sponsors.num = donations.snum AND bnum = 5; SELECT sponsors.name, birds.name FROM sponsors, donations, birds WHERE sponsors.num = donations.snum AND donations.bnum = birds.num;
Write SQL queries to retrieve the following: