插入表並將另一列設置為自動增加的列值

[英]Insert into a table and set another column to autoincremented column value


Let's say I have a simple table:

假設我有一個簡單的表格:

create table foo
{
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    bar INTEGER
}

And I want to insert a new row, such that id == bar where value for id is chosen by the database, a.k.a. auto-increment.

我想插入一個新行,例如id == bar,其中id的值由數據庫選擇,a.k.a。自動增量。

Something like this:

像這樣的東西:

INSERT INTO foo (id, bar) VALUES (NULL, id)

Is it possible do this in one statement?

有可能在一個聲明中這樣做嗎?

What is the SQL syntax for that?

那是什么SQL語法?

7 个解决方案

#1


6  

In SQLite you can

在SQLite中你可以

BEGIN TRANSACTION;
  INSERT INTO foo (id, bar) VALUES (NULL, 0);
  UPDATE foo SET bar = id WHERE _ROWID_ = last_insert_rowid();
COMMIT;

to make sure no other statement gets in the way of your two-statement expression.

確保沒有其他語句妨礙你的雙語句表達式。

#2


3  

You can't have two auto increment fields. You should use a single auto increment field. Given that both fields would always have the same value for every row, there's no reason to have to such fields anyway.

您不能有兩個自動增量字段。您應該使用單個自動增量字段。鑒於兩個字段對於每一行總是具有相同的值,所以無論如何都沒有理由擁有這樣的字段。

But you can just make trigger which will update another field equal to auto incremented value after inserting row. And delete that trigger when you don't want them to have same values.

但是你可以設置觸發器,它會在插入行后更新另一個等於自動遞增值的字段。並且當您不希望它們具有相同的值時刪除該觸發器。

CREATE TRIGGER update_foo AFTER INSERT ON foo 
  BEGIN
    UPDATE foo SET bar = NEW.id ;
  END;

When eventually bar will be changed to have not same value as id, then delete trigger

當最終bar將被更改為與id不同的值時,則刪除觸發器

DROP TRIGGER update_foo

#3


1  

This isn't done in one query, but it could be used within a stored procedure. The set is repeated to show that it does insert and update based upon the database created ID. This was done on SQL Server 2008R2

這不是在一個查詢中完成的,但可以在存儲過程中使用。重復該集合以顯示它根據數據庫創建的ID進行插入和更新。這是在SQL Server 2008R2上完成的

declare @tmpTable TABLE (
    id INT identity(1,1),
    bar INT
    )

    declare @myId INT
    insert @tmpTable (bar) values (0)
    SET @myId = SCOPE_IDENTITY() 
    update @tmpTable SET bar = @myId where id = @myId 

    insert @tmpTable (bar) values (0)
    SET @myId = SCOPE_IDENTITY() 
    update @tmpTable SET bar = @myId where id = @myId 


    insert @tmpTable (bar) values (0)
    SET @myId = SCOPE_IDENTITY() 
    update @tmpTable SET bar = @myId where id = @myId 


    insert @tmpTable (bar) values (0)
    SET @myId = SCOPE_IDENTITY() 
    update @tmpTable SET bar = @myId where id = @myId 

    select * FROM @tmpTable 

OUTPUT

OUTPUT

id  bar
1   1
2   2
3   3
4   4

#4


0  

As was mentioned in the comments, I believe that this will be specific to each database implementation, so it'd be useful to know which server you're using. However, for MySQL, you could do something like this:

正如評論中提到的,我認為這將特定於每個數據庫實現,因此了解您正在使用的服務器是有用的。但是,對於MySQL,你可以這樣做:

INSERT INTO foo (bar) 
  SELECT AUTO_INCREMENT 
  FROM information_schema.tables 
  WHERE table_schema = DATABASE() 
    AND TABLE_NAME = 'foo';

Or, since you're probably using SQLite (based on the tag), you could try this:

或者,既然您可能正在使用SQLite(基於標記),您可以嘗試這樣做:

INSERT INTO foo (bar) 
  SELECT (seq + 1)
  FROM sqlite_sequence
  WHERE name = 'foo';

#5


0  

You can use it as:

您可以將其用作:

INSERT INTO foo (bar) VALUES (last_insert_rowid()+1)

The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.

生成的ID在每個連接的基礎上在服務器中維護。這意味着函數返回給定客戶端的值是為該客戶端影響AUTO_INCREMENT列的最新語句生成的第一個AUTO_INCREMENT值。其他客戶端不會影響此值,即使它們生成自己的AUTO_INCREMENT值也是如此。此行為可確保每個客戶端都可以檢索自己的ID,而無需考慮其他客戶端的活動,也無需鎖定或事務。

#6


0  

insert into [Test].[dbo].[foo] (bar) select MAX(id)+1 from [Test].[dbo].[foo]

#7


-2  

INSERT INTO foo (bar) VALUES (id)


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/12/15/ef125713151f579a58073b354e6a8b40.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com