'Can we have multivalored attributes related to a two-column primary key?
I'm working on a Java project that has a Database developed in MySQL software, and this job consists on having an entity identified by two columns, named CPF and ID.
This entity has some multivalored attributes, like CELLPHONE, and as usual, I'm creating a new table for multivalored attributes, using the PRIMARY KEY(ID,CPF) identification, so we can relate the tables between them.
However, I'm not able to create multiple rows in my CELLPHONE table, and MySQL warns me Error Code: 1062. Duplicate entry 'CPF' for key 'cellphones.CPF'.
In summary, I'm trying to use a two-column primary key to identify an entity, and using this primary key to create a multivalored attribute table, but I'm not having success.
I'm leaving below my code referring to the entity where I create the two-column primary key (PESSOA) and the code referring the multivalored table I'm trying to create (TELEFONES--the CELLPHONE I mentioned above).
CREATE TABLE PESSOA(
CPF VARCHAR(12) NOT NULL UNIQUE,
ID BOOL NOT NULL,
NOME VARCHAR(40) NOT NULL,
SEXO BOOL,
FOTO BLOB,
SALDO_BANCARIO DECIMAL(5,2) NOT NULL,
PRIMARY KEY(ID,CPF)
);
CREATE TABLE TELEFONES(
CPF VARCHAR(12) NOT NULL UNIQUE,
ID BOOL NOT NULL,
NUMERO VARCHAR(15),
FOREIGN KEY (ID,CPF) REFERENCES PESSOA(ID,CPF) ON DELETE CASCADE ON UPDATE CASCADE
);
Solution 1:[1]
If you want to add multiple row in cellphone table then you can't add primary key on multiple field. You have to remove primary key from TELEFONES and make both row indexed. Because of two row primary key it will not accept ID and CPF duplicate row again.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jagdish Bhadani |
