I'm currently building a database and I'm not sure when to use foreign keys on the tables.
Most of the tables are for transaction, logging, and history.
Is there a general rule in which tables to put the foreign keys?
I have this table for the general details of a specific transaction, then another table for the specific details. Should I put a foreign key on the table for the specific details?
There's also this table used for storing user info and another for logging user activities. Should I also place a foreign key to the table that logs user activities?
If there is a relation between two tables you should always enforce that using a foreign key constraint.
I prevents logically "corrupted" data (e.g. details for a transaction that doesn't actually exist) and - equally important - it documents your database model.
Related
I'm trying to create a 1:1 relationship at MySQL Workbench (without using Diagram). I have two tables called agricultural_machine and tractor. And I would like a relationship 1:1 being that the agricultural_machine_id is referenced with tractor_id.
Foreign Keys at agricultural_machine table
Foreign Keys at tractor table
The Diagram is being formed like this
I would like this
In order to implement true 1:1 relationships you need deferrable constraints. That means the foreign key contraints in this case need to be ignored for a short time (probably until the end of the transaction) while you insert "the other row" in the other table. Once both rows are inserted the foreign key constraint can be reactivated and validated now. Otherwise, which row do you insert first? Any insert will fail since the FK won't be validated.
Unfortunately MySQL does not implement this standard SQL feature, so you won't be able to do it. You'll need to change the database to PostgreSQL or Oracle to implement 1:1 relationships.
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
So I was mapping out the relationships between tables in a database I'm working on but I ran into some problems.
It was recommended to me that I use the "Enforce Referential Integrity" function when working with table relationships but everytime I try and and make more than one relationship between tables it gives me the error: "No unique index found for the referenced field of the Primary table".
If I don't use the "Enforce Referential Integrity" function then it seems to work fine. Will it negativity effect my table if I don't use the function and if yes then how can I solve the error?
Thanks in advance.
The purpose of referential integrity is data integrity, if you won't enforce it, the data may become inconsistent, may appear logical errors, so enforcing is highly recommended. Additionally you can enable cascading and the data from detail table will be deleted or updated automatically if you delete/update the key field in main table.
As of error, it means that your main table should have primary key or unique index on fields you map to detail table. If main table has primary key with few fields, all those fields should be mapped to detail table.
Please post the picture of your relationships map and describe which fields should be mapped.
I have a SQL table called "user" and a table "login" that has a foreign key constraint to a user. I want to be able to delete a row in the user table, even if there are login rows that reference it. Right now the database stops me from doing this.
Does anyone know how I can alter the table (through SQL or preferably through PHPmyAdmin to allow me to do this?
The tables were created automatically through Django.
Edit: To clarify: I don't want to cascade the delete. That is, I want the rows in the Login table to remain even though the user they reference is gone.
If you want this kind of behavior you have to create the foreign key with an ON DELETE CASCADE clause. With an ON DELETE CASCADE foreign key all rows referencing the user will be deleted with the user.
See: http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
Edit: If you want to keep the user_id in your login tables you just have to drop the foreign key. Anyway, If you are asking this is because you should probably do a logical delete instead of a physical delete: Physical vs. logical / soft delete of database record?
Proper way to do this is to mark offending users as "inactive" so they can't login and you still maintain referential integrity of your database.
Deleting data from master table that has referential integrity links to some data in slave table is bad praxis.
Do I have to fill the field of a foreign key in MySQL or any other database manager?.
I'm writing the data of a table and when I get to the field that is a FK from another table, I have to write something, is this necessary?
I understand that the value in that FK is stored inside the parent table where it comes from.
You have to provide a value unless the foreign key column is nullable.
It depends on whether the is actually a foreign key constraint in place (available in InnoDB only). In some cases frameworks, applications, or database management tools create "false" foreign keys that exist only in the application and not actually in the database. Also, the limits on how you can insert/update/delete data realted to the foreign keys can differ based on the type on constraint in place.
Here is the MYSQL documentation for definitive information:
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
Specifically, look at the "Referential Actions" section for comments on the behavior between the tables.