MySQL relation without using foreign key references - php

I have question about making relation between two tables in mysql. I create table with column which is foreign key, but I dont use foreign key references keyword. I connect tables in code(php/asp.net). I dont know if it is good habit? Thanks for your help.

It's generally seen as a good habit to create a foreign key constraint as it will enforce data integrity between the two columns.
Yes, you can have 100% valid data in your database without using any foreign key constraints at all, but implementing them will make it impossible for a flawed update, delete, or insert to violate the foreign constraint between them in the future.

Related

differences between adding Foreign constraint and without on mysql table

At past, I was used to make a table relationship programmatically, which is quite handy since you don't need to make FK constraint to each table which have relation.
But, I wonder what is the differences or the advantages of giving a FK constraint to tables that have relation, instead of just creating an attribute and retrieve them programmatically (calls the tables where field = another table PK).
Just some information, I work on php independent MVC framework without any dependency to eloquent or something else.
Hope someone give me some short lesson on this :D Thank you and have a nice day!
There are certain principles that you should follow while coding and development, I can say that there is no issue whether or not you create a foreign key constraint to a table that has relation or not but you know that won't restrict the column to have only those values that are being referenced by it. So basically it is not a good DB Schema and may lead to inconsistencies. For example deleting a parent table's row you will have to manually delete the child table's row on the other hand if you have a foreign key constraint that to onDelete = cascade, your database will automatically take care of everything and there won't any inconsistencies.

Zf2 Doctrine2 - Can not add relationship in existing database tables which does not have any FK

I am currently doing ZF2 project with Doctrine2. I have existing database where tables does not have foreign key constraints and any relation. Using doctrine can I generate schema based on relation without foreign key constraints.
I tried to add the column without foreign key constraints and its violated. Also I tried the column null able still doctrine try to modify the db schema to add foreign key constraints which eventually fails.
Also tried to add another temporary column as foreign key and later on write a script top transfer existing column to the foreign key constraint column. But it also failed as there have some columns who does not exist in the second table( may be deleted)
still does not find any suitable way to do that.
Any idea? or its not possible?

is there a way to disable symfony2/doctrine constraints?

I get that foreign key constraints great for integrity of a database and all, but it's also a huge overhead to use constraints when dealing with tables that are in the millions and growing.
I want to remove foreign key constraints from my application. In past symfony2 projects I've removed constraints manually, but I'm assuming maybe there is a way to simply tell symfony2 to do this?
If anyone is aware of a way to do this within the framework please let me know :)
edit:
Let's say for example in a manyToMany relationship, it auto-creates the relationship table with the proper indexes but it also puts a foreign key constraint on both columns as well, or if i have a oneToMany relationship it puts a foreign key constraint on that relationship.
I don't want these foreign key constraints to be created.
Found in Doctrine's JIRA:
You can disable the exporting of foreign keys for specific models:
User:
attributes:
export: tables
columns:
or with php:
$userTable->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_TABLES);
Now it will only export the table definition and none of the foreign keys. You can use: none, tables, constraints, plugins, or all.

MySQL foreign key relations vs mysql_insert_id to relate tables

This is for a sort of proof of concept draft to get things working, but don't want to have completely crap code. For my database, I tried to get true foreign key relations going using innoDB, but couldn't get it.
Instead of using foreign keys, I decided to just pull mysql_insert_id() after inserts, saving it as a variable, then putting that variable into the related table.
Is this horrible? Everything seems to work well, and I'm able to connect and relate ID's as needed. What benefits would using foreign keys give me over my method (besides updates/deletes cascading)?
To create a relation (master->detail), you have to always supply the keys by yourself, either using mysql_insert_id, natural keys or key generated by your applications. The FOREIGN KEY is not going to make that work for you.
What FOREIGN KEY does is
Helping you enforce the relationship/the integrity of your data (so the "detail" record does not point to an invalid parent)
Handles deletion or key alterations of master records (ON DELETE ..., ON UPDATE ...).
It's also creating an index in your "detail"-table for the "master_id"-row if it doesn't exist yet (okay, you could also do that without FOREIGN KEY)
Has also some kind of documenting purpose for example an ERM-tool could reengineer the relationship model from your schema (okay, this point is a slight long shot)
The cost of adding the FOREIGN KEY constraint statement is small compared to its benefits.

Are foreign keys mandatory in MySQL?

Do I need to assign a foreign key in MySQL?
(Navicat for MySQL, PHP, I'm programming a social network for sake of fun)
See this http://www.databasejournal.com/features/mysql/article.php/2248101/Referential-Integrity-in-MySQL.htm
You can 'define' a foreign key in any
MySQL table type (including the
default MyISAM table type), but they
do not actually do anything - they are
only used to enforce referential
integrity in InnoDB tables.
The OP should be excused to think it is an "imaginary" concept.

Categories