Database administration tool Navicat Premium, use a trigger to verify the data in MySQL 8

Navicat Premium is a database management tool that lets you use a program that can simultaneously connect to MySQL, MariaDB, SQL Server, SQLite, Oracle and PostgreSQL databases. It is, Amazon Aurora, Amazon Redshift, Microsoft Azure, Oracle Cloud, MongoDB Atlas, Ali cloud, cloud and Huawei Tencent cloud and other cloud database compatible with Amazon RDS.

There are some very good reasons why the best data validation at the database level instead of performing at the application level. For example, multiple applications can access the same data source. Therefore, you can rely on the consistency and validity of the data without having to rely on the validation logic in the application-side applications, which may differ in different implementations. In addition, verification trigger is ideal, since they can be performed before inserting or updating data. Triggers can also prevent the application database transaction in providing an error message.

In this article, we will write a trigger in Navicat Premium for verification insert on the MySQL database tables.

Design Trigger

We will use Sakila sample database. It contains some fictitious video rental stores as the theme of the relevant form. Here, they Navicat Premium Navigation pane:

90sakila_tables.jpg

We will add a trigger to the movie table. If you open it in Designer, you will see several tabs:

91designer_tabs.jpg

Click on the "Trigger" tab displays a number of triggers have been defined for the table. For example, ins_film trigger information to the data copy film inserted film_text table. This is a common task assigned to the trigger.

92existing-triggers.jpg

Now we will add a trigger, be sure to use original_language_id insert foreign films.

Find language movie is actually stored in the language table:

93film_language_relationship.jpg

LANGUAGE_ID name  latest update
1 English 2006-02-15 05:02:19
2 Italy 2006-02-15 05:02:19
3 Japan 2006-02-15 05:02:19
4 Mandarin 2006-02-15 05:02:19
5 France 2006-02-15 05:02:19
6 German 2006-02-15 05:02:19

Any language_id 1 except should also have original_language_id. Our trigger will check the value of original_language_id column.

1, in the "Design View" movie list, select the "Trigger" tab, then click the "Add Trigger" button.

This will add a new row in the trigger table.

2, assign the name "ins_validate_language", select BEFORE Fires from the drop-down menu, and then click Insert check box.

3, which is the trigger Body:

BEGIN
  IF NEW.language_id != 1 AND NEW.original_language_id IS NULL
  THEN
    SIGNAL SQLSTATE '45000'
      SET MESSAGE_TEXT = 'Original language id is required for Foreign language films.';
  END IF;
END

This is our trigger, all fields are filled:

94ins_validate_language.jpg


4. Click the "Save" button to create the trigger.

Test Triggers

Now is the time to verify our flip-flops are working as expected. To test it, let's add a new row to the table with a foreign language film language_id.

1, open the movie in the table editor.

2. Navigate to the last line.

3, select the Form View, and then click Add a new line plus sign (+) button:

95record_form.jpg

4, in the form, you only need to enter a title and language_id; all other fields with default values ​​or not necessary.

5, click Add (check mark) button, you should see our error message:

96alert_message.jpg

in conclusion

Verify trigger is ideal, since they can be performed before inserting or updating data. We write triggers in Navicat Premium, to understand how a trigger for verification purposes.


Guess you like

Origin blog.51cto.com/14467432/2432525