Databases – DebugEverything https://blog.debugeverything.com A new way to learn programming Fri, 06 Oct 2023 01:48:01 +0000 en-US hourly 1 https://wordpress.org/?v=7.1.4 https://blog.debugeverything.com/wp-content/uploads/2021/04/cropped-DE_white-32x32.png Databases – DebugEverything https://blog.debugeverything.com 32 32 What type of data is best stored using mongodb? https://blog.debugeverything.com/what-type-of-data-is-best-stored-using-mongodb/ https://blog.debugeverything.com/what-type-of-data-is-best-stored-using-mongodb/#respond Sun, 02 Apr 2023 11:30:01 +0000 https://blog.debugeverything.com/?p=2570 MongoDB is a popular NoSQL database that has gained a lot of traction over the years due to its high performance, scalability, and flexibility. One of its defining characteristics is its ability to handle unstructured or semi-structured data in a way that makes it easy to store, retrieve and manipulate. But what types of data are best suited for MongoDB? In this article, we’ll explore some examples of the data that is best stored using MongoDB.

  1. Real-time data
    MongoDB is a great choice for storing real-time data such as sensor readings, log files, and social media activity. Real-time data is often unstructured and comes in a variety of formats, making it difficult to store and process using traditional relational databases. With MongoDB, however, you can store this data in a flexible document format that can be easily updated and queried in real-time.
  2. Big data
    MongoDB is also a great choice for storing big data. As the amount of data generated by businesses continues to grow, traditional databases struggle to keep up. MongoDB, on the other hand, was designed to handle large amounts of data, making it a popular choice for big data applications such as analytics, data mining, and machine learning.
  3. Content management systems
    Content management systems (CMS) are another example of data that is well-suited for MongoDB. CMS systems often deal with unstructured data such as articles, images, and videos, making MongoDB’s document-based approach a good fit. MongoDB’s flexible schema design also makes it easy to add new fields or modify existing ones without having to change the entire database schema.
  4. E-commerce data
    E-commerce applications generate a lot of data, from customer orders to product catalogs to shipping information. This data is often unstructured and changes frequently, making MongoDB an ideal choice for storing and managing it. MongoDB’s flexible schema design makes it easy to add or modify fields, while its high-performance querying capabilities allow for fast retrieval of data.
  5. Mobile and web applications
    MongoDB is a popular choice for mobile and web applications that require real-time data synchronization between multiple devices. MongoDB’s flexible schema design and document-based storage make it easy to handle changes in data structures and allow for efficient data synchronization between devices.

In conclusion, MongoDB is a powerful NoSQL database that is well-suited for a wide range of data types, including real-time data, big data, content management systems, e-commerce data, and mobile and web applications. Its document-based approach, flexible schema design, and high-performance querying capabilities make it a popular choice for modern data-driven applications.

]]>
https://blog.debugeverything.com/what-type-of-data-is-best-stored-using-mongodb/feed/ 0
MongoDB Advantages and Disadvantages https://blog.debugeverything.com/mongodb-advantages-and-disadvantages/ https://blog.debugeverything.com/mongodb-advantages-and-disadvantages/#respond Wed, 25 May 2022 23:02:15 +0000 https://blog.debugeverything.com/?p=2424 Before we talk about the advantages and disadvantages of MongoDB, it’s important to know what MongoDB is, and get a basic understanding of its features and operation. If you don’t already know what MongoDB is, you might want to start with this post: MongoDB for Beginners – An Introduction to MongoDB, then come back here.

What’s MongoDb?

Very briefly, MongoDB is an open source database that uses a data storage schema that can be customised (more on this in the post).

It was released in 2007, and today is one of the most popular non-relational databases, it was built with scalability, maximum availability and good performance in mind, and as in 2022 it is currently at version 5.0.

And I will start asking, what is the difference between relational and non-relational database? A NoSQL database stores data differently than relational databases; instead of storing data in row and column tables, all records in MongoDB databases are documents defined in a binary representation of data called BSON. This information is retrieved by applications in JSON format.

NoSQL means ‘Not only SQL’, and there are many types in it, such as columns, documents, charts, key-value pairs, etc.; MongoDB is of the document type as mentioned above.

In relational databases, developers need to translate tables to the object model to make them suitable for use in the application; however with MongoDB, both the stored data and the object model are of the same BSON structure which is easily translated to JSON.

Advantages

Greater flexibility with documents

As mentioned before MongoDB uses documents as a base data store, this allows almost any data structure to be modelled and manipulated easily. MongoDB’s BSON data format, inspired by JSON, allows you to have objects in a collection with different sets of fields, for example if a user is married we should also store the name of the spouse, if the user is single this field simply doesn’t need to exist.

This flexibility is an incredible advantage when dealing with real-world data and changing in business rules or requirements.

Widely supported and native access in code

Most databases force us to use frameworks, wrappers or heavy-duty tools like ORMs (Object-Relational Mappers) to get data in the form of Objects for use in programs

With MongoDB’s decision to store and represent data in a document format means that you can access it from any language, in data structures native to that language (e.g. Dictionaries in Python, Objects in JavaScript, Maps in Java, etc).

However, you will need a driver (A library that creates a connection between your application and the MongoDB database), you can check the list with all official MongoDB drivers on this link.

In this example below, we can see how you could do to connect and save new records to a MongoDB database using PHP.

<?php

$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->demo->students;

$result = $collection->insertOne( [ 'name' => 'Gustavo', 'group' => '9A' ] );
echo "New student ID: '{$resultado->getInsertedId()}'";

$result = $collection->insertOne( [ 'name' => 'Tomas', 'group' => '9C', 'shift' => 'morning' ] );
echo "New student ID: '{$result->getInsertedId()}'";
?>

In the example, I open a connection to the database called demo and look for the students collection, in this collection I insert two new records, and as you notice record 2 has different parameters compared to record 1, which is something totally acceptable with MongoDB.

High performance

Thanks to the document model used in MongoDB, information can be embedded within a single document rather than relying on the JOIN operations of traditional relational databases.

For example, in the traditional database model, we would have a table for our customers, and a separate table with the addresses of those customers.

We can easily reproduce this same model in MongoDB by simply creating two collections, one for clients and one for addresses. And to create the reference between the documents each address would have a parameter that is responsible for making the link, in this case the client_id.

// client document
{
   _id: "72c4hxrt",
   name: "Joe Bookreader"
}

// address documents
{
   client_id: "72c4hxrt", // reference to the customer's document
   street: "123 Rua dos anjos",
   city: "São Paulo",
   state: "SP",
   postal_code: "200128-120"
}

{
   client_id: "72c4hxrt",
   street: "1 Rua Oliveira ",
   city: "Salvador",
   state: "BA",
   postal_code: "291021-122"
}

With MongoDB we can simplify and unite all the information into just one document in a much simpler way using the Embedded Document Pattern, in this case, aggregating the client addresses into the client document makes the most sense.

