MYSQL - 如何获得第二个唯一的字符串键?
这是我的货物表。
+----------------------+---------------+------+-----+---------+-------+
| 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 UNIQUE 约束。有关详细信息,请查看此处。
You can use the UNIQUE constraint. For more information check here.
我认为你根本不想这样做。相反,
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 togoods_category.id
. If the insertion order is important, you can add aninsert_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.
首先你应该做数据库方面的所有事情。所以没有php.这就是我的建议。
然后在MySQL中没有序列,我会建议你:
然后你插入
这样你将永远有下一个可用的数字。
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 :
Then you insert
This way you will always have the next number available.
您可以做到这一点,但您需要非常小心,以确保两个同时插入不会获得相同的 gkey。我会用这两个设计点来设计它:
详细信息如下:
1. 在 GoodsCategory 表中添加几列:
KeyCode 具有“BOOK”或“PHONE”,NextID 字段存储一个 int,用于生成该类型的下一个键,即如果表如下所示
:下次添加一本书时,应为其指定 gkey“BOOK-3”,并且 NextID 递增到 4。
2:锁定需要在存储例程<中完成/strong>,因为它涉及事务中的多个语句并且也使用局部变量。它的核心应该是这样的:
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:
Here's the details:
1. Add a couple of columns to the GoodsCategory table:
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:
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:
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.