So far we’ve been studying the details of Ruby and Oriented-object programming. All of this with the idea of starting working with Rails. We are one step closer to that. But before we start studying Rails we have to understand a little bit about Databases.
Why are databases so important for us right now?
Is simple. With Rails as a back-end framework, we’ll be dealing with data all the time, and as we saw in the last blog post about the
communication between Clients and Servers, we have to deal with persisted data on the server side.
In that blog post we did an example about launching our own e-commerce. Just think about that example for a while: if you have to store inventories about the t-shirts you’re going to sell, you have to persist that data in some place. The right place to store that data is in a database, and that’s the importance of knowing about it. You have to create, read, update or delete that data
CRUD
CRUD stands for Create, Read, Update and Delete. As you can see these are the main actions we have to do with our database. CRUD is also sometimes used to describe user interface conventions that facilitate viewing, searching, and changing information using computer-based forms and reports.
CRUD is also relevant at the user interface level of most applications. For example, in an ecommerce software, the basic storage unit is an individual product. As a bare minimum, the software must allow the user to:
- - Create, or add new products
- - Read, retrieve, search, or view existing products
- - Update, or edit existing products
- - Delete, deactivate, or remove existing products
Types of databases
There are tons of different kinds of databases. It's actually an entire field of study. Just to mention a few, we have Relational databases, Non-relational databases, key-value databases, graph databases, document databases and so on.
This is a good resource to see more details about each one of them,
In Rails the type of problem we’ll be dealing with are mostly Relational databases. But what is that?
Relational databases
A relational database is a collection of data items with pre-defined relationships between them. These items are organized as a set of tables with columns and rows. Tables are used to hold information about the objects to be represented in the database. Each column in a table holds a certain kind of data and a field stores the actual value of an attribute.
The rows in the table represent a collection of related values of one object or entity. Each row in a table could be marked with a unique identifier called a primary key, and rows among multiple tables can be made related using foreign keys. This data can be accessed in many different ways without reorganizing the database tables themselves. You can find
more info here.
As you can see, here we have a ton of new different terms. What you can imagine here is an spreadsheet:
- Database == Folder containing all the Tables
- Tables == Spreadsheet file
- - Columns == Spreadsheet column
- - Rows == Spreadsheet row
- - Objects == Data inside each row that belongs to a column
- - Primary Key == A spreadsheet column with an unique number as an identifier
- - Foreign key == A spreadsheet column with an identifier that belongs to other tab or file
- - Related values == Different spreadsheet tabs (or files), related between them
Later we’ll be clarifying these concepts and doing exercises. So let’s recap
- - A database is a set of data stored in a machine.
- - A relational database is a type of database that uses a structure that allows us to identify and access data in relation to another dataset within the database.
- - It is organized in tables
Note: So far we’ve been using spreadsheets as an example, because if this is your first approach to database concepts, it is easier to imagine the tables, columns, rows and other concepts right from there. However, databases are usually runned using a database management system.
Database Management Systems (DBMS)
DBMS works a little bit as spreadsheets but with tons of advantages and specializations. DBMS are software systems used to store, retrieve, and run queries on data. A DBMS serves as an interface between an end-user and a database, allowing users to create, read, update, and delete data in the database.
DBMS manages the data, the database engine, and the database schema, allowing for data to be manipulated or extracted by users and other programs. This helps provide data security, data integrity, concurrency, and uniform data administration procedures.
DBMS optimizes the organization of data by following a database schema design technique called normalization, which splits a large table into smaller tables when any of its attributes have redundancy in values. DBMS offer many benefits over traditional file systems, including flexibility and a more complex backup system.
Database management systems can be classified based on a variety of criteria such as the data model, the database distribution, or user numbers. The most widely used types of DBMS software are relational, distributed, hierarchical, object-oriented, and network.
More info here.
Examples of DBMS
There is a wide range of database software solutions, including both enterprise and open source solutions, available for database management. Here are some of the most popular database management systems:
- - PostgreSQL: The open-source DBMS solution gets attention because of the invigorating indexing and configuration options. PostgreSQL is ideal if your daily business activities require you to import or export data. As of now, PostgreSQL supports Python and JSON programming languages. Although it is a relational database solution, users are free to create NoSQL databases. Besides, the open-source community has created a wide array of plug-ins to boost the functionality of the software.
- - MySQL: MySQL is a high-speed data processing and data productivity tool with comprehensive features. The tool is designed to increase the security and scalability of your databases. A reliably cost-effective tool offers technical support and counteracts potential risks. Furthermore, high-volume business sites can deploy business-oriented complex MySQL applications.
- - SQLite: SQLite is designed to cater to small and medium-sized businesses (SMEs). Its light structure and layout design help users store and manage data quite easily. The SQL engine of the tool is highly reliable and self-contained. In fact, the DBMS program is available on several mobile applications.
- - Microsoft SQL Server: Microsoft's SQL Server is one of the most effective DBMS in existence. The free tag of the tool certainly attracts a large user base. Its custom-built graphical integration of best database designs has saved users' valuable time for years. Similarly, the diagrams that you can make with the help of this tool can be easily added to a new or existing project library.
As you can see we have different options, but it is fair to say that we’ll be working mostly with PostgreSQL in Ruby on Rails.
Installing PostgreSQL
Now it’s time to install the DBMS selected in your local machine. In this case PostgreSQL. This installation will depend on the operating system you have. So I encourage you to search on Google for the next keywords:
- Install PostgreSQL in Linux
- Install PostgreSQL in macOS
- Install PostgreSQL in Windows
Of course, you’ll see different tutorials and different ways to install it. For instance, if you have Ubuntu,
this tutorial can help you. There are two ways to install it, any of them works fine:
Same thing happens for other operating systems. For instance, here you can find
how to do it in Windows. As you can see there are different steps, basically following the pop-up window that guides you through the whole installation.
$ sudo su - postgres
postgres@daniel-MS-7A15:~$ psql
psql (13.3 (Ubuntu 13.3-1.pgdg16.04+1), server 9.6.22)
Type "help" for help.
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".
postgres=#
You can see that we connect and access PostgreSQL via the console. When we need to create a new table or new rows everything needs to be done using the console. If you are not familiar with the console, probably you’ll be fighting with it. However my advice is to try to be comfortable with it, because as developers we’ll be using console interfaces a lot! But if you resist to do this, we have a visual option: pgAdmin
pgAdmin
It’s a very popular open source platform fully dedicated to PostgreSQL and has graphical user interface administration tools to manage your relational databases. Some features include a query tool for SQL statements and importing/exporting csv files.
What’s Covered:
- Download pgAdmin & PostgreSQL
- pgAdmin Overview
- Connect an Existing Database(AWS)
If you follow that post, it is easy to do everything from the UI, like creating tables, data, or even to monitor other things like performance and so on.
I think we have enough context now about databases, and next post we’ll be creating out first database and tables.
I hope you learned a lot
Thanks for reading
Daniel Morales