First Question
I have a table (several but for this example we’ll use one) where I want to track the created timestamp and the modified timestamp for the row. When I set this up, I tried making both fields a timestamp datatype, not null and a default of CURRENT_TIMESTAMP. However, MySQL has some limitation where you can’t have two columns in the same table with a default of CURRENT_TIMESTAMP. I didn’t really look into it, I just figured I’d work around the issue by removing the default value from the modified field. Nope, I get errors because the default value for a timestamp datatype can not be none when not null is set. So, I decided to work around this once more and remove the not null option. This isn’t ideal but it works. I’m wondering what other people do as this has to have been encountered by many of you before. Second Question [SIZE=4][SIZE=2]I have a many to many lookup table, for example:
[SIZE=2]Here’s the problem, the grade can either be a count (eg: a number between 1-30) or it can be a percentage. Normally I’d store a count as an integer and a percentage as a decimal(3,2) but considering the grade can be either, I’m not sure what datatype to use or if this is even the proper way to go about this. Currently, I just have an integer datatype and adjust it programmatically which probably isn’t ideal.
Third Question[SIZE=4][SIZE=2]
I’ve been using SQLyog’s find redundant indexes feature and I’m confused slightly about what it’s finding. Here is an example:
As you can see, I have three indexes. One for the primary key and one for each foreign key. As I understand it, in multi-column indexes, the values are indexed together, not separate. Therefore, the need for the two foreign key indexes. If that’s the case, then niether foreign key should be redundant. However, as you can see it thinks I’m wrong (and maybe I am). So, if I am wrong, then technically shouldn’t both foreign keys be considered redundant to the primary key?
use DATETIME for both columns, and always provide a value, both created and modified
use two grade columns, both nullable, instead of one
suppose you have a phone book, which lists people by last name, and within last name by first name – do you need another phone book which lists people by last name only?
instead of one relationship table holding three different grade columns, three separate relationship tables, each one having only one grade column, which will always be the same type
again, each index entry contains a pointer to where the row is on the disk
finally, the third index –
category_id
15
25
26
33
36
42
55
thus, if you were going to write a query to return something for a specific category, you could use either the first index (the composite one) or the third one
the second index is ~not~ redundant, because neither the first one nor the third one can be used to find a specific commodity
Hmm, that gives me some thinking to do. I’ll have to see where this project is headed in the future. It might make more sense to do it that way if there’s going to be additional ways to grade these tests in the future.
and for each type of grade (int, percentage, text) there be separate tables: GRADES_INT, GRADES_PERCENTAGE, GRADES_TEXT.
EDIT: Or maybe different partitions in the same GRADES table! This sounds better. Together with the extra null fields for each type of grade: int, percentage, char.
The grade_type_id would point to the right grades table, and the grade_id would point to a record in that particular grades table.
All in all, having int and percentage saved as text, and to have only one grades table would be better, I think. What I suggested first. Doing something like ‘85.25’*1 would implicitly cast the int/percentage value stored as text back to numeric.