MySQL DB Foreign Key - mysql

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.

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 Updating/adding foreign key constraints

I have to update foreign constraints in a lot of tables in a lot of databases. The databases should(!) have the same structure, but I realized that there are sometimes little differences (e.g. constraints are different).
So my idea is, to "normalize" all tables by dropping foreign key constraints first.
Is there a way to drop all foreign key constraints referenced to a specified table/column from all tables?
For example:
DROP FOREIGN KEY FROM ... WHERE referenceTable = 'myTable'
AND referenceCol' = 'myId'
I think you need to look here:
http://dev.mysql.com/doc/refman/5.6/en/innodb-information-schema-system-tables.html
It is feasible. Youc could certainly do a single query to drop the keys you need to remove.

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

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.

foreign key constraint not respected

I have recently switched jobs and at this new company we are using MySQL. I don't have any expereince with MySQL, although I have used SQL Server and Oracle for over 4 years now.
Now the strange thing I see with MySQL is that it does not seem to resepect some of the basic things like Foreign Key Constraints (meaning a column is a foregin key but i can insert any value here no matter if it's present in the other table where this FK related to). Now I know in SQL Server there is this concept of a NOCHECK foriegn key constraint but the guy at new company responsible for MySQL db say that not respecting a FK is a normal thing in MySQL and it does not need to have any special settings (like NOCHECK FK constraint).
I fail to understand that in a database system how can you ensure referential integirty without having these basic checks in place. I am not sure if the local mySQL "expert" know it well or it's just that mySQL really does not respect FK rules. Any thoughts?
Check that your tables are using the InnoDB engine. When using the MyISAM engine (which was the default until recently), foreign keys declarations are not enforced.
MySQL have different DB Engines -
MyISAM - default, no FK support
InnoDB - have FK support - but no fulltext search like in MyISAM
On both engines you can create table and try to create FK, but MyISAM will simply ignore it.
Also, make sure foreign keys are being enforced. For some reason they weren't on mine, leading to one week of headache!
Check:
SELECT ##FOREIGN_KEY_CHECKS
Set:
SET FOREIGN_KEY_CHECKS=1

Can FOREIGN KEY and CREATE VIEW be used together?

I would like to make a view, and in that view alter the tables to have foreign keys.
From the MySQL manual can I see, that foreign keys only work on InnoDB, but my database is MyISAM.
So my question is, is it possible to create a view, and then create foreign keys in that view?
http:// dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
You have got everything completely wrong.
First of all MySQL allows you to use different table engines in a single table, so for example one table could be a MyISAM table and the other table could be an InnoDB table, it all depends on your need. The statement my database is MyISAM is completely wrong.
Secondly if you need for key constraints then use InnoDB tables and specify the constraints in the table definitions. You cannot specify foreign key constraints in views. Foreign key constraints are defined either when creating table or when altering the table.
A view is something else. Views are stored queries that when invoked produce a result set. See http://dev.mysql.com/doc/refman/5.0/en/views.html.