你可以嘗試這樣的事情作爲一個起點 - 這是SQL Server(不知道MySQL的不夠好,爲您提供該觸發代碼):
-- create the trigger, give it a meaningful name
CREATE TRIGGER PrependHttpPrefix
ON dbo.YourTableName -- it's on a specific table
AFTER INSERT, UPDATE -- it's for a specific operation, or several
AS
BEGIN
-- the newly inserted rows are stored in the "Inserted" pseudo table.
-- It has the exact same structure as your table that this trigger is
-- attached to.
-- SQL Server works in such a way that if the INSERT affected multiple
-- rows, the trigger is called *once* and "Inserted" contains those
-- multiple rows - you need to work with "Inserted" as a multi-row data set
--
-- You need to join the "Inserted" rows to your table (based on the
-- primary key for the table); for those rows newly inserted that
-- **do not** start with "http://" in "YourColumn", you need to set
-- that column value to the fixed text "http:/" plus whatever has been inserted
UPDATE tbl
SET YourColumn = 'http://' + i.YourColumn
FROM dbo.YourTableName tbl
INNER JOIN Inserted i ON tbl.PKColumn = i.PKColumn
WHERE LEFT(i.YourColumn, 7) <> 'http://'
END
不要忘記更新觸發太。 (或者使用存儲過程管理所有插入/更新,並且無需直接在表上插入/更新權限。) – jarlh
是的,觸發器絕對是您的選擇。由於這些是**高度**供應商特定的,我們真的需要知道你正在使用的具體RDBMS - 'oracle','sql-server','postgresql'等。 –
嗨@marc_s,謝謝你你的評論(@jarlh)。擁有SQL-SERVER和MYSQL的方法會很棒,但如果我必須選擇一個,它將是SQL-SERVER。 –