I have a question that relates to setting up the third table (Repair_Parts table) with the following structure:
Part Table
Part_id (INT, unsigned, autoincrement) – primary key
Part_name (VARCHAR(50)) – unique
Part_descrip (TEXT)
Repair Table
Repair_id (INT, unsigned, autoincrement) – primary key
Repair_name (VARCHAR(50)) – unique
Repair_descrip (TEXT)
Repair_Parts Table
Repair_id (Foreign key – relates to Repair Table’s Repair_id field)
Part_id (Foreign key – relates to Part Table’s Part_id field)
Repair_Part_Item# – numbering based on the number of Part_id field entries related to a particular Repair_id (used to store data about the parts associated with a repair) – I want this field to start numbering with “1” for each different repair_id.
Question
For example, lets say a php program allows a user to create directions on how to do an automotive repair. The user is indicates that the repair is “Check Battery” (which inserts data into the Repair table under the Repair_name field (VARCHAR(50)) and a short description of the repair (which inserts data into the Repair table under the Repair_descrip field (TEXT)). The user is also able to specify the particular parts involved in the “Check Battery” repair, which may be one or many, but in this case it is only 2 different parts, an “Alternator” and “Battery”. In a different entry a user specifies three parts for a different repair.
How would one go about creating this Repair_Parts table in phpMyAdmin or MySQL so that the database keeps track of the particular Parts necessary for the Repair? What Engine should be used? MyISAM or INNODB?
From what I’ve read about normalization of databases, I seem to have the correct way to structure my tables, but I can’t figure out how to make the Repair_Part_Item# field to store data the way I want. Specifically, assuming I have broken out my tables and their fields in an acceptable manner, how would I get the Repair_Part_Item# field to start numbering at 1 for each different Repair_id field entry, and to count up for each row with the same Repair_id with a different Part_id (and not allowing duplicate rows with the same Repair_id and Part_id)?
From what I’ve read about normalization this seems to be the correct way to structure my tables, but I can’t figure out how to do make the Repair_Part_Item# field work correctly.
I’m a relative newbie to MySQL and PHP, and am just trying to set up the databases before getting into the PHP side of the equation.
If I understood correctly, what you want is to have certain field that generates a sequence starting from 1, based on current
Repair_id. This feature is best achieved by using a feature of MyISAM engine where you create a compound primary key ((Repair_id, id)withidbeingauto_increment) – however, MyISAM tables silently ignore foreign key relations so there will be no cascading deletes or updates. InnoDB supports foreign key constraints, but it doesn’t support the auto_increment generation in the same fashion as MyISAM does. The best thing you can do is create a trigger that will generate a sequential number incremented by 1 based on currentRepair_id.