我们可以使用与两列主键有关的多估属性吗?
我正在研究一个Java项目,该项目在MySQL软件中开发了一个数据库,并且该作业包括由由两列标识的实体,名为cpf
和id> id
。
该实体具有一些多价属性,例如手机
,并且像往常一样,我使用primary键(ID,CPF)
标识,为多估件属性创建一个新表格,因此,我们可以将它们之间的桌子联系起来。
但是,我无法在我的手机
表中创建多个行,MySQL警告我错误代码:1062。重复的键'cpf'for键'手机.cpf'
。
总而言之,我正在尝试使用两列主键来识别实体,并使用此主键来创建一个多估件的属性表,但我没有成功。
我要在我的代码下方留下参考我创建两列主键(pessoa)的实体,并参考了我要创建的多估计表(Telefones-我上面提到的手机)的代码。
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
);
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
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要在手机表中添加多个行,则无法在多个字段上添加主键。您必须从电视节目中删除主键,并将两个行索引。由于两行主键,它将不接受ID和CPF重复行。
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.