Iāve done as suggested from #17 everything seems to be working ok, no breakdowns. And the hits table is also setup with two columns venue_id and dtstamp both index and PK.
then you can create the hits table as i outlined before, with venue_id as FK to id
Do you mean add the FK onto venue_id inside the new hits table, which is also the PK?
Is the hits table acting as the child table to venues table?
Or do I need to put the FK on id inside the venues table?
Iāve also realised that all my tables are using MyISAM I understand FKs can only be used on InnoDB.
The new hits table is InnoDB, my question, will the venues table break, cause any issues if I change from MyISAM to InnoDB which has lots of live data and queries?
Does sql act differently when using different storage engines? Meaning do I need to write the queries/code in a different way
just to confirm what you mean by āindex and PKā could you please do a SHOW CREATE TABLE
the most effective way i can think of to answer this question is to give you two hints:
FK always work in one direction only, from the dependent table to the table it references. In other words, one table, often called the parent, acts as the repository of valid values, and the other table, often called the child, is not allowed to have any values in it that cannot be referenced in the parent table
after i said āwith venue_id as FK to idā, which direction do you think i meant?
fortunately, converting the tables is easyā¦ iāll let you google it
at what time in the past have you ever coded any sql differently because of the target table type?
A couple of important questions so I can bring this to a close.
Is SHOW CREATE TABLE snippet in my reply in #23 correct?
As mentioned about the hits venue_id which is unique PK also FK to venues table. Is this correct?
The reason I ask, what I readā¦
"Foreign keys are almost always āAllow Duplicates,ā which would make them unsuitable as a Primary Key.
Instead, find a field that uniquely identifies each record in the table, or add a new field (either an auto-incrementing integer or a GUID) to act as the primary key.
The only exception to this are tables with a one-to-one relationship, where the foreign key and primary key of the linked table are one and the same
After reading these comments, and if we stick with the PK and FK combination in the hits table, that would mean weāre using a one-to-one relationship, correct?
And could I use the same combination with a one-to-many relationship?
however, you also need to insert a row for each hit into the venue_hits table
Who ever wrote that should be smacked. A PK does not need to be a autoincrement field or a GUID. They are convenient when youāre using it as a FK to another table, but even then, they arenāt required. TECHNICALLY, a primary key isnāt required, period. Strongly recommended, and required by a lot of entity frameworks, but otherwise not required.
But your venue_id, dtstamp will most likely give you a unique value, so itās fine for a primary key. And it will be, by definition, a one-to-many relationship because you will have multiple entries in tbl_venue_hits for a particular venue_id which ties to tbl_venues.
Thats great Dave, thanks for clearing this up. Puts my mind at ease
Couple of final questions:
So in theory, I can create other FKs from other tables also pointing to the venues table id?
Reason being, I have a comments table, photo table about the same venue which Iāll be able to connect once I convert my tables to InnoDB.
What is the best way to add a row/hit to my hits table based on the snippet I mention in #24
Since updating my database from MyISAM to InnoDB my row count has now become a minus -49, is the normal?
[quote=ācomputerbarry, post:35, topic:242540, full:trueā]
If anybody can help, small error [/quote]
itās telling you that youāre trying to violate the FK constraint
youāre trying to add a row with a value for venue_id that doesnāt exist in the id column of tbl_venues_innodb
itās doing exactly what you asked it to do ā preventing you from inserting invalild data
do not insert a row into the hits table for a venue that doesnāt exist in the venues table
But thats the thing r937ā¦ āvenuenameā is a row inside the venues table, itās the venue_id of a venue, I just change it here to venuename for example.