Incrementing a column value in a SQL database if there is a duplicate row

sql query to increment a column value by 1
sql increment value by 1 in select
sql increment value by 1 in insert
alter column auto increment sql server
auto increment in sql query
sql server increment value by 1 in select
how to update a column with sequence number in sql server
update column with incremental values in sql

Suppose I have a table like this:

Number   Product    Type
 1        Meat      Cow
 1        Milk       A

If I insert a new row:

INSERT INTO t1 (Product, Type) VALUES (Meat, Cow)

I don't want a new row. I want the column "Number" where Product = Meat and Type = Cow be incremented by 1:

Number   Product    Type
 2        Meat      Cow
 1        Milk       A

Is there a SQL command to do this?


You may try the following by using ON CONFLICT that starts from version 9.5 :

create table t1( Number int, 
                 Product varchar(15), 
                 Type varchar(15),
                 primary key (Product, Type)
               );

insert into t1(Number,Product,Type) values(1,'Meat','Cow');               

insert into t1(Number,Product,Type) values(1,'Meat','Cow')
on conflict (Product,Type) do update set Number = t1.Number + 1;
select * from t1;  

number  product type
------  ------- ----
  2      Meat   Cow

Rextester Demo

where composite unique(primary) key is a must for Product and Type columns, if not exists than 42P10: there is no unique or exclusion constraint matching the ON CONFLICT specification error raises.

I have a database table that has a lot of data already in the table and I Using an Identity Column to Increment the Value by 1 In this step we are doing an UPDATE to the table and for each row Warning: Possible Duplicate Values If you have the need to add a new sequential value to your tables or  Table 'accounts'. Scan count 1, logical reads 23751, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. SQL Server Execution Times: CPU time = 6281 ms, elapsed time = 48701 ms. SQL Server Execution Times: CPU time = 6281 ms, elapsed time = 48474 ms.


You can use triggers to achieve what you want. Format :

CREATE OR REPLACE TRIGGER trigger_name
INSTEAD OF INSERT ON table_name
WHEN (your_condition_here) --for example, row exist 
BEGIN 
    UPDATE ... --your update query
END;

The world's most popular open source database XA Transaction SQL Statements With an auto-increment column, an INSERT statement increases the With ON DUPLICATE KEY UPDATE , the affected-rows value per row is 1 if the row is Beginning with MySQL 8.0.19, it is possible to use an alias for the row, with,  SQL only one cell from column can have X value mysql,sql Is there a way to ensure, when you design a table - SQL, MySQL -, that only one cell from column X is true, the rest being automatically false? When you change the value of a different cell to true, all other cells should automatically become false. A radio button


Create Unique Index on Product and Type, then

INSERT INTO t1 (Product, Type) VALUES (Meat, Cow) 
ON CONFLICT ON CONSTRAINT <your_key> 
UPDATE set Number = Number + 1

Should work.

The world's most popular open source database XA Transaction SQL Statements With an auto-increment column, an INSERT statement increases the With ON DUPLICATE KEY UPDATE , the affected-rows value per row is 1 if the row is locks any partitions of the table in which a partitioning key column is updated. The duplicate rows will have repeated values in the a and b columns, but different row numbers as shown in the following picture: Second, the outer query removes the first row in each group. Generally, This statement uses the ROW_NUMBER() function to find the duplicate values in one column of a table:


If it can be solved via an AD-HOC Query or similar structure, I would needs to work is that if there is only one row in the returned data that has a given -​duplicate-values-in-column-of-sql-table-increment-a-count-column-by. This primary key column is known as an identity or auto increment column. When a new row is inserted into the auto-increment column, an auto-generated sequential integer is used for the insert. For example, if the value of the first row is 1, then the value of the second row is 2, and so on. Some databases systems allow you to define the


This data tutorial will explain basic table creation and information around using When a new record is inserted, we not only must manually enter a value for id , but we columns can contain duplicate, non-unique values, but the PRIMARY KEY SQL Server to auto increment the numeric value within the specified column  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Incrementing a column value in a SQL database if there is a duplicate row


Write a SQL statement to insert a record with your own value into the table ensure that the country_id column will not contain any duplicate data and incremented and the column country_name will be filled up by 'N/A' if no  Please Sign up or sign in to vote. I am facing a problem related to SQL query. I need to update a table column values (data type 'int') in increment/decrement on single update query. Like..there are values in column1 as:- Please Sign up or sign in to vote. But this is for simple +1 known value addition. If OP was looking for just single fixed