Jenny Jirathammakul High Distinction Assignment Database Principles Autumn, 2007 Melbourne City Toyota
: Melbourne City Toyota This database is based on the Melbourne City Toyota website: Allows users to search the website for New Toyotas, Used Cars or Demonstration Cars. The Cars are sorted into Car Brands that is further arranged into the Car Style to make it easier to browse through the different categories. Extras are also included, such as Accessories and Parts, with New Cars if the User wished to look through additional things for a particular car. Enquiries by Users concerning the Cars are sorted by the CarID. This is to ensure that the Users’ enquiries would be specific to the particular car.
: Melbourne City Toyota
: Entity Relationship Diagram
: One to Many Relationship – 1:M CarBrandIDCarBrandCarModelCarMakeCarBody 1ToyotaRAV4CV4WD 2ToyotaCamrySportivoSedan 6HondaAccordEXI 4WSSedan 8LexusIS200SportsSedan 11SuzukiSwiftCinoHatchback CarID CarBrandID* CarYearCarOwnTypeColourKilometresListPriceInternetPriceRegistration N NewSilver31990 U UsedLight Gold ULS948 U UsedLight Teal UPZ685 N UsedBlack33000 U UsedWhite ULJ121 Primary Key Foreign Key* This 1:M relationship shows that one Car Style has one or many cars with that style CarBrandID 1 & 2 show this association CarStyle Car
: Many to Many Relationship – M:M CarIDCarBrandID*CarYearCarOwnTypeColourKilometresListPriceInternetPriceRegistration N NewSilver31990 U UsedLight Gold ULS948 U UsedLight Teal UPZ685 N NewBlack33000 U UsedWhite ULJ121 CarID*AccessoryID*PartsID* N N N N AccessoryIDAccessoryNameAccessoryPrice 1Safety Pack750 2Metallic Paint300 9New Camry Bootlip Spoilers550 13Leather Trim Moonroof1650 Car Extras Accessory This M:M relationship shows that many cars can have many accessories because: A Car can have many Accessories and; An Accessory can be apart of many Cars
: Simple Query of a Single Table CarBrandIDCarBrandCarModelCarMakeCarBody 6HondaAccordEXI 4WSSedan 7HondaHRVSport4WD select * from carstyle where carbrand = ‘Honda’ order by carbrandid; List all cars manufactured by Honda select accessoryname, accessoryprice from accessory where accessoryname like ‘G%’; Show all Accessory Names starting with ‘G’ AccessoryNameAccessoryPrice Genuine Toyota Park Assist595 Genuine Toyota Protection Packs180 Genuine Toyota Satellite Navagation System3500
: Query – Natural Join select enquiriesid, carid, username, carowntype, question from users natural join enquiries natural join car where carid = ‘U37975’; Show EnquiriesID, CarID, UserName, CarOwnType and Question for the car ‘U37975’ EnquiriesIDCarIDUserNameCarOwnTypeQuestion 3U37975NicoleUsed Would the car get the same kind of since as a newly bought Toyota since it has only been used for a while 1U37975TonyUsedWhat is the average Fuel consumption rate for this car
: Cross-Product Join select carid, carbrand, carmodel, carmake, caryear from car, carstyle where car.carbrandid = carstyle.carbrandid and carbody = ‘Hatchback’; Show the carid, carbrand, carmodel and carmake, caryear of all cars that are hatchbacks CarIDCarBrandCarModelCarMakeCarYear N12345SuzukiSwiftCino1998 U36892ToyotaYarisYR2007
: Group By Find out how many CarBrandIDs have registrations (that is, their registration is not null) select carbrandid, count(*) from car where registration is not null group by carbrandid; CarBrandIDCount
: Group By with Having Display car colours that have more than one car having that colour select colour, count(*) as total from car group by colour having count(*) > 1; ColourTotal Silver3 Black3 White4
: Sub-Query List the carid, carbrand, carmodel, carmake, kilometres of the car with the minimum amount of kilometres driven that was manufactured before the year 2000 select carid, carbrand, carmodel, carmake, kilometres from car natural join carstyle where caryear < 2000 and kilometres <= (select min(kilometres) from car where caryear < 2000); CarIDCarBrandCarModelCarMakeKilometres U38412SuzukiSwiftCino95876
: Self-Join List the CarID of cars that have the same colour ‘Black’ select c1.colour, c2.carid from car c1, car c2 where c1.carid = ‘U38523’ and c1.colour = c2.colour; ColourCarID BlackN24689 BlackU38523 BlackU37947
: Data Integrity – CHECK Statements These CHECK statements make sure that the values entered into the INSERT statements do not breach the restrictions placed upon them CONSTRAINTCHECK CONSTRAINT Car_Year CHECK ((CarYear > 1900) AND (CarYear <= 2007)), CONSTRAINTCHECK CONSTRAINT Car_ListPrice CHECK (ListPrice > 0), CONSTRAINTCHECK CONSTRAINT Car_InternetPrice CHECK ((InternetPrice > 0) OR (InternetPrice IS NULL)) This CHECK statements make sure that the values entered into the INSERT statements include only these types of values, that is, the only values that can be entered into Car Brand are only those types of car brands to ensure that misspellings do not occur CONSTRAINTCHECK CONSTRAINT CarStyle_Brand CHECK (CarBrand IN ( 'Alfa Romeo','Audi','BMW','Chrysler','Citroen', 'Daewoo','Daihatsu','Ford','Holden','Honda', 'Hyundai','Isuzu','Jeep','Land Rover','Lexus', 'Mazda','Mercedes-Benz','Mitsubishi','Nissan', 'Peugeot','Proton','Renault','Saab','Smart', 'Subaru','Suzuki','Toyota','Volkswagen','Volvo')) Check Statements prevents the SQL from creating data that does not conform to the guidelines that have been put into place and an error would thus occur because of a breach of the contraints
: Action Statements – ON DELETE RESTRICT “ON DELETE RESTRICT” prevents the deletion of data that is being used by other categories in another table These Restrictions prevent the deletion of AccessoryIDs and PartsIDs if they are currently being used by other categories in another table CONSTRAINT ExtrasFK_aid FOREIGN KEY (AccessoryID) REFERENCES Accessory (AccessoryID) ON DELETE RESTRICT ON UPDATE CASCADE, CONSTRAINT ExtrasFK_pid FOREIGN KEY (PartsID) REFERENCES Parts (PartsID) ON DELETE RESTRICT ON UPDATE CASCADE
: Action Statements – ON DELETE CASCADE “ON DELETE CASCADE” allows data to be deleted from a table that will delete itself in another table as well This deletion of CarID in a table will also be deleted in the extras table also CONSTRAINT ExtrasFK_cid FOREIGN KEY (CarID) REFERENCES Car (CarID) ON DELETE CASCADE ON UPDATE CASCADE,
: Creating a View create view specs (carid, year, brand, model, make, kms, owntype, rego) as select carid, caryear, carbrand, carmodel, carmake, kilometres, carowntype, registration from car, carstyle where car.carbrandid = carstyle.carbrandid and carbody = ‘4WD’; Create a view that shows the car style, year, kilometres, ownership type and registration of 4WDs select * from specs; CarIDYearBrandModelMakeKmsOwnTypeRego N ToyotaRAV4CVNew U FordTerritoryTS47088UsedSZS937 U HondaHRVSport123000UsedPUR303 U ToyotaRAV4CV75324UsedULS948 U ToyotaRAV4CV7499UsedUPZ685
: Querying a View select * from specs where brand <>‘Toyota’; Show all 4WDs that are not Toyota from the created view ‘specs’ CarIDYearBrandModelMakeKmsOwnTypeRego U FordTerritoryTS47088UsedSZS937 U HondaHRVSport123000UsedPUR303