innodb non-identifying foreign key requires key to be present? - mysql

i am creating a database with a EER Diagram and used non-identifying foreign key relationships to create my foreign key.
what i need for my foreign keys:
Default values should be 0 and should be used when no value is given for the FK
the presences of the keys in the related table should not be required
dont allow null values
what i get when synchronizing data models:
Default values are not synced to DB from EER Diagram
Default values are not used when implemented manually
the presences of the keys in the related table are required
FK fields dont allow nulls (yay!)
what am i doing wrong? i tought i had read on the web that non-identifying Foreign keys did what i needed? if everything fails i could create simple columns and only put an index on em but i tought it could be handy in the future to use foreign keys plus it looks better in my EER Diagram.

A non-identifying Foreign key means that your entity can exists without relation to the other entity, and you got it.
But technically in mysql this is achived by using null and not 0, which means instead "linked to an entity with ID=0"

Related

MySQL style for creating foreign key

I've searched a bit for this, but I actually haven't found what is the style conception in MySQL for creating a foreign key - in the create table definition or in an alter statement. Thank you.
When to create foreign key:
If at the time of table creation it is clear you that you need foreign key then do at the time of creation, but if you realize later then do it in alter.
Best practices: you can follow below practice, it is not must but you can get benefits-
constraint fk_tableName_colName foreign key (colName) references parent_table(referenced_col_Name) cascading if required
Note: As foreign key name must be unique, so it will help to maintain it.
Points Need to remember:
referenced table in parent table must be indexed (if primary key then no need as it will be indexed).
column in both tables (parent/child) must have same schema.
Have a look at the docs on how to create foreign keys...
http://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html
When a foreign key gets added can be during the initial architecture of the application being built or it can be added later as the application evolves.

Cardinality and foreign key relationship

I am new to database design and am trying to understand the best practice for using foreign keys. I know that when you have a 1:m relationship, we don't have to create a relation for the relationship; instead we could add a foreign key to the m-side of the relationship(which corresponds to the primary key on the 1-side) so as to preserve referential integrity. My question however is: Under what other circumstances could we do the same? Can we do the same when we have a 0..1 to 1 or 1-1 relationship as well? What is the best practice for this type of situation when referential integrity is as important as the computational cost?
There are three possible approaches when we are mapping 1:1 relation to Relational Model:
Foreign Key approach: Choose one of the relations-say S-and include a
foreign key in S the primary key of T. It is better to choose an entity type
with total participation in R in the role of S.
Merged relation option: An alternate mapping of a 1:1 relationship type
is possible by merging the two entity types and the relationship into a
single relation. This may be appropriate when both participations are
total.
Cross-reference or relationship relation option: The third alternative
is to set up a third relation R for the purpose of cross-referencing the
primary keys of the two relations S and T representing the entity types.
For more details you can look into this home.iitj.ac.in/~ramana/ch7-mapping-ER-EER-relations.pdf
Foreign key constraints are restrictions, not references. A relation exists implicitly wherever different columns represent the same domain, and their tables can be joined, with or without foreign keys. The constraint just ensures that the values/entities stored in one column exists in another column. They're appropriate to use wherever a relation is dependent on another, regardless of the cardinality of either. The typical use case here is subtyping of entity sets.
The main benefits of foreign key constraints is the performance gains (reduced calls across the wire as well as development time) enabled by cascading updates/deletes from a single statement instead of having to query for an ID before executing multiple update/delete statements wrapped in a transaction. Not to mention repair time if changes weren't propagated properly.
the M-to-M relationship is equivalent to two 1:M relationships .we can not assign primary key of 1 side as a foreign key of other for this purpose we use a middle entitiy that resolve an M:M and that entity is tipically called "association " or intersection entity. e.g A M:M relationship between project and employee a new midlle entity can be assignment so in this way the relation will convert into 1:M and we can assign a foreign key easilly

phpMyAdmin - how to define a FK constraint on a complex key

I am familiar with foreign keys and referential integrity. I have a table in which the PK is made up of two fields. That PK is also part of the primary key in a child table. When the parent record is deleted, I want the child record deleted as well (cascade delete). I know how to define a complex fk constraint on the child table using SQL.
How do you define a complex fk key constraint through the phpMyAdmin user interface with the relations view? There does not appear to be a mechanism for having the constraint based on a two field combination.
I only want the child record deleted if the parent table record is deleted, which means both fields that make up the foreign key are being deleted and not just one.
Thanks in advance for anyone who can tell me how to do this or for letting me know it is not possible.

Documentation says CakePHP does not support composite keys

In the "conventions page" it says (caution noob here):
"CakePHP does not support composite primary keys. If you want to directly manipulate your join table data, use direct query calls or add a primary key to act on it as a normal model. "
I am very confused by this statement. Composite keys are very basic and form many database structures with identifying relationships.
I am developing my schema and am trying to get around using composites, but it is impossible.
How does this work exactly?
CakePHP expects you to have set up a database. You must use certain naming conventions. You then describe it to your CakePHP code via a configuration file.
The database should have its primary keys, alternate candidate keys, foreign keys and constraints as appropriate. But the quoted line is telling you to make sure that every table has a one-column primary key. So this means that where you wouldn't already have such a primary key you add a column, make it the primary key, and declare what were the primary key columns as UNIQUE NON NULL (which is what PRIMARY KEY does), ie as an alternate candidate key.
You should also add any appropriate new versions of constraints including new foreign keys. Note that the old foreign keys can just stay as they were since an SQL FOREIGN KEY declaration only needs to reference a unique field, not a primary key per se. (Ie it's really a foreign superkey declaration.) You should also constrain such new+old foreign key pairs in a table to actually be be a valid pair of values in the referenced table, ie declare the combined columns to be a foreign key to the combined columns as unique. (Ie you don't just want both primary & alternate key values to be in the referenced table, you want them to be there together.)

What is the meaning of self referencing foreign key?

I went over a legacy database and found a couple of foreign keys that reference a column to itself. The referenced column is the primary key column.
ALTER TABLE [SchemaName].[TableName] WITH CHECK ADD
CONSTRAINT [FK_TableName_TableName] FOREIGN KEY([Id])
REFERENCES [SchemaName].[TableName] ([Id])
What is the meaning of it?
ALTER TABLE [SchemaName].[TableName] WITH CHECK ADD
CONSTRAINT [FK_TableName_TableName] FOREIGN KEY([Id])
REFERENCES [SchemaName].[TableName] ([Id])
This foreign key is completely redundant and pointless just delete it. It can never be violated as a row matches itself validating the constraint.
In a hierarchical table the relationship would be between two different columns (e.g. Id and ParentId)
As for why it may have been created quite likely through use of the visual designer if you right click the "Keys" node in object explorer and choose "New Foreign Key" then close the dialogue box without deleting the created foreign key and then make some other changes in the opened table designer and save it will create this sort of redundant constraint.
In some cases this is a preferred way to reduce redundancy in your model. In using the self referencing foreign key (as shown in you example) you create a hierarchical relationship between rows in your table. Pay attention to what happens when you delete a row from the table, cascading on delete might remove rows you still want.
Using these sort of keys moves some of the data validation to the DB model as opposed to making this a responsibility of the program/programmer. Some outfits prefer this way of doing things. I prefer to make sure programs and programmers are responsible - data models can be hard to refactor and upgrade in production environments.