我们可以使用与两列主键有关的多估属性吗?

发布于 2025-01-24 03:30:07 字数 1003 浏览 5 评论 0原文

我正在研究一个Java项目,该项目在MySQL软件中开发了一个数据库,并且该作业包括由由两列标识的实体,名为cpfid> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

压抑⊿情绪 2025-01-31 03:30:07

如果要在手机表中添加多个行,则无法在多个字段上添加主键。您必须从电视节目中删除主键,并将两个行索引。由于两行主键,它将不接受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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文