Introduction To SQL
Introduction To SQL
Tables (1)
Table Players
Tables (2)
Table Teams Table Matches
Table Penalties
Tables (3)
Table Committee_members
TEAMS
teamno smallint not null PK playerno smallint not null
MATCHES
matchno smallint not null PK teamno smallint not null playerno smallint not null won smallint not null lost smallint not null
MATCHES smallint <pk> smallint <fk1> smallint <fk2> smallint smallint not not not not not null null null null null TEAMS smallint <pk> not null TEAMNO not null PLAYERNO smallint not null char(6) DIVISION
COMMITTEE_MEMBERS PLAYERNO BEGIN_DATE END_DATE POSITION smallint <pk,fk> not null not null datetime <pk> null datetime null char(20)
Other constraints
Column value constraints:
SEX IN {'M','F'} WON, LOST IN {0,1,2,3} POSITION IN {'Chairman', 'Secretary', 'Treasurer', 'General member'}
SQL
SQL consists of: Data Definition Language (DDL)
CREATE TABLE, ALTER TABLE, DROP TABLE
SQL
SQL consists of: Data Definition Language (DDL)
CREATE TABLE, ALTER TABLE, DROP TABLE
SELECT-statement (1)
Example 8.5: Get the number, name, sex and birth date of each male player born after 1970. Sort the result by name and birth_date. Query:
playerno, name, sex, birth_date Players sex = M Year(birth_date) > 1970 name, birth_date
SEX M
BIRTH_DATE 1971-08-17
SELECT-statement (2)
Example 10.2: For each town find the number of players Query:
SELECT town, COUNT (*) FROM Players GROUP BY town;
SELECT-statement (3)
Example of section 6.2: Get the number of each player who has incurred more than one penalty with an amount more than 25.
Query:
SELECT FROM WHERE GROUP BY HAVING ORDER BY playerno Penalties amount > 25 playerno COUNT(*) > 1 playerno
SELECT-statement (4)
Example: List for each player who has incurred one or more penalties the playernumber and the number of penalties. Order descending on the number of penalties and playerno. Query:
SELECT FROM GROUP BY ORDER BY playerno, COUNT(*) Penalties playerno 2 DESC, 1
clauses SELECT-statement
defines the source tables selects rows that satisfy the condition(s) groups rows on base of equal values in columns selects groups that satisfy the condition(s) select column(s)
ORDER BY
logical operators
Example: Which male players joined in 1979 or 1980? List player number, name, sex and the year joined Query:
SELECT * FROM Players WHERE sex = M AND joined = 1979 OR joined = 1980
Result:
PLAYERNO 8 39 44 100 NAME Newcastle Bishop Baker Parmenter SEX F M M M JOINED 1980 1980 1980 1979
What is wrong?
Result:
TEAMNO NAME 1 Parmenter 2 Collins
Join condition
With pseudonyms:
SELECT FROM WHERE PAYMENTNO, AMOUNT, PEN.PLAYERNO, NAME, INITIALS PENALTIES PEN, PLAYERS P PEN.PLAYERNO = P.PLAYERNO;
With pseudonyms:
SELECT FROM PAYMENTNO, AMOUNT, PEN.PLAYERNO, NAME, INITIALS PENALTIES PEN INNER JOIN PLAYERS P ON PEN.PLAYERNO = P.PLAYERNO;
Alternative (note: the oldest player(s) is(are) those whose date of birth is less than or equal to that of every player, themselves included:
SELECT FROM WHERE PLAYERNO, NAME, BIRTH_DATE PLAYERS BIRTH_DATE <= ALL (SELECT BIRTH_DATE FROM PLAYERS);
Result:
PLAYERNO 2 NAME Everett BIRTH_DATE 1948-09-01
More DML
DML:
SELECT INSERT UPDATE DELETE
INSERT (1)
Example 15.1: A new team has enrolled in the league. This third team will be captained by player 100 and will compete in the third division. SQL statement:
INSERT INTO TEAMS (TEAMNO, PLAYERNO, DIVISION) VALUES (3, 100, 'third');
Also possible:
INSERT INTO TEAMS (TEAMNO, DIVISION, PLAYERNO) VALUES (3, 'third', 100);
Also possible:
INSERT INTO TEAMS VALUES (3, 100, 'third');
INSERT (2)
Example 15.2: Make a separate table in which the number, name, town and telephone number of each non-competition player is recorded. SQL statements:
CREATE TABLE (PLAYERNO NAME TOWN PHONENO PRIMARY KEY RECR_PLAYERS SMALLINT NOT NULL, CHAR(15) NOT NULL, CHAR(10) NOT NULL, CHAR(10) , (PLAYERNO) );
INSERT INTO RECR_PLAYERS SELECT PLAYERNO, NAME, TOWN, PHONENO FROM PLAYERS WHERE LEAGUENO IS NULL;
UPDATE
Example 15.7: The Parmenter family has moved house to 83 Palmer Street in Inglewood; the postal code has become 1234UU and the telephone number is unknown. SQL statement:
UPDATE SET PLAYERS STREET = 'Palmer Street',
HOUSENO
TOWN PHONENO WHERE NAME
= '83',
= 'Inglewood', = NULL = 'Parmenter';
POSTCODE = '1234UU',
DELETE
Example: Delete all penalties with an amount less than the average amount of penalties. Delete statement:
DELETE FROM WHERE PENALTIES AMOUNT < (SELECT FROM AVG (AMOUNT) PENALTIES);
SQL statement:
CREATE TABLE PLAYERS (PLAYERNO SMALLINT NAME CHAR(15) INITIALS CHAR(3) BIRTH_DATE DATE SEX CHAR(1) JOINED SMALLINT STREET CHAR(15) HOUSENO CHAR(4) POSTCODE CHAR(6) TOWN CHAR(10) PHONENO CHAR(10) LEAGUENO CHAR(4) PRIMARY KEY (PLAYERNO) CHECK (PLAYERNO >= 1) CHECK (SEX IN ('M', 'F')) CHECK (JOINED >= 1970) CHECK (YEAR (BIRTH_DATE) <= NOT NULL, NOT NULL, NOT NULL, , NOT NULL, NOT NULL, NOT NULL, , , NOT NULL, , , , , , , JOINED) );
ALTER TABLE
Nowadays many SQL products support foreign keys definition in ALTER TABLE statements after all tables have been defined:
CREATE TABLE PENALTIES (PAYMENTNO INTEGER NOT NULL, PLAYERNO SMALLINT NOT NULL, PAYMENT_DATE DATE NOT NULL, AMOUNT DECIMAL(7,2) NOT NULL, PRIMARY KEY (PAYMENTNO) , CHECK (PAYMENTNO >= 1) , CHECK (PAYMENT_DATE >= '1980-01-01'), CHECK (AMOUNT >= 0.00) );
ALTER TABLE PENALTIES ADD FOREIGN KEY (PLAYERNO) REFERENCES PLAYERS (PLAYERNO) ON UPDATE CASCADE ON DELETE CASCADE;
1.
2.
3.
4. List the player number of all players who won a match for team 1. Additional question: Display also the name of the player. 5. List the numbers of all players who have a total penalty amount of more than $100? Additional question: Display also the name of the player.
1.
2.
Which players have a total penalty amount of more than 100? List player number, name and the total penalty amount.
List for each penalty incurred by a team captain the payment number, the player number and the player name.
1.
List match number, player number and score of all matches played for team 2 by a female player. Remark: Use a query with subquery.
List the player number, name, initials of all players who played matches for team 1 and team 2. List the data of the most recent penalty. Give also the name and initials of the player involved. (This one is more advanced.) Can the next query be written as a join?
SELECT FROM WHERE PLAYERNO, NAME, INITIALS PLAYERS PLAYERNO NOT IN ( SELECT PLAYERNO FROM MATCHES WHERE TEAMNO = 1 );
2. 3. 4.