The topic of TRIGGERS is a level-up from creating queries and sub-queries in the handling databases.
A super-easy guide to triggers in SQL π§΅π
βοΈ What is a trigger in the laymen (standard) language?
When someone pokes you or does something to agitate you, there is a high chance that you might get 'triggered' automatically! Right?
βοΈ Triggers in SQL
So, when we say TRIGGERS in SQL, it is simply a stored program that gets executed on its own when a triggering event occurs. Now, triggers are a part of PL/SQL.
PL/SQL is an extension of SQL where SQL queries are used and procedural statements/language.
Stored Program is an SQL code that can be saved and reused multiple times.
For example, You have a query that has to be implemented numerous times, instead of writing it every single time, you can save it as a stored program and call that program to run that query when you want.
Triggering Events -> Triggers are triggered/executed in response to DELETE, UPDATE and INSERT queries.
βοΈ How to create a trigger?
CREATE TRIGGER trigger_name
{ BEFORE | AFTER} {INSERT | UPDATE| DELETE }
ON table_name FOR EACH ROW
trigger_body;
π Syntax Explanation: CREATE TRIGGER phrase is used to create the trigger, followed by the trigger's name. You can name your trigger whatever you want.
Decide when you want your trigger to be invoked, before or after, and likewise write that keyword.
Follow that with either insert, delete or update event.
Trigger body would include the actions you want the trigger to perform.
π EXAMPLE!!
Let's create a table named 'allemployees'.
It has columns:-
Eid -> Employee ID
Contact -> Employee contact number
Name -> Employee Name
Refer the Image
π Creating a TRIGGER before the insert query is executed.
(See the image for the code)
Explanation: A trigger named 'bef_insert_emp' is created on the table 'allemployees'
The trigger would be executed before the values are entered in the table and would perform the assigned function.
The assigned function is that whatever name is being inserted in the table should be in LOWER CASE.
Here, NEW is a keyword for the latest values being filled in the table.
So, New.Name is referring to the latest value in the Name column.
When we try to put an upper case name or a mixture of the upper and lower case name, the trigger will convert the name into the lower case before inserting it into the table.
If you like the explanation and are interested in content related to Data Science, consider liking and retweeting the post.
Follow me too! π
β’ β’ β’
Missing some Tweet in this thread? You can try to
force a refresh
A Beginner-friendly version of SQL (UPDATE and DELETE)
π§΅π
π Let's look at the table first!
The name of the table is -> faculties
We have columns as FacultyId, Name, Class, EmailId and Salary.
βοΈ UPDATE
This clause is used to change values in a specified column.
You may or may not provide a condition along with the change you want to make.
π Update without condition:-
UPDATE table_name SET column_name = column_value;
SQL - A Beginner-friendly version! π
(Clauses: IN, BETWEEN and LIKE)
Thread π§΅
πWhich table are we working on today?
The name of the table is "products". So the columns that we have are productCode, productName, productDescription, among others.
Take a look π
βοΈ IN
This clause would reduce the efforts to write multiple OR conditions. It helps you find a specific match for a value.
π select column1, column2 from table_name where EXPRESSION IN ('value1', 'value2')
Here is another thread of the most important clauses to boost your SQL queries to the next level!
Advance SQL, A beginner-friendly version! (#DataScience)
Thread π§΅
Let me explain what the table looks like.
The table name is customer_list, where the columns are ID, name, address, zip code, phone, city, country, notes & SID.
See the image for reference
βοΈ GROUP BY
This clause is used to group rows that have the same values together. It summarises data from the database.
Note: The group by clause returns one row for each group.
π Query: select col_name from table_name GROUP BY column_name;