仅当表中尚不存在某个值时才插入 SQL?

发布于 2024-12-16 19:05:36 字数 271 浏览 1 评论 0原文

仅当表中尚无该数字时,如何将数字插入表中?

我正在寻找特定的 SQL 代码,不太确定如何解决这个问题。尝试了几件事,没有任何效果。

编辑

表如下所示:

PK   ID    Value
1     4     500
2     9     3

因此,如果我尝试INSERT (ID, Value) VALUES (4,100),它不应该尝试这样做!

How to insert a number into the table, only if the table does not already have that number in it?

I am looking for specific SQL code, not really sure how to approach this. Tried several things, nothing's working.

EDIT

Table looks like this:

PK   ID    Value
1     4     500
2     9     3

So if I am trying to INSERT (ID, Value) VALUES (4,100) it should not try to do it!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只有影子陪我不离不弃 2024-12-23 19:05:37

如果 ID 应该是唯一的,则应该在表上定义唯一约束。如果您尝试插入已经存在的值,这将引发错误如果

ALTER TABLE table_name
  ADD CONSTRAINT uk_id UNIQUE( id );

尝试插入重复的键,您可以捕获错误并执行您想要的任何操作 - 从忽略错误到记录和重新引发错误引发自定义异常的例外

BEGIN
  INSERT INTO table_name( id, value ) 
    VALUES( 4, 100 );
EXCEPTION
  WHEN dup_val_on_index
  THEN
    <<do something>>
END;

您还可以对 INSERT 进行编码,以便插入 0 行(从数据模型的角度来看,您仍然希望存在唯一约束,因为它为优化器提供了更多信息并且可能使将来的查询更加高效)

INSERT INTO table_name( id, value )
  SELECT 4, 100
    FROM dual
   WHERE NOT EXISTS( 
    SELECT 1
      FROM table_name
     WHERE id = 4 )

或者您可以编写 MERGE 代码,以便将 VALUE 列从 500 更新为 100,而不是插入新行。

If ID is supposed to be unique, there should be a unique constraint defined on the table. That will throw an error if you try to insert a value that already exists

ALTER TABLE table_name
  ADD CONSTRAINT uk_id UNIQUE( id );

You can catch the error and do whatever you'd like if an attempt is made to insert a duplicate key-- anything from ignoring the error to logging and re-raising the exception to raising a custom exception

BEGIN
  INSERT INTO table_name( id, value ) 
    VALUES( 4, 100 );
EXCEPTION
  WHEN dup_val_on_index
  THEN
    <<do something>>
END;

You can also code the INSERT so that it inserts 0 rows (you would still want the unique constraint in place both from a data model standpoint and because it gives the optimizer more information and may make future queries more efficient)

INSERT INTO table_name( id, value )
  SELECT 4, 100
    FROM dual
   WHERE NOT EXISTS( 
    SELECT 1
      FROM table_name
     WHERE id = 4 )

Or you could code a MERGE instead so that you update the VALUE column from 500 to 100 rather than inserting a new row.

情独悲 2024-12-23 19:05:37

尝试 MERGE 语句:

MERGE INTO tbl USING
    (SELECT 4 id, 100 value FROM dual) data
ON (data.id = tbl.id)
WHEN NOT MATCHED THEN
    INSERT (id, value) VALUES (data.id, data.value)

Try MERGE statement:

MERGE INTO tbl USING
    (SELECT 4 id, 100 value FROM dual) data
ON (data.id = tbl.id)
WHEN NOT MATCHED THEN
    INSERT (id, value) VALUES (data.id, data.value)
别闹i 2024-12-23 19:05:37
INSERT INTO YOUR_TABLE (YOUR_FIELD)
SELECT '1' FROM YOUR_TABLE YT WHERE YT.YOUR_FIELD <> '1' LIMIT 1

当然,那个“1”将是你的数字或你的变量。
可以使用INSERT+SELECT来解决这个问题。

INSERT INTO YOUR_TABLE (YOUR_FIELD)
SELECT '1' FROM YOUR_TABLE YT WHERE YT.YOUR_FIELD <> '1' LIMIT 1

Of course, that '1' will be your number or your variable.
You can use INSERT + SELECT to solve this problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文