Sometimes, when inserting a database, we may insert duplicate data, but do not want
Modify existingProgram, We can use triggers to avoid modifying programs and solve problems directly in the database.
Test procedure:
1. First, create a test database table.
Create Table "Gzrdya". "tbtest"
("ID" Number ( 13 , 0 ),
"Name" nvarchar2 ( 200 )
)
2. Create a trigger.
Check whether the IDs are repeated. If there are duplicates, an error is thrown and no data is inserted.
Code
Create Or Replace Trigger Trig_tbtest
Before Insert On Tbtest For Each row
Declare
V_count Number ( 5 );
Begin
Select Count ( * ) Into V_count From Tbtest Where ID = : New. ID;
If (V_count > 0 ) Then
Raise_application_error ( - 20000 , ' Data is duplication ' );
End If ;
End ;
3. Test
Try to insert duplicate ID data. If you find that the data cannot be inserted, an error is thrown.