MYSQL - 如何获得第二个唯一的字符串键?

发布于 2024-10-07 04:39:29 字数 1620 浏览 0 评论 0原文

这是我的货物表。

+----------------------+---------------+------+-----+---------+-------+
| Field                | Type          | Null | Key | Default | Extra |
+----------------------+---------------+------+-----+---------+-------+
| ID                   | decimal(18,0) | NO   | PRI |         |       |
| gkey                 | varchar(255)  | YES  | MUL | NULL    |       |
| GOODS                | decimal(18,0) | YES  | MUL | NULL    |       |

列 ID 自动递增。 GOODS 是商品类别的 id。 这是商品类别表。

+-------+---------------------+
| ID    | NAME                |
+-------+---------------------+
| 1     | book                |
| 2     | phone               |
+-------+---------------------+

我的问题是在货物表中我需要 gkey 也是一个带有前缀 id 的唯一键(这里 id 从 1 开始。)像 BOOK-1,BOOK-2....PHONE-1,PHONE-2...当将新的货物记录插入货物表时。 像这样:

+--------------------+---------------+------+-----+---------+-------+
| ID                 | GKEY          |GOODS | PRI | COUNTRY | Extra |
+--------------------+---------------+------+-----+---------+-------+
| 1                  | BOOK-1        | 1    | 10  |         |       |
| 2                  | PHONE-1       | 2    | 12  |         |       |
| 3                  | BOOK-2        | 1    | 13  |         |       |
| 4                  | BOOK-3        | 1    | 10  |         |       |
| 5                  | PHONE-2       | 2    | 10  |         |       |
| 6                  | PHONE-3       | 2    | 20  |         |       |
+--------------------+---------------+------+-----+---------+-------+ 

如何在PHP+MYSQL中获取GKEY?

谢谢。

Here is my goods table.

+----------------------+---------------+------+-----+---------+-------+
| Field                | Type          | Null | Key | Default | Extra |
+----------------------+---------------+------+-----+---------+-------+
| ID                   | decimal(18,0) | NO   | PRI |         |       |
| gkey                 | varchar(255)  | YES  | MUL | NULL    |       |
| GOODS                | decimal(18,0) | YES  | MUL | NULL    |       |

Column ID is auto-increment.
GOODS is the id of a goods category.
Here is the goods category table.

+-------+---------------------+
| ID    | NAME                |
+-------+---------------------+
| 1     | book                |
| 2     | phone               |
+-------+---------------------+

My question is in goods table I need the gkey is also an unique key with prefix-id(here id is started from 1.) Like BOOK-1,BOOK-2....PHONE-1,PHONE-2... when insert a new goods record into goods table.
Like that:

+--------------------+---------------+------+-----+---------+-------+
| ID                 | GKEY          |GOODS | PRI | COUNTRY | Extra |
+--------------------+---------------+------+-----+---------+-------+
| 1                  | BOOK-1        | 1    | 10  |         |       |
| 2                  | PHONE-1       | 2    | 12  |         |       |
| 3                  | BOOK-2        | 1    | 13  |         |       |
| 4                  | BOOK-3        | 1    | 10  |         |       |
| 5                  | PHONE-2       | 2    | 10  |         |       |
| 6                  | PHONE-3       | 2    | 20  |         |       |
+--------------------+---------------+------+-----+---------+-------+ 

How to get that GKEY in PHP+MYSQL?

thanks.

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

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

发布评论

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