// client document
{
  _id: "72c4hxrt",
  name: "Joe Bookreader",
  addresses: [
    {
      street: "123 Rua dos anjos",
      city: "São Paulo",
      state: "SP",
      postal_code: "200128-120"
    },
    {
      street: "1 Rua Oliveira ",
      city: "Salvador",
      state: "BA",
      postal_code: "291021-122"
    }
  ]
}

This adds up to much faster queries and returns all the necessary information in a single call to the database, see what the new document would look like aggregating the addresses.

When it comes to write performance, MongoDB offers functionality to insert and update multiple records at once with insertMany and updateMany. These two functions offer a significant performance increase when compared to batch writes to traditional databases.

For our example, where we created two student records in two separate calls of the insertOne method, we could have written using the insertMany method as you can see below.

<?php

$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->demo->students;

$result = $collection->insertMany( 
            [ 'name' => 'Gustavo', 'group' => '9A' ], 
            [ 'name' => 'Tomas', 'group' => '9C', 'shift' => 'morning' ] 
);
?>

Another big advantage of MongoDB, is that it stores most of the data in RAM instead of the hard disk which allows for faster performance when executing queries. For the fastest processing, ensure that your indexes fit entirely in RAM so that the system can avoid reading the index from disk.

MongoDB supports transactions

A transactional database is a database that supports ACID (atomicity, consistency, isolation and durability) transactions. A transaction is a set of database read and write operations where all or none of the operations are successful.

Single document transactions have always been atomic in MongoDB. MongoDB added support for multi-document ACID transactions in version 4.0, and expanded that support to include distributed transactions in version 4.2.

The guarantees provided by MongoDB ensure complete isolation while a document is updated; any error causes the operation to be rolled back returning the document unchanged.

With proper modeling transactions that include multiple records are not always necessary. Data in MongoDB, as we saw earlier, can be related and modeled in a single data structure using a variety of types, including sub-documents and arrays, by doing this users will get the same data integrity guarantees as those provided by relational databases.

Change-friendly structure

If you’re used to having to take down your site or application to change the data structure, you’re in luck: MongoDB is designed for change.

Unlike SQL databases, where you must determine and declare a table’s schema before inserting data, MongoDB collections, by default, do not require your documents to have the same schema.

This means no downtime to change schemas, you can start writing new data with different structures at any time without interrupting your operations.

This flexibility makes it easy to map documents to an entity or object. Each document can be mapped with the fields of an Object, even if the document has substantial variation from other documents in the collection.

Disadvantages

Like any software, MongoDB also has its drawbacks. Most of them are actually limitations that can be fixed or improved in the future, but at the moment (2022), some of the points I’m going to mention below may become a problem for those who are thinking about using MongoDB.

Size of BSON document

The maximum size of the BSON document is 16 megabytes.

The maximum document size helps ensure that a single document cannot use an excessive amount of RAM or, during transmission, an excessive amount of bandwidth. To store documents larger than the maximum size, MongoDB provides the GridFS API.

Nesting limit in BSON documents

MongoDB supports no more than 100 levels of nesting for BSON documents. Each object or array adds one level.

To exemplify this limitation:

{
  "_id": 1,
  "Universe": {
    "Virgo Cluster": [
      {
        "Virgo Supercluster": {
          "Local Group": {
            "Milky Way": {
              "Solar System": [
                {
                  "Earth": [
                    { "Asia": ["Countries", "..."], "Europe": ["Countries", ".."], "America": ["Countries", "..."] }
                  ]
                }
              ]
            }
          }
        }
      },
      { "Laniakea Supercluster": "..." }
    ]
  }
}

Limited sort operations

If MongoDB cannot use an index or indexes to sort the fields in a document, MongoDB initiates a blocking data sort operation.

The name refers to a SORT operation where all documents in a collection are read to return an output document, in which case the data flow for that particular query must be blocked.

If MongoDB requires the use of more than 100 megabytes of system memory for the sort operation, MongoDB returns an error unless the query specifies cursor.allowDiskUse() (New in MongoDB 4.4).

allowDiskUse() allows MongoDB to use temporary disk files to store data that exceeds the 100 megabyte memory limit while processing a sort operation.

db.COLLECTION_NAME.find().sort({KEY:1})

MongoDB Key Terms/Concepts

SQL Terms/ConceptsMongoDB Terms/Concepts
databasedatabase
tablecollection
rowdocument or BSON document
columnfield
indexindex
primary_key
Specifies a single column or combination of columns as the primary key.
primary_key
In MongoDB, the primary key is automatically set to the _id field.
]]>
https://blog.debugeverything.com/mongodb-advantages-and-disadvantages/feed/ 0
Understanding MySQL: DBMS, SQL and MySQL Workbench https://blog.debugeverything.com/mysql-is-not-a-database/ https://blog.debugeverything.com/mysql-is-not-a-database/#comments Tue, 21 Sep 2021 14:20:24 +0000 https://blog.debugeverything.com/?p=2321 What is MySQL? And what is a database? 🤔

We will answer both answers in this post.

What is MySQL?

Ok we already know what MySQL is not, it is not a database.

MySQL is a relational database management system. 🤯

Okay, now we need to define what a database management system is.

What is a Database Management System (DBMS)?

A Database Management System (DBMS) is software designed to store, retrieve, define, and manage data or data structure in a database.

Data in DBMS is stored in database objects called tables. A table is a collection of related data entries and consists of columns and rows.

Users of this system are given tools to perform various types of operations on the table structures or on the data.

A DBMS acts mainly as an interface between the end user and the database, managing both the data and the database structure in order to facilitate data organisation and manipulation.

Some examples of DBMS include MySQL, PostgreSQL, Microsoft Access, SQL Server and Oracle.

What is a database?

A database is an organised collection of data, typically stored electronically in a digital system in table forms.

In this post we are considering only relational databases, but there is another category of database, called non-relational databases, like MongoDB, you can better understand the difference between relational and non-relational databases here.

A database is usually controlled by a database management system (DBMS). Together, the data the DBMS and associated applications are referred to as a database system, often shortened to just database.

Data, within the most common types of databases currently in operation, is typically modeled in rows and columns in a series of tables to make data processing and querying efficient.

The data can then be easily retrieved, managed, modified, updated, controlled, and organised. Most databases use SQL to write and query data.

What is the difference between a database and an Excel spreadsheet?

Databases and Excel spreadsheets are both convenient ways to store information. The main differences between the two are:

  • How data is stored and manipulated
  • Who can access the data
  • How much data can be stored

The spreadsheets were originally created for one user, or for a small number of users who do not need to do a lot of manipulation on the data.

Databases, on the other hand, are designed to hold much larger collections of information. Databases allow multiple users at the same time to manage and query the data quickly and securely, using logic and a more complex language.

