Reader small image

You're reading from  The SQL Workshop

Product typeBook
Published inDec 2019
Reading LevelBeginner
PublisherPackt
ISBN-139781838642358
Edition1st Edition
Languages
Concepts
Right arrow
Authors (3):
Frank Solomon
Frank Solomon
author image
Frank Solomon

Frank Solomon started out building front-end and database software with Microsoft stack tools such as SQL Server and ASP and then extended into ASP.NET, C#, VB.NET, JavaScript, and more. He learns about new products, languages, and technologies all the time, and he pushed into technical writing as a way to present and express his research and discoveries. He works as a contractor right now. He has worked in start-ups himself, and he knows that the hard work of writers and developers makes the end user experience easier.
Read more about Frank Solomon

Prashanth Jayaram
Prashanth Jayaram
author image
Prashanth Jayaram

Prashanth Jayaram is a database technologist, blogger, engineering lead, automation expert, TechNet wiki ninja, PowerShell Geek, and technologist enthusiast with extensive experience in designing database solutions. He is the author of PowerShell 6.0 Linux Administration Cookbook and has hands-on experience with the next generation database technologies. He has been awarded as ABOVE and BEYOND and Best SQL Author 2018 towards his contribution to SQL Server technology. He has articulated over 200+ articles widespread across SQL, NoSQL, PowerShell, Python, SQL on Linux, SQL on Azure, and SQL on AWS arenas.
Read more about Prashanth Jayaram

Awni Al Saqqa
Awni Al Saqqa
author image
Awni Al Saqqa

Awni Al Saqqa is a Microsoft Technology Specialist in MS SQL Server and a certified solutions developer since 2007. He has over a decade of experience with database development and administration on SQL Server, Oracle, and MySQL. He is a solutions architect, who is hands-on in many enterprise projects for different business sectors, such as education, hospitality, retail, manufacturing, marketing, and more, which has given him the perfect combination between business and technical experience.
Read more about Awni Al Saqqa

View More author details
Right arrow

8. SQL Programming

Overview

In this chapter, we'll see how we can build programs and software that reliably automate database operations, thus returning huge savings of time, effort, and money.

By the end of this chapter, you'll be able to build and execute stored procedures that automate database operations. You will be able to build and execute functions that encapsulate repeated statements in defined, structured units. Moreover, you will be equipped to build and execute triggers that automatically execute database operations when predefined events occur.

Introduction

In the previous chapter, we explored SQL subqueries, CASE statements, and views. These features offer great flexibility when we work with database resources. To use these features, and all the other SQL product query features we have seen so far, we type a SQL command into the development environment, run it, and look for the results somewhere within that environment. This approach certainly works well enough, and we relied on it as we learned about MySQL. However, it won't work for applications that rely on SQL database products as data resources. We need a product feature that can somehow automate the queries we want to run and reliably handle all the required management and overhead. Fortunately, modern SQL products, including MySQL, offer the following features to solve this problem:

  • Stored procedures
  • Functions
  • Triggers

These features involve actual programming, and in this chapter, we'll explore the basic programming concepts. We&apos...

Programming for SQL Products – The Basics

MySQL programming, like all programming and software development, relies on a small set of core ideas. Variables are an important part of that idea set, and we'll see how they work as we begin to work with MySQL.

When we build programs in MySQL or in any other software development language, we use variables as buckets to hold information, or values, that the programs need. A variable has a name, and in most software products—including MySQL—it has an information type. In a structured, predictable way, the program can change the value of a variable based on the behavior of the program, information that comes into the program from outside of the program, or both. A variable will reliably hold the last value assigned to it. Before we look at SQL stored procedures and functions, we'll see how variables operate in MySQL.

Launch MySQL and place the following statements in the editor or the query window:

SET...

Functions

The complexity of the software we build expands as we build it. In part, this becomes unavoidable because the mass of the software itself also expands. However, we can definitely avoid the complexity of repeating blocks of code. Modern software development products give us a way to place one copy of a repeating block of code in one defined location. Then, in the software, we can replace all repeated instances of that code block with a call to that one defined copy of the code. We call this one defined block of software a function. Think of a function as a box that takes in zero or more values and returns one or more values in a structured way. A function simplifies software, and it makes the software we build much easier to maintain, repair, and enhance. If a program has the same identical code block repeated one hundred times, the same required change to all of those blocks would require the same work and testing—one hundred times. If we replace those blocks with...

Summary

In this chapter, we saw that we can build powerful, structured software with SQL stored procedures, SQL functions, and SQL triggers. These tools integrate core programming concepts with SQL machinery to build reliable, flexible solutions to complex programming problems. With these tools, we can leverage the power and potential of SQL database products. In the next chapter, we will cover the best practices for securing database resources from common potential threats.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
The SQL Workshop
Published in: Dec 2019Publisher: PacktISBN-13: 9781838642358
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Frank Solomon

Frank Solomon started out building front-end and database software with Microsoft stack tools such as SQL Server and ASP and then extended into ASP.NET, C#, VB.NET, JavaScript, and more. He learns about new products, languages, and technologies all the time, and he pushed into technical writing as a way to present and express his research and discoveries. He works as a contractor right now. He has worked in start-ups himself, and he knows that the hard work of writers and developers makes the end user experience easier.
Read more about Frank Solomon

author image
Prashanth Jayaram

Prashanth Jayaram is a database technologist, blogger, engineering lead, automation expert, TechNet wiki ninja, PowerShell Geek, and technologist enthusiast with extensive experience in designing database solutions. He is the author of PowerShell 6.0 Linux Administration Cookbook and has hands-on experience with the next generation database technologies. He has been awarded as ABOVE and BEYOND and Best SQL Author 2018 towards his contribution to SQL Server technology. He has articulated over 200+ articles widespread across SQL, NoSQL, PowerShell, Python, SQL on Linux, SQL on Azure, and SQL on AWS arenas.
Read more about Prashanth Jayaram

author image
Awni Al Saqqa

Awni Al Saqqa is a Microsoft Technology Specialist in MS SQL Server and a certified solutions developer since 2007. He has over a decade of experience with database development and administration on SQL Server, Oracle, and MySQL. He is a solutions architect, who is hands-on in many enterprise projects for different business sectors, such as education, hospitality, retail, manufacturing, marketing, and more, which has given him the perfect combination between business and technical experience.
Read more about Awni Al Saqqa