English 中文(简体)
在MySQL中,当出现“重复入境”错误时,我如何防止主要钥匙自动焚化?
原标题:In MySQL, when there is a "duplicate entry" error, how do I prevent the primary key from auto incrementing?

我不想 the首,即使它打上了重复的入境错误!

最佳回答

您可使用表格和触发器具,以实施像顺序一样的矿石:

drop table if exists users_seq;
create table users_seq
(
next_val int unsigned not null default 0
)
engine=innodb;

drop table if exists users;
create table users
(
user_id int unsigned not null,
username varchar(32) unique not null
)
engine=innodb;

delimiter #

create trigger users_before_ins_trig before insert on users
for each row
begin
 declare v_id int unsigned default 0;

 select next_val + 1 into v_id from users_seq;

 set new.user_id = v_id;

 update users_seq set next_val = v_id;

end#

delimiter ;

insert into users_seq values (0);
insert into users (username) values ( alpha ),( beta );

Query OK, 2 rows affected, 1 warning (0.03 sec)

select * from users_seq;

+----------+
| next_val |
+----------+
|        2 |
+----------+
1 row in set (0.00 sec)

select * from users;

+---------+----------+
| user_id | username |
+---------+----------+
|       1 | alpha    |
|       2 | beta     |
+---------+----------+
2 rows in set (0.00 sec)

insert into users (username) values ( alpha );

ERROR 1062 (23000): Duplicate entry  alpha  for key  username 

select * from users_seq;
+----------+
| next_val |
+----------+
|        2 |
+----------+
1 row in set (0.00 sec)

select * from users;

+---------+----------+
| user_id | username |
+---------+----------+
|       1 | alpha    |
|       2 | beta     |
+---------+----------+
2 rows in set (0.00 sec)

insert into users (username) values ( gamma );

Query OK, 1 row affected, 1 warning (0.03 sec)

select * from users_seq;

+----------+
| next_val |
+----------+
|        3 |
+----------+
1 row in set (0.00 sec)

select * from users;

+---------+----------+
| user_id | username |
+---------+----------+
|       1 | alpha    |
|       2 | beta     |
|       3 | gamma    |
+---------+----------+
3 rows in set (0.00 sec)

希望它有助于:

问题回答

回答载于你的问题:简言之,这并不使它自动增加一栏。 相反,数字显示正确的价值分析,处理重复的关键错误。

仅仅把这个领域确定为是绝对的,而没有非洲联盟-联合国达尔富尔混合行动,你就可以控制这个领域的价值。

就在这种“丑闻”中。 与这种行为一样,因为它使我申请中的“UNI Request”一栏的任何使用不相称。

www.un.org/Depts/DGACM/index_spanish.htm 因此,我的解决办法是:,在添加和从独一无二钥匙上辞职之前,检查是否存在独特的钥匙(与选取钥匙)。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签