mysql主键不唯一,mysql不唯一自动递增,主键两个字段

I want to create a table structure like this in MySQL:

id | area | name

----+----------+------------

1 | name-1 | test

| |

1 | name-1 | value2

| |

2 | name-2 | test-value

| |

3 | name-3 | test

| |

3 | name-3 | test

ie. the primary key will be: primary_key( id, area ) and the id will be auto_increment, but i only want the id to increment for every new unique area

is this possible?

解决方案

What you want is not possible. You want id and area to be the primary key but in your example they are not unique.

If you define a table key it must be unique and in your example that would mean you need to include name in your primary key. But why not make just id the primary key and auto-increment it? That is the common usage.

EDIT :

You could create an extra field called area_id for instance. You can add the auto-increment functionality to the field likE this:

CREATE TABLE `areas`

(

`id` INT NOT NULL ,

`area_id` INT NOT NULL AUTO_INCREMENT ,

`area` VARCHAR(100) NULL ,

`name` VARCHAR(100) NULL ,

PRIMARY KEY (`id`)

);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值