0
Hello, everybody!
Well... here’s my problem: I have a Mysql database that has 3 tables (Client, Event, Equipment). The event table has two foreign keys, the first of them referencing the id of the Client table (id_client) and the second referencing the id of the Equipment table. When entering values in the Client and Equipment tables everything ok, but when entering values in the Event table, referencing a specific id of the other tables, the inserted values also reference the other registered id’s.
To be clearer:
An example: I will register an event contracted by the id 1 client, put all the data in their respective places, including the id number equal to 1 where the foreign key value has to be. The code is executed, but when I run a "Select" to check, the entered data is also referencing the id client 2.
"Select" displaying the values entered in the Event table:
This "Select" shows the problem. The value "Santo Antônio" in the "city" column should belong only to the id 1 account.
PS: I’m sorry for any mistakes, I’m new here, and I’ve never even participated in question and answer websites. Anyway... Can you help me? What should I do? Why am I facing this
The problem is that by making one
SELECT
comes the result of allCliente
? I’m sorry, but I don’t understand your problem.– Thiago Magalhães
No, @Thiagomagalhães. I’ll use the example I used in the post. The problem is as follows: Suppose I have registered an event contracted by an id client 1. The entered data (using INSERT INTO), instead of referencing only the id client 1 (which was passed as the primary key value in INSERT INTO) also reference the id client 2. That is, all information of the event contracted by the id client 1, are registered as if they were also id client 2. Got it?
– Jov1c
understood. can put the structure of tables?
– Thiago Magalhães
Yes, @Thiagomagalhães. I will post one at a time because of the character limit.
– Jov1c
create table client( id_client int auto_increment not null, name varchar(35), numero varchar(15), Primary key(id_client) );
– Jov1c
create table equipment( id_equipment int auto_increment not null, name varchar(8), Primary key(id_equipment) );
– Jov1c
create table event( id_event int auto_increment not null, endereco varchar(40), dia varchar(8), horario varchar(5), fk_client int not null, fk_equipamento int not null, Primary key(id_event), Constraint Foreign key (fk_client) Customer Resets client (id_client), Constraint Foreign key (fk_equipment) References equipment (id_equipment) );
– Jov1c
You’re making the select that way:
SELECT * FROM evento e WHERE e.fk_cliente = 1
?– Thiago Magalhães
@Thiagomagalhães, take a look at the prints I just put in the post, maybe it will help you understand the problem.
– Jov1c
Testing:
SELECT * FROM evento e
INNER JOIN cliente c ON (e.fk_cliente = c.id_cliente)
. If the city repeats, it is pq more than one event in the same city were registered to different customers.– Thiago Magalhães
NOTE: I changed the name of the column "address", which was in the structure of the table "event" shown above, to "city".
– Jov1c
@Thiagomagalhães, the information was displayed right now. But explain me one thing, if possible: why this problem was occurring?
– Jov1c
How was the consultation you were doing?
– Thiago Magalhães
@Thiagomagalhães, sorry for the delay in answering. I was making the query as follows: SELECT id_event, name, number, city, day, time FROM event, client WHERE id_event = 1; It was more or less like this, I don’t remember well.
– Jov1c
By the way... Thanks a lot for the help! The way you told me solved the problem. Thank you very much, @Thiagomagalhães!
– Jov1c