What is SQL?

SQL (Structured Query Language) is the standard language for querying and manipulating relational databases.

Yes, SQL is a programming language. It offers loops, logic directives, variables, and so on.

But rather than being a programming language for building websites, applications, or software, SQL is a language focused on storing, retrieving, and manipulating data in databases.

Here are some examples of SQL commands:

SELECT * FROM table_name WHERE column_name='...';
CREATE TABLE table_name (COLUMN_NAME DATATYPES[,....]);  
ALTER TABLE table_name ADD column_name COLUMN-definition; 
DROP TABLE table_name;  
TRUNCATE TABLE table_name;  

What can you do with SQL?

  • SQL can execute queries on a database
  • SQL can retrieve data from a database
  • SQL can insert records into a database
  • SQL can update records in a database
  • SQL can delete records from a database
  • SQL can create new databases
  • SQL can create new tables in a database
  • SQL can set permissions on tables, procedures, and views

How do SQL and DBMS relate to each other?

DBMS is a database management system. And SQL is the language used to communicate with the data in a DBMS.

Or, in simple terms:

DBMS is a book and SQL is the language being used in the book. Want to read or write in the book? Use SQL.

MySQL Workbench

MySQL Workbench is a visual database design tool that integrates the development, administration, design, creation and maintenance of SQL databases into a single integrated development environment (IDE) for the MySQL database system.

MySQL Workbench provides a set of tools to improve the performance of MySQL applications, provides a visual platform to manage MySQL environments and gain better visibility into databases, also provides visual tools to create, execute, and optimize SQL queries.

There many other tools like that, at some level, it turns out to be your preference to choose one to go with. Some good options are TablePlus and Sequel Pro if you use Mac, otherwise MySql workbench will do the job.

Connecting the parts

To understand how all these pieces are connected, take a look at the image below.

]]>
https://blog.debugeverything.com/mysql-is-not-a-database/feed/ 1
SQL: Essentials https://blog.debugeverything.com/sql-essentials/ https://blog.debugeverything.com/sql-essentials/#comments Wed, 08 Sep 2021 02:03:21 +0000 https://blog.debugeverything.com/?p=2268 Okay, in this article we will look at the basic concepts needed to write your first SQL queries.

If you are still in doubt about what SQL is I recommend you read this article first.

SQL SELECT

Let’s start with the most basic and simple, SQL SELECT.

This command is used to select data from a database. Take a look at the example below.

SELECT column1, column2, ...
FROM table_name;

Might not be necessary, but we can ‘translate’ this as: “Select the columns ‘column1’ and ‘column2’ from the table ‘table_name’.

Here, column1, column2, must be the field names of the table from which you want to select data. If you want to select all the available fields in the table, use the following syntax:

SELECT * FROM table_name;

As soon as you run this command, the data returned is stored in a result table, called the result set.

I think I can show this and the next examples better using table. So let’s create one for practice.

The table we will use is as follows. Table “clients”.

idnameaddresscitycountry
1
Rodrigo NascimentoBerguvsvägen 8BerlinGermany
2Marcio MorenoAv. Brasil 222Rio de JaneiroBraszil
3Antonio PereiraAv. Paulista 321São PauloBrazil
4
Clovis OliveiraP. Sherman 42, Wallaby WaySydneyAustralia
5Helder Santos120 Hanover Sq.BrisbaneAustralia
Table: clients

The following SQL statement selects the “name” and “city” columns from the “clients” table:

SELECT name, city FROM clients;

And your set of results should look like this:

idnamecity
1Rodrigo NascimentoBerlin
2Marcio MorenoRio de Janeiro
3Antonio PereiraSão Paulo
4Clovis OliveiraSydney
5Helder SantosBrisbane
Result set

SQL WHERE

As we have just seen, with SELECT you can filter the columns, right? Now let’s say you want to filter some of the elements in each row.

For example, you want to know which customers live in Brazil or Australia. Using just SELECT this is not possible.

For that we need a WHERE.

The SQL WHERE clause is used to specify a condition when retrieving data from a single table or joining multiple tables.

If the given condition is satisfied, it returns all records that match the condition.

The goal is to use the WHERE clause to filter the records and fetch only the records you need.

For the example above, we could write a query like this:

SELECT name, city FROM clients WHERE country = 'Brazil';
idnamecity
2Marcio MorenoRio de Janeiro
3Antonio PereiraSão Paulo
Results set

Ok, one thing you need to keep in mind, SQL requires single quotes around text values like in ‘Brazil’ up there (most DBMS will also accept double quotes as well).

However, for numeric fields you should not put quotes around them:

// Wrong
SELECT name, city FROM clients WHERE id = '1';

//Correct
SELECT name, city FROM clients WHERE id = 1;

SQL LIKE

Nothing more convenient than to continue this list with the LIKE operator, as it is used in a WHERE clause to look for a specified pattern in a column.

I know, better with an example, right?

Let’s say we need to update the customer’s address in our database.

Let’s say that we need change every Brazilian address that has the abbreviation “Av” to “Avenida”(Avenue), to help with delivery logistics.

But before this update the manager needs to see how many customers will be affected by this change.

Since “Avenue” or “Av” are not columns in our table, we can’t use just the WHERE.

For this we need to use LIKE, the query looks like this:

SELECT name, address FROM clients
WHERE address LIKE 'Av%';
idnameaddress
2Marcio MorenoAv. Brasil 222
3Antonio PereiraAv. Paulista 321
Result set

You might be wondering what the hell this “%” is doing there, or that I made a typo, but no, it’s all right.

There are two wildcards often used in conjunction with the LIKE operator:

  • The percent sign (%), which can represent zero, one, or multiple characters.
  • The underscore sign (_), which represents a single character.

There are many possible combinations, but I will list those that I consider to be the most important.

Operador LIKEDescrição
WHERE column LIKE ‘a%’Find any values that start with “a”.
WHERE column LIKE ‘%a’Find any values ending with “a”.
WHERE column LIKE ‘%or%’Find any values that have “or” in any position
WHERE column LIKE ‘_r%’Find any values that have “r” in the second position
WHERE column LIKE ‘a_%’Find any values that start with “a” and are at least 2 characters long
WHERE column LIKE ‘a__%’Find any values that start with “a” and are at least 3 characters long
WHERE column LIKE ‘a%o’It finds any values starting with “a” and ending with “o”.
Operator LIKE

So in our example, I’m using the first command in this table. And it should return me customers with id = 2 and id = 3.

But if you want, you can be very specific, and use LIKE without any wildcard. Like this:

SELECT * FROM clients
WHERE address LIKE 'Av. Brazil 222'; 

SELECT * FROM clients
WHERE address LIKE 'Av. Paulista 321';

We can still upgrade this query, instead of running two queries, we can run only one query that will solve our problems. Our next operators are here for that.

Operators AND, OR e NOT

