sql >> データベース >  >> RDS >> Mysql

mysqlの列の整数値を制限する

    テーブルにINSERT/UPDATEトリガーを追加し、値が定義された範囲内にないINSERTSを拒否します。

    例を含めるように編集

    /*
    -- ================================================
    -- Template generated from Template Explorer using:
    -- Create Trigger (New Menu).SQL
    --
    -- Use the Specify Values for Template Parameters 
    -- command (Ctrl-Shift-M) to fill in the parameter 
    -- values below.
    --
    -- See additional Create Trigger templates for more
    -- examples of different Trigger statements.
    --
    -- This block of comments will not be included in
    -- the definition of the function.
    -- ================================================
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    -- =============================================
    -- Author:      <Author,,Name>
    -- Create date: <Create Date,,>
    -- Description: <Description,,>
    -- =============================================
    CREATE TRIGGER <Schema_Name, sysname, Schema_Name>.<Trigger_Name, sysname, Trigger_Name> 
       ON  <Schema_Name, sysname, Schema_Name>.<Table_Name, sysname, Table_Name> 
       AFTER <Data_Modification_Statements, , INSERT,DELETE,UPDATE>
    AS 
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
    
        -- Insert statements for trigger here
    
    END
    GO
    */
    
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    
    CREATE TRIGGER [dbo].[utg_ClientsInsert]
    ON [dbo].[Clients]
    AFTER INSERT,UPDATE
    AS
    BEGIN
        SET NO COUNT ON
    
        IF(SELECT COUNT(*) FROM INSERTED) = 0
         BEGIN
            RETURN -- do not execute trigger if no rows
         END
    
        IF(SELECT COUNT(*) FROM INSERTED WHERE empno NOT BETWEEN 7000 AND 8000) > 0
         BEGIN
            -- don't allow the insert to happen
            RAISERROR('Employee # must be between 7,000 and 8,000.', 18, 1)
         END
    
    END
    
    GO
    



    1. トランザクションログ構成の問題

    2. データベースの日付を+1か月で更新します

    3. MySQL定義者の問題をトラブルシューティングする方法

    4. IDEF1X表記