Generating SQL from an ERD - how are relations mapped into SQL? - mysql

I'm using Mysql workbench EER where I draw my ERD.
There are all kind of relationships between the tables (optional/mandatory,non identifying/identifying)
and I use the Forward engineering to generate the underlying SQL.
As far as the relationships go, for an optional relationship it generates a NULL FK
while in a mandatory a NOT NULL FK and that's it.
I mean shouldn't it also based on the relationships generate Cascades deletes for example?
i.e if I have an identifying relationships, then when the parent is deleted the child should be deleted too thus a cascade delete would have been generated
Or, in other words the relationships modeled in an ERD have no practical value other than conceptually know how your database is modeled on paper?
For example what should be the generated sql code for an identifying mandatory relationship?

What you are referring to is called a relationship. In database management terms a relation is something different.
Your relationships should get implemented as foreign keys. The practical value of a foreign key is that it enforces referential integrity. Cascaded deletes are not typically the desired behaviour and in SQL the default referntial integrity action is that delete of a row in a parent table is not permitted if the row is being referenced in another table. If you want cascaded deletes then you have to specify that. Note that in MySQL only the InnoDB database engine supports referential integrity.

Related

MySQL - Trouble to create a 1:1 relationship

I'm trying to create a 1:1 relationship at MySQL Workbench (without using Diagram). I have two tables called agricultural_machine and tractor. And I would like a relationship 1:1 being that the agricultural_machine_id is referenced with tractor_id.
Foreign Keys at agricultural_machine table
Foreign Keys at tractor table
The Diagram is being formed like this
I would like this
In order to implement true 1:1 relationships you need deferrable constraints. That means the foreign key contraints in this case need to be ignored for a short time (probably until the end of the transaction) while you insert "the other row" in the other table. Once both rows are inserted the foreign key constraint can be reactivated and validated now. Otherwise, which row do you insert first? Any insert will fail since the FK won't be validated.
Unfortunately MySQL does not implement this standard SQL feature, so you won't be able to do it. You'll need to change the database to PostgreSQL or Oracle to implement 1:1 relationships.

MySQL database one-to-one relation

If I have a database with multiple tables all with one to one relations. What is the best approach for when I register a row in one table, it's also registers a row across the related tables. I know you can use cascade to update and delete using foreign keys so is there a similar approach for creating these related rows with default values?
Creating null rows is not great design. Better create them only when necessary using foreign keys. If you still must, look at transactional stored procedures.

Issue with enforcing referential integrity In Access 2013

So I was mapping out the relationships between tables in a database I'm working on but I ran into some problems.
It was recommended to me that I use the "Enforce Referential Integrity" function when working with table relationships but everytime I try and and make more than one relationship between tables it gives me the error: "No unique index found for the referenced field of the Primary table".
If I don't use the "Enforce Referential Integrity" function then it seems to work fine. Will it negativity effect my table if I don't use the function and if yes then how can I solve the error?
Thanks in advance.
The purpose of referential integrity is data integrity, if you won't enforce it, the data may become inconsistent, may appear logical errors, so enforcing is highly recommended. Additionally you can enable cascading and the data from detail table will be deleted or updated automatically if you delete/update the key field in main table.
As of error, it means that your main table should have primary key or unique index on fields you map to detail table. If main table has primary key with few fields, all those fields should be mapped to detail table.
Please post the picture of your relationships map and describe which fields should be mapped.

MySQL DB Foreign Key

What are the pros and cons of creating table relationships in a MySQL database using queries (JOINS) as opposed to doing it with DDL using Foreign key and referential integrity constraints? I have received a database that has not relationships (No FK) on its tables to identify relationships among tables. The relationships are being created on JOINS when data is being queried.
The main reason for using foreign keys is that data is always consistent in the database. This is "independent" from joins - when you use foreign keys you still need to use joins.
In MySQL it also has a nice side effect: if you use foreign keys, you have to define indexes which might speed up queries.
But with foreign keys you can make sure, that the row which you are referring must exist in the database. See https://en.wikipedia.org/wiki/Foreign_key.

When to use foreign keys in mysql

I'm currently building a database and I'm not sure when to use foreign keys on the tables.
Most of the tables are for transaction, logging, and history.
Is there a general rule in which tables to put the foreign keys?
I have this table for the general details of a specific transaction, then another table for the specific details. Should I put a foreign key on the table for the specific details?
There's also this table used for storing user info and another for logging user activities. Should I also place a foreign key to the table that logs user activities?
If there is a relation between two tables you should always enforce that using a foreign key constraint.
I prevents logically "corrupted" data (e.g. details for a transaction that doesn't actually exist) and - equally important - it documents your database model.