Write a NoSQL database python in Python

Source: Internet
Author: User

The term NoSQL is becoming ubiquitous in recent years. But what exactly is "NoSQL" referring to? How and why is it so useful? In this article, we will write a NoSQL database to answer these questions through pure Python (which I prefer to call it, "light-structured pseudocode").

Oldsql

In many cases, SQL has become a synonym for the database. In fact, SQL is the acronym for Strctured Query Language, not the database technology itself. Rather, it refers to a language that retrieves data from an RDBMS (relational database management system, relational databases Management systems). MySQL, MS sql Server and Oracle belong to one of the RDBMS.

The R in the RDBMS, the "relational" (relational, associative), is the richest part of the content. Data is organized by tables (table), and each table is made up of columns (column) that are associated with the type. The type of all tables, columns, and their classes is referred to as the database schema (schema or pattern). The schema of each table depicts the structure of the database in its entirety. For example, a table called Car might have some of the following columns:

Make:a string

Model:a string

year:a four-digit number; Alternatively, a date

Color:a string

VIN (Vehicle identification number): a string

In a table, each single entry is called a row, or a record. To differentiate each record, a primary key (primary key) is typically defined. The primary key in the table is one of the columns that uniquely identifies each row. In table Car, VIN is a natural primary key choice because it ensures that each vehicle has a unique identity. Two different rows may have the same value on make, Model, year, and Color columns, but for different cars, there will definitely be different VIN. Conversely, as long as two lines have the same VIN, we don't have to check the other columns to see that the two lines refer to the same car.

Querying

SQL allows us to get useful information by querying the database. In simple queries, a query is a structured language that questions the RDBMS and interprets the rows it returns as the answer to the question. Assuming that the database represents all registered vehicles in the United States, in order to obtain all the records, we can make the following SQL query on the database:

SELECT make, Model from Car;

To roughly translate SQL into Chinese:

"Select": "Show Me"

"Make, Model": "The value of make and model"

"From Car": "For each row in table car"

That is, "show me the values of make and Model in each row of the table Car." After executing the query, we will get the results of some queries, each of which is make and Model. If we only care about the color of the car registered in 1994, then you can:

SELECT Color from Car WHERE year = 1994;

At this point, we will get a list similar to the following:

Black

Red

Red

White

Blue

Black

White

Yellow

Finally, we can specify the query for a car by using the table's (primary key) primary key, which is VIN:

SELECT * from Car WHERE VIN = ' 2134afger245267 '

The above query will return the property information for the specified vehicle.

The primary key is defined as unique and non-repeatable. In other words, a vehicle with a specified VIN can appear at most once in a table. This is very important, why? Take a look at an example:

Relations

Suppose we are running a car repair business. In addition to some other necessary things, we also need to track the service history of a car, that is, all the trimming records on that vehicle. Then we might create a servicehistory table with some of the following columns:

VIN | Make | Model | Year | Color | Service performed | Mechanic | Price | Date

Write a NoSQL database python in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.