Whenever i make a database backup using this command:
mysqldump --all-databases
It will change all the foreign key constraints names. We are using Syfmony2 framework with doctrine migrations. It looks like mysql apllies a naming strategy for foreign keys. I wonder if it's possible too keep the foreign key constraints names the same.
Any ideas?
For example: on the constraint for user is FK_B2D59BB34A8BC2 In the backup it says User_ibfk_1
Thanks.
Related
I am using SQLAlchemy with Flask to create database tables - every table has at least one foreign key - it works with sqlite but not MySQL - I get foreign key integrity error when creating the tables in MySQL (the parent table is not created when creating the child table). I use "SET foreign_key_checks = 0" to solve the problem but that does not work with sqlite. Is there a way to configure SQLAlchemy to ignore foreign key checks?
if you are user mysql, you can connect to mysql and use SET GLOBAL FOREIGN_KEY_CHECKS = 0; delete the db table you want, and again SET GLOBAL FOREIGN_KEY_CHECKS = 1;. This value verifies foreign relationships in the db tables.
First of all, why would you want to ignore foreign key constraints? When you define your foreign key you can pass a string with the foreign table and column names, and those will be resolved correctly even if the foreign table hasn't been created yet.
But in any case, if you want to have foreign keys that are not enforced, just don't define your foreign key columns as foreign keys, define them as simple columns and manage the foreign key dependency yourself. Not a good idea, in my opinion, but it can be done.
I have two databases on two different physical servers. At this time, I want to make relation between some tables in database1 and some tables in database2. I want to know is there anyway for doing that?
Note I have both mysql and oracle databases on my two servers and I can use them. Is it possible to make join between to physical databases with mysql or oracle?
Hamed, check this out.
In oracle it is possible to have a foreign key constraint based on a view. So follow these steps:
first: create a view to a table on the remote database. example:
create view test_view_dblink as
select * from some_table#some_dblink;
second: create a foreign key constraint on the view. example:
alter view test_view_dblink
add constraint test_view_dblink_fk foreign key (column_name)
references table_view_in_your_database(column_name) disable;
the "disable" in the constraint definition is very important.
in my environment this works perfect!
You can also have a primary key:
alter view test_view_dblink
add constraint test_view_dblink_pk primary key (column_name) disable;
I export my SQL form MySQL Workbench and noticed there is an option to not including the FOREIGN KEY in the SQL file. Is it better to export with a foreign key or without?
You should include the foreign key if you plan on using the schema for something critical. Without the foreign keys, you will jeopardize the integrity of your data.
I have created a foreign key with NaviCat (a MySQL application), but the instant I create it, it disappears from the foreign key list and a new Index get added. Does that mean something went wrong or is that normal to happen?
I have tried using the information_schema How to check if a column is already a foreign key? but that resulted in Unknown table 'REFERENTIAL_CONSTRAINTS' in information_schema. Is it possible that query is for MsSQL and is different with MySQL?
The table you are probably creating the foreign key is MyISAM. Go to the Table Design view and go to the Options tab to change the table Engine to InnoDB
You can change all your tables to InnoDB following the steps at http://kevin.vanzonneveld.net/techblog/article/convert_all_tables_to_innodb_in_one_go/
Add default-storage-engine=innodb in the [mysqld] section of your MySQL configuration file (usually my.cnf) from http://dev.mysql.com/doc/refman/5.5/en/innodb-default-se.html
There are no flaws after all from MySQL 5.5 InnoDB is the default storage engine.
execute
SHOW CREATE TABLE myTable;
and then check for
ENGINE = InnoDB
if this is true, you can use foreign keys. check if it contains something like this:
FOREIGN KEY (customer_id) REFERENCES customer(id)
hope that helps !
When I create a foreign key in MySQL workbench, a new entry appears on the "Indexes" tab with the exact same same as the foreign key that I just created.
Is this actually the foreign key, showing up on the "Indexes" tab for some reason? Or does MySQL Workbench try to be helpful and create an index for me, knowing that I'm likely to be selecting against that column, and give it (confusingly) the same name as the foreign key?
It's MySQL doing that, not workbench.
And yes, it is being helpful to create an index when you create a foreign key constraint.
Foreign keys in innodb require an index or a prefix of an index with the same fields as the constraint in the same order. It seems MySQL Workbench automatically creates these since they appear in the SQL script exported from MySQL Workbench.
This is helpful but the problem is that it does not recognize the prefix from other indexes so it always creates an index even when it is unnecessary.