[英]How can i insert into table on the basis of a column value is same or not in Postgresql
I am inserting data into a table looks like this我正在将数据插入表中,如下所示
| num | name | value |
----------------------------------
| 1 | name1 | 1 |
| 2 | name2 | 1 |
| 3 | name3 | 1 |
| 4 | name4 | 2 |
| 5 | name5 | 3 |
I wanted to insert with where clause like insert into table (num, name, value) values(6,name,1) when (num and value together) not exist
in any row together我想用 where 子句插入,比如
insert into table (num, name, value) values(6,name,1) when (num and value together) not exist
I tried to select first and insert on basis of that result but I think that is not the best way I want it in a single query我首先尝试 select 并根据该结果插入,但我认为这不是我在单个查询中想要的最佳方式
tried like: select * from the table where name=$name and value= $value if I got result then not insert otherwise insert.
尝试过:
select * from the table where name=$name and value= $value if I got result then not insert otherwise insert.
It was done with two queries but i don't want it.它是通过两个查询完成的,但我不想要它。
Any help will be appriciated.任何帮助都会得到帮助。
Use a unique constraint to enforce uniqueness for (num, value)
:使用唯一约束来强制
(num, value)
的唯一性:
alter table t add constraint unq_t_num_value unique (num, value);
Then the database ensures that the integrity of the table -- that these values are unique.然后数据库确保表的完整性——即这些值是唯一的。 You don't have to do it explicitly.
你不必明确地这样做。
Note that if the unique constraint is violated, you get an error and the insert
is aborted (along with other rows that might be inserted).请注意,如果违反了唯一约束,您将收到错误并且
insert
被中止(以及可能插入的其他行)。 If you want to ignore the error instead, you can use on conflict ignore
.如果您想忽略错误,可以使用
on conflict ignore
。
Basically, first you need to do check if record with same num and Value is already present in table or not.基本上,首先您需要检查具有相同 num 和 Value 的记录是否已经存在于表中。 if it is present then don't insert else insert the new record.
如果存在则不要插入,否则插入新记录。
for this you can try to insert value with procedure:为此,您可以尝试使用过程插入值:
the below procedure will help based on your need:以下程序将根据您的需要提供帮助:
create procedure InsertRecVali(@num int,@name varchar(max),@value int)
as
begin
if not exist(select 1 from table where num=@num and value=@value)
insert into table values(@num,@name,@value)
Else
PRINT 'Cannot Insert Duplicate Value'
End;
After creating procedure, execute this procedure by passing the the values which you want to insert in table.创建过程后,通过传递要在表中插入的值来执行此过程。
so before inserting record in table, it will check if there is already record present in table with same num and value then it will not insert it and give error ' Cannot Insert duplicate Value '.所以在表中插入记录之前,它会检查表中是否已经存在具有相同数量和值的记录,然后它不会插入它并给出错误“无法插入重复值”。
otherwise if the record with same num and value is not already present in table then only it will insert record in table.否则,如果表中不存在具有相同num和value的记录,则只有它会在表中插入记录。
below is the example to execute this procedure:以下是执行此过程的示例:
EXEC dbo.InsertRecVali(1,'abc',6)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.