finish lab
This commit is contained in:
1 parent
2035387730
commit
f52207eed3
1 file changed
+63
-1
@@ -300,4 +300,66 @@ WHERE `FunctionList`.`modelID` is NULL
|
||||
>
|
||||
|
||||
|
||||
# Test constraints
|
||||
# Test constraints
|
||||
|
||||
#### 1) Try to insert a record in a table using an existing primary key.
|
||||
```sql
|
||||
INSERT INTO `Code`(`codeId`, `codeValue`)
|
||||
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.
|
||||
```sql
|
||||
INSERT INTO `Model`(`modelId`, `manufacturerID`, `typeID`, `modelName`)
|
||||
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.
|
||||
```sql
|
||||
DELETE FROM `Manufacturer`
|
||||
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.
|
||||
```sql
|
||||
ALTER TABLE `Model` CHANGE `manufacturerID` `manufacturerID` INT NULL;
|
||||
|
||||
ALTER TABLE `Model` DROP FOREIGN KEY `Model_ibfk_1`;
|
||||
|
||||
ALTER TABLE `Model` ADD CONSTRAINT `Model_ibfk_1`
|
||||
FOREIGN KEY (`manufacturerID`) REFERENCES `Manufacturer`(`manufacturerID`)
|
||||
ON DELETE SET NULL ON UPDATE RESTRICT;
|
||||
|
||||
DELETE FROM `Manufacturer`
|
||||
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!
|
||||
```sql
|
||||
ALTER TABLE `Model` DROP FOREIGN KEY `Model_ibfk_1`;
|
||||
|
||||
ALTER TABLE `Model` ADD CONSTRAINT `Model_ibfk_1`
|
||||
FOREIGN KEY (`manufacturerID`) REFERENCES `Manufacturer`(`manufacturerID`)
|
||||
ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
DELETE FROM `Manufacturer`
|
||||
WHERE `Manufacturer`.`manufacturerName` like 'Sony'
|
||||
```
|
||||
> [!NOTE|label:Result]
|
||||
> #1451 - Cannot delete or update a parent row: a foreign key constraint fails (`sdi04`.`FunctionList`, CONSTRAINT `FunctionList_ibfk_3` FOREIGN KEY (`modelID`) REFERENCES `Model` (`modelID`) ON DELETE RESTRICT ON UPDATE RESTRICT)
|
||||
|
||||
|
||||
```sql
|
||||
DELETE FROM `Manufacturer`
|
||||
WHERE `Manufacturer`.`manufacturerName` like 'Onkyo'
|
||||
```
|
||||
> [!NOTE|label:Result]
|
||||
> 1 row deleted
|
||||
Reference in new issue
Block a user