In the phpMyAdmin relation view, there is a column for "internal relation" right next tor "foreign key constraint". I know what foreign keys are used for in mySQL, but I've never heard of internal relations.
Is this a phpMyAdmin thing?
This is a phpmyadmin internal mechanism to manage relationship between tables.
This feature is actually useful for MYISAM tables which don't support foreign keys and constraints.
By defining internal relations in phpmyadmin you link tables together which otherwise can't be linked. These information are stored in a phpmyadmin specific table inside your MySQL server (phpmyadmin.PMA_relation).
However this is just a phpmyadmin internal definition and has no effect on mysql itself (no foreign key constraints or referential integrity are enforced).
See here for additional information.
Related
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.
I have created one database by executing generated script(schema).
After that I am copying database,only data while copying I facing problem of foreign key violation and primary key violation
What should I do for this issue.
key Violation
You really only have two options here, go though the data your importing and change the conflicting value or drop the constrains all together. Once the data is in the new DB you can not enable constraints until you address the duplicate data. See this SO for in on removing FKs, it also applies to dropping PKs
How do I drop a foreign key in SQL Server?
I would highly recommend seeing if you can change the data so it no longer violates the key constrains
I have successfully linked my MySQL database with my Access database file. Everything is working fine except the relationships in the MySQL database are not appearing in Access.
I have made a plenty of relationships in the MySQL tables using foreign keys, but these relationships are not reflected in Access. Kindly help me to import the relationships from the MySQL database into Access.
Software I'm using: MySQL version 5, Microsoft Office 2013, Access file format: .accdb
While it is true that the MySQL foreign key constraints don't show up by default in the Relationships tab in Access, those constraints are still in place in MySQL and are still enforced for linked tables.
For example, say I have two MySQL tables, [customers] and [orders], with a foreign-key constraint on [orders]. If I link to those tables in Access and I try to insert a row into my [orders] linked table where the [customerID] does not match a [customerID] in my [customers] linked table the insert fails:
ODBC --insert on a linked table 'orders' failed.
[MySQL][ODBC 5.2(w) Driver][mysqld-5.5.29-0ubuntu0.12.04.2]Cannot add or update a child row: a foreign key constraint fails (`zzzTest`,`orders`, CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`customerID`) REFERENCES `customers` (`customerID`)) (#1452)
You can go into the Relationships tab in Access and create "Access-side" relationships for the MySQL tables...
...but notice that the "Enforce Referential Integrity" options are greyed out because that is a function of the database setup at the server, not in Access. So really, the only benefits that the "Access-side" relationships would offer are:
"documentation" of the relationships (which you could get from a database diagram generated against the MySQL database), and
"automatic" joins between the linked tables in the Access query designer (which can also happen without [Access] Relationships if tables have columns with the same name).
It's up to you to decide whether it would be worth the trouble to create those "Access-side" relationships.
Since this is cross databases, it may have to recreated manually.
Check out this one, Importing .sql into MS Access using OBDC
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?
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