<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>TyroCity</title>
    <description>The most recent home feed on TyroCity.</description>
    <link>https://tyrocity.com</link>
    <atom:link rel="self" type="application/rss+xml" href="https://tyrocity.com/feed"/>
    <language>en</language>
    <item>
      <title>SQL aggregate functions activities</title>
      <dc:creator>Angel Paudel</dc:creator>
      <pubDate>Sat, 29 Nov 2025 14:14:07 +0000</pubDate>
      <link>https://tyrocity.com/database/sql-aggregate-functions-activities-5c47</link>
      <guid>https://tyrocity.com/database/sql-aggregate-functions-activities-5c47</guid>
      <description>&lt;p&gt;Choose to either use the existing database or create a new one as you prefer.&lt;/p&gt;

&lt;p&gt;If you want to use existing database simply type &lt;code&gt;USE DatabaseName&lt;/code&gt; - where DatabaseName is the name of the database that exists within your system that you want to use.&lt;/p&gt;

&lt;p&gt;If you want to create a new database simply type &lt;code&gt;CREATE DATABASE DatabaseName&lt;/code&gt; - where DatabaseName is the name of the database you seek to create/add. Then follow the same USE command to continue with that database.&lt;/p&gt;

&lt;p&gt;Let's create 3 tables and add values under it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Table 1: Patients&lt;/li&gt;
&lt;li&gt;Table 2: Therapists&lt;/li&gt;
&lt;li&gt;Table 3: TherapySessions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;a id="t1"&gt;Table 1: Patients&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Code to create table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Patients 
(patient_id INT NOT NULL PRIMARY KEY, 
first_name VARCHAR(50) NOT NULL, 
last_name VARCHAR(50) NOT NULL, 
address VARCHAR(100) NOT NULL, 
city VARCHAR(50) NOT NULL, 
state VARCHAR(50) NOT NULL, 
zip_code VARCHAR(10) NOT NULL, 
phone_number VARCHAR(20) NOT NULL, 
balance DECIMAL(10,2) NOT NULL );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Code to insert values within the table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Patients 
(patient_id, first_name, last_name, address, city, state, zip_code, phone_number, balance) 
VALUES 
(1, 'John', 'Doe', '123 Main St', 'Waterville', 'ME', '04901', '555-1234', 500.00), 
(2, 'Jane', 'Smith', '456 Elm St', 'Augusta', 'ME', '04330', '555-5678', 1000.00), 
(3, 'Bob', 'Johnson', '789 Maple St', 'Waterville', 'ME', '04901', '555-9012', 1500.00), 
(4, 'Sarah', 'Lee', '321 Oak St', 'Bangor', 'ME', '04401', '555-3456', 2000.00), 
(5, 'Mike', 'Brown', '654 Pine St', 'Portland', 'ME', '04101', '555-7890', 2500.00), 
(6, 'Lisa', 'Taylor', '987 Cedar St', 'Waterville', 'ME', '04901', '555-2345', 3000.00);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;a id="t2"&gt;Table 2: Therapists&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Code to create table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Therapists 
(therapist_id INT AUTO_INCREMENT PRIMARY KEY, 
first_name VARCHAR(50), 
last_name VARCHAR(50), 
email VARCHAR(50), 
phone VARCHAR(20), 
street_address VARCHAR(100), 
city VARCHAR(50), 
state VARCHAR(2), 
zip_code VARCHAR(10), 
date_hired DATE, 
active TINYINT);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Code to insert values within the table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Therapists 
(first_name, last_name, email, phone, street_address, city, state, zip_code, date_hired, active) 
VALUES 
('John', 'Doe', 'johndoe@example.com', '555-123-4567', '123 Main St', 'Los Angeles', 'CA', '90001', '2021-01-01', 1), 
('Jane', 'Smith', 'janesmith@example.com', '555-234-5678', '456 Oak Ave', 'San Francisco', 'CA', '94101', '2021-02-15', 1), 
('Tom', 'Wilson', 'tomwilson@example.com', '555-345-6789', '789 Pine St', 'San Diego', 'CA', '92001', '2021-03-15', 1), 
('Emily', 'Jones', 'emilyjones@example.com', '555-456-7890', '321 Elm St', 'Sacramento', 'CA', '95814', '2021-04-01', 1), 
('Mark', 'Lee', 'marklee@example.com', '555-567-8901', '654 Cedar St', 'San Jose', 'CA', '95101', '2021-05-15', 1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;a id="t3"&gt;Table 3: TherapySessions&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Code to create table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE TherapySessions 
(session_id INT NOT NULL PRIMARY KEY, 
therapist_id INT NOT NULL, 
patient_id INT NOT NULL, 
session_date DATE NOT NULL, 
session_time TIME NOT NULL, 
therapy_description VARCHAR(100) NOT NULL, 
session_length INT NOT NULL, 
session_cost DECIMAL(10,2) NOT NULL, 
FOREIGN KEY (therapist_id) REFERENCES Therapists(therapist_id),
FOREIGN KEY (patient_id) REFERENCES Patients(patient_id) );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Code to insert values within the table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO TherapySessions 
(session_id, therapist_id, patient_id, session_date, session_time, therapy_description, session_length, session_cost) 
VALUES 
(1, 1, 1, '2021-10-18', '10:00:00', 'Physical Therapy', 30, 50.00),
(2, 2, 2, '2021-10-19', '11:00:00', 'Occupational Therapy', 45, 75.00), 
(3, 3, 3, '2021-10-20', '14:00:00', 'Speech Therapy', 60, 100.00), 
(4, 4, 1, '2021-10-21', '12:00:00', 'Movement Therapy', 60, 90.00), 
(5, 2, 5, '2021-10-22', '09:00:00', 'Physical Therapy', 30, 50.00), 
(6, 3, 6, '2021-10-23', '10:00:00', 'Speech Therapy', 60, 100.00);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the end of it, you should have 3 tables with data populated under each of it.&lt;/p&gt;

&lt;p&gt;Verify if the table is created successfully or not by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SHOW TABLES;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the outcome shows all table names - you're good.&lt;/p&gt;

&lt;p&gt;Check if each of the 3 tables have the required data added correct or not by entering:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;SELECT * FROM &lt;strong&gt;TableName&lt;/strong&gt;;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;where TableName is the name of the table you created. Use it 3 times - once per table to see their values.&lt;/p&gt;




&lt;p&gt;Questions to answer:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How many therapists are there? Answer in one number only.&lt;/li&gt;
&lt;li&gt;How many therapists are there with their first name Tom? Answer in one number only.&lt;/li&gt;
&lt;li&gt;How many therapists are there living in San Jose? Answer in one number only.&lt;/li&gt;
&lt;li&gt;How much is the total session cost in the organization? &lt;/li&gt;
&lt;li&gt;How much total session cost is the organization getting for Physical Therapy? &lt;/li&gt;
&lt;li&gt;How much average session cost is the organization getting? &lt;/li&gt;
&lt;li&gt;How much average session cost is the organization getting for Physical Therapy? &lt;/li&gt;
&lt;li&gt;What is the lowest session cost that the organization is getting? &lt;/li&gt;
&lt;li&gt;What is the lowest session cost  that the organization is getting for Physical Therapy? &lt;/li&gt;
&lt;li&gt;What is the highest session cost that the organization is paying for Physical Therapy? &lt;/li&gt;
&lt;li&gt;What is the highest session cost that the organization is getting? &lt;/li&gt;
&lt;li&gt;How many sessions have the highest session cost? Answer in one number only.&lt;/li&gt;
&lt;li&gt;Extract just the month for each of the entries under "session_date" under TherapySessions. &lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;For the next phase of activities - let's create another table and populate it with data:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating table Employee&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Employee 
(Name VARCHAR(10), 
Gender VARCHAR(10), 
Department VARCHAR(10), 
Salary INT);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's insert data under the table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Employee 
(Name, Gender, Department, Salary) 
VALUES 
('Ram', 'Male', 'Sales', 50000), 
('Sita', 'Female', 'IT', 70000), 
('Hari', 'Male', 'IT', 60000), 
('Geeta', 'Female', 'Sales', 50000), 
('Hari', 'Male', 'IT', 50000), 
('Rita', 'Female', 'Sales', 60000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once done, we should have the table as below created:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Gender&lt;/th&gt;
&lt;th&gt;Department&lt;/th&gt;
&lt;th&gt;Salary&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Ram&lt;/td&gt;
&lt;td&gt;Male&lt;/td&gt;
&lt;td&gt;Sales&lt;/td&gt;
&lt;td&gt;50,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sita&lt;/td&gt;
&lt;td&gt;Female&lt;/td&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;td&gt;70,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hari&lt;/td&gt;
&lt;td&gt;Male&lt;/td&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;td&gt;60,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Geeta&lt;/td&gt;
&lt;td&gt;Female&lt;/td&gt;
&lt;td&gt;Sales&lt;/td&gt;
&lt;td&gt;50,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hari&lt;/td&gt;
&lt;td&gt;Male&lt;/td&gt;
&lt;td&gt;IT&lt;/td&gt;
&lt;td&gt;50,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rita&lt;/td&gt;
&lt;td&gt;Female&lt;/td&gt;
&lt;td&gt;Sales&lt;/td&gt;
&lt;td&gt;60,000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Let's continue with this to perform further activities.&lt;/p&gt;

</description>
      <category>bsit</category>
      <category>bscs</category>
    </item>
    <item>
      <title>WK2 Activity - Working with queries</title>
      <dc:creator>Angel Paudel</dc:creator>
      <pubDate>Thu, 16 May 2024 15:15:55 +0000</pubDate>
      <link>https://tyrocity.com/database/wk2-activity-working-with-queries-58k6</link>
      <guid>https://tyrocity.com/database/wk2-activity-working-with-queries-58k6</guid>
      <description>&lt;p&gt;Practice table::&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Order ID&lt;/th&gt;
&lt;th&gt;Customer Name&lt;/th&gt;
&lt;th&gt;Order Date&lt;/th&gt;
&lt;th&gt;Total Cost&lt;/th&gt;
&lt;th&gt;Status&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Jane Doe&lt;/td&gt;
&lt;td&gt;1/1/2022&lt;/td&gt;
&lt;td&gt;$100.00&lt;/td&gt;
&lt;td&gt;Shipped&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Jane Doe&lt;/td&gt;
&lt;td&gt;1/2/2022&lt;/td&gt;
&lt;td&gt;$75.00&lt;/td&gt;
&lt;td&gt;Pending&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Mark Smith&lt;/td&gt;
&lt;td&gt;1/3/2022&lt;/td&gt;
&lt;td&gt;$50.00&lt;/td&gt;
&lt;td&gt;Shipped&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Emily Gray&lt;/td&gt;
&lt;td&gt;1/4/2022&lt;/td&gt;
&lt;td&gt;$200.00&lt;/td&gt;
&lt;td&gt;Shipped&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Tom Hill&lt;/td&gt;
&lt;td&gt;1/5/2022&lt;/td&gt;
&lt;td&gt;$150.00&lt;/td&gt;
&lt;td&gt;Pending&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Sarah Jones&lt;/td&gt;
&lt;td&gt;1/6/2022&lt;/td&gt;
&lt;td&gt;$25.00&lt;/td&gt;
&lt;td&gt;Shipped&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;Mike Brown&lt;/td&gt;
&lt;td&gt;1/7/2022&lt;/td&gt;
&lt;td&gt;$300.00&lt;/td&gt;
&lt;td&gt;Pending&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Anna Green&lt;/td&gt;
&lt;td&gt;1/8/2022&lt;/td&gt;
&lt;td&gt;$100.00&lt;/td&gt;
&lt;td&gt;Shipped&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;David White&lt;/td&gt;
&lt;td&gt;1/9/2022&lt;/td&gt;
&lt;td&gt;$50.00&lt;/td&gt;
&lt;td&gt;Pending&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;Karen Black&lt;/td&gt;
&lt;td&gt;2/10/2022&lt;/td&gt;
&lt;td&gt;$175.00&lt;/td&gt;
&lt;td&gt;Shipped&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Questions to answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Retrieve all orders that have been shipped.&lt;/li&gt;
&lt;li&gt; Retrieve all orders that are still pending.&lt;/li&gt;
&lt;li&gt; Retrieve all orders that have a total cost of $100 or more.&lt;/li&gt;
&lt;li&gt; Retrieve all orders that were placed in January 2022.&lt;/li&gt;
&lt;li&gt; Retrieve all orders that were placed by customers whose name starts with "J".&lt;/li&gt;
&lt;li&gt; Retrieve all orders that were placed by customers whose name contains the letter "a".&lt;/li&gt;
&lt;li&gt; Retrieve the total cost of all orders that have been shipped.&lt;/li&gt;
&lt;li&gt; Retrieve the average cost of all orders that are still pending.&lt;/li&gt;
&lt;li&gt; Retrieve the earliest order date for each customer.&lt;/li&gt;
&lt;li&gt;Retrieve the customer who has placed the most orders.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>bsit</category>
    </item>
    <item>
      <title>WK3 Assignment</title>
      <dc:creator>Angel Paudel</dc:creator>
      <pubDate>Sun, 28 Jan 2024 14:33:15 +0000</pubDate>
      <link>https://tyrocity.com/database/wk3-assignment-ach</link>
      <guid>https://tyrocity.com/database/wk3-assignment-ach</guid>
      <description>&lt;p&gt;To create database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE DATABASE WK3ASSIGNMENT;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To create Patient table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Patient 
(
    PatientNum  INT,
    LastName    VARCHAR(512),
    FirstName   VARCHAR(512),
    Address VARCHAR(512),
    City    VARCHAR(512),
    State   VARCHAR(512),
    ZipCode INT,
    Balance VARCHAR(512)
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To input data into Patient table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Patient (PatientNum, LastName, FirstName, Address, City, State, ZipCode, Balance) VALUES
    ('1010', 'Koehler', 'Robbie', '119  West Bay Dr.', 'San  Vista', 'TX', '72510', '1,535.15 '),
    ('1011', 'King', 'Joseph', '941  Treemont', 'Oak  Hills', 'TX', '74081', '212.80 '),
    ('1012', 'Houghland', 'Susan', '7841  Lake  Side Dr.', 'Munster', 'TX', '72380', '1,955.40 '),
    ('1013', 'Falls', 'Tierra', '44 Applewood Ave.', 'Palm  Rivers', 'TX', '72511', '1,000.35 '),
    ('1014', 'Odepaul', 'Ben', '546  WCR 150  South', 'Munster', 'TX', '74093', '525.00 '),
    ('1015', 'Venable', 'Isaiah', '37 High School Road', 'Waterville', 'TX', '74183', '432.30 '),
    ('1016', 'Waggoner', 'Brianna', '2691  Westgrove St.', 'Delbert', 'TX', '72381', '714.25 '),
    ('1017', 'Short', 'Tobey', '1928  10th Ave.', 'Munster', 'TX', '72512', '967.60 '),
    ('1018', 'Baptist', 'Joseph', '300  Erin  Dr.', 'Waterville', 'TX', '76658', '1,846.75 '),
    ('1019', 'Culling', 'Latisha', '4238  East  71st  St.', 'San  Vista', 'TX', '74071', '1,988.50 '),
    ('1020', 'Marino', 'Andre', '919  Horton Ave.', 'Georgetown', 'TX', '72379', '688.95 '),
    ('1021', 'Wilson', 'Tammy', '424  October Blvd.', 'Waterville', 'TX', '76658', '2,015.30');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To create another table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Session 
(
    SessionNum  INT,
    SessionDate VARCHAR(512),
    PatientNum  INT,
    LengthOfSession INT,
    TherapistID VARCHAR(512),
    TherapyCode INT
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And, to input data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Session (SessionNum, SessionDate, PatientNum, LengthOfSession, TherapistID, TherapyCode) VALUES
    ('27', '10/10/2021', '1011', '45', 'JR085', '92507'),
    ('28', '10/11/2021', '1016', '30', 'AS648', '97010'),
    ('29', '10/11/2021', '1014', '60', 'SW124', '97014'),
    ('30', '10/12/2021', '1013', '30', 'BM273', '97033'),
    ('31', '10/15/2021', '1016', '90', 'AS648', '98960'),
    ('32', '10/16/2021', '1018', '15', 'JR085', '97035'),
    ('33', '10/17/2021', '1017', '60', 'SN852', '97039'),
    ('34', '10/17/2021', '1015', '45', 'BM273', '97112'),
    ('35', '10/18/2021', '1010', '30', 'SW124', '97113'),
    ('36', '10/18/2021', '1019', '75', 'SN852', '97116'),
    ('37', '10/19/2021', '1020', '30', 'BM273', '97124'),
    ('38', '10/19/2021', '1021', '60', 'AS648', '97535');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To create Therapist table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Therapist 
(
    TherapistID VARCHAR(512),
    LastName    VARCHAR(512),
    FirstName   VARCHAR(512),
    Street  VARCHAR(512),
    City    VARCHAR(512),
    State   VARCHAR(512),
    ZipCode INT
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To insert data into the table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Therapist (TherapistID, LastName, FirstName, Street, City, State, ZipCode) VALUES
    ('AS648', 'Shields', 'Anthony', '5222  Eagle Court', 'Palm  Rivers', 'TX', '72511'),
    ('BM273', 'McClain', 'Bridgette', '385  West Mill St.', 'Waterville', 'TX', '76658'),
    ('JR085', 'Risk', 'Jonathan', '1010  650  North', 'Palm  Rivers', 'TX', '72511'),
    ('SN852', 'Nair', 'Saritha', '25 North Elm St.', 'Livewood', 'TX', '72512'),
    ('SW124', 'Wilder', 'Steven', '7354  Rockville Road', 'San  Vista', 'TX', '72510');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To create Therapies table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Therapies 
(
    TherapyCode INT,
    Description VARCHAR(512),
    UnitOfTime  VARCHAR(512)
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To insert data into it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Therapies (TherapyCode, Description, UnitOfTime) VALUES
    ('90901', 'Biofeedback training by any modality', ''),
    ('92240', 'Shoulder strapping', ''),
    ('92507', 'Treatment of speech', '15'),
    ('92530', 'Knee strapping', ''),
    ('92540', 'Ankle and/or foot strapping', ''),
    ('95831', 'Extremity or trunk muscle testing', ''),
    ('97010', 'Hot or cold pack application', ''),
    ('97012', 'Mechanical traction', ''),
    ('97014', 'Electrical stimulation', ''),
    ('97016', 'Vasopneumatic devices', ''),
    ('97018', 'Paraf n bath', ''),
    ('97022', 'Whirlpool', ''),
    ('97026', 'Infrared', ''),
    ('97032', 'Electrical stimulation', '15'),
    ('97033', 'Iontophoresis', '15'),
    ('97035', 'Ultrasound', '15'),
    ('97039', 'Unlisted  modality', '15'),
    ('97110', 'Therapeutic exercises to develop  strength and endurance, range of motion, and   exibility', '15'),
    ('97112', 'Neuromuscular re-education of movement, balance, coordination, etc.', '15'),
    ('97113', 'Aquatic  therapy with therapeutic exercises', '15'),
    ('97116', 'Gait training', '15'),
    ('97124', 'Massage', '15'),
    ('97139', 'Unlisted  therapeutic procedure', ''),
    ('97140', 'Manual therapy techniques', '15'),
    ('97150', 'Group  therapeutic procedure', '15'),
    ('97530', 'Dynamic activities to improve functional performance, direct (one-on-one) with the patient', '15'),
    ('97535', 'Self-care/home management training', '15'),
    ('97750', 'Physical performance test  or measurement', '15'),
    ('97799', 'Unlisted  physical medicine/rehabilitation service  or procedure', ''),
    ('98941', 'CMT of the spine', ''),
    ('98960', 'Education and training for patient self-management', '30');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;P.S. in each of the cases - it's down to your personal discretion to choose the data types for the values and make alterations to them as is required for your case. These data types provided are just placeholder and as such you're recommended to update those as is best.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Questions to answer based on it:&lt;/p&gt;

&lt;p&gt;-- 1. List all the information in the patient’s table sorted by city.&lt;/p&gt;

&lt;p&gt;-- 2. List the last names of patients whose balance is greater than 1,000.&lt;/p&gt;

&lt;p&gt;-- 3. List the city, last name, and balance of patients who live in Waterville and have a balance greater than 1,000.&lt;/p&gt;

&lt;p&gt;-- 4. List the last name, city, and balance of patients who live in Waterville or have a balance greater than $2,000.&lt;/p&gt;

&lt;p&gt;-- 5. Increase the balance by 2% of any patient whose balance is greater than $2,000.&lt;/p&gt;

&lt;p&gt;-- 6. List the session dates and numbers for those sessions scheduled between 10/18/2021 and 10/21/2021.&lt;/p&gt;

&lt;p&gt;-- 7. List the full name of the therapist scheduled to work on 10/16/2021.&lt;/p&gt;

&lt;p&gt;-- 8. How long are the therapy sessions for the therapist whose last name is Shields? List the length of each session.&lt;/p&gt;

&lt;p&gt;-- 9. Which therapies have the word “movement” in their description? List the therapy description only.&lt;/p&gt;

&lt;p&gt;-- 10. How many therapies are offered? Answer with one number only.&lt;/p&gt;

&lt;p&gt;-- 11. Write an SQL query that displays the therapies and their unit of time for the therapies that include the word bath, hot, or electrical.&lt;/p&gt;

&lt;p&gt;-- 12. Write an SQL query to display every therapist’s first name and last name as their full name, but only for those instructors not living in zip code 72511.&lt;/p&gt;

</description>
      <category>bsit</category>
    </item>
    <item>
      <title>DATA 210 WK4 Assignment</title>
      <dc:creator>Angel Paudel</dc:creator>
      <pubDate>Sun, 26 Mar 2023 17:01:05 +0000</pubDate>
      <link>https://tyrocity.com/database/data-210-wk4-assignment-48h8</link>
      <guid>https://tyrocity.com/database/data-210-wk4-assignment-48h8</guid>
      <description>&lt;p&gt;In the following exercises, you will use the data in the JC Consulting database. Don’t forget to use a copy of the JC Consulting database so any changes you make will not affect future modules.&lt;/p&gt;

&lt;p&gt;Examine the JC Consulting data and complete the following case exercises/problems:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What is or are not a benefit(s) of a view within the JC Consulting database?&lt;/li&gt;
&lt;li&gt;Which fields from the Projects table is or are not a good candidate to index?&lt;/li&gt;
&lt;li&gt;Which security features is or are not available to the JC Consulting database given it is an Access database?&lt;/li&gt;
&lt;li&gt;In the Employees table, how could you ensure that no HireDate values were entered prior to 1/1/2019?&lt;/li&gt;
&lt;li&gt;To query whether the Clients table has records that do not have any matching records in the Projects table, which join statement should be used?&lt;/li&gt;
&lt;li&gt;Write a SQL command that adds a new field named Region to the Clients table.&lt;/li&gt;
&lt;li&gt;What feature could you use to create system information about the tables and fields in the JC Consulting database?&lt;/li&gt;
&lt;li&gt;Where do you create data macros in the JC Consulting Access database?&lt;/li&gt;
&lt;li&gt;According to the article referenced by the Bureau of Labor Statistics, which fundamental competency for a job in database administration is not worth knowing?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Apply key module concepts to the JC Consulting case:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using Access, open the Relationships window and identify all of the one-to-many relationships in the database as well as the field used in both tables to make the connection using the following pattern found in SQL, when connecting two tables using a WHERE or INNER JOIN clause. An example of one of the relationships is provided as a guide.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Clients.ClientID = Projects.ClientID&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using Access, an employee at JC Consulting tried to delete TaskID CODE01 from the
TaskMasterList table and received the following error message: “The record cannot be
deleted or changed because table ‘ProjectLineItemsincludes related records.”&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Why did the employee receive this error message?&lt;/p&gt;

&lt;p&gt;Submit your answers to the Examine and Apply questions in a single Word document.&lt;/p&gt;

&lt;p&gt;===&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick Guide:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Q1: List the benefits of "view" for JC Consulting DB. What does it enable? Think and answer based on it.&lt;/p&gt;

&lt;p&gt;Q2: Index refers to primary, foreign key. From the projects table - what is and what is not the index?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tyrocity.com/images/sHou2xrAN6DP2eQ6ZQqyckkt_YY1F4o6M4Vq9GTLyPk/rt:fit/w:800/g:sm/q:0/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9ncGhsYjYwbnZ6/eGs0aHBoOTFqei5w/bmc" class="article-body-image-wrapper"&gt;&lt;img src="https://tyrocity.com/images/sHou2xrAN6DP2eQ6ZQqyckkt_YY1F4o6M4Vq9GTLyPk/rt:fit/w:800/g:sm/q:0/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9ncGhsYjYwbnZ6/eGs0aHBoOTFqei5w/bmc" alt="index identification: projects table" width="800" height="517"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Q3: This is a MSAccess DB - so what security limitations does it have? Go general.&lt;/p&gt;

&lt;p&gt;Q4: You can do this in either &lt;a href="https://tyrocity.com/database/triggers-in-mysql-25f1"&gt;MySQL (trigger&lt;/a&gt; to make it no new entries are with that detail) or Access (with criteria)&lt;/p&gt;

&lt;p&gt;Q5: Review the relationships of the table - you simply need to answer what type of JOIN is to be used and why (or write the query itself)&lt;/p&gt;

&lt;p&gt;Q6: SQL query - general.&lt;/p&gt;

&lt;p&gt;Q7: General question, no review of data itself required.&lt;/p&gt;

&lt;p&gt;Q8: Macro creation - one of your group presented about it in the class itself. Does not ask you to write a macro itself but rather "where do you create" - as think smart.&lt;/p&gt;

&lt;p&gt;Q9: Refer to post here: &lt;a href="https://www.bls.gov/ooh/computer-and-information-technology/database-administrators.htm#tab-1" rel="noopener noreferrer"&gt;https://www.bls.gov/ooh/computer-and-information-technology/database-administrators.htm#tab-1&lt;/a&gt; or any other as you see fit. Navigate across different tabs for further info.&lt;/p&gt;

&lt;p&gt;Part 2 Q1: Below is the relation chat - one of the answer is already there in question list other such answers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tyrocity.com/images/MdP9FmjDXMTuXBwb4wji6gsTp9guJTAU9uOsNNT03aI/rt:fit/w:800/g:sm/q:0/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy8yOTJzdjhzbWc4/ZHRwdXZ0YWRsYy5w/bmc" class="article-body-image-wrapper"&gt;&lt;img src="https://tyrocity.com/images/MdP9FmjDXMTuXBwb4wji6gsTp9guJTAU9uOsNNT03aI/rt:fit/w:800/g:sm/q:0/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy8yOTJzdjhzbWc4/ZHRwdXZ0YWRsYy5w/bmc" alt="JC Consulting DB relationship" width="800" height="381"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Part 2 Q2: Why is there the error message - a simple answer would suffice.&lt;/p&gt;

</description>
      <category>bsit</category>
    </item>
    <item>
      <title>Triggers in MySQL</title>
      <dc:creator>Angel Paudel</dc:creator>
      <pubDate>Thu, 23 Mar 2023 16:26:06 +0000</pubDate>
      <link>https://tyrocity.com/database/triggers-in-mysql-25f1</link>
      <guid>https://tyrocity.com/database/triggers-in-mysql-25f1</guid>
      <description>&lt;p&gt;Triggers are associated with a table and can not operate on it's own. If it's not specified to a table - it does not exist.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types of Triggers:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;BEFORE INSERT&lt;/li&gt;
&lt;li&gt;AFTER INSERT&lt;/li&gt;
&lt;li&gt;BEFORE UPDATE&lt;/li&gt;
&lt;li&gt;AFTER UPDATE&lt;/li&gt;
&lt;li&gt;BEFORE DELETE&lt;/li&gt;
&lt;li&gt;AFTER DELETE&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Trigger general flow / Syntax&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tyrocity.com/images/5HejZBI6fc2aVxCGHyxiz7rNH-fpPtS24oQzLCiZFFs/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9zOGU2ZXN4MXA4/Yndib3h0ZDF0Zy5w/bmc" class="article-body-image-wrapper"&gt;&lt;img src="https://tyrocity.com/images/5HejZBI6fc2aVxCGHyxiz7rNH-fpPtS24oQzLCiZFFs/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9zOGU2ZXN4MXA4/Yndib3h0ZDF0Zy5w/bmc" alt="Trigger syntax" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In text:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TRIGGER TriggerName
    (AFTER | BEFORE) (INSERT | UPDATE | DELETE)  
         ON TableName FOR EACH ROW    
         BEGIN    
        -- trigger body   
        END;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A new terminology: &lt;strong&gt;DELIMITER&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The default delimiter MySQL considers is semicolon (;). However, in cases where ; might be used before the actual end of the query, command - we need to find a way to get past it. This is where we set the delimiter to be anything else. Often times, it is commonly set to $$, which means that the semicolon (;) used in the trigger code WILL NOT be interpreted as the end of the statement.&lt;/p&gt;

&lt;p&gt;Then, after the END statement of the trigger, you need to use the same delimiter ($$) again to tell MySQL that the trigger definition has ended, and reset the delimiter to its default value (;) using the statement DELIMITER ;.&lt;/p&gt;

&lt;p&gt;An example of the same as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELIMITER $$
CREATE TRIGGER TriggerName
    (AFTER | BEFORE) (INSERT | UPDATE | DELETE)  
         ON TableName FOR EACH ROW    
         BEGIN    
        -- trigger body   
        END $$
DELIMITER ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's practice.&lt;/p&gt;

&lt;p&gt;First, let's create the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE DATABASE triggers;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's create our first table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE `employee` (
  `name` varchar(45) NOT NULL,
  `occupation` varchar(35) DEFAULT NULL,
  `working_date` date DEFAULT NULL,
  `working_hours` varchar(10) DEFAULT NULL,
  `Age` varchar(3) DEFAULT NULL
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Dumping data for table &lt;code&gt;employee&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO `employee` (`name`, `occupation`, `working_date`, `working_hours`, `Age`) VALUES
('Antonio', 'Business', '2020-10-04', '11', '52'),
('Brayden', 'Teacher', '2020-10-04', '12', '44'),
('Marco', 'Doctor', '2020-10-04', '14', '28'),
('Peter', 'Actor', '2020-10-04', '13', '26'),
('Robin', 'Scientist', '2020-10-04', '12', '29'),
('Warner', 'Engineer', '2020-10-04', '10', '32');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;BEFORE INSERT Trigger - 1&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELIMITER $$
CREATE TRIGGER capitalize_name
BEFORE INSERT ON employee
FOR EACH ROW
BEGIN
SET NEW.name = CONCAT(UCASE(LEFT(NEW.name, 1)), SUBSTRING(NEW.name, 2));
END $$
DELIMITER ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Where to find the triggers?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;From your phpMyAdmin interface - click on database you're working under. On the page that opens, from top menu bar locate "Triggers" and click on it to find it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tyrocity.com/images/Y2Bg-07eoJPWSL-Fo5svnoIGbecV7dvocvHfVsST1co/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9zbnJiOGl5M3Qy/Y3g1ajR2Zjg1ZS5w/bmc" class="article-body-image-wrapper"&gt;&lt;img src="https://tyrocity.com/images/Y2Bg-07eoJPWSL-Fo5svnoIGbecV7dvocvHfVsST1co/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9zbnJiOGl5M3Qy/Y3g1ajR2Zjg1ZS5w/bmc" alt="locating trigger in phpMyAdmin" width="880" height="188"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Clicking on it would open interface as below where all the avaiable triggers for the database along with the table those trigger are associated with is displayed:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tyrocity.com/images/zIXXd23ZZ6laqKpRi2DViCqrdpF7-rEgj9KGSn_pie4/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9lYnkxdGpxZGpw/N3g4cW5qcjBsOS5w/bmc" class="article-body-image-wrapper"&gt;&lt;img src="https://tyrocity.com/images/zIXXd23ZZ6laqKpRi2DViCqrdpF7-rEgj9KGSn_pie4/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9lYnkxdGpxZGpw/N3g4cW5qcjBsOS5w/bmc" alt="trigger list phpMyAdmin" width="880" height="193"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ok, so let's get to the first trigger we just created - what does it do and how does it work?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ignoring the general structure for trigger, and just taking the query from it, that would be:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SET NEW.name = CONCAT(UCASE(LEFT(NEW.name, 1)), SUBSTRING(NEW.name, 2));&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here's a breakdown of what each function does: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;LEFT(NEW.name, 1)&lt;/strong&gt;: Returns the leftmost character of the name field, i.e. the first letter. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;UCASE((LEFT(NEW.name, 1))&lt;/strong&gt;: Converts the first letter from left to uppercase.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SUBSTRING(NEW.name, 2)&lt;/strong&gt;: Returns the remainder of the name field starting from the second character.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CONCAT()&lt;/strong&gt;: Concatenates the capitalized first letter with the rest of the name field.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This will take the first character of the name and capitalize it and then join it with all the other text to insert the name.&lt;/p&gt;

&lt;p&gt;Let's test if the trigger work by attempting to insert a name which is in lower case via SQL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO `employee` (`name`, `occupation`, `working_date`, `working_hours`, `Age`) VALUES
('john', 'Business', '2020-10-04', '11', '52');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The outcome however, has the first letter 'j' of "john" capitalized  even if we entered it in all lower case:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tyrocity.com/images/YLRy2COm-rY-2LJJI-xc5BpHRjxuiy38mwSqdOrwW_U/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy80dWNzZWNyeXR3/enMxZXFzOTNzbC5w/bmc" class="article-body-image-wrapper"&gt;&lt;img src="https://tyrocity.com/images/YLRy2COm-rY-2LJJI-xc5BpHRjxuiy38mwSqdOrwW_U/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy80dWNzZWNyeXR3/enMxZXFzOTNzbC5w/bmc" alt="trigger outcome" width="858" height="628"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This confirms that the query did work as required.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BEFORE INSERT Trigger - 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's test with another trigger for the same table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELIMITER $$
CREATE TRIGGER set_min_working_hours
BEFORE INSERT ON employee
FOR EACH ROW
BEGIN
    IF NEW.working_hours &amp;lt; 8 THEN
        SET NEW.working_hours = 8;
    END IF;
END $$
DELIMITER ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This trigger do not allow a new employee with working hours less than 8. If it's any lower it'll automatically set it to be 8.&lt;/p&gt;

&lt;p&gt;Let's test this by inserting an employee with working hour less than 8:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO employee (name,working_hours) VALUES
('master',3);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The outcome:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tyrocity.com/images/mX0VDYkq7np7Hc6nxgNJyAvdrTdrrjwDo8EQBICm3Ow/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy83MnZxanIzdjFx/M291bmY0djQ1ay5w/bmc" class="article-body-image-wrapper"&gt;&lt;img src="https://tyrocity.com/images/mX0VDYkq7np7Hc6nxgNJyAvdrTdrrjwDo8EQBICm3Ow/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy83MnZxanIzdjFx/M291bmY0djQ1ay5w/bmc" alt="trigger outcome 2" width="880" height="637"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see from the above outcome - the name even if was inserted in all lower case has it's first letter capital while the working hour even if was set to be 3 is coming as 8.&lt;/p&gt;

&lt;p&gt;This refers that both the triggers are working for the following table.&lt;/p&gt;




&lt;p&gt;Let's create 2 tables &lt;code&gt;order&lt;/code&gt; and &lt;code&gt;counter&lt;/code&gt; to test AFTER INSERT trigger. Below is the code used to create the table and dump values  as required. We will only populate one of the two tables as of now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE orders (
    id INT PRIMARY KEY AUTO_INCREMENT,
    order_date DATE,
    customer_name VARCHAR(50),
    total_amount DECIMAL(10, 2)
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE counter (
    id INT PRIMARY KEY,
    order_count INT DEFAULT 0
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO counter (id, order_count) VALUES (1, 0);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's now create a trigger to count the number of orders i.e. as any new values are inserted in the table &lt;code&gt;orders&lt;/code&gt; - the &lt;code&gt;order_count&lt;/code&gt; field under &lt;code&gt;counter&lt;/code&gt; table should update as well with the correct count.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AFTER INSERT Trigger - 1&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELIMITER $$
CREATE TRIGGER orders_insert_trigger
AFTER INSERT ON orders
FOR EACH ROW
BEGIN
    UPDATE counter SET order_count = order_count + 1;
END $$
DELIMITER ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's try inserting the values to see how it works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO orders (order_date, customer_name, total_amount) VALUES ('2023-03-21', 'John Doe', 100.00);
INSERT INTO orders (order_date, customer_name, total_amount) VALUES ('2023-03-21', 'Ram Bahadur', 300.00);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's the outcome:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tyrocity.com/images/Ya0hgLUBOKIfCUvgyRX5IZwD93Xjzs8Pxf1jM7dqcFc/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9wdTRncWYxamc1/ZTY4MWo4a2VpYi5w/bmc" class="article-body-image-wrapper"&gt;&lt;img src="https://tyrocity.com/images/Ya0hgLUBOKIfCUvgyRX5IZwD93Xjzs8Pxf1jM7dqcFc/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9wdTRncWYxamc1/ZTY4MWo4a2VpYi5w/bmc" alt="trigger order count outcome" width="880" height="528"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With 2 new values inserted to &lt;code&gt;order&lt;/code&gt; table - the &lt;code&gt;order_count&lt;/code&gt; increased accordingly. You can test this further by adding more orders.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AFTER INSERT Trigger - 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For this, let's try to calculate the total order value. And, for which we will create a table &lt;code&gt;amount_counter&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE amount_counter (
  id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  total_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO amount_counter (total_amount) VALUES (0);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's create a trigger for the same as below to calculate the total order value (P.S. this will only count for new orders as such if you do already have fields in the table the outcome will be different) :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELIMITER $$
CREATE TRIGGER orders_amount_trigger 
AFTER INSERT ON orders 
FOR EACH ROW 
BEGIN
  UPDATE amount_counter SET total_amount = total_amount + NEW.total_amount WHERE id = 1;
END $$
DELIMITER ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's test this by inserting two new values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO orders (order_date, customer_name, total_amount) VALUES ('2023-03-22', 'Sita Kumari', 200.00);
INSERT INTO orders (order_date, customer_name, total_amount) VALUES ('2023-03-22', 'RK Bahadur', 400.00);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, visiting &lt;code&gt;amount_counter&lt;/code&gt; table, we can see that the &lt;code&gt;total_amount&lt;/code&gt; is 600 as it should be based on the two values we added:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tyrocity.com/images/izNX9euzE7UKIcK2CPPWZDoc0b6fxv78rCEKbin2lrM/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9qcG03YTZqamdw/emZnMXRwdng5dS5w/bmc" class="article-body-image-wrapper"&gt;&lt;img src="https://tyrocity.com/images/izNX9euzE7UKIcK2CPPWZDoc0b6fxv78rCEKbin2lrM/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9qcG03YTZqamdw/emZnMXRwdng5dS5w/bmc" alt="amount counter trigger outcome" width="844" height="526"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;For learning about BEFORE UPDATE trigger - let's start by first creating the table and inserting dummy data to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE employee_salary (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    salary INT
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO employee_salary (id, name, salary) VALUES
(1, 'John', 50000),
(2, 'Sarah', 60000),
(3, 'Mike', 45000),
(4, 'Emily', 55000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the table is created - let's now create the trigger for &lt;strong&gt;BEFORE UPDATE&lt;/strong&gt; action. In this trigger, we check for UPDATE action if the salary is anything lower than 40,000 and if it is the case - the salary is auto set to the lowest limit allowed which would be 40,000.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELIMITER $$
CREATE TRIGGER update_salary
BEFORE UPDATE ON employee_salary
FOR EACH ROW
BEGIN
    IF NEW.salary &amp;lt; 40000 THEN
        SET NEW.salary = 40000;
    END IF;
END $$
DELIMITER ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's test the trigger to action - for this let's try setting the salary for one of the employee to 35,000 and see what happens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE employee_salary SET salary = 35000 WHERE id = 3;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://tyrocity.com/images/D0xkzE05yLIff9Y6idbxK7Ig42K4_W0YDAEisEFItTA/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy8yOG1vcTZlcTFq/bHg5NmF1dnBlbi5w/bmc" class="article-body-image-wrapper"&gt;&lt;img src="https://tyrocity.com/images/D0xkzE05yLIff9Y6idbxK7Ig42K4_W0YDAEisEFItTA/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy8yOG1vcTZlcTFq/bHg5NmF1dnBlbi5w/bmc" alt="trigger update salary outcome" width="796" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see that the employee with id 3 has salary set to 40,000 by the system even if we had updated the salary to be 35,000 due to the trigger in place.&lt;/p&gt;

&lt;p&gt;The trigger prevented the salary to be set any lower than 40k - anything lower and it's set to 40k automatically.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;AFTER UPDATE trigger exercise&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's create 2 tables &lt;code&gt;product&lt;/code&gt; and &lt;code&gt;product_inventory&lt;/code&gt; to test AFTER UPDATE trigger. Below is the code used to create the table and dump values  as required:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE product (
  id INT PRIMARY KEY,
  name VARCHAR(50),
  price DECIMAL(10,2),
  quantity INT
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO product (id, name, price, quantity)
VALUES
  (1, 'Product 1', 10.00, 5),
  (2, 'Product 2', 20.00, 3),
  (3, 'Product 3', 30.00, 7),
  (4, 'Product 4', 15.00, 2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE product_inventory (
  product_id INT NOT NULL,
  quantity INT NOT NULL,
  PRIMARY KEY (product_id)
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO product_inventory (product_id, quantity)
VALUES
  (1, 5);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's create a trigger to update the &lt;code&gt;product_inventory&lt;/code&gt; table with the updated stock for product_id 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELIMITER $$
CREATE TRIGGER update_product_inventory
AFTER UPDATE ON product
FOR EACH ROW
BEGIN
  DECLARE inventory INT;

  SET inventory = NEW.quantity - OLD.quantity;

  UPDATE product_inventory
  SET quantity = quantity + inventory
  WHERE product_id = NEW.id;
END $$
DELIMITER ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to test it let's update quantity for Product 1 from 5 to 8 and see what happens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE product
SET quantity = 8
WHERE id = 1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The outcome as below shows that the &lt;code&gt;product_inventory&lt;/code&gt; table does have quantity increased to 8 instead of previous 5 even if the above UPDATE command was only done for product table, confirming the query action:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tyrocity.com/images/mXq_0eg0BPOqg0F8_g6ENZYAGzyAylFUn7nn4YmLlho/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9mOTFyOGc4dm42/OWYweGs5NGg2cC5w/bmc" class="article-body-image-wrapper"&gt;&lt;img src="https://tyrocity.com/images/mXq_0eg0BPOqg0F8_g6ENZYAGzyAylFUn7nn4YmLlho/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9mOTFyOGc4dm42/OWYweGs5NGg2cC5w/bmc" alt="after update query output" width="853" height="408"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;BEFORE DELETE trigger exercise&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For the following exercise, let's create a table &lt;code&gt;products&lt;/code&gt; of whose structure and dummy data are as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE products (
  id INT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  price DECIMAL(10,2) NOT NULL,
  stock_quantity INT NOT NULL
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO products (id, name, price, stock_quantity) VALUES
(1, 'Product 1', 10.50, 0),
(2, 'Product 2', 5.99, -50),
(3, 'Product 3', 20.00, 25),
(4, 'Product 4', 15.75, 75);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's create a trigger which will not allow the removal of any product if it has any stuck left (meaning - it'll only allow to remove products with 0 or lesser stock_quantity):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELIMITER $$
CREATE TRIGGER delete_products
BEFORE DELETE ON products
FOR EACH ROW
BEGIN
  IF OLD.stock_quantity &amp;gt; 0 THEN
    SIGNAL SQLSTATE '45000'
      SET MESSAGE_TEXT = 'Cannot delete product, stock quantity still left.';
  END IF;
END $$
DELIMITER ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's test the function by removing &lt;code&gt;product_id&lt;/code&gt; 1 which has 0 as &lt;code&gt;stock_quantity&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELETE FROM products WHERE id = 1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Outcome is successful execution of the same and data removal:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tyrocity.com/images/BBCwq88YdxneLM9RpIJMLjUIP85NloElraOP36qnHVo/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy82ZjdqaDFpaXlw/eG9hb3d3bXN4ai5w/bmc" class="article-body-image-wrapper"&gt;&lt;img src="https://tyrocity.com/images/BBCwq88YdxneLM9RpIJMLjUIP85NloElraOP36qnHVo/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy82ZjdqaDFpaXlw/eG9hb3d3bXN4ai5w/bmc" alt="BEFORE DELETE trigger outcome 1" width="535" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's now try removing another product with &lt;code&gt;product_id&lt;/code&gt; 3 which has 25 as &lt;code&gt;stock_quantity&lt;/code&gt; (which is higher than 0) :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELETE FROM products WHERE id = 3;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Outcome for this is a failed process:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tyrocity.com/images/xTcidXaw8So2JOGAqCFijYJzz5yW1NUx3OUeekIDQcU/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy92OGJzanJwMTFu/OGFuYjlrY3hidy5w/bmc" class="article-body-image-wrapper"&gt;&lt;img src="https://tyrocity.com/images/xTcidXaw8So2JOGAqCFijYJzz5yW1NUx3OUeekIDQcU/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy92OGJzanJwMTFu/OGFuYjlrY3hidy5w/bmc" alt="BEFORE DELETE trigger outcome 2" width="661" height="357"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, we can see the successful execution of the BEFORE DELETE trigger here which did not allow the operation to complete if there's at least one or more quantity left but did allow the removal of a product if it has 0 or lesser product left to sell.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;AFTER DELETE trigger exercise&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For the following activity/learning let's create two tables &lt;code&gt;salaries&lt;/code&gt; and &lt;code&gt;total_salary_budget&lt;/code&gt; and insert dummy data to it as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE salaries (  
    emp_num INT PRIMARY KEY,  
    salary DEC(8 , 2 ) NOT NULL DEFAULT 0  
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO salaries (emp_num, salary)  
VALUES  
    (101, 50000),  
    (102, 55000),  
    (103, 60000),
    (104, 65000),
    (105, 70000),              
    (106, 75000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE total_salary_budget(  
    total_budget DECIMAL(10,2) NOT NULL  
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO total_salary_budget (total_budget)  
SELECT SUM(salary) FROM salaries;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's create a trigger to update &lt;code&gt;total_salary_budget&lt;/code&gt; table's &lt;code&gt;total_budget&lt;/code&gt; field with the updated budget as a employee is fired/leaves the job (here: removed from the database table):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELIMITER $$    
CREATE TRIGGER after_delete_total_salary  
AFTER DELETE  
ON salaries FOR EACH ROW  
BEGIN  
   UPDATE total_salary_budget SET total_budget = total_budget - old.salary;  
END $$   
DELIMITER ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before writing a query to remove an employee - let's take a minute to review the current &lt;code&gt;total_budget&lt;/code&gt; where we can see it's 375,000.00:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tyrocity.com/images/ILi8gb-30x1PykEOagrlmg4fYcmy8KNf2ZnWsBUm0Ns/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy80eG9obDVwMXBq/cjRocHlybTk5bC5w/bmc" class="article-body-image-wrapper"&gt;&lt;img src="https://tyrocity.com/images/ILi8gb-30x1PykEOagrlmg4fYcmy8KNf2ZnWsBUm0Ns/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy80eG9obDVwMXBq/cjRocHlybTk5bC5w/bmc" alt="total budget - AFTER DELETE trigger learning" width="841" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's remove &lt;code&gt;emp_num&lt;/code&gt; from &lt;code&gt;salaries&lt;/code&gt; table using the following command and see what happens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELETE FROM salaries WHERE emp_num = 101;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;emp_num&lt;/code&gt; 101 had a salary of 50,000 as such when the employ is removed from the salary slab the &lt;code&gt;total_budget&lt;/code&gt; which was previously 375,000 should come as 325,000 if it was successfully updated. Let's check:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://tyrocity.com/images/KUY1rHfMkACecZ2knZd2T2MykqgJol5hh_k21P0prYE/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9hM2lxOHI1MTd4/dmk0emJncHRyZC5w/bmc" class="article-body-image-wrapper"&gt;&lt;img src="https://tyrocity.com/images/KUY1rHfMkACecZ2knZd2T2MykqgJol5hh_k21P0prYE/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9hM2lxOHI1MTd4/dmk0emJncHRyZC5w/bmc" alt="AFTER DELETE trigger test outcome" width="877" height="478"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can indeed see that the &lt;code&gt;total_budget&lt;/code&gt; is now 325,000 and as such we can confirm that the trigger did function as was intended.&lt;/p&gt;

&lt;p&gt;This is all for Triggers in MySQL. If you have any further concerns or questions - do drop them below in the comments section.&lt;/p&gt;

</description>
      <category>bsit</category>
    </item>
    <item>
      <title>DATA 210 Week 3 Class 3 Answers</title>
      <dc:creator>Angel Paudel</dc:creator>
      <pubDate>Fri, 17 Mar 2023 03:38:00 +0000</pubDate>
      <link>https://tyrocity.com/database/data-210-week-3-class-3-answers-270n</link>
      <guid>https://tyrocity.com/database/data-210-week-3-class-3-answers-270n</guid>
      <description>&lt;p&gt;-- 1. SUM: What is the total amount of all orders?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT SUM(order_total) FROM orders;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-- 2. COUNT: How many customers are there in the database?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT COUNT(*) FROM customers;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-- 3. AVG: What is the average total amount of all orders?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT AVG(order_total) FROM orders;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-- 4. MIN: What is the minimum total amount of all orders?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT MIN(order_total) FROM orders;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-- 5. MAX: What is the maximum total amount of all orders?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT MAX(order_total) FROM orders;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-- 6. GROUP BY: What is the total amount of orders for each customer?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT customer_id, SUM(order_total) FROM orders GROUP BY customer_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-- 7. SELECT subquery: What is the first name and last name of the customer with the highest total amount of orders?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT first_name, last_name FROM customers WHERE customer_id = (SELECT customer_id FROM orders GROUP BY customer_id ORDER BY SUM(order_total) DESC LIMIT 1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-- 8. WHERE subquery: What is the total amount of orders made by customer with last_name in Smith?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT SUM(order_total) FROM orders WHERE customer_id IN (SELECT customer_id FROM customers WHERE last_name = 'Smith');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-- 9. FROM subquery: What is the total amount of orders made by customers who live in the same city as customer_id 2? (ALTER table to include city field and add values to it with UPDATE)&lt;/p&gt;

&lt;p&gt;===========&lt;/p&gt;

&lt;h1&gt;
  
  
  EXTRA STEPS - Do first
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE customers ADD COLUMN city VARCHAR(255);

UPDATE customers SET city = 'New York' WHERE customer_id = 1;
UPDATE customers SET city = 'Kathmandu' WHERE customer_id = 2;
UPDATE customers SET city = 'Kathmandu' WHERE customer_id = 3;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT SUM(order_total) FROM orders WHERE customer_id IN (SELECT customer_id FROM customers WHERE city = (SELECT city FROM customers WHERE customer_id = 2));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-- 10. UNION: What are the customer names of customers who have placed an order, as well as customers who have not placed an order?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT first_name, last_name FROM customers WHERE customer_id IN (SELECT customer_id FROM orders) UNION SELECT first_name, last_name FROM customers WHERE customer_id NOT IN (SELECT customer_id FROM orders);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-- 11. UNION ALL: What are the customer names of customers who have placed an order, as well as customers who have not placed an order, and what is the corresponding city for each customer?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT first_name, last_name, city FROM customers WHERE customer_id IN (SELECT customer_id FROM orders) UNION ALL SELECT first_name, last_name, city FROM customers WHERE customer_id NOT IN (SELECT customer_id FROM orders);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-- 12. UPDATE: Change the total amount of order_id 3 to 100.00.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE orders SET order_total = 100.00 WHERE order_id = 3;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-- 13. INSERT: Add a new order for customer_id 101 with order_total 150.00.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO orders (customer_id, order_total) VALUES (101, 150.00);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-- 14. DELETE: Delete all orders with customer_id more than 100&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELETE FROM orders WHERE customer_id &amp;gt; 100;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>bsit</category>
    </item>
    <item>
      <title>DATA 210 Week 3 Class 3 Resources</title>
      <dc:creator>Angel Paudel</dc:creator>
      <pubDate>Thu, 16 Mar 2023 15:15:31 +0000</pubDate>
      <link>https://tyrocity.com/database/data-210-week-3-class-3-resources-5fje</link>
      <guid>https://tyrocity.com/database/data-210-week-3-class-3-resources-5fje</guid>
      <description>&lt;p&gt;Let's create a database to start with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE DATABASE WK3CLASS3;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's create 2 tables (orders &amp;amp; customers) and input data for the table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE orders (
  order_id INT NOT NULL AUTO_INCREMENT,
  customer_id INT NOT NULL,
  order_date DATE NOT NULL,
  order_total DECIMAL(10,2) NOT NULL,
  PRIMARY KEY (order_id)
);

INSERT INTO orders (customer_id, order_date, order_total)
VALUES (1, '2022-12-01', 100.50),
       (1, '2022-12-02', 75.25),
       (2, '2022-12-01', 200.00),
       (2, '2022-12-03', 150.75),
       (3, '2022-12-02', 50.00),
       (3, '2022-12-03', 125.50);

CREATE TABLE customers (
  customer_id INT NOT NULL AUTO_INCREMENT,
  first_name VARCHAR(50) NOT NULL,
  last_name VARCHAR(50) NOT NULL,
  email VARCHAR(100) NOT NULL,
  PRIMARY KEY (customer_id)
);

INSERT INTO customers (first_name, last_name, email)
VALUES ('John', 'Doe', 'jdoe@example.com'),
       ('Jane', 'Smith', 'jsmith@example.com'),
       ('Bob', 'Johnson', 'bjohnson@example.com');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Questions to practice&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;-- 1. SUM: What is the total amount of all orders?&lt;/p&gt;

&lt;p&gt;-- 2. COUNT: How many customers are there in the database?&lt;/p&gt;

&lt;p&gt;-- 3. AVG: What is the average total amount of all orders?&lt;/p&gt;

&lt;p&gt;-- 4. MIN: What is the minimum total amount of all orders?&lt;/p&gt;

&lt;p&gt;-- 5. MAX: What is the maximum total amount of all orders?&lt;/p&gt;

&lt;p&gt;-- 6. GROUP BY: What is the total amount of orders for each customer?&lt;/p&gt;

&lt;p&gt;-- 7. SELECT subquery: What is the first name and last name of the customer with the highest total amount of orders?&lt;/p&gt;

&lt;p&gt;-- 8. WHERE subquery: What is the total amount of orders made by customer with last_name in Smith?&lt;/p&gt;

&lt;p&gt;-- 9. FROM subquery: What is the total amount of orders made by customers who live in the same city as customer_id 2? (ALTER table to include city field and add values to it with UPDATE)&lt;/p&gt;

&lt;p&gt;Do perform the following action before writing SQL for the question::&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE customers ADD COLUMN city VARCHAR(255);

UPDATE customers SET city = 'New York' WHERE customer_id = 1;
UPDATE customers SET city = 'Kathmandu' WHERE customer_id = 2;
UPDATE customers SET city = 'Kathmandu' WHERE customer_id = 3;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-- 10. UNION: What are the customer names of customers who have placed an order, as well as customers who have not placed an order?&lt;/p&gt;

&lt;p&gt;Do perform the following action before writing the query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO customers (first_name, last_name, email)
VALUES ('Happy', 'Says', 'happy@example.com');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-- 11. UNION ALL: What are the customer names of customers who have placed an order, as well as customers who have not placed an order, and what is the corresponding city for each customer?&lt;/p&gt;

&lt;p&gt;-- 12. UPDATE: Change the total amount of order_id 3 to 100.00.&lt;/p&gt;

&lt;p&gt;-- 13. INSERT: Add a new order for customer_id 101 with order_total 150.00.&lt;/p&gt;

&lt;p&gt;-- 14. DELETE: Delete all orders with customer_id more than 100&lt;/p&gt;

</description>
      <category>bsit</category>
    </item>
    <item>
      <title>Title:- Limitless hope in limited life</title>
      <dc:creator>Crijan Stha</dc:creator>
      <pubDate>Thu, 07 Apr 2022 03:14:38 +0000</pubDate>
      <link>https://tyrocity.com/crijan_stha/title-limitless-hopes-in-limited-life-2lck</link>
      <guid>https://tyrocity.com/crijan_stha/title-limitless-hopes-in-limited-life-2lck</guid>
      <description>&lt;p&gt;&lt;a href="https://tyrocity.com/images/qzEZmQRjk9vOuDjZ8M_0QOff8CvzrrOe48njAPE1VKk/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9tZGNsOHJwaGts/aDV4NmhmNTc1by5K/UEc" class="article-body-image-wrapper"&gt;&lt;img src="https://tyrocity.com/images/qzEZmQRjk9vOuDjZ8M_0QOff8CvzrrOe48njAPE1VKk/w:880/mb:500000/ar:1/aHR0cHM6Ly90eXJv/Y2l0eS5jb20vdXBs/b2Fkcy9hcnRpY2xl/cy9tZGNsOHJwaGts/aDV4NmhmNTc1by5K/UEc" alt="Limitless hope in limited life" width="880" height="1151"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>articlehub</category>
    </item>
    <item>
      <title>Unexpected problems while using email</title>
      <dc:creator>Discuss User</dc:creator>
      <pubDate>Wed, 23 Mar 2022 10:23:10 +0000</pubDate>
      <link>https://tyrocity.com/mba/unexpected-problems-while-using-email-39d4</link>
      <guid>https://tyrocity.com/mba/unexpected-problems-while-using-email-39d4</guid>
      <description>&lt;p&gt;What unexpected problems might you face as a user of electronic mail?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post was part of TyroCity discussion forum&lt;br&gt;
Question asked by binsa_thapa&lt;/em&gt;&lt;/p&gt;

</description>
      <category>mba</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Causes and solution of child Labour</title>
      <dc:creator>Discuss User</dc:creator>
      <pubDate>Wed, 23 Mar 2022 10:22:41 +0000</pubDate>
      <link>https://tyrocity.com/discuss/causes-and-solution-of-child-labour-10e</link>
      <guid>https://tyrocity.com/discuss/causes-and-solution-of-child-labour-10e</guid>
      <description>&lt;p&gt;What are the causes of child labour? And what will be the solutions for it?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post was part of TyroCity discussion forum&lt;br&gt;
Question asked by Lazarus&lt;/em&gt;&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>What do you understand by viscosity?</title>
      <dc:creator>Discuss User</dc:creator>
      <pubDate>Wed, 23 Mar 2022 10:22:06 +0000</pubDate>
      <link>https://tyrocity.com/discuss/what-do-you-understand-by-viscosity-436h</link>
      <guid>https://tyrocity.com/discuss/what-do-you-understand-by-viscosity-436h</guid>
      <description>&lt;p&gt;What do you understand by viscosity?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post was part of TyroCity discussion forum&lt;br&gt;
Question asked by aadarsh_gautam&lt;/em&gt;&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>States of matter ( Gaseous State)</title>
      <dc:creator>Discuss User</dc:creator>
      <pubDate>Wed, 23 Mar 2022 10:21:41 +0000</pubDate>
      <link>https://tyrocity.com/discuss/states-of-matter-gaseous-state-3n53</link>
      <guid>https://tyrocity.com/discuss/states-of-matter-gaseous-state-3n53</guid>
      <description>&lt;p&gt;How would you define universal gas constant R&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post was part of TyroCity discussion forum&lt;br&gt;
Question asked by Heena&lt;/em&gt;&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
  </channel>
</rss>
