0
I started a project at the same time that I started studies in a database. I created a customer phone only table and another table for all customer data, including phone as Foreign key. in the phone table I created a key Primary and used the client id as Foreign key.. When I insert data into client table requires that phone table already has Primary key. Tables were created without problem. but when I enter data, in whatever table, I have problem. how should I do?
table_clientes(
idcliente int auto_increment,
nome varchar(10),
idfone int,
primary key(idcliente),
foreign key(idfone) references table_fone(idfone)
)
table_fone(
idfone int auto_increment,
fone1 varchar(10) not null,
fone2 varchar(10),
celular varchar(10),
primary key(idfone),
idcliente int,
foreign key (idcliente) references tbclientes (idcliente)
)
First, you must have an FK in each table, without it you can’t continue, even if it’s just a symbol. After, create the records in the primary table and after the record linked to the secondary table.
– Jakson Fischer
I think you should rethink your data model.
– anonimo
@Jaksonfischer, so I should create a PF(worthless) and use it as FK in the other table? Because I’ve already created a FK in each table.
– Marin
@anonimo, can you explain to me where my data model failed? so maybe you can rethink.
– Marin
If you only have one phone of each type per customer, you don’t need a separate table for phones. And if there is more than one, it makes no sense FK on the customer table. FK should be at only one end.
– bfavaretto
@bfavaretto, actually the phone table has phone1,phone2,extension, cell phone.. and so on. Then I should create only a phone table with the idclient to identify and perform query in this table to know the phone?
– Marin
That’s why I said "one phone of every kind". This way you modeled each row in the table is a "package" with potentially 3 phone numbers. But it’s not the only way to model. The principle is, if you determine that each customer can only have up to 3 phones, the simplest is to create 3 phone columns in the customer table. If the maximum number of phones per customer is undetermined, then yes the only way is to use a separate table. understood?
– bfavaretto
@bfavaretto, got it. It makes sense what you said. I’ll review my modeling. Thank you so much for your help.
– Marin