评论(4

梦太阳 2024-10-14 04:39:29

您可以使用 UNIQUE 约束。有关详细信息,请查看此处

You can use the UNIQUE constraint. For more information check here.

暮光沉寂 2024-10-14 04:39:29

我认为你根本不想这样做。相反,good.gkey 应该是 goods_category.id 的外键。如果广告订单很重要,您可以向商品表添加 insert_date 日期字段。

从那里您可以运行各种查询,并具有表上引用完整性的额外好处。

I don't think you want to be doing this at all. Instead, good.gkey should be a foreign key to goods_category.id. If the insertion order is important, you can add an insert_date date field to the goods table.

From there you can run all sorts of queries, with the added bonus of having referential integrity on your tables.

久夏青 2024-10-14 04:39:29

首先你应该做数据库方面的所有事情。所以没有php.这就是我的建议。

然后在MySQL中没有序列,我会建议你:

SELECT COUNT(id)
FROM GOODS
WHERE GKEY LIKE('BOOK-%')

然后你插入

INSERT INTO goods (id, "BOOK-" || SELECT MAX(SUBSTR(LENGTH(GKEY -1), LENGTH(GKEY))FROM GOODS WHERE GKEY LIKE('BOOK-%') + 1, ...)

这样你将永远有下一个可用的数字。

First you should do everything Database side. So no php. That would be my advice.

Then not having sequence in MySQL that what I would Suggest you :

SELECT COUNT(id)
FROM GOODS
WHERE GKEY LIKE('BOOK-%')

Then you insert

INSERT INTO goods (id, "BOOK-" || SELECT MAX(SUBSTR(LENGTH(GKEY -1), LENGTH(GKEY))FROM GOODS WHERE GKEY LIKE('BOOK-%') + 1, ...)

This way you will always have the next number available.

他不在意 2024-10-14 04:39:29

您可以做到这一点,但您需要非常小心,以确保两个同时插入不会获得相同的 gkey。我会用这两个设计点来设计它:

  1. 在 GoodsCategory 表中,有一个计数器,它有助于生成下一个 gkey。
  2. 使用,以便任何时候只有一个进程可以生成新密钥。

详细信息如下:
1. 在 GoodsCategory 表中添加几列:

+----------------------+---------------+------+-----+---------+-------+
| Field                | Type          | Null | Key | Default | Extra |
+----------------------+---------------+------+-----+---------+-------+
| ID                   | TINYINT       | NO   | PRI |         |       |
| NAME                 | VARCHAR(80)   | NO   |     |         |       |
| KeyCode              | CHAR(5)       | NO   |     |         |       |
| NextID               | INT           | NO   |     | 0       |       |
+----------------------+---------------+------+-----+---------+-------+

KeyCode 具有“BOOK”或“PHONE”,NextID 字段存储一个 int,用于生成该类型的下一个键,即如果表如下所示

+----------------+------------+---------+--------+
| ID             | NAME       | KeyCode | NextID |
+----------------+------------+---------+--------+
| 1              | book       | BOOK    | 3      |
| 2              | phone      | PHONE   | 7      |
+----------------+------------+---------+--------+

:下次添加一本书时,应为其指定 gkey“BOOK-3”,并且 NextID 递增到 4。

2:锁定需要在存储例程<中完成/strong>,因为它涉及事务中的多个语句并且也使用局部变量。它的核心应该是这样的:

START TRANSACTION;
SELECT KeyCode, NextID INTO v_kc, v_int FROM GoodsCategory WHERE ID = 2 FOR UPDATE;
SET v_newgkey = v_kc + '-' + CAST(v_int AS CHAR);
INSERT INTO goods (gkey, goods, ...) VALUES (v_newgkey, 2, etc);
UPDATE GoodsCategory SET NextID = NextID + 1 WHERE ID = 2;
COMMIT;

FOR UPDATE 位至关重要;查看 mysql 手册 其中讨论了如何使用锁来生成 ID,而不受其他进程的干扰。

You can do it, but you need to be quite careful to make sure that two simultaneous inserts don't get given the same gkey. I'd design it with these two design points:

  1. In the GoodsCategory table, have a counter which helps to generate the next gkey.
  2. Use locks so that only one process can generate a new key at any one time.

Here's the details:
1. Add a couple of columns to the GoodsCategory table:

+----------------------+---------------+------+-----+---------+-------+
| Field                | Type          | Null | Key | Default | Extra |
+----------------------+---------------+------+-----+---------+-------+
| ID                   | TINYINT       | NO   | PRI |         |       |
| NAME                 | VARCHAR(80)   | NO   |     |         |       |
| KeyCode              | CHAR(5)       | NO   |     |         |       |
| NextID               | INT           | NO   |     | 0       |       |
+----------------------+---------------+------+-----+---------+-------+

The KeyCode has 'BOOK' or 'PHONE', and the NextID field stores an int which is used to generate the next key of that type i.e. if the table looks like this:

+----------------+------------+---------+--------+
| ID             | NAME       | KeyCode | NextID |
+----------------+------------+---------+--------+
| 1              | book       | BOOK    | 3      |
| 2              | phone      | PHONE   | 7      |
+----------------+------------+---------+--------+

Then the next time you add a book, it should be given gkey 'BOOK-3', and NextID is incremented to 4.

2: The locking will need to be done in a stored routine, because it involves multiple statements in a transaction and uses local variables too. The core of it should look something like this:

START TRANSACTION;
SELECT KeyCode, NextID INTO v_kc, v_int FROM GoodsCategory WHERE ID = 2 FOR UPDATE;
SET v_newgkey = v_kc + '-' + CAST(v_int AS CHAR);
INSERT INTO goods (gkey, goods, ...) VALUES (v_newgkey, 2, etc);
UPDATE GoodsCategory SET NextID = NextID + 1 WHERE ID = 2;
COMMIT;

The FOR UPDATE bit is crucial; have a look at the Usage Examples in the mysql manual where it discusses how to use locks to generate an ID without interference from another process.

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