Now we can combine the WHERE clause with the AND, OR, and NOT operators, and simplify the query.

And to be clear, AND and OR are used to filter records based on more than one condition, just like our example above.

And NOT to negate a condition, that is, anything that is NOT equal to the condition.

Using these operators, you will write your queries using this syntax:

//AND
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

//OR
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

//NOT
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

In the table below is a brief description of what each operator does.

OperatorDescription
ANDDisplays a record if all conditions separated by AND are TRUE.
ORDisplays a record if any of the conditions separated by OR are TRUE.
NOTDisplays a record if the condition(s) is NOT TRUE.
Operators AND, OR e NOT

So, as I said before, we can simplify the query in our last example, instead of using two separate queries, we can write just one, using one of the operators above, in this case, we will use OR.

After simplifying our query the result is this:

SELECT name, address FROM clients
WHERE (address LIKE 'Av. Brazil 222' or address LIKE 'Av. Paulista 321');
idnameaddress
2Marcio MorenoAv. Brasil 222
3Antonio PereiraAv. Paulista 321
Result set

SQL UPDATE

Now we have two methods for selecting and viewing data. But usually we want to do something with this data, right?

For example, let’s say one of our customers has moved city or maybe even country, so the fields “address”, “city” and “country” will change, and we need to update this customer’s record. How to do it?

Well, the first step is to select the Customer that is moving, right? And we already know how to do that, using SELECT and WHERE, but this only doesn’t help, we need an extra thing.

Let’s use the UPDATE method, that does exactly the same thing, with an extra feature, we will update the values at the same time. Like this:

UPDATE clients
SET city = 'Paris', country = 'Franca', address = '60 rue La Boétie'
WHERE id = 1;
idnameaddresscitycountry
1
Rodrigo Nascimento60 rue La BoétieParisFranca
Table: clients

Here we have the UPDATE command instead of SELECT. And right after we have the line with the SET command (or keyword), it is used with the UPDATE command to specify which columns and values should be updated in a table.

So, what we are doing here is simple, let’s translate this command.
“Update within the ‘customers’ table the ‘city’, ‘country’ and ‘address’ of the customer whose id equals 1”.

Simple, isn’t it? We just sent Rodrigo to France.

Now, attention!

Be careful when updating records in a table! Note the WHERE statement in the UPDATE statement. The WHERE clause specifies which record(s) should be updated. If you omit the WHERE clause, all records in the table will be updated!

SQL DELETE

Everyone, at one time or another in life does a little 💩 right?

In the case of the database, if you have saved a record that you shouldn’t, and you need to remove it, this is the method you will use.

Let’s imagine this scenario, that the customer Clovis no longer wants to be part of our customer club. By company policy, all of that customer’s data needs to be deleted.

This is the cue to use DELETE.

In the example, it would look like this:

DELETE FROM clients WHERE name = 'Clovis Oliveira';
idnameaddresscitycountry
1
Rodrigo NascimentoBerguvsvägen 8BerlinAlemanha
2Marcio MorenoAv. Brasil 222Rio de JaneiroBrasil
3Antonio PereiraAv. Paulista 321São PauloBrasil
5Helder Santos120 Hanover Sq.BrisbaneAustralia
Table: clients

And that’s it, that line will be completely deleted from the database.

But beware!

If you don’t provide a WHERE in the clause with DELETE, the entire table data will be deleted!

!!! THIS WILL ERASE ALL THE DATA FROM THIS TABLE!!!
DELETE from clients;

Only run this command if you are sure of what you are doing. Preferably only run it on a test database.

SQL Joins

Structured Query Language (SQL) is a programming language that is used in relational databases. And by “relational” I mean that data between tables has some kind of relationship with each other. And to handle many of these cases, the commands we have seen so far are insufficient. Let’s expand our SQL vocabulary with JOINS.

Undoubtedly one of the most important SQL clauses, let’s understand what it does and how it works.

The JOIN clause is used to combine rows from two or more tables, based on a related column between them.

Ok, to better explain what this command does we will need some examples.

For these examples we will need a second table, which we will call “orders”.

IDclient_idtotaldue_date
12$ 34212/08/2022
24$ 62904/11/2022
Table: orders

Note that the “customer_id” column in the “orders” table refers to the “id” in the “customers” table.

From this we can extract our relationship, because when we talk about a customer we want to know if he has any orders, and when we look at the list of orders, we need to know who that order belongs to.

JOIN solves this problem for us, and the result is shown in the table below.

SELECT orders.total, orders.due_date, clients.name
FROM orders
INNER JOIN clients ON orders.client_id=clients.id;
totaldue_datename
$ 34212/08/2022Marcio Moreno
$ 62904/11/2022Clovis Oliveira
SQL JOIN result

Ok, it is not the simplest of queries, but it is easy to understand. Let’s translate.

“Select the “total” and “due_date” columns in the “orders” table and the “name” column in the “clients” table from the “orders” table by merging with the “clients” table where the “client_id” column in the “orders” table is equal to the “id” in the “client” table.”

Still confusing? let’s break it down into parts, to see if it helps.

The first part we have already seen in the SELECT example. Very similar

Select the “total” and “due_date” columns in the “orders” table and the “name” column in the “clients” table from the “orders” table.

SELECT orders.total, orders.due_date, clients.name
FROM orders

Okay, we have two things to point out here, one is that we are using “orders.total” instead of just “total”. The other is that we have “clients.name” which has nothing to do with the orders table.

That’s the magic of the JOIN command, it helps you join columns from several tables into a single result.

We have the columns “due_date” and “total” coming from the order table and “name” coming from the “customers” table, so we need to specify where each piece of data is coming from.

We do this using the notation: table.column

Now the novelty is the second part:

INNER JOIN clients ON orders.client_id=clients.id;

Which translates as something like this:

“Make union with table “clients” where column “client_id” in table “orders” is equal to “id” in table “clients”.”

The summary then, when “client.id” and “order.client_id “match, show me those three columns order.due_date, order.total, client.name.

An INNER JOIN is nothing more than a union, you probably saw this in school, if you didn’t skip math class. It was that class where they talked about Venn Diagrams. Here is a picture to help you out.

SQL JOINS
Different types of SQL JOINs
TypeDescription
(INNER) JOINReturns records that have matching values in both tables
LEFT (OUTER) JOINReturns all records from the left table, and the combined records from the right table
RIGHT (OUTER) JOINReturns all records from the right table, and the combined records from the left table
FULL (OUTER) JOINReturns all records when there is a match in the left or right table
Different types of SQL JOINs

From the table above you see that we have several types of JOINS, the one we are using in this example is INNER JOIN, but we could have used any of the others if they solved our problem.

Read the description of each method and try to understand what each one does.

Bottom line

That’s it. I believe that if you want to get started with SQL, these methods and clauses are a good start, no doubt about it. Leave a comment if you missed something, I’ll read them all, I swear.

