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
Related
My server data .. Ubuntu - NGINX - MySQL
I need to create a table in wordpress database with a foreign key with wp_posts table .. but I noticed that to create a foreign key in the new table I must include ENGINE keyword into create table statement, I don't know why but creating foreign key failed when ENGINE keyword not included.
Of course I need this statement to be programmatically for my clients so I cannot include ENGINE='USER DB ENGINE' .. So Is there anyway to use the default database engine?
P.S. I should include ENGINE keyword.
MySQL supports foreign keys only for specific database engines. The two most commonly used and present in an installation are:
MyISAM
InnoDB
InnoDB supports foreign key constraints, MyISAM does not. It is somewhere on the to do list to implement foreign key constraints in MyISAM, however nobody got to that in the last 15 years (plus/minus a few).
I've noticed that I can't create two different foreign key constraints that have the same name, regardless of the set of tables in the schema that they're affecting.
Which I can cope with that, I couldn't find in the MySQL documentation where it says this is the case, nor why its the case. I'm using InnoDB, so maybe it's something specific to that, but I'm not finding that documented either.
I have a database that I built a while back. Every table in the database is InnoDb. Several tables had foreign key constraints, and I set them up for On Delete = Cascade. When I was using an earlier version of phpmyadmin, working with these was simple: I'd just go to the Structure tab of a table, click the Relation View link, and as long as I had the correct indexes set up on the correct columns, I could set the foreign keys as I saw fit.
Since upgrading to version 4, it's become a nightmare. For some tables, I go to the relation view and everything is just fine. But for others--even when they already have foreign key constraints set--I can't see any options for working with them.
To make matters worse, I've even tried dropping the indexes and re-adding them, only to be given the following error: Cannot drop index [index_name]: needed in a foreign key constraint. So unless I'm mistaken, the constraint is there, but phpmyadmin is refusing to show it to me.
Is there something I have to do to make them show up again? This is extremely frustrating to say the least: something that worked just fine before now does not thanks to an upgrade.
OK, after playing around with the tables a bit, I figured out what's going on. The only time the foreign key constraint options don't show up are when the table names contain capital letters. Very frustrating to say the least.
I just filed a bug report for phpmyadmin: https://github.com/phpmyadmin/phpmyadmin/issues/11461
It should be an easy fix.
happened to me because i used '&" in the database name.
In my case it is that I used two columns (A and B) both as foreigns keys to other tables then I also used a composite unique for ([A, B]), phpMyAdmin does not show the existed foreign index of column A but does show that for column B.
My system version are as follows:
Server version: 5.7.30 - MySQL Community Server (GPL)
I wrote a simple SQL statement to create addon for wordpress - just few tables with relations.
I decided to test it a bit in MySQL workbench and all goes fine, got tables and relations. Then I'm trying to forward engineer it - workbench slightly changes the text... and then reports an error:
Honestly, I'm quite puzzled here... schema looks very simple, I'm sure I didn't make any type so why the error?
Make sure the columns are the same in both tables including sign for the foreign key and the primary key.
Could it be the engine? InnoDB handles foreign keys, but I think the standard MyISAM doesn't, or at least, didn't always, depending on version.
If you are trying to add the foreign key restriction to an attribute in a table containing data you can have problems if the foreign key restriction is not fulfilled. I mean, if you have a value in the attribute that now is a foreign key, and this value is not contained in the attribute of the table that the foreign key references.
Let's say I have a bunch of MyISAM tables in a MySQL database. I know that if my tables are InnoDB MySQL Workbench will pick up the relationships between the tables automatically. But if my tables are MyISAM, is there a way that I can select which column to use as a foreign key in MySQL Workbench without MySQL Workbench adding a column in my table schema? For example, let's say I have these columns in a table called users:
id PRIMARY KEY
username VARCHAR (255)
password VARCHAR (255)
email VARCHAR (255)
user_type_id INT
As you can see above, user_type_id would be a foreign key coming from a table called user_types. If I add a many-to-one relationship between my tables users and user_types in MySQL Workbench, a column user_types_id will be automatically added to the schema of my table users (because that's what MySQL Workbench considers as a foreign key usually). I don't want that to happen, I want to be able to tell MySQL Workbench to use the column user_type_id as my foreign key. Any way I can do that?
Thank you
NOTE: There is a Foreign Keys tab when I double click on a table on the model view in MySQL Workbench, but when I do so, I get the following text:
Note: foreign keys can only be defined for certain storage engines (like InnoDB). The server accepts foreign key definitions for other storage engines but silently ignores them. Switch your table engine to one that supports foreign keys to allow adjustments here.
You could temporarily switch to InnoDB, define the relationship and switch back to MyISAM. The relationships will remain. BUT, they are of no real use except to document your intention.
If that's all you want then go ahead.
Btw: the FK tab page is only unavailable on Windows (for engines that don't support FKs), as we have seen many complaints from users that defined FKs for MyISAM, just to see no effect in their target DB. On Linux + OS X you can work on FKs regardless of the selected storage engine.
If I'm understanding your question correctly, I believe you can do this from the Foreign Keys tab on the table itself. If you double click a table from the model overview, on the bottom tabs there is a Foreign Key tab. You can manually add in foreign keys however you choose from that dialog. Does that make sense?