How to run a basic Trigger in SQL using  mysql  and arctype.

How to run a basic Trigger in SQL using mysql and arctype.

In this article you will learn how to

  • connect your mysql database to arctype.
  • create a database table in Mysql.
  • run a basic trigger in SQL using mysql and arctype SQL editor.

Introduction

Triggers are sql codes that are automatically executed when a specific event occurs in a database. Triggers help databases ensure certain actions, such as maintaining an audit file, detecting errors on the database level, and providing a method to check the database's data integrity.

Prerequisite

  • working knowledge on SQL
  • arctype or a mysql development environment.
  • my sql 8.0 or newer installed on your machine.

Connecting your Mysql database to arctype

What is ARCTYPE

Arctype is a SQL database management system and visualization tool that helps you write queries faster and easier with its autocomplete features while also allowing you to visualize your data. It basically allows you to manage your database with ease using the tools it provides, as well as collaborate with other users by sharing saved queries and managing workspace user permissions.

Getting started with arctype

Install Arctype from the link here, then sign in to follow the tutorial

Arctype 1.jpg

Now you have installed Arctype into your machine, You will need to connect it to your mysql database, But first you will need to create a database in your mysql using the mysql command line using the SQL code below.

CREATE DATABASE dbname;

**Note: You can use any name of your choice . Now that you have succesfully created your DATABASE, you can now connect it to Arctype.

arctype 3.jpg

Click on the mysql option which will take you to where you will fill in the database credentials to be able to connect it to mysql.

arctype 4.jpg

You can use any Name of your choice as seen above but make sure you use the Database name you created earlier for the database option above. You can now save your connection details to connect to your mysql database.

arctype 5.jpg

You now have your mysql database connected to arctype, you can now create the table you will need to run your Trigger.

Create a database table in Mysql

You can create your table using the following SQL code below

CREATE TABLE employee (
  emp_id INT PRIMARY KEY,
  first_name VARCHAR(40),
  last_name VARCHAR(40),
  birth_day DATE,
  sex VARCHAR(1),
  salary INT,
);

Now you have created your table you will need to insert some data into the table.

INSERT INTO employee VALUES(103, 'Adeda', 'Faithful', '1980-06-25', 'F', 65000);
INSERT INTO employee VALUES(104, 'Joel', 'Prince', '1987-02-05', 'F', 75000);
INSERT INTO employee VALUES(105, 'Doro', 'Onome', '1960-02-19', 'M', 67000);

You should be able to see the table you created in your artype table section below.

arctype 7.jpg

Now you have your table, you can now run some basic triggers..

Run a basic trigger in SQL using mysql and arctype SQL editor.

You'll need to create a new table to test your trigger with before you can run it. To create your trigger table, copy and paste the following code.

CREATE TABLE trigger_test (
     message VARCHAR(100)
);

This table you have created will be used to test your triggers. Now that you have created a trigger_test table, You can go ahead and create your trigger using the code as shown below.

CREATE
    TRIGGER my_trigger BEFORE INSERT
    ON employee
    FOR EACH ROW 
     INSERT INTO trigger_test VALUES('added new employee');

For you to successfully know your trigger is working perfectly, you can insert some employee details into your employee table.

INSERT INTO employee
VALUES(106, 'Oscar', 'Martin', '1968-02-19', 'M', 68000);

Now your trigger should run immediately you insert new data into your employee table. Your trigger_test table should also be updated with the trigger value "added new employee"

arctype 8.jpg

Conclusion

This article introduced you to arctype and also how to run a basic SQL trigger using the mysql database , in addition you learned how to connect your mysql database to arctype.

If you have any question dont hesitate to contact me on twitter :).