简体   繁体   English

如何根据 Postgresql 中的列值是否相同而插入表中

[英]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.否则,如果表中不存在具有相同numvalue的记录,则只有它会在表中插入记录。

below is the example to execute this procedure:以下是执行此过程的示例:

EXEC dbo.InsertRecVali(1,'abc',6)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据同一表中另一列的几何形状将值插入一列 - How to insert value to a column as a result of geometry of another column in the same table 如何在向另一个表的属性插入值的基础上增加一个表的属性值? - How can I increase the value of an attribute of one table on the basis of the insertion of value to an attribute of another table? 如何在 postgresql 中插入其他表的列 - How to insert column of other table in postgresql 如何按特定列值将记录从表插入到另一个表顺序? - How can I insert records from a table to another table order by the specific column value? 我如何插入到table1中,其中table2.column = value? - How can i insert into Table1 where table2.column = value? 如何拆分字符串值并将其插入同一行的不同列中? - How can I split a string value and insert it in a different column in the same row? 如何基于sql中同一表中的另一列设置列的总和值的别名? - how can i set the alias of column`s sum value based on another column in same table in sql? 如何根据同一表中另一列的顺序更新该列中的所有值? - How can I update all the value in the column base on the order of another column in the same table? 如何在列包含特殊字符的表中插入值? - How can I insert a value into a table where the column contains special character? 如何在其值来自另一个表的位置插入新列? - How can I insert new column where its value coming from another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM