fix display lab (#15)

This commit is contained in:
Rémi Heredero authored and GitHub committed 2024-02-29 15:27:08 +01:00
commit ae4d5b8f33
1 file changed
+63 -26
+63 -26
View File
@@ -9,7 +9,8 @@ Lab databases
# Queries
#### 1) the description of all Function
### Query 1
The description of all Function
```sql
SELECT `description` FROM `Function`
@@ -42,8 +43,10 @@ SELECT `description` FROM `Function`
> "Select the source of the input (BD, DVD, HDMI, ...)" <br>
> "Select the Audio format to use with the current source (2ch stereo, 5.1, 7.2, 9.2, ...)" <br>
----------
#### 2) the `name` of manufacturers that have at least one entry in the `Model` table
### Query 2
The `name` of manufacturers that have at least one entry in the `Model` table
```sql
SELECT `Manufacturer`.`manufacturerName`
FROM `Manufacturer`
@@ -58,9 +61,10 @@ WHERE EXISTS( SELECT *
> "Sony" <br>
> "Onkyo"
----------
#### 3) the `modelName` of device models together with the `manufacturerName` of their manufacturers
### Query 3
The `modelName` of device models together with the `manufacturerName` of their manufacturers
```sql
SELECT `Model`.`modelName`, `Manufacturer`.`manufacturerName`
FROM `Model`, `Manufacturer`
@@ -76,7 +80,10 @@ WHERE `Model`.`manufacturerID` = `Manufacturer`.`manufacturerID`
> "UBP-X800M2","Sony" <br>
> "TX-RZ70","Onkyo"
#### 4) the description of the Function with functionLabel “Stop”
----------
### Query 4
The description of the Function with functionLabel “Stop”
```sql
SELECT `Function`.`description`
FROM `Function`
@@ -86,7 +93,10 @@ WHERE `Function`.`functionLabel` like 'Stop'
> "description" <br>
> "Stops the current track"
#### 5) the `functionLabel` for `Function` with `functionID` > 10 and with a `description` containing the word “`preset`” (SQL wildcard character is “`%`”)
----------
### Query 5
The `functionLabel` for `Function` with `functionID` > 10 and with a `description` containing the word “`preset`” (SQL wildcard character is “`%`”)
```sql
SELECT `Function`.`functionLabel`
FROM `Function`
@@ -99,7 +109,10 @@ WHERE `Function`.`functionID` > '10' AND `Function`.`description` like '%preset%
> "Preset 3" <br>
> "Save preset"
#### 6) the product of the tables `Code` and `FunctionList` (with and without the `JOIN` keyword), as well as the number of entries in the resulting product table
----------
### Query 6
The product of the tables `Code` and `FunctionList` (with and without the `JOIN` keyword), as well as the number of entries in the resulting product table
<b><u>With JOIN keyword</u></b><br>
26 elements in 0,0003 seconds
```sql
@@ -174,7 +187,10 @@ WHERE `Code`.`codeID` = `FunctionList`.`codeID`;
> "1948573645","4","6" <br>
> "1948573646","1","8"
#### 7) All `codeValue` for `modelID 4`
----------
### Query 7
All `codeValue` for `modelID 4`
```sql
SELECT `Code`.`codeValue`
FROM `Code`
@@ -193,7 +209,10 @@ WHERE `FunctionList`.`modelID` = 4;
> "1164736495" <br>
> "1164736496"
#### 8) The number of `codeValue` for `modelID 4`
----------
### Query 8
The number of `codeValue` for `modelID 4`
```sql
SELECT COUNT(`Code`.`codeValue`)
FROM `Code`
@@ -204,7 +223,10 @@ WHERE `FunctionList`.`modelID` = 4;
> [!NOTE|label:Result]
> 8
#### 9) All `codeValue` and `functionLabel` for Model with `modelName “SDi-Tuner-01”`
----------
### Query 9
All `codeValue` and `functionLabel` for Model with `modelName “SDi-Tuner-01”`
```sql
SELECT `Code`.`codeValue`, `Function`.`functionLabel`
FROM `Model`
@@ -224,7 +246,10 @@ WHERE `Model`.`modelName` like 'SDi-Tuner-01'
> "1164736495","Tune down" <br>
> "1164736496","Tune up"
#### 10) The number of `Function` items per `modelName`
----------
### Query 10
The number of `Function` items per `modelName`
```sql
SELECT `Model`.`modelName`, COUNT(`FunctionList`.`functionID`)
FROM `Model`
@@ -240,8 +265,10 @@ GROUP BY `Model`.`modelName`
> "UBP-X800M2","0" <br>
> "TX-RZ70","0"
----------
#### 11) All `functionLabel` and `description` with their corresponding `modelID`, `functionID` and `codeID`. Be sure to include all `functionLabel` even if they are not used in any device !
### Query 11
All `functionLabel` and `description` with their corresponding `modelID`, `functionID` and `codeID`. Be sure to include all `functionLabel` even if they are not used in any device !
```sql
SELECT `Function`.`functionLabel`, `Function`.`description`, `FunctionList`.`modelID`, `FunctionList`.`codeID`
FROM `FunctionList`
@@ -277,8 +304,10 @@ RIGHT JOIN `Function` ON `FunctionList`.`functionID` = `Function`.`functionID`;
> "Input source","Select the source of the input (BD, DVD, HDMI, ...)",NULL,NULL <br>
> "Audio format","Select the Audio format to use with the current source (2ch stereo, 5.1, 7.2, 9.2, ...)",NULL,NULL
----------
#### 12) All `functionLabel` and `description` of non-used functions in any device. (insert beforehand a new function in `Function`, with no reference in the `FunctionList` table). Add an unused function in the CSV file
### Query 12
All `functionLabel` and `description` of non-used functions in any device. (insert beforehand a new function in `Function`, with no reference in the `FunctionList` table). Add an unused function in the CSV file
```sql
SELECT `Function`.`functionLabel`, `Function`.`description`
FROM `FunctionList`
@@ -291,18 +320,12 @@ WHERE `FunctionList`.`modelID` is NULL
> "Audio format","Select the Audio format to use with the current source (2ch stereo, 5.1, 7.2, 9.2, ...)" <br>
> "Test Function","This function should not be referenced in the FunctionList"
#### 13) Add a VIEW to your DB to list all HEVs appliances with their `manufacturerName`, `modelName`, `typeLabel`, `functionLabel` and `codeValue`
```sql
```
> [!NOTE|label:Result]
>
----------
# Test constraints
#### 1) Try to insert a record in a table using an existing primary key.
### Test 1
Try to insert a record in a table using an existing primary key.
```sql
INSERT INTO `Code`(`codeId`, `codeValue`)
VALUES (1, 42)
@@ -310,7 +333,10 @@ VALUES (1, 42)
> [!NOTE|label:Result]
> #1062 - Duplicate entry '1' for key 'Code.PRIMARY'
#### 2) Choose a table with at least a foreign key. Try to insert a record in that table, with the foreign key pointing to a non-existent record.
----------
### Test 2
Choose a table with at least a foreign key. Try to insert a record in that table, with the foreign key pointing to a non-existent record.
```sql
INSERT INTO `Model`(`modelId`, `manufacturerID`, `typeID`, `modelName`)
VALUES (7, 1, 42, 'dummy test')
@@ -318,7 +344,10 @@ VALUES (7, 1, 42, 'dummy test')
> [!NOTE|label:Result]
> #1452 - Cannot add or update a child row: a foreign key constraint fails (`sdi04`.`Model`, CONSTRAINT `Model_ibfk_2` FOREIGN KEY (`typeID`) REFERENCES `Type` (`typeID`) ON DELETE RESTRICT ON UPDATE RESTRICT)
#### 3) Try to delete the record with name “`HEVs`” in the table `Manufacturer`. Explain why this operation fails.
----------
### Test 3
Try to delete the record with name “`HEVs`” in the table `Manufacturer`. Explain why this operation fails.
```sql
DELETE FROM `Manufacturer`
WHERE `Manufacturer`.`manufacturerName` like 'HEVs'
@@ -326,7 +355,10 @@ WHERE `Manufacturer`.`manufacturerName` like 'HEVs'
> [!NOTE|label:Result]
> #1451 - Cannot delete or update a parent row: a foreign key constraint fails (`sdi04`.`Model`, CONSTRAINT `Model_ibfk_1` FOREIGN KEY (`manufacturerID`) REFERENCES `Manufacturer` (`manufacturerID`) ON DELETE RESTRICT ON UPDATE RESTRICT)
#### 4) Modify the foreign key definition in `Model` so that “`HEVs`” entry in `Manufacturer` can be deleted. The foreign key `manufacturerID` value in `Model` should be set to `NULL`. Test that it works as expected.
----------
### Test 4
Modify the foreign key definition in `Model` so that “`HEVs`” entry in `Manufacturer` can be deleted. The foreign key `manufacturerID` value in `Model` should be set to `NULL`. Test that it works as expected.
```sql
ALTER TABLE `Model` CHANGE `manufacturerID` `manufacturerID` INT NULL;
@@ -342,7 +374,10 @@ WHERE `Manufacturer`.`manufacturerName` like 'HEVs'
> [!NOTE|label:Result]
> 1 row deleted
#### 5) We want now to delete all `Model` entries somehow related to a manufacturer when the latter is deleted from `Manufacturer`. To implement this feature, we should add a constraint `ON DELETE CASCADE` for the `manufacturerID` foreign key in `Model`. Try this and notice that this does not work. The reason is the following: you are not allowed to delete an entry in `Model` because a `FunctionList` refers to that entry. You could use `ON DELETE CASCADE` for all foreign keys, but deleting a single `Manufacturer` can trigger deletion of a good portion of the database!
----------
### Test 5
We want now to delete all `Model` entries somehow related to a manufacturer when the latter is deleted from `Manufacturer`. To implement this feature, we should add a constraint `ON DELETE CASCADE` for the `manufacturerID` foreign key in `Model`. Try this and notice that this does not work. The reason is the following: you are not allowed to delete an entry in `Model` because a `FunctionList` refers to that entry. You could use `ON DELETE CASCADE` for all foreign keys, but deleting a single `Manufacturer` can trigger deletion of a good portion of the database!
```sql
ALTER TABLE `Model` DROP FOREIGN KEY `Model_ibfk_1`;
@@ -363,3 +398,5 @@ WHERE `Manufacturer`.`manufacturerName` like 'Onkyo'
```
> [!NOTE|label:Result]
> 1 row deleted
----------