你可以尝试这样的事情作为一个起点 - 这是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。 –