So I’ve taken over a database and am having a bit of trouble understanding what exactly these four lines mean. I realize that these lines are enforcing the relational aspect of the DB but I was hoping to pinpoint exactly what this syntax is doing.
Key x (y):
What is it?/What is relation of location_sh to dep_name and cat_name and loc_name?Constraint
This appears to be making owner connected with the column username in the table users but why is the constraint part needed (i.e. what does constraint location_sh do)?ON DELETE SET NULL, ON UPDATE CASCADE
Just checking that when the username is deleted or changed that the deletion or change is reflected.-
Should I make any changes to this snippet?
KEY `location_sh` (`dep_name`,`cat_name`,`loc_name`), KEY `owner_sh` (`owner`), CONSTRAINT `location_sh` FOREIGN KEY (`dep_name`, `cat_name`, `loc_name`) REFERENCES `locations` (`dep_name`, `cat_name`, `loc_name`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `owner_sh` FOREIGN KEY (`owner`) REFERENCES `users` (`username`) ON DELETE SET NULL ON UPDATE CASCADE
Mysql page on these very items: http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
1) Key defines a key for table searching. Keys are indexed by the db engine to provide faster results for queries.
2) Constraints apply “rules” for foreign keys. This says that these keys
dep_name, cat_name, loc_nameall point to columns of the same name in thelocationstable3) ON DELETE actions are very well described in the link provided.
4) It looks good… though it is tough to tell without knowing context of the situation