I'm using symfony 4 and when I do links between tables there are indexes created. I do not understand how they work.
What is their purpose and why is it impossible to import data file into those files.
Is it possible to 'bypass' this mysql restriction and import data-files into mysql even if there is a constraint (without breaking the indexes)?
Thanks
MySQL is a relational database system. If there is a relationship between 2 tables, it is there for a reason. Think of your entities in Symfony. If one entity (a primary entity) has several related entities and those related entities can only exist if there is a primary entity, then the constraint must exist to prevent orphaned records.
For example:
Consider the relationship between companies and divisions. A company can have 0 or more divisions, but a division can only exist as a part of a company. In this case, a record in the division table would have a reference to a record in the company table. However, a record in a company table would have no direct reference in MySQL to any records in the division table.
To prevent a division from being created without a company, you cannot insert a row into the division table that does not reference a record in the company table.
To finally answer the question directly, you can only import data that does not reference the primary table by removing the foreign key constraint (this will not destroy the index). Keep in mind that this will likely result in records that are "orphaned" and do not fit the business model you are trying to create.
Rather than removing the foreign key constraint, you should first import the data to the referenced table (scoring?) and then update the data being imported to contain the correct ids to reference the primary table.
Based on the information in the image provided, each record in the file being imported should have a value for scoring_id that is equal to (presumably) the id field in the scoring table. If there is some other piece of data that can be used to link the 2 tables, use that and configure your entities appropriately.
Mysql FOREIGN KEY is used for data integrity. So what happens is you have a Foreign key which refers to column in another table .It's a way to link data relationships between tables
When Using Import if that key does not exist in the other table the mysql import will throw errors .
If you remove the FOREIGN KEY flag you would be able to import that table
refer to MYSQL manual
on Foreign Keys
Foreign keys will check if the other table has already that element that you try to enter.
What you can do for the import temporarily
use FOREIGN_KEY_CHECKS
Before the import run
SET FOREIGN_KEY_CHECKS=0;
and When it is finished
SET FOREIGN_KEY_CHECKS=1;
Or
use DISABLE KEYS:
ALTER TABLE table_name DISABLE KEYS;
and when the import is finished:
ALTER TABLE table_name ENABLE KEYS;
Of course you have to change table_name
Related
I need to create the database schema and include it in my software requirements specification for my school project, however, when I try to create a relationship between 2 tables, I get Error: Missing index on column(s).
I think #HazarathChillara has this right; you need to create primary, unique, or index keys.
You said every table has an primary key, but did you make each foreign and referenced key an index as well? It sounds like you neglected to properly set up your table structure; I only get the error when I don't have a primary key or index on the particular columns I'm working with.
"MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan"
You can just put an INDEX on the foreign key (often my referenced key is a primary key anyway, so I don't need any additional key on that column).
This error appears only when you neglect table structure. Make sure that you Indexed a foreign key as well. you can see i marked how could i select my foreign key as index.In this Image I am indexing selected, 'sr' my foreign key
As Usman Khan said you have to go to the structure tab of the particular table and clicked on more options and select 'INDEX' for the foreign key.
the below image will help you how to do it
I think i have another simple solve,
thing is, phpMyAdmin wont allow the addition of foreign keys to an already available data entry, so here is the my simple solve,
1. ensure to backup your database
2. confirm that your data was backed-up securely, recommended Offline backups
4. delete all data entries in all tables that will be part of the new relationship.
5. now Create the relevant relationships.
6. be sure you have created all required and preferred relations to avoid the need to
export data again
I'm developing an Android application in which the data is stored in a SQLite database.
I have made sync with a MySQL database, in the web, to where I'm sending the data stored in the SQLite in the device.
The problem is that I don't know how to maintain the relations between tables, because the primary keys are going to be updated with AUTO_INCREMENT, and the foreign keys remain the same, breaking the relations between tables.
If this is a full migration, don't use auto increment during migration - create tables with normal columns. Use ALTER TABLE to change the model after import.
For incremental sync, the easiest way I see is additional column in each MySQL table called sqlite_id and filled with original id. Then you can update references using UPDATE (with joins).
Alternatives involve temporary tables for storing data and an auxiliary table used for pairing. Tedious for bigger data model.
The approach I tend to use, if possible, is to avoid auto increment in such situations. I have usaully an auxiliary table with four columns like this: t_import(tablename, operationid, sqlite_id, mysqlid).
Process is the following:
Import the primary keys into t_import. Use operationid to separate parallel imports if needed.
Generate new keys for data tables and store them into t_import table. This can be combined with step one.
Import the actual data and use t_import for setting new primary keys and restore relations.
That should work for most scenarios I know about.
Thanks or the help, you have given me some ideas.
I will try to add a id2 field to the tables that will store the same value as the primary key (_id)
When I send the information from SQLite to MySQL and the primary key is incremented I will have the id2 field with the original value of the primary key so that I can compare it with the foreign key of the other tables and update it.
Let’s see if it works.
Thanks
I'm new to programming so please forgive my ignorance. I'm trying to get MySQL tables to update each other. For example: When I insert data into a Primary Key column in one table, it does not appear in it's Foreign Key column in another table.
Does anyone have any advice on this issue?
Thank you!
Additional Information: I used phpMyAdmin to create my tables and then added SQL code to create the Foreign Keys. Example of code is below.
ALTER TABLE CourseSchedule
ADD FOREIGN KEY (CourseId)
REFERENCES Course(CourseId)
I believe what we have here is a slight misunderstanding of the documentation.
For storage engines supporting foreign keys, MySQL rejects any INSERT
or UPDATE operation that attempts to create a foreign key value in a
child table if there is no a matching candidate key value in the
parent table.
When an UPDATE or DELETE operation affects a key value in the parent
table that has matching rows in the child table, the result depends on
the referential action specified using ON UPDATE and ON DELETE
subclauses of the FOREIGN KEY clause. MySQL supports five options
regarding the action to be taken, listed here
What your foreign key actually does is to make sure that you cannot insert values into your CourseSchedule table that do not correspond to an entry in the Courses table.
To give you an example, suppose you were to try to enter schedule a python course for every wednesday at 9:00 but you dont' actually have an entry for python in your Courses table. Then mysql will refuse to create that entry. Mysql cannot do the reverse. It doesn't know details about your python course. So it cannot automatically create a entry in the Courses table for you. Similarly, if oyu created an entr in the courses table. Mysql cannot automatically create a CourseSchedule for you because it doesn't know at what time it should be scheduled.
Hi I am trying to import a csv file in Mysql but i am getting an error saying that "Cannot add or update a child row: a foreign key constraint fails ".Why is this happening and how should i correct it???
One of the columns on the table you're importing data into has a relationship (a foreign key constraint) with another column on another table.
The file you have contains data on at least one column of one row that is not present on the associated column of that other table, thus violating that constraint.
You must identify the row of data that is failing to import. If the tool you're using does not identify the row, you can insert the rows one at a time to see at what point it fails.
Afterwards, you can either fix that row -- if it needs fixing -- or add the value that is being inserted into whatever related table.
The corrective action depends on whether the data you're trying to import is bad or the foreign table is missing something.
I m trying to import data from one table to another table across the schema.but foreign key constraint is giving issue.
Suppose i m having schema one and schema two.
schema one has tables -->
user
behavior
userbehavior(id from user and behavior table are foreign keys in
userbehavior table)
same way i have tables structure in schema two.
schema 2 has tables-->
user1
behavior1
userbehavior1(id from user and behavior table are foreign keys in userbehavior table)
I have successfully imported records from user to user1 and behavior to behavior1 but when I m trying to import data from userbehavior to userbehavior1 i m getting following error::
cannot add or update a child row.foreign key constraint fails.
wat could be an issue?
Thanks in advance.
You have inconsistent data, which the database refuses to import, and for a good reason.
I'd create TEMPORARY tables (i.e. tables that are automatically deleted after the session ends, import the data there, use a few queries that show me the rows from userbehaviour that violate the FOREIGN KEY constraint, clear these up manually and then use SELECT INTO to copy the data to the real tables.
cause : There is at least one row in table userbehaviour which have no parent in other table
todo: first remove all foreign key constraints from both schema and Insert and then set constraint back
In case of exporting
open exported sql file and write
SET FOREIGN_KEY_CHECKS=0; on the top of the file
and SET FOREIGN_KEY_CHECKS==1; on the bottom of file
or
export through phpMyAdmin then check Disable foreign key checks