SQL Query
In this microlearning, we will explore the fundamentals of SQL queries. By understanding these concepts, you will be equipped to write basic queries to retrieve and modify data in a database.
Should you have any questions, please get in touch with academy@emagiz.com.
1. Prerequisites
- Basic knowledge of the eMagiz platform
2. Key concepts
Each type of external database will need specific queries to perform actions on the database level. Mostly we see that SQL queries are needed; therefore, we focus on them in this microlearning.
3. SQL Query
In this microlearning, we will learn the basics of SQL queries. With the help of this information, you can start writing the correct queries to retrieve and write data from and to a database. Just as with REST web services, the CRUD operations are represented within the SQL language. They are described as follows:
- INSERT = Create
- SELECT = Read
- UPDATE = Update
- DELETE = Delete
In the remainder of the microlearning, we will take a look at each of these CRUD operations in a little bit more detail. Note that we will cover the more straightforward cases and that your SQL query could be immensely more complex depending on your use case.
3.1 INSERT
With an insert statement, you create a new record in the database. Within an insert statement, you define the following:
- The table name (mytable)
- The column names (id, created date, contents)
- The value per column (:headers[id], :headers[timestamp], :payload)
When combining all of this you will end up with something like this: INSERT INTO mytable (id, created date, contents) values (:headers[id], :headers[timestamp], :payload). As you can see, we want to insert our row into the table called mytable. We want to insert three values in three separate columns (id, created date, contents). Furthermore, you should note that you can use the header values and (part of) the payload as dynamic input for those values. The notation as depicted above is paramount in making this work.
3.2 SELECT
With a select statement, you read one or more records from the database. Within a select statement, you define the following:
- The columns you want to be returned (* in case of all columns)
- The table from which to read
- Optional: A condition (WHERE x=x)
When combining all of this, you will end up, in the simplest form, with something like this: SELECT * from mytable. Expanding on that, we could define that we only want to retrieve the id and contents column. To do so, we slightly alter our SQL query to this: SELECT id,contents from mytable. Building on that further, we could add a condition to the statement, a so-called where clause. With the help of that clause, we can even further narrow down our result set. An example of that would be SELECT id,contents from mytable WHERE id = :headers[id].
3.3 UPDATE
With an update statement, you update one or more records in the database. Within an update statement, you define the following:
- The table name
- What you want to change (SET x to y)
- Optional: A condition (WHERE x=x)
When combining all of this you will end up with something as follows UPDATE mytable SET changeddate = :headers[timestamp] where id = :headers[id]. This statement will update the changed date of the record with a specific id to reflect the provided timestamp.
3.4 DELETE
With an update statement, you delete one or more records in the database. Within a delete statement, you define the following:
- The table name
- Optional: A condition (WHERE x=x)
When combining all of this, you will end up with something as follows DELETE FROM mytable WHERE id = :headers[id]. This statement will delete the row within the table for which the id matches the id in my header. This way, you can clean up parts of your database table.
4. Key takeaways
- These examples cover the basic SQL operations: INSERT (Create), SELECT (Read), UPDATE (Update), and DELETE (Delete).
- SQL queries allow you to perform essential CRUD operations to manage data in a database.
- You can use dynamic values in SQL queries through SpEL expressions, making your queries more adaptable and flexible.
- While SQL is commonly used, other databases may require different query languages or syntax for similar operations.
- Always ensure conditions (like WHERE clauses) are applied to avoid unintentional modifications or deletions of data.
5. Suggested additional readings
If you are interested in this topic and want more information on it, please read the help text provided by eMagiz and see the following links: