0% found this document useful (0 votes)
5 views

ExerciseSeries1-SQL at A Glance

The document outlines steps to create SQL databases and tables to store film and actor data, insert records, and perform queries. It demonstrates creating tables, inserting data, updating records, and performing joins to retrieve related data from multiple tables.

Uploaded by

seppeloeys
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

ExerciseSeries1-SQL at A Glance

The document outlines steps to create SQL databases and tables to store film and actor data, insert records, and perform queries. It demonstrates creating tables, inserting data, updating records, and performing joins to retrieve related data from multiple tables.

Uploaded by

seppeloeys
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Exercise series 1: SQL at a glance

1. Install MySQL. See 0.3.1 in the course.

2. Install SQLyog and connect to MySQL. See 0.3.2 in the course.

3. Create the database myfilms. View the database in the Object Browser.

4. Create the table films (filmid, title, year, score).

5. Add the following films.


filmid title year score
1 The Da Vinci Code 2006 4.2
2 The Circle 2017 4
3 The Professor and the Madman 2019 3.9

insert into myfilms.films (title, year, score) values ('The Da Vinci Code',
2006, 4.2);
insert into myfilms.films (title, year, score) values ('The Circle', 2017, 4);
insert into myfilms.films (title, year, score) values ('The Professor and the
Madman', 2019, 3.9);

6. Give all the films.

select *
from myfilms.films

7. Change the score of 'The Professor and the Madman' to 4.1.

update myfilms.films
set score = 4.1
where title='The Professor and the Madman'
CREATE TABLE actors ( actorid INT NOT NULL AUTO_INCREMENT PRIMARY
KEY, lastname CHAR(45) NOT NULL, firstname CHAR(30) NOT NULL, birthdate
DATE, )

8. Create the table actors (actorid, lastname, firstname, birthdate).

CREATE TABLE actors ( actorid INT NOT NULL AUTO_INCREMENT PRIMARY


KEY, lastname CHAR(45) NOT NULL, firstname CHAR(30) NOT NULL, birthdate
DATE, );

9. Add the following actors.


actorid Lastname firstname birthdate
1 Hanks Tom 9 July 1956
2 Zeta-Jones Catherine 25 September 1969
3 Tautou Audrey August 9, 1976
4 McKellen Ian
5 Watson Emma 15 April 1990
6 Gibson Mel 3 January 1956
7 Penn Sean 17 August1960
8 Dormer Natalie February 11, 1982

INSERT INTO actors(lastname, firstname, birthdate)


VALUES ('Hanks', 'Tom', '1956-07-09'),
('Zeta-Jones', 'Catherine', '1969-09-25'),
('Tautou', 'Audrey', '1976-08-09'),
('McKellen', 'Ian', NULL),
('Watson', 'Emma', '1990-04-15'),
('Gibson', 'Mel', '1956-01-03'),
('Penn', 'Sean', '1960-08-17'),
('Dormer', 'Natalie', '1982-02-11');

10. Give all the actors.

SELECT * FROM myfilms.actors

11. Change the birthdate of Ian McKellen: 25 May 1939.

UPDATE actors
SET birthdate = '1939-05-25'
WHERE firstname = 'Ian' AND lastname = 'McKellen';

12. Create the table players (filmid, actorid).

CREATE TABLE players (


filmid INT NOT NULL,
actorid INT NOT NULL,
PRIMARY KEY (filmid, actorid),
CONSTRAINT fk1_players FOREIGN KEY (filmid) REFERENCES films(filmid),
CONSTRAINT fk2_players FOREIGN KEY (actorid) REFERENCES actors(actorid)
)`mysql``myfilms`
13. Add the following data to the table players.
FilmId ActorId
1 (The Da Vinci Code) 1 (Tom Hanks)
1 (The Da Vinci Code) 3 (Audrey Tautou)
1 (The Da Vinci Code) 4 (Ian McKellen)
2 (The Circle) 1 (Tom Hanks)
2 (The Circle) 5 (Emma Watson)
3 (The Professor and the Madman) 6 (Mel Gibson)
3 (The Professor and the Madman) 7 (Sean Penn)
3 (The Professor and the Madman) 8 (Natalie Dormer)

INSERT INTO players(filmid, actorid)


VALUES (1, 1),
(1, 3),
(1, 4),
(2, 1),
(2, 5),
(3,6),
(3,7),
(3,8);

14. Give all the films with their actors.

SELECT *
FROM players
ORDER BY filmid

15. Add the next row to the table players. This does not work: explain the error message.
filmid actorid
1 (The Da Vinci Code) 55

Actorid 55 does not exists

16. Add the next row to the table players. This doesn't work either: explain.
filmid actorid
88 2

Filmid 88 does not exist

17. Add the next row to the table players. Explain why this doesn't work.
filmid actorid
2 5

The filmid is already linked to the actorid

18. Add the next row to the table actors. Explain why this doesn't work.
actorid Lastname firstname birthdate
9 Carter

Firstname is NOT NULL, so we need a value for it


19. Give all the films with their actors. Sort by score.

SELECT films.*, actors.* FROM films JOIN players ON films.filmid = players.filmid JOIN
actors ON players.actorid = actors.actorid ORDER BY score

20. In preparation for the following work seminars: register on SQL SPOJ with your Howest
email address and a specific password for SQL SPOJ. Note: when using SQL SPOJ at
home, first make a VPN connection.
http://sqlspoj.ti.howest.be/ or http://172.21.22.52

You might also like