Most voted "foreign-key" questions
It is usually used to describe a problem or question directly related to the link between tables in databases. DO NOT USE THIS TAG simply because your table has a foreign key, but when the key is part of the core of the question.
Learn more…120 questions
Sort by count of
-
15
votes4
answers2051
viewsDropping indexes or Foreign Keys in a table can slow the query?
What I always read some tutorials talking about Mysql and I’ve also heard from some fellow programmers is that the lack of indexes or foreign keys in a table can make a query slow. For example, I…
-
10
votes2
answers8715
viewsWhat is the use of using foreign keys?
I’ve been learning about foreign keys and I’m having a hard time understanding what they’re useful for. Its purpose would be to "divide a table into several tables, leaving its data always atomic",…
-
8
votes2
answers1800
viewsIn which table is the foreign key in a ratio 1 to 1?
In an SQL relationship of two tables 1 to 1, there is some rule that defines which table the foreign key will be in?
-
7
votes2
answers577
viewsIs it good practice to keep foreign key columns redundant?
Consider that we have the models A, B, C and D, each one referencing to the above, ie, A has many Bs, B has many Cs and C has many Ds: Redundancy-free A | id | ... | ------------ | ... | ... | B |…
-
7
votes2
answers1014
viewsSetting up a database, how do I make this relationship?
I’m assembling the EER diagram of an ERP. Almost all tables have a column called "id_company" that relates to the "Companies" table. This is because the user will be able to manage one or more…
-
6
votes1
answer609
viewsMysql JOIN with or without Foreign key?
What’s the difference between relating 2 tables using and not using a Foreign key? With regard to performance, it is recommended to use Foreign key to make the relationship? In which situation would…
-
6
votes2
answers91876
viewsHow to add a Foreign key to an already created table
I’m a beginner in mysql and can’t add a fk to a table I created. I have two tables (person, object) and I want to create a 'fk_person' in object that receives the value of the 'id' attribute of…
-
5
votes2
answers2215
viewsWhat is the difference between using a "Constraint" in "Foreign key" or not?
When creating a table in Mysql I used to create fields CONSTRAINT FOREIGN KEY how are you following to create the foreign key: CREATE TABLE socio ( id_socio INTEGER NOT NULL, nome VARCHAR(256) NOT…
-
4
votes3
answers176
viewsHow to work with tests and fixtures in a bank that contains foreign key constraints in Rails?
I am using Rubyonrails 4.1 and Postgresql database and use the Gem Foreigner to create foreign key restrictions in the bank. It turns out that this makes some tasks with fixtures. When I try to run…
-
4
votes1
answer75
viewsWhen should I create a Constraint like DEFERRED?
As far as I know, creating a Constraint (like a foreign key or a check) like DEFERRED makes it only validated at the end of the transaction. What is this used for? And when it should not be used?…
-
4
votes2
answers872
viewsWhat is the best way to create a conditional foreign key Constraint?
In some cases, it is interesting to create a foreign key reference conditional, that is, that does not allow to reference all the records of another table, but only those that assume a condition.…
-
4
votes2
answers1848
viewsIs referencing the table itself in Mysql correct?
I am working on a system where the previous programmer structured a particular table of posts referencing itself (to be able to identify what would be the post comment) Sort of like this: Posts --…
-
4
votes2
answers355
viewsForeign Key does not respect referential integrity
I’m trying to put together tables and add one chave estrangeira, for example, a shopping list has referência of id from the buyers' table. I did it this way: alter table compras add foreign key…
-
4
votes1
answer462
viewsWhat is the foreign key (SQL) for
For what I’ve been researching, when pulling the information of two tables in a database, JOIN is used, which has nothing to do with foreign key. So what’s the point of declaring a field as Foreign…
-
4
votes1
answer2433
viewsPostgresql - Partial Foreign Key of Composite Primary Key
Fala galera, I need to create a structure model where all my tables have primary key composed by id and version, so I can have multiple versions for each id. Problem: CREATE TABLE table1 { id…
-
3
votes1
answer264
viewsRelational model in Firebird with C#
Developing a small C# project with Visual Studio, Firebird bank and ibexpert. With the tables WITHOUT RELATIONSHIP I can perform everything well. But I need to relate CLIENTE 1:N with BONUS And…
-
3
votes2
answers220
viewsIs it worth putting two foreign keys on a table?
having the tables: Companies Customers empresa_id: foreign key Sales cliente_id: foreign key and the relationships are: Companies and Clients => 1:N Customers and Sales => 1:N Considering that…
-
3
votes5
answers1137
viewsPostgresql - Remove tuple referenced by another table
I’m having a problem that I didn’t find anything in Google (maybe I didn’t know to ask him) Just imagine the following: I have 11 tables, one call tbl_pai and 10 other calls tbl_filha1, tbl_filha2,…
-
3
votes1
answer32
viewshow to create a postgresql table? how do you use alter table and drop table? and what are the differences between Primary key and Foreign key?
People help me with postgresql studies.How do you create a postgresql table? how do you use the alter table and drop table? and what differences of key Primary and key Foreign ?
-
3
votes1
answer8774
viewsError creating Mysql foreign key in phpMyAdmin?
I’m having trouble creating foreign keys on mysql through the phpmyadmin. I have these tables CREATE TABLE `cidade` ( `idCidade` int PRIMARY KEY AUTO_INCREMENT, `nome` varchar(25), `Uf` CHAR(2) );…
-
3
votes1
answer3038
viewsForeign key creation error in Mariadb
When trying to create the toy table, the following error appears: ERROR 1005 (HY000): Can’t create table brinquedos.brinquedo (Rrno: 150 "Foreign key Constraint is incorrectly Formed") Follows the…
-
2
votes1
answer572
viewsUsing Foreign key with Cascade
On my system, when deleting a post, all comments that have the id_publicacao same as the publication are deleted from a table called comentarios_publicacao. And I do it simply by doing a check like:…
-
2
votes1
answer1016
viewsPopulate tables with multiple foreign keys
I have a database with the following tables: Airport ( Iata PK, airport, city, state, country, lat, long) Causa_cancellation ( code PK, description) Manufacturer (Idfabricante PK, manufacturer name)…
-
2
votes2
answers1206
viewsHow to reference foreign keys in registration forms?
I’m making a library website as college work, which has the following table in the database: My problem is that I don’t know how to handle php code and the form of fields that receive foreign keys.…
-
2
votes1
answer929
viewsmysql Workbench error
I’m trying to make a database for an academic project, I’m doing the model on the Workbench and then I try to synchronize with phpmyadmin, it turns out that when making the Foreign Keys the same…
-
2
votes1
answer1778
viewsKey Foreign Relationship Problem in Phpmyadmin - Wamp
I have 'problem' in my Wamp, more precisely in the latest version (Wampserver 3.0.6 64 bit x64). When I create 2 tables, example: CREATE TABLE categoria( id_categoria INT NOT NULL AUTO_INCREMENT,…
-
2
votes1
answer201
viewsError while deleting data in Mysql by Java
In the database I have three tables, where one of them keeps the foreign keys. Error made: Error: com.mysql.jdbc.exceptions.jdbc4.Mysqlintegrityconstraintviolationexception: Cannot delete or update…
-
2
votes1
answer114
viewsRelating distinct foreign keys in a column?
Hello, I am creating a table to register materials or services that depend on two other tables to form your cost center (Foreign key), depending on the type of registration. If I’m going to register…
-
2
votes1
answer289
viewsForeign key with UNIQUE
What happens if I put one foreign key as UNIQUE in a table? I tested that possibility but maybe I misunderstood, if we apply a UNIQUE in a foreign key and insert the wrong value in this table, the…
-
1
votes1
answer1387
viewsInsert into two tables in Android Sqlite
How do I add data to two tables on Android in a single transaction? I have two tables Client and Address, is last with a foreign key referencing the client table, and must be registered in a single…
-
1
votes3
answers906
viewsCan a primary key be used on a foreign key?
The following example may occur? | Id | nome | data | qnt | PK -> Id FK -> (Id, qnt) Or is there some kind of restriction?
-
1
votes1
answer233
viewsDoubts about the datagridview
Hello I have some questions about the datagridview of visual studio, I would like to know how I can change the values of the columns that appear to Foreign key of sql server by the value of the…
-
1
votes1
answer305
viewsseveral foreign SQL keys
I have the following table: items (ref) the attribute "ref" as the primary key of the table, may have associated several secondary keys in other tables [ex:Tabela1(ref) and table2(ref) being "ref"…
-
1
votes1
answer408
viewsInsert to foreign SQL keys
I am learning a programming language and I set out to develop a budget program. My first obstacle is to add the values of the primary keys in the foreign keys of the respective tables. This code…
-
1
votes0
answers552
viewsHow to put an attribute as a foreign and primary key at the same time?
Utilise Mysql Workbench and I have to represent a DER in the physical model. I come across a specialization: TABELA_PESSOA cpf* nome sexo TABELA_CLIENTE cpf_pessoa* email senha TABELA_FUNCIONARIO…
-
1
votes2
answers410
views1215 SQL code "Cannot add Foreign key Constraint"
When creating this code always comes the error: 1215 "Cannot add Foreign key Constraint" I’ve tried everything, checked the foreign key restrictions and nothing. Someone can help me? create table…
-
1
votes1
answer435
viewsHow to insert ON DELETE and ON UPDATE
I have a database structure MySQL ready, but the ALTER TABLE of FOREIGN KEY's did not specify settings for ON DELETE and ON UPDATE. How do I enter these settings now, with tables and keys already…
-
1
votes0
answers731
viewsData insertion - Foreign keys in Java
I’m having a little trouble implementing the foreign keys in my project. Daoproduct.java. public class DAOProdutoImpl implements DAOProduto { private GerenciadorDeConexao gc; public…
-
1
votes1
answer1898
viewsDifference and use between Primary Key and Foreign Key
I did a database modeling relating some tables and at the end of everything I was told that I was wrong for not having PK and FK keys. The question is, how do I differentiate a Primary Key from a…
-
1
votes1
answer110
viewsShould I use foreign_key in both has_many and belongs_to?
I have two models. Office and Employee. Employee has office_id as foreign key. So, which is correct? class Office < ActiveRecord::Base has_many :employees, foreign_key: 'office_id' end class…
-
1
votes1
answer122
viewsCan I get more than one FK referencing the same PK?
I wonder if it is possible to reference foreign keys (FK) from different tables to the same primary key (PK). Below is a piece of the SQL command CREATE TABLE Run( Project_Code INT FOREIGN KEY…
-
1
votes3
answers1439
viewsSaving ID from a table with two Primary Keys with Hibernate
Hello, I am having a problem while saving data in one last table. Well, this table has two Foreign Keys and the same are Primary Keys: When going to Controller, save the table, I use the following…
-
1
votes1
answer3398
viewsForeign Key Sql Server
I have a question about Foreign key in the following situation: Tehno table UF, with 3 columns: ID_UF (PK), Cod_uf (int) and Descuf (nvarchar(50)) I have another table called IDE, with 3 columns:…
-
1
votes2
answers1305
viewsHow to store product lists of an order in a database table?
I have the following database modeling so far: I’m wondering how best to store the products in the order table. One string with Id of all products for each order in the table? I think very bad. What…
-
1
votes2
answers133
viewsCakephp - 1~n ratio
Good evening, I’m having a hard time on Cakephp 3 and the problem is this:: Cannot Insert Row, some of the Primary key values are Missing. Got (, ), expecting (idstorie, users_iduser) The system…
-
1
votes1
answer1944
viewsAuto-relationship in mysql
I am trying to implement a table in MYSQL with self relationship, it is a table of employee registration, in which the administrator employee (F.A) registers the common employee (F.C) It would be…
-
1
votes1
answer79
viewsWhat does a foreign key actually do in the database?
The foreign key in Mysql does some action in the database, or just serves for me to "see" which fields are related there is an auxiliary table? How do I make a foreign key in Mysql?
-
1
votes1
answer507
viewsString binding of SQL tables instead of id
Data from user accounts id nome sobrenome pais estado cidade telefone url_perfil Login data of users id email senha prioridade My doubts are: How can I link them to foreign key using string ? It is…
-
1
votes0
answers395
viewsJava Hibernate Query (HQL) with foreign key... ERROR!
I need help with my code, I have two tables: -CDF (official register) CREATE TABLE public.cdf( ativo character varying(1) COLLATE pg_catalog."default", reg integer NOT NULL, nome character…
-
1
votes0
answers40
viewsDoubt in the order of the fields of the table in case of foreign keys with less importance hierarchically
Hello dear programmers! I’m just a beginner in the world of programming and Stackoverflow and need to ask a question! I’m making some adjustments in a database I’m working on after finding great…