If you want to practice a little of what you learned, take a look at this link, you can test the queries with a real database, it is really cool.

See you next time!

]]>
https://blog.debugeverything.com/sql-essentials/feed/ 2
Database Demystified: Relational vs. Non-relational Showdown https://blog.debugeverything.com/database-demystified-relational-vs-non-relational-showdown/ https://blog.debugeverything.com/database-demystified-relational-vs-non-relational-showdown/#comments Thu, 23 Jul 2020 17:12:05 +0000 https://debugeverything.com/?p=1600

By the end of this decade, our digital data universe will grow from 4.4 zettabytes today to about 44 zettabytes, or 44 trillion gigabytes.

OK, so we know you’re here to weigh the technical differences between the relational and non-relational databases. But before we dive in, we need to define some important details. You need to determine what priority scalability represents for your particular model.

To make your data model as accurate as possible, you need to ask the right questions. Otherwise, your solution may not be the most appropriate for your problem. If you already have this well defined, go ahead.

What is a relational database?

A relational database is a digital database based on the relational data model as proposed by E. F. Codd in 1970, an intuitive and direct way to represent data in tables. Relational databases store and provide access to data points that are related to each other.

In a relational database, each table row is a record with a unique identification called a primary key. The table columns contain the data attributes, and each record usually has a value for each attribute, making it easier to establish the relationships between the data. In this example below the Primary Key is the id.

There are several systems you can use to manage relational databases, they are known as Relational Database Management Systems (DBMS). The most popular among them is MySQL, but we also have other options like Oracle Database, Microsoft SQL Server, and IBM DB2.

The vast majority of RDBMS offer the option to use SQL (Structured Query Language) for database query and maintenance.

Types of relationship

One of the advantages of a relational database is that once you have your data kept in clearly defined tables you can connect or relate the data between different tables. In order to identify these relationships, you need to examine the data and have an understanding of the business rules that apply to the data and tables.

When creating relationships, you always work with two tables at a time. One table is called the main table or parent table and the other is the related table or child table.

The three types of relationships you will find in the relational databases are, one-to-one, one-to-many, and many-to-many. The relationships are described below.

  • One-to-one (1…1): Both tables can have only one record on each side of the relationship. Each primary table value refers to only one (or no) record in the related table.
  • One-to-many (1…n): The primary table contains only one record that can relate to none, one, or many records in the related table.
  • Many-to-many (n…n): Each record in both tables can relate to any number of records (or no records) in the other table. Many relationships of type n…n require a third table, known as a link table or associated table, because relational systems cannot directly accommodate the relationship.

What is a non-relational database?

A non-relational database is any database that does not follow the relational model provided by traditional relational database management systems (RDBMS). This category of databases is also known as NoSQL databases.

The most popular being MongoDB, DocumentDB, Cassandra, Couchbase, HBase, Redis, and Neo4j. These databases are generally grouped into four categories: Key-value stores, Graph stores, Column stores, and Document stores.

Types of non-relational database

Key-value stores

As the name suggests, in a key-value database, the data is represented as a collection of pairs of key values. They are also known as associative arrays, organized in rows.

These databases store the data as a hash table with a unique key and pointer to a particular data item. Similar to traditional hash tables, this type of database allows the storage and retrieval of data using keys.

Key-value databases are used whenever data is queried by precise parameters and need to be recovered quickly.

Graph stores

Although key-value databases can handle a large amount of data, they are designed for a high level (low depth) view of the data. Graph databases can maintain a minimum storage size, even with a greater depth of data than other types of databases.

The graph-type NoSQL database was specially developed to handle very large sets of structured, semi-structured, or unstructured data. It helps organizations access, integrate, and analyze data from various sources.

Widely used when it is necessary to analyze data from social networks, for example.

Column stores

A column data storage, also known as DBMS in column, uses columns to store data instead of rows as in the relational model. Both row-based and column-based DBMS use SQL as the query language, but column-based DBMS can offer better performance.

Column store databases use a concept called keyspace. A keyspace is like a schema in the relational model. The keyspace contains all column families (such as tables in the relational model), which contain rows, which contain columns.

Document stores

The document store type database, also called document oriented DBMS, is characterized by its data organization without schemas.

This means that the records do not need to have a uniform structure, that is, different records can have different columns. The value types can be different for each record, the columns can have more than one value (arrays), and the records can have a nested structure.

Documents are usually stored in a structure similar to JSON, which makes life easier for the programmer, and JSON is one of the most common data structures, especially working with Javascript.

What is the difference between relational and non-relational databases?

Relational databases like MySQL, PostgreSQL and SQLite3 represent and store data in tables and queues. Non-relational databases like MongoDB represent data in JSON document collections.

Relational databases use Structured Query Language (SQL), making them a good choice for applications that involve the management of various transactions. The structure of a relational database allows linking information from different tables through the use of foreign keys (or indexes).

Se você estiver lidando com uma quantidade fenomenal de dados, a complexidade do banco de dados relacional e das queries necessárias também vai crescer na mesma proporção. Nessa situação, talvez você precise considerar a possibilidade de utilizar um base de dados não relacional. Uma base de dados não relacional apenas armazena dados sem uma mecânica explícita e estruturada para vincular dados de diferentes tabelas uns aos outros.

If you are dealing with a phenomenal amount of data, the complexity of the relational database and the required queries will also grow in the same proportion. In this situation, you may need to consider using a non-relational database. A non-relational database can store data without an explicit and structured mechanics to link data from different tables to each other.

Disadvantages of relational databases

Although relational databases are great, they come with trade-offs. One of them is ORM Impedance Mismatching, because relational databases were not initially created with OOP languages in mind. The best way to avoid this issue is to create your database schema with referenced integrity. Therefore, when using a relational database with OOP (such as Ruby or Java), you have to think about how to configure your primary and foreign keys, the use of constraints (including deletion and cascading), and how to write your migrations.

Disadvantages of non-relational databases

In non-relational databases like MongoDB, there are no joins like in relational databases. This means you need to perform multiple queries and join the data manually within your code.

Since Mongo does not automatically treat the operations as transactions in the same way as a relational database, you must choose to create a transaction and then check it, commit, or rollback manually.

My suggestion is, analyze the data you will store. Ask the right questions to find out what data model you’ll need. Choosing the programming language to write your application and using a specific database will get your application on the right track.

If you like the article, be sure to comment and share. And so you don’t miss any news here, subscribe to the newsletter and receive exclusive content every week! See you next time!

]]>
https://blog.debugeverything.com/database-demystified-relational-vs-non-relational-showdown/feed/ 4
MongoDB for Beginners – Comprehensive Introduction to MongoDB https://blog.debugeverything.com/introduction-to-mongodb/ https://blog.debugeverything.com/introduction-to-mongodb/#comments Thu, 21 May 2020 09:17:49 +0000 https://debugeverything.com/?p=1269

MongoDB is a general-purpose, distributed, document-based NoSQL database.

SQL databases are still the vast majority. But with the exponential increase in consumption and data creation we need to prepare for what is to come. And NoSQL databases are proving to be an excellent alternative for that.

In this post we will understand better what are NoSQL databases and what are their advantages compared to traditional relational databases.

But before we go ahead and talk about MongoDB, let’s give an introduction about NoSQL databases.

A comparison between relational and non-relational databases, you can check in this other post.

What is a NoSQL database?

NoSQL databases (also known as “not only SQL”) are not tabular and store data differently from relational tables.

In relational databases you need to create tables, define schemas, define the types of data fields, define the relationships between the tables, etc, before you can actually insert the data.

In NoSQL databases you don’t have to worry about that. The process of creating the documents, in the case of MongoDB is much simpler and more dynamic.

NoSQL databases come in a variety of types based on the data models. The main types are documents, key-value, wide-column, and graph. They provide flexible and scalable schemas and support high user demands.

One of the advantages of the NoSQL database is that they are really easy to scale and are much faster in most types of operations we perform on the database.

There are certain situations where using the relational database may be a better choice than NoSQL, but when you are dealing with a large amount of data, the NoSQL database should be the best choice.

NoSQL Advantages

Today, the advantages of NoSQL databases are no secret, especially when cloud computing has gained widespread adoption.

NoSQL databases were created in response to the limitations of traditional relational database technology. When compared to relational databases, NoSQL databases are more scalable and offer superior performance, and their data model solves several deficiencies of the relational model.

NoSQL’s advantages include, but are not limited to, the:

  • Support for large volumes of structured, semi-structured, and unstructured data.
  • Ease of dealing with changes over time.
  • Ability to scale horizontally on ‘common’ hardware.
  • Efficient and scalable architecture instead of expensive and monolithic architecture.
  • Support for multiple data structures.

Introduction to MongoDB

MongoDB is a document-oriented NoSQL database used for storing large volumes of data. Instead of using tables and rows as in traditional relational databases, MongoDB makes use of collections and documents.

What is document-based storage?

Documents consist of pairs of key-values that are the basic unit of data in MongoDB. Collections contain sets of documents and functions that are almost equivalent to relational database tables.

If you come from a relational database, you can think of documents as relational database rows and collections as tables.

{
    "_id": "5",
    "isActive": false,
    "age": 24,
    "eyeColor": "brown",
    "name": "Ada Compton"
}

This is a similar structure to JSON. Where data is stored in the form of pairs of keys and values.

And a collection, is nothing more than a set of documents. that do not necessarily have to have the same fields, as in the example of the collection below:

{
    "_id": "5",
    "isActive": false,
    "age": 24,
    "eyeColor": "brown",
    "name": "Ada Compton"
}
{
    "_id": "61",
    "username": "debug",
    "password": "4#$%ˆ&1233",
    "admin": true,
}

What is the difference between MongoDB and an RDBMS?

The biggest difference between MongoDB and SQL databases is the way they handle data. In SQL databases, data is stored in the form of a traditional two-dimensional row-column structure, while MongoDB uses documents to store information, which allows the storage of any type of data.

Let’s put face to face some of the main differences between MongoDB and other SQL databases:

The database uses Tables with rows and columns.BaseDatabase uses Documents that consist of key-value pairs.
RDBMS is a relational database management system and works with relational database.ConceptMongoDB is a document-oriented, non-relational database management system that works with document-based databases.
RDBMS is vertically scalable. Performance increases with increasing RAM.ScalabilityMongoDB supports horizontal scaling through Sharding , distributing data across multiple machines.
Difficulty to store hierarchical data.HierarchicalIt has built-in support to store hierarchical data.
RDBMS supports complex joins.JoinsDatabases inSQL like MongoDB use denormalized data, so ‘relational’ joins are impossible.
RDBMS is slower in processing large hierarchical data.PerformanceMongoDB is extremely fast in processing large hierarchical data.
It follows the principles ACID, Atomicity, Consistency, Insulation and Durability.PrincipleIt follows the CAP principle, Consistency, Availability and Partition Tolerance.
RDBMS uses SQL to query database.Query LanguageMongoDB uses BSON to query database.
Vulnerable to SQL Injection attacks.SQL InjectionSQL injection is not possible.
The schema must be defined in RDBMS before you can use a database.SchemaThe schema can be created and accessed dynamically on MongoDB.

MongoDB Advantages

MongoDB has no defined schemas, i.e. each document can have its own set of unique fields within a collection. In addition, it is distributed and easily scalable geographically/horizontally for better performance.

When considering moving to a document-oriented database, you may consider the following points:

Access to native data

Most databases force you to use heavy mappings, such as ORMs (Object Relational Mappers), to get the object shaped data to use in the programs. Like Java Hibernate and C# Entity Framework.

MongoDB’s decision to store and represent data in a document format using JSON means that you can access it from any language, in data structures native to that language (e.g. dictionaries in Python, associative arrays in JavaScript, Maps in Java, etc.).

Change-friendly structure

If you’re used to having to take down your site or application to change the structure of your data, you’re in luck: MongoDB is designed to accept changes much easier.

No downtime is required to change schemes, and you can start writing new data to MongoDB at any time without interrupting your operations.

Powerful queries and analysis

What good is a database if you can’t find things in it? MongoDB is designed to facilitate access to data.

MongoDB Query Language (MQL) is a complete and powerful language that allows you to do deep document queries and even run complex analytical pipelines with just a few lines of JSON-like MQL.

Easy horizontal scalability

MongoDB is designed from the ground up to be a distributed database. Create replication clusters in real-time, and make large or high-performance collections across multiple clusters to sustain performance and scale-up horizontally.

Bottom Line

Document oriented database has certain advantages: flexibility (lack of rigid structure), good adaptation to modern JavaScript frameworks (direct use of JSON), large data processing, and real-time statistics/data analysis.

MongoDB is one of the most widely used NoSQL databases. It is easy to understand. The document query language provides many options and is as powerful as SQL. Unlike relational databases, MongoDB is easy to scale. MongoDB is widely used along with NodeJS and AngularJS and ReactJS frameworks. It is a central part of the MEAN and MERN stack.

I hope you enjoyed the content. Leave a comment down below!

We’ll see you next time.

]]>
https://blog.debugeverything.com/introduction-to-mongodb/feed/ 3
What you should know about relational and non-relational databases https://blog.debugeverything.com/must-know-about-relational-and-non-relational-databases/ https://blog.debugeverything.com/must-know-about-relational-and-non-relational-databases/#comments Tue, 05 May 2020 23:57:57 +0000 https://debugeverything.com/?p=1139 More than 2.5 quintillion bytes of data are created every day, and it is only predicted to grow from there according to the last DOMO report. By the end of 2020, it is estimated that 1.7MB of data will be created every second for every person on Earth. Can you see the importance of the database?

Companies have invested literally billions of dollars in infrastructure to not only store but also search, update and delete this simply absurd amount of data.

So let’s understand what they are, what they’re for, and what the main types of databases we have at our disposal today, and find out what the ideal database is for your next project.

What is a database?

In the simplest of definitions, a database is just one way to store and organize information. Ideally, this information is stored in an orderly manner to assist in searches.

You can imagine a database as if it were a library of books, each book is organized according to some kind of category, either by genre, author, language in which it was written, etc.

And each library has a system of organization that depends on factors such as the size of the library, what kind of books it stores, how many visitors it receives, and so on.

Databases are very similar structures. Choosing the best type for your project and organizing your data in the best possible way will make all the difference.

Today we have two main types of databases:

  • relational database.
  • non-relational database(NoSQL).

To talk about these two, I need to introduce what SQL is.

What is SQL?

SQL stands for Structured Query Language

It is a programming language used to create a connection between data stored in a Relational Database Management System (RDBMS).

Yes, SQL is a programming language, but not for general purposes like Java, PHP, Python, and others. SQL is a specific language for database communication.

An example of code in SQL would be like this, this code retrieves all names and emails from the Users table, for example.

SELECT username, email FROM Users;

Many RDBMSs use SQL (and variations of SQL) to access data in tables.

An RDBMS is a program that allows you to create, update, and manage relational databases.

MySQL, for example, is RDBMS.

Relational databases

A relational database is a type of database. It stores and provides access to related data. Hence the name ‘relational’. These relationships are built using tables, similar to this one:

Idusernameemailpassword
1user1user1@example.com!*&JGs1ˆ&
2user2user2@example.com($%ˆgˆ8r*7
3user3user3@example.com$%rtg&nd
  • Each column represents the information referring to specific data, for example in the column email you will have all the emails referring to all users.
  • In each line you have the complete information of each user, for example in the second line we have all the user2 data (username, email, and password).

In the relational model, each row in the table is a record with a unique ID called the primary key.

It is from this primary key that we can create the relationships between the tables’ data.

For example, we could have a second table with addresses. And we can generate a relationship between them through the user ID.

IdaddresscountryuserID
1Main Av, 500Brazil1
2Second Av, 300Portugal2
3Third Av, 100Mozambique3

With this second table it is simple to recognize that the address with Id 1 belongs to the user with Id 1, address with Id 2 belongs to the user with Id 2, and address with Id 3 belongs to the user with Id 3.

If we wanted to retrieve user1’s address through an SQL Querry, we could do so:

SELECT * FROM Address WHERE userId = 1;

If you enjoyed this structure and method of organizing the data, I have some great news for you, there are several free options you can choose to work with. I’ll mention the main ones down here.

The main Relational Database Management Systems

MySQL

According to themselves “MySQL is the most popular Open Source SQL database management system”. MySQL is supported by Oracle.

As key advantages of MySQL I can mention:

  • User friendly.
  • Cheap, and offers community Free version.
  • Ensures data security with exceptional data protection features.
  • Large developer community.

In this project I showed how to connect Python to a MySQL database.

Difficulties in scalability and poor performance involving tables with a lot of data can be mentioned with some of the weaknesses of this system.

PostgreSQL

PostgreSQL is also an open-source RDBMS, but it is not controlled by any corporation.

PostgreSQL provides many of the same benefits as MySQL. It is easy to use, inexpensive, reliable and has a broad community of developers.

PostgreSQL has a typical lower performance than other RDBMSs, such as MySQL, and although it has been used more in recent years, PostgreSQL lags behind in terms of popularity.

OracleDB

Many of the world’s leading banks operate with Oracle’s systems, as Oracle offers a strong mix of pre-integrated, robust business software and technologies, including features designed specifically for banks.

Oracle offers a free version called OracleDB Express Edition (XE).

The disadvantage may be the high cost. But you are paying for high performance, excellent customer service, and several other benefits, the high cost may be justified.

SQL Server

Microsoft also offers an RDBMS. With a free entry version, which by the way has the same name as the competitor, Express.

It also focuses on large business applications with large infrastructure.

Just like Oracle’s system, it’s not open source either.

The downside is also the high cost and the fact that it is necessary to invest in Good hardware to run the latest versions.

SQLite

SQLite is a popular open-source SQL database. It can store an entire collection in a single file.

One of the most significant advantages of this is that all data can be stored locally without the need to connect your database to a server. Widely used in mobile applications (Android and iOS).

Non-relational databases (NoSQL)

NoSQL databases (also known as “not only SQL”) are not made up of tables, they store data differently from relational tables.

They appeared in the late 2000s when the cost of storage dropped dramatically.

NoSQL databases come in a variety of types based on your data model. The main types are document, key-value, wide-column, and graph. They provide flexible schemas and scale easily with large amounts of data and high user loads.

A common misunderstanding is to think that NoSQL databases do not store relational data well. NoSQL can store relational data – they only store it differently than the relational model does.

When compared to an SQL database, many find that modeling relationship data in NoSQL is easier than in SQL, because the related data does not need to be split between tables.

NoSQL data models allow related data to be nested within a single data structure.

What are the Types of Non-Relational Databases?

So far, four main types of NoSQL databases have emerged:

  • Document Oriented Store
  • Key-value Store
  • Wide-column Store
  • Graph Store

Let’s get a look at each fellow.

Document Oriented Database

Document type NoSQL stores data in structures similar to JSON objects (JavaScript Object Notation). Something like this:

{
    "_id" : ObjectId("5ea3f99880aeb46f07d550fe"),
    "id" : 1,
    "password" : "pbkdf2_qg5uny4$9mVb1OqdkvvRubzHay0I3EzODn7i37icn6GQVHLqtiY=",
   "is_superuser" : true,
    "username" : "debug",
    "first_name" : "Debug",
    "last_name" : "Everything",
    "email" : 'admin@debugeverything.com',
    "date_joined" : ISODate("2020-04-25T08:49:28.513Z")
}

Each document contains pairs of fields and values. The values can typically be a variety of types, including things like strings, numbers, booleans, arrays, or objects.

Due to its variety of field value types and powerful query languages, document databases are great for a wide variety of use cases and can be used for general-purposes.

Key-value database

A Key-Value NoSQL stores data as a set of Keys and Values, also known as an associative array, organized into rows.

An example of what an electronics store database might look like.

Key-value databases are great for use cases where you need to store large amounts of data but do not need to perform complex queries to retrieve it. A very common use is to store user preferences or cache.

Wide-Column Store

In this type of NoSQL database, the columns in each row are contained within that row. An image to explain better:

Each column family is a container of rows in a table. The key identifies the row that consists of multiple columns.

The rows do not have to contain the same number of columns or the same information. Columns can be added to any row at any time without having to add it to other rows.

Wide columns databases are great for when you need to store large amounts of data and you can predict what your query patterns will be

This type of model is often used to store user profile data.

Graph Database

Nodes and relationships are the essential constituents of this type of database. A node represents an entity. A relationship represents how two nodes are associated.

The graph model use nodes that contain lists of related records. These records represent relationships with other nodes, which eliminates the (time-consuming) search and comparison operation found in the relational database.

The relationships between the nodes are predetermined, that is, they are not determined at the time of consultation.

Main NoSQL databases

MongoDB

The most famous non-relational database.

MongoDB stores data in flexible JSON-type documents, which means that the fields can vary from document to document and the data structure can change over time.

Also, you will receive benefits such as full index support, high availability via WANs and LANs, along with easy replication, horizontal scalability, rich document-based queries, flexibility in data processing and aggregation, along with adequate training, support, and consultation.

Besides everything, MongoDB is free to use.

Redis

Redis, which stands for Remote Dictionary Server, is a fast, open-source storage database with stores key-value in memory to fast access.

One of the main attractions of using Redis is its speed. Redis data is kept in memory, while other databases store data on disk or on SSDs, which allows Redis to access information much faster. Redis will only store data on disk when it is necessary to persist.

Cassandra

Cassandra If you are looking for high availability and scalability without compromising on performance, then the Cassandra database is right for you.

Apache Cassandra is a free and open-source distributed software that uses the wide column standard.

Cassandra is highly scalable and high performance, designed to handle large amounts of data on many servers.

Neo4j

Neo4j is another open-source project, native graph NoSQL database that provides an ACID compatible transactional backend for your applications.

It is referred to as native because it efficiently implements the proprietary graph model down to the storage level. This means that the data is stored exactly as you store it, and it uses pointers to navigate and cross the graph.

Bottom Line

I believe we were able to clarify the differences between SQL and NoSQL.
Most of the projects are still based on this first model. But large companies are already migrating some of their databases to NoSQL systems.

It may become a new standard in the future, or it may continue to co-exist with SQL. Only time will tell.

Don’t forget to subscribe to receive all the new and exclusive content!

I’ll see you next time.

]]>
https://blog.debugeverything.com/must-know-about-relational-and-non-relational-databases/feed/ 1
How to connect to MySQL database using Python https://blog.debugeverything.com/how-to-connect-python-to-mysql-database/ https://blog.debugeverything.com/how-to-connect-python-to-mysql-database/#comments Thu, 16 Apr 2020 03:02:29 +0000 https://debugeverything.com/?p=775 Don‘t you hate it when you close a file just to, later on, realize that you forgot to save it? It‘s always an unpleasant feeling, but it does happen from time to time, regardless of your experience. This is what happens when you don‘t connect your project to a database.

In this article I’ll show you how to connect Python to MySQL and how to create your first database.

Well, you already have your project up and running. Why would you need a database connected to it?

Unless you don’t need your project to save any info, you don’t really need it. A database goal is to store information, so we can later on use it instead of restarting everything from the ground again.

Let’s do a step-by-step guide of how to connect Python to Mysql being able to create our first database using only Python.

What is a Database, and what makes it so important?

A database stores data in a organised way, so you can search and retrieve it afterwards.

A database must contain at least one table. It looks pretty much like a spreadsheet: It’s basically a set of rows and columns.

Every row has the same columns, and each column has its data. If it may help, you can think of these tables as Excel tables.

The data can be inserted, read, updated and deleted from a table. Although we often say ‘inserted’ in the database, the most correct term is ‘created’; And, with these operations, we have our lovely acronym: CRUD (Create, Read, Update and Delete).

A relational database organizes it’s data in tables, and binds them, based on defined relations. These relations allow data to be retrieved and combined, from one or more tables in just one query. 

A database will be extremely useful if you have followed my tutorial, where I showed how to create a login page from scratch.

But this was just a load of words. If you truly wish to understand the relational database, you must create your own. So, let’s go.

Installing MySQL database

The first thing you need to do, if you haven’t already, is to go to MySQL’s site, then download and install the latest version of MySQL into your computer. The version I’m using here is MySQL Community Server 8.0.19

I also strongly advise that you install a graphical interface to see your database, like MySQLWorkbench or phpMyAdmin.

When you’re done, just check if your database is up and running with any of the tools above, using the login and password you created while setting it up.

Python to MySQL Connector

Now, let’s connect Python to MySQL, and create our very first database.

By default, Python doesn’t come with this connector installed. You must first download it. A connector is nothing more than a module, that helps the communication with Python to MySQL. There are at least 5 of these available, but here we are just going to use the one given by MySQL itself.

To install the connector, the simplest (and most advised) way is by using pip (the Package Installer for Python). I already mentioned pip in another post where How to create a login page with Python . If you already have pip installed, just run the code below to install the Python to MySQL Connector.

pip install mysql-connector-python

Connecting Python to MySQL

It’s very easy and simple to connect Python to MySQL using our recently installed Connector.

First things first, we need to import our Connector. Then, we use the function connect() with the credentials you generated during MySQL setup. The host can either be localhost or 127.0.0.1. They are synonyms.

In this test we are just gonna open up a connection and close it afterwards, with the function close().

import mysql.connector

cnx = mysql.connector.connect(user='root', password='********', host='127.0.0.1')

cnx.close()

Copy and paste the code above, then save it on a file called connection.py (or any other name you want). Open up a window in your terminal, and go to the folder of the file you just created. Then just run the command: python connection.py

If the code above runs without any problems, it means that it worked and you can already use MySQL.

Creating a Database in MySQL using Python

Now it‘s time to create our very first database. Let‘s just change the code above a bit. Let’s create an instance of a MySQLCursor class object instead of just closing the connection. The object interacts with the MySQL serve using the object MySQLConnection.

To create the cursor, just use the method cursor() on an already existing connection. Now, we write our SQL Query and run it, using the method execute() in the object mycursor, from the connection we just made. 

Below, is the SQL command to create a Database:

CREATE DATABASE mydatabase

Putting it all together, we have the following code:

import mysql.connector

cnx = mysql.connector.connect(user='root', password='********', host='127.0.0.1')

mycursor = cnx.cursor()
mycursor.execute("CREATE DATABASE mydatabase")

Just run this file with the same command I showed you at the start: python connection.py. If it also runs without any problems, Congratulations! You just created your first database with Python.

Bottom Line

To check on your recently created database, you can use one of the tools I’ve mentioned before. While using MySQLWorkbench, I’ve got the following results:

Now, a recap of everything we saw:

In this article we learned how to connect Python to MySQL using Python MySQL connector for that,

Well, I hope this made clear as to why you should have a database in your project and how simple it is to create a connection.

Until next time.

]]>
https://blog.debugeverything.com/how-to-connect-python-to-mysql-database/feed/ 3