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 allemployees table
πŸ‘‰ 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' Creation of Trigger in SQL
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.

For that, we use the SET keyword.
SET NEW.Name = LOWER(https://t.co/UOUAZLE9s2);

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. Inserting values to see if ...
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
γ€€

Keep Current with Sukriti Macker

Sukriti Macker Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @Sukriti_Macker

9 Jul
Do you know about the SLICING technique that helps you work around strings in Python??

To handle sub-strings, you can use the SLICING method.
πŸ‘‡πŸ‘‡
Let's say we have a string.

a = "This is a string."

You can slice between the string!

πŸ‘‰ a[start_index : end_index : stride]

Terms:-
start_index -> Takes an integer value; this is the index value (inclusive) from where you want to start slicing.
end_index -> Takes an integer value; this is the index value (exclusive) where you want the slicing to end.

stride -> Takes an integer value; indicates how many characters to skip after the first character is retrieved.

Note: The first character index is always 0.
Read 8 tweets
2 Jul
STRINGS - Handling them like a PRO! 😎

Some in-built functions that can make your task of working around strings a cakewalk!

Thread 🧡
β˜‘οΈ What are STRINGS?
In Python, there is no character! Even a single character is considered a string. Strings are an immutable sequence.

Immutable -> Once strings are created, they cannot be changed.
So, you can't change the string, but the reference to it can be changed.
β˜‘οΈ .split()
a = "this is a string"
a.split()

O/P: ['this', 'is', 'a', 'string']

The string is split into each word in the sentence, considering the DELIMITER as space. The output of the split function is given in a LIST.
Read 9 tweets
29 Jun
Hey, here is a compiled version of how to proceed with SQL in a non-threatening way. :)

SQL is fun, easy to grasp and can kick start your programming journey. Learn something for fun and satiate your curiosity. πŸ“š

A Beginner-friendly guide to begin your SQL Journey! πŸ§΅πŸ‘‡
Read 11 tweets
28 Jun
What else can I say about SQL to prove that it is crucial to know how to work around databases?

Having said that, SQL could be the one thing that you could begin your programming journey with. 🀩

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. "Faculties" Table.
β˜‘οΈ 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;
Read 9 tweets
25 Jun
I hope you are practising SQL whilst you are learning it. This technique makes it easier to understand concepts.

Alrighty, here is another thread for SQL. 🀩

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 πŸ‘‡ Column namesOverview of the information in the columns
β˜‘οΈ 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')
Read 15 tweets
24 Jun
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 The table and column names in that table.
β˜‘οΈ 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;
Read 13 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(