MySQL 不尊重唯一键和主键

发布于 2024-11-09 04:08:48 字数 759 浏览 4 评论 0原文

嘿。
首先,我不得不说,这是我第一次尝试编写 SQL,这意味着我是一个 n00b。请耐心等待。

现在,我正在尝试在数据库中创建一个名为“push”的表,如下所示:

CREATE TABLE push
(id int NOT NULL AUTO_INCREMENT,
UDID varchar(40) NOT NULL,
token varchar(64) NOT NULL,
lastpost int DEFAULT '0',
PRIMARY KEY(id),
UNIQUE KEY(id, UDID, token));

可以,但不符合预期。如果我现在尝试在此处插入一些值,如下所示:

INSERT INTO push (UDID, token, lastpost)
VALUES ('123456789abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwqyz123456789', 211);
INSERT INTO push (UDID, token, lastpost)
VALUES ('123456789abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwqyz123456789', 211);

在我看来,这会导致错误,因为 UDID 和令牌相等,但它根本不会触发任何错误,它只是插入重复项。

我可能在这里错过了一些东西,但我无法找出是什么。我怎样才能让它返回预期的结果?
谢谢。

Hey.
To start with, I have to say that this is the first time I have ever tried to write SQL, which means I'm a n00b. Have some patience, please..

Now, I'm trying to create a table called "push" in my database like this:

CREATE TABLE push
(id int NOT NULL AUTO_INCREMENT,
UDID varchar(40) NOT NULL,
token varchar(64) NOT NULL,
lastpost int DEFAULT '0',
PRIMARY KEY(id),
UNIQUE KEY(id, UDID, token));

That works, but not as expected. If I now try to insert some values here like this:

INSERT INTO push (UDID, token, lastpost)
VALUES ('123456789abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwqyz123456789', 211);
INSERT INTO push (UDID, token, lastpost)
VALUES ('123456789abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwqyz123456789', 211);

That would, in my eyes, cause an error, because the UDID and token are equal, but it does not trigger any error at all, it just inserts the duplicate.

I might have missed something here, but I can't find out what. How can I make this return the expected result?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

憧憬巴黎街头的黎明 2024-11-16 04:08:48

this:

UNIQUE KEY(id, UDID, token));

意味着这 3 者的组合应该是唯一的。两行的 id 字段(自动增量)将不同,因此它将满足该规则

如果组合应该是唯一的,请使其不带 id

UNIQUE KEY(UDID, token));

this:

UNIQUE KEY(id, UDID, token));

Means that the combination of those 3 should be unique. The id field (auto-increment) will be different for the 2 rows, so it will satisfy that rule

If the combination should be unique, make it without the id

UNIQUE KEY(UDID, token));
靖瑶 2024-11-16 04:08:48

此处的此位

UNIQUE KEY(id, UDID, token));

可确保您始终可以输入重复的 UDID 值,并且该值将被接受,因为唯一性测试包括 ID;它被定义为auto_increment。从此密钥中删除ID,您应该得到您想要的检查,即

CREATE TABLE push
(id int NOT NULL AUTO_INCREMENT,
UDID varchar(40) NOT NULL,
token varchar(64) NOT NULL,
lastpost int DEFAULT '0',
PRIMARY KEY(id),
UNIQUE KEY(UDID, token)); -- ID now excluded here

This bit here

UNIQUE KEY(id, UDID, token));

ensures that you can always enter a duplicate value for UDID, and it will be accepted as the test for uniqeness includes ID; which is defined as auto_increment. Remove ID from this key and you should have the check you want i.e.

CREATE TABLE push
(id int NOT NULL AUTO_INCREMENT,
UDID varchar(40) NOT NULL,
token varchar(64) NOT NULL,
lastpost int DEFAULT '0',
PRIMARY KEY(id),
UNIQUE KEY(UDID, token)); -- ID now excluded here
所谓喜欢 2024-11-16 04:08:48

如果您希望 UDIDtoken 都是唯一的,或者换句话说,不应允许两个 UDID 相同,也不应允许两个令牌相同,请尝试:

CREATE TABLE push
  ( id int NOT NULL AUTO_INCREMENT,
    UDID varchar(40) NOT NULL,
    token varchar(64) NOT NULL,
    lastpost int DEFAULT '0',
    PRIMARY KEY(id),
    UNIQUE KEY( UDID ),
    UNIQUE KEY( token )
  ) ;

If you want both UDID and token to be unique or in other words no two UDID should be allowed to be the same nor two token should be allowed to be the same, try:

CREATE TABLE push
  ( id int NOT NULL AUTO_INCREMENT,
    UDID varchar(40) NOT NULL,
    token varchar(64) NOT NULL,
    lastpost int DEFAULT '0',
    PRIMARY KEY(id),
    UNIQUE KEY( UDID ),
    UNIQUE KEY( token )
  ) ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文