Database scheme, autoincrement

后端 未结 2 1855
[愿得一人]
[愿得一人] 2020-11-27 23:05

Database question here. Is it possible to make an autoincrement on a secondary or a thirtiary ID? I need to make something versionbased, so imagine this:

ID          


        
2条回答
  •  庸人自扰
    2020-11-27 23:53

    This can be accomplished via an insert trigger on the table:

    CREATE TABLE Phrases (
        ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY, 
        PhraseID INT NOT NULL DEFAULT(0), 
        PhraseVersion INT NOT NULL DEFAULT(0))
    GO
    
    -- ==========================================================================================
    -- Author:         Donna Landy
    -- Create Date:    21 Nov 2019
    -- Purpose:        To populate the PhraseVersion column (subordinate numeric key) on Insert
    -- Note:           Must cater for >1 row being inserted when this trigger is called
    -- Strategy:       Construct a temp table with the rows we need to consider, then update one row at a time
    -- ==========================================================================================
    
    CREATE TRIGGER Phrases_Insert ON Phrases AFTER INSERT
    AS
    BEGIN
    
        DECLARE @ID INT
        DECLARE @PhraseID INT
        DECLARE @PhraseVersion INT
    
        DECLARE @i INT
        DECLARE @iMax INT
    
        -- Create and populate temp table
        IF OBJECT_ID('tempdb..#phrases', 'U') IS NOT NULL DROP TABLE #phrases
        CREATE TABLE #phrases (i INT IDENTITY(1,1) PRIMARY KEY, ID INT, PhraseID INT)
        INSERT INTO #phrases (ID, PhraseID) SELECT ID, PhraseID FROM inserted
    
        -- Scan temp table
        SET @i=1
        SELECT @iMax=MAX(i) FROM #phrases
    
        WHILE @i <= @iMax BEGIN
    
            -- Fetch PhraseID & PhraseVersion for the row we are going to update
            SELECT @ID=ID, @PhraseID=PhraseID FROM #phrases WHERE i=@i
    
            -- Find the highest current Ref
            SELECT @PhraseVersion=ISNULL(MAX(PhraseVersion),0) FROM Phrases WHERE PhraseID=@PhraseID
    
            -- Update the row 
            UPDATE Phrases SET PhraseVersion=@PhraseVersion+1 WHERE ID=@ID 
    
            -- Increment loop counter
            SET @i+=1
    
        END
    
        -- Remove temp table
        IF OBJECT_ID('tempdb..#phrases', 'U') IS NOT NULL DROP TABLE #phrases
    
    END
    GO 
    

提交回复
热议问题