DEV Community

Devincb93
Devincb93

Posted on • Updated on

SQL

SQL, or Structured Query Language, is a powerful programming language used primarily for managing and manipulating databases. It's an invaluable tool for inserting, modifying, and even deleting data within databases.

Recently, I embarked on a project to create a simple Library database system. This project highlighted the critical role SQL plays in database management. Without SQL, it wouldn't have been possible to efficiently handle the various data operations needed for the system. During my journey, I faced a few challenges when learning SQL—because who doesn't have trouble when they first start learning something?

The main issue I encountered was surprisingly related to using ORM (Object-Relational Mapping) methods. I kept trying to create new functions for methods that were already available in the ORM. It was like reinventing the wheel without realizing the tools already at my disposal. Once I took a step back and thought, "Why am I doing this?" I immediately started to see the true power in SQL and ORM. The 'Aha' moment finally clicked. My project suddenly became much easier to manage and create. I never knew it could be this easy; honestly, I wished I had figured it out sooner.

What are ORM Methods?
Object-Relational Mapping is a technique software developers use to interact with databases using their preferred programming language. In my case, it's Python. Instead of having to write redundant code, like the snippet below:

def grab_authors():
    authors = []
    for author in Author.all_authors:
        if author.name not in authors:
            authors.append(author.name)
    return authors
Enter fullscreen mode Exit fullscreen mode

ORM bridges the gap between writing such repetitive code and being able to easily manipulate a database with simple methods. Developers can use these methods to create, read, update, and delete functions effortlessly. Let's break down CRUD, which stands for 'create, read, update, and delete', below:

Create Operation
The create operation simplifies the process of adding new objects or items to the database. With the power of SQL's 'INSERT' commands, ORM makes it easier and more efficient to manage databases. In many ORMs, the objects or items can be saved to the database using the save() method, which consists of the create operation.

Read Operation
The read operation consists of methods like get(), filter(), and all(). These methods make retrieving objects easier. For example, get() allows us to grab a single object based on a criterion; in the case below, it would be the name of the author:

author = Author.get(name="Christopher Paolini")
return author
Enter fullscreen mode Exit fullscreen mode

The filter() method lets us retrieve objects based on certain criteria and can return more than one object. Suppose we have a library of books associated with our authors, and we want to grab all the books that have the ID connected to a specific author:

books_written = Book.filter(author_id=2)
return books_written
Enter fullscreen mode Exit fullscreen mode

This code would return all the books with the author_id of 2, which we will say is Christopher Paolini.

Update Operation
The update operation helps update or modify existing data in the database. It is straightforward and typically involves retrieving the object, changing its attributes, and saving the changes.

Delete Operation
The delete operation is used for removing objects from the database. For example, we can first filter the author we want to delete:

filtered_author = Author.filter(id=1)
Enter fullscreen mode Exit fullscreen mode

Then, we use the delete operation to remove it from our database:

Author.delete(filtered_author)
Enter fullscreen mode Exit fullscreen mode

Conclusion
CRUD operations make managing databases much simpler, and I've gained a deeper appreciation for SQL and its capabilities. SQL simplifies complex data manipulations, making it easier to work with databases. It allows for powerful querying capabilities that streamline the handling of large datasets, which is crucial and useful for making tasks easier in the long run.

If you're working on any project involving data, mastering SQL is a game-changer. I hope someone out there finds this interesting and starts dabbling with SQL themselves.

Top comments (0)