由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - 如何把某些行的某个列(unique)的值加1?
相关主题
Problem when using SQL " Insert...." to AutoNumber.Merge table with one single query?
MS T-SQL 问题SQL Conditional Select
Oracle insert primary key violation问一个SQL Server的问题
请教2个sql query 问题谁给我优化一下把重复的产品下架的SQL
About INSERT IGNORE请教大牛一道有趣的SQL题
[转载] Can anyone interpret this simple SQL?紧急求助, 关于SQL Server
今典问题: 这个Self Query咋写?如何在Unbuntu下启动oracle
Access门外汉问题求教urgent help! insert value into table
相关话题的讨论汇总
话题: coolbid话题: indie话题: sql话题: unique话题: insert
进入Database版参与讨论
1 (共1页)
b***i
发帖数: 3043
1
比如列叫column,必须满足column > 10,由于unique, 不能直接加,因为会暂时重复
怎么做呢?
c*****d
发帖数: 6045
2
update table_name
set column_1 = column_1 + 1

【在 b***i 的大作中提到】
: 比如列叫column,必须满足column > 10,由于unique, 不能直接加,因为会暂时重复
: 怎么做呢?

r**********d
发帖数: 510
3
the column should be processed desc.
probably can do it in a stored proc.
for each
select *
from t
order by col desc
loop
if > 10 then
update
end if
end loop
if new col doesnt have to be old col + 1, you can update set col = next
value of the sequence.
c*****d
发帖数: 6045
4
tom kyte说过,能用一句sql完成的,永远不要用cursor或者stored procedure
这个用一个update就搞定了

【在 r**********d 的大作中提到】
: the column should be processed desc.
: probably can do it in a stored proc.
: for each
: select *
: from t
: order by col desc
: loop
: if > 10 then
: update
: end if

c*****d
发帖数: 6045
5
to: bihai & rslgreencard
SQL> select * from v$version;
BANNER
------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE 11.2.0.4.0 Production
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production
5 rows selected.
SQL> create table coolbid (id number unique);
Table created.
SQL> insert into coolbid values (1);
1 row created.
SQL> insert into coolbid values (2);
1 row created.
SQL> insert into coolbid values (3);
1 row created.
SQL> insert into coolbid values (3);
insert into coolbid values (3)
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.SYS_C009453) violated
SQL> select * from coolbid;
ID
----------
1
2
3
3 rows selected.
SQL> update coolbid set id=id+1;
3 rows updated.
SQL> select * from coolbid;
ID
----------
2
3
4
3 rows selected.
r**********d
发帖数: 510
6
select @@version;
Microsoft SQL Server 2012 - 11.0.2218.0 (X64)
Jun 12 2012 13:05:25

Copyright (c) Microsoft Corporation
Enterprise Evaluation Edition (64-bit) on Windows NT 6.1 (Build
7601: Service Pack 1)
CREATE TABLE coolbid
(
ID INT UNIQUE
);
GO
INSERT INTO coolbid
VALUES (1),
(2),
(3);
GO
(3 row(s) affected)
INSERT INTO coolbid
VALUES (1);
GO
Msg 2627, Level 14, State 1, Line 2
Violation of UNIQUE KEY constraint 'UQ__coolbid__3214EC26654E5167'. Cannot
insert duplicate key in object 'dbo.coolbid'. The duplicate key value is (1).
The statement has been terminated.
SELECT * FROM coolbid;
1
2
3
UPDATE coolbid
set id = id + 1;
works in sql server.
indie=> select version();
version

----------------------------------------------------------------------------
---------------------------
PostgreSQL 9.1.6 on x86_64-unknown-linux-gnu, compiled by gcc-4.4.real (
Debian 4.4.5-8) 4.4.5, 64-bit
(1 row)
indie=> CREATE TABLE coolbid
indie-> (
indie(> ID INT UNIQUE
indie(> );
NOTICE: CREATE TABLE / UNIQUE will create implicit index "coolbid_id_key"
for table "coolbid"
CREATE TABLE
indie=>
indie=> INSERT INTO coolbid
indie-> VALUES (1),
indie-> (2),
indie-> (3);
INSERT 0 3
indie=>
indie=> INSERT INTO coolbid
indie-> VALUES (1);
ERROR: duplicate key value violates unique constraint "coolbid_id_key"
DETAIL: Key (id)=(1) already exists.
indie=>
indie=> SELECT * FROM coolbid;
id
----
1
2
3
(3 rows)
indie=>
indie=> UPDATE coolbid
indie-> set id = id + 1;
ERROR: duplicate key value violates unique constraint "coolbid_id_key"
DETAIL: Key (id)=(2) already exists.
doesnt work in postgres.
thanks for the posting. I learned something new today.
B*****g
发帖数: 34098
7
大师讲一讲里面的机制吧

Production

【在 c*****d 的大作中提到】
: to: bihai & rslgreencard
: SQL> select * from v$version;
: BANNER
: ------------------------------------------------------------------
: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
: PL/SQL Release 11.2.0.4.0 - Production
: CORE 11.2.0.4.0 Production
: TNS for Linux: Version 11.2.0.4.0 - Production
: NLSRTL Version 11.2.0.4.0 - Production
: 5 rows selected.

1 (共1页)
进入Database版参与讨论
相关主题
urgent help! insert value into tableAbout INSERT IGNORE
[转载] help database question[转载] Can anyone interpret this simple SQL?
复制ORA-02062错误今典问题: 这个Self Query咋写?
a simple question about T-SQLAccess门外汉问题求教
Problem when using SQL " Insert...." to AutoNumber.Merge table with one single query?
MS T-SQL 问题SQL Conditional Select
Oracle insert primary key violation问一个SQL Server的问题
请教2个sql query 问题谁给我优化一下把重复的产品下架的SQL
相关话题的讨论汇总
话题: coolbid话题: indie话题: sql话题: unique话题: insert