14
APPLY TESTING TECHNIQUES FOR SOFTWARE DEVELOPMENT ICTPRG529

DipDB_ICTPRG529_PowerPointSlides_Session4

Embed Size (px)

Citation preview

APPLY TESTING TECHNIQUES FOR SOFTWARE DEVELOPMENTICTPRG529

SESSION 4 OVERVIEW

• Primary key, foreign key revision

• Primary keys

• Foreign keys

• Compound primary keys 

• Key relational database concepts

• Referential integrity

• Orphan records

• Linking tables in script via Foreign Key constraints

• Foreign key constraint

• On delete, on update options

• Ordering of Drop table statements when foreign key constraints exist

• Practical tasks

Global Holidays relational database mud map

Global Holidays normalised tables

GlobalHolidays database:Countries (CountryID, Country)Cities (CityID, City, CountryID)Guests (GuestID, [FirstName], [LastName], [Gender{M,F}], [CountryID], JoinDate, [Email], [Password])Hotels (HotelID, HotelName, CityID, [ParkingSpaces], [PetFriendly], [JoinDate], [HotelViewDesc])HotelPhotos (HotelPhotoID, HotelID, HotelPhotoName, MainDisplayPhoto{Y,N})BedTypes (BedTypeID, BedTypeDesc)Rooms (HotelID, HotelRoomNum, BedTypeID, [RoomDesc], [StandardPrice], [Sleeps])Bookings (BookingID, GuestID, HotelID, HotelRoomNum, Checkin, Checkout, [NumGuests]) Notes:- Table names are in bold- Primary keys are underlined- Foreign keys are not explicitly labelled as foreign keys- Columns that can have NULLS i.e. it is optional to enter data in them, are in [square brackets]- Columns where the data entered can only be one of the listed alternatives are in {curly brackets separated by a comma} e.g. Gender {M,F} means a user can only enter a ‘M’, or a ‘F’ (M=Male, F=Female)

Foreign keys

- Which team does James Faulkner play for?

- Who plays for the Perth Scorchers?

- What is the name of any players from Afghanistan?

- Which team/s do the players from England play for?

Compound primary keys

- How many runs did Glenn Maxwell score in the game on the 4th Dec 2015?

- How many balls did Brad Hogg face in the game on the 2nd Dec 2015?

- A primary key column must never have any values in it repeat

- The PlayerID column is NOT the primary key of the Batting table. It is a part of the primary key for the Batting table. The primary key for the Batting table is the 2 columns combined - PlayerID and GameID. The PlayerID column on its own in the Batting table is however a foreign key. This is because it is the entire primary key of a separate table i.e. the Players table.

Foreign keys, referential integrity, orphan records

- BookingID 4 is for GuestID 7, Peter Stevenson. BookingID 10 is for GuestID 11, Penny Jackson. But who is BookingID 12 for?

- Orphan record

- Referential integrity

IF we had foreign key constraints in place:- if an orphan record was going to be created by

inserting a new row for BookingID 12 into the Booking table, then MySQL would have checked that GuestID 8 existed first. If GuestID 8 did not exist, MySQL would have given an error and BookingID 12 would never have been inserted.

- if BookingID 12 and GuestID 8 existed already, and if the orphan record was going to be created by deleting GuestID 8 from the Guests table, then MySQL would have first checked to see if GuestID 8 had any bookings. If so, MySQL would either have given an error message and stopped the deletion of GuestID 8, or MySQL would have deleted BookingID 12 from the Booking table before deleting GuestID 8 from the Guests table. The decision for which of these options MySQL would take, depends on what you specify in the foreign key constraint on the create table statement for the Guests table.

- if BookingID 12 already existed and if GuestID 8 did not exist. If the orphan record was going to be created by updating a valid existing GuestID in the bookings table to GuestID 8, when GuestID 8 does not exist in the Guests table, MySQL would have given an error and the GuestID in the bookings table would never have been changed.

• Foreign key (foreign_key_column_from_this_table) references other_table_name (primary_key_column_from_the_other_table)

• CREATE TABLE Countries ( CountryID smallint NOT NULL AUTO_INCREMENT, Country varchar(45) NOT NULL, PRIMARY KEY (CountryID));

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

-- Table structure for table Cities

• CREATE TABLE Cities ( CityID int NOT NULL AUTO_INCREMENT, City varchar(45) NOT NULL, CountryID smallint NOT NULL, PRIMARY KEY (CityID), FOREIGN KEY (CountryID) REFERENCES Countries (CountryID) ON DELETE NO ACTION ON UPDATE NO ACTION);

• Following the words ‘Foreign key’ is the name of the column that is the foreign key in this table that is

being created

• Following the word ‘references’ is the name of the other table that this foreign key is going to connect to

• Following the other table name, in brackets is the name of the primary key column/s that the foreign key

is going to connect

Foreign key constraints

• Foreign key (foreign_key_column_from_this_table) references other_table_name (primary_key_column_from_the_other_table) on delete option on update option

• Option can be either RESTRICT or CASCADE or SET NULL or NO ACTION

• Restrict: this is the default for both Delete and Update

• Cascade: if a country is deleted, all cities for that country would also

be automatically deleted without warning.

• Set Null: if a country is deleted, all cities for that country would

automatically have their CountryID values changed to NULL.

• No Action: ‘No action’ is the same as ‘Restrict’ except that it can be

deferred till you do some other action first and then it will treat it like a

‘Restrict’. Don’t use this option unless in rarer circumstances you have

a need for it.

Foreign key constraints (cont’d)

• CREATE TABLE Countries ( CountryID smallint NOT NULL AUTO_INCREMENT, Country varchar(45) NOT NULL, PRIMARY KEY (CountryID));

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

• CREATE TABLE Cities ( CityID int NOT NULL AUTO_INCREMENT, City varchar(45) NOT NULL, CountryID smallint NOT NULL, PRIMARY KEY (CityID), FOREIGN KEY (CountryID) REFERENCES Countries (CountryID));

• Drop tables in the reverse order of how they are created when using foreign key constraints

• Multiple foreign key constraints and compound foreign keys

• CREATE TABLE Bookings ( BookingID int NOT NULL AUTO_INCREMENT, GuestID int NOT NULL, HotelID int NOT NULL, HotelRoomNum varchar(10) NOT NULL, Checkin date NOT NULL, Checkout date NOT NULL, NumGuests tinyint, PRIMARY KEY (BookingID), FOREIGN KEY (GuestID) REFERENCES Guests (GuestID), FOREIGN KEY (HotelID) REFERENCES Hotels (HotelID), FOREIGN KEY (HotelID, HotelRoomNum) REFERENCES Rooms (HotelID, HotelRoomNum));

Foreign key constraints (cont’d)

The Global holidays database data (1)

Cities

Hotels

Countries

HotelPhotos

The Global holidays database data (2)Rooms

BedTypes

The Global holidays database data (3)Guests

Bookings

ANY QUESTIONS