Database comparison and ER model (relationship of table records)

In the previous article "Database Overview and Introduction to Common Databases" mentioned database rankings and common databases, then the description of the database in the top ten databases:
1. Relational: relational
2. Document: document-based
3. Key- value: based on key-value pairs
4.Search engine: based on search engines
insert image description here

1. Relational database (RDBMS)

1. Substance

Relational database is the oldest type of database. The RDBMS model boils down complex data structures into simple binary relationships (that is, in the form of two-dimensional tables), and stores data in the form of rows and columns . A series of rows and columns become a table , and a group of tables constitutes a database.
insert image description here
There is a relationship between the records in the table and the table, and the various connections between various entities in reality are Represented by a relational model
A relational database is a database based on a relational model , and SQL is the query language of a relational database

2. Advantages

  • Complex queries : SQL statements can be used to facilitate very complex data queries between one table and multiple tables
  • Transaction support : enables data access requirements with high security performance to be realized

2. Non-relational database

1 Introduction

Non-relational databases can be regarded as a castrated version of traditional relational database functions . They store data based on key-value pairs and do not need to be parsed by the SQL layer. The performance is very high . At the same time, by reducing the functions that are not commonly used, the performance is further improved.
At present, most of the mainstream non-relational databases are basically free.

2. NoSQL type

Compared with SQL, NoSQL generally refers to non-relational databases, including key-value types, document types, search engines, and column storage on the list, as well as graph databases. Only use the term NoSQL to include these technologies

2.1. Key-value pair database

Store data in the way of Key-Value key value, where Key and Value can be simple objects or complex objects. Key is used as a unique identifier. The advantage is that the search speed is fast, which is obviously better than the relational type in this respect . The disadvantage of the database is that it cannot use conditional filtering (such as WHERE) like a relational database. If you do not know where the data is, you need to traverse all the keys, which consumes a lot of calculations. The typical usage scenario of a key-value database is as a
memory cache . Redis is the most popular key-value database

2.2. Document database

This type of database can store and retrieve documents, which can be in formats such as XML and JSON. In a database, a document is the basic unit for processing information, and a document is equivalent to a record. The documents stored in the document database are equivalent to the "values" stored in the key-value pair database. MongoDB is the most popular document database. In addition, there are CouchDB etc.

2.3. Search engine database


The data storage form applied in the search engine field, because the search engine will crawl a large amount of data and store it in a specific format, so as to ensure the best performance during retrieval. The core principle is the typical product of "inverted index": Solr, Elaticsearch, Splunk, etc.

2.4. Columnar database

Compared with row-based databases, databases such as Oracle, MySQL, and SQL Server all use row-based storage (Row-based), while column-based databases store data in the database according to columns. The advantage of this is that it can It greatly reduces the I/O of the system and is suitable for distributed file systems. The disadvantage is that the functions are relatively limited.
Typical products: Hbase, etc.

2.5, graph database

The graph data structure is used to store the relationship between entities. The most typical example of a graph database is the relationship between people in a social network. The data model is mainly realized by nodes and edges (relationships), and its feature is that it can efficiently solve auxiliary relationship problems. Typical
products: Neo4j, InfoGrid, etc.

3. Summary

NoSQL is a good complement to SQL. In actual development, some requirements do not provide complete relational database functions. It is more sensible to use non-relational databases with higher performance and lower cost . For example: log collection, leaderboard, timer and other functions
Even though there are many categories of NoSQ, the proportion of SQL is larger. According to the ranking, 4 of the top 5 are relational databases, so it is very necessary to master SQL

4. Relational database design rules

  • The typical structure of a relational database is a data table , and the composition of these data tables is structured (Structured)
  • Put the data into the table, and then put the table into the library
  • There can be multiple tables in a database, and each table has a name to identify itself, indicating uniqueness
  • Tables have some characteristics that define how data is stored in the table, similar to the design of "classes" in Java and Python

1. Tables, records, fields

There are three main concepts in the ER (entity-relationship, entity-relationship) model: entity set, attribute, relationship set

  • An entity set (class) corresponds to a table (table) in the database
  • An entity (instance) corresponds to a row (row) in the database table, also known as a record (record)
  • An attribute corresponds to a column in a database table, also known as a field
    insert image description here

2. Table relationship

2.1, one-to-one relationship (one-to-one)

There are not many applications in actual development, because one-to-one can be created as a table.
For example: user table (student number, name, mobile phone number, ID card number, home address, emergency contact)
is split into two tables: two The records of each table are one-to-one correspondence.
Basic information table (commonly used information): student number, name, mobile phone number, ID number
file information table (not commonly used information): student number, home address, and emergency contact
. in principle:

  • Unique foreign key : the primary key of the main table and the foreign key (unique) of the secondary table form a primary-foreign key relationship, and the foreign key is unique
  • The foreign key is the primary key : the primary key of the primary table and the primary key of the secondary table form a primary-foreign key relationship
    insert image description here
2.2, one-to-many relationship (one-to-many)

Common example scenarios: customer table and order table , category table and commodity table , department table and employee table
such as:

  • Employee table : number, name, ..., department
  • Department table : number, name, introduction
    Principle of table creation:
    Create a field in the slave table (multi-party), and the field serves as a foreign key pointing to the primary key of the main table (one party)
    insert image description here
2.3, many-to-many relationship (many-to-many)

To represent a many-to-many relationship, a third table must be created, often called a join table , which divides the many-to-many relationship into two one-to-many relationships, inserting the primary keys of both tables into the third In the table,
for example:

  • Image information table: image id, size, location
  • Image tag table: tag id, tag name, profile
  • Picture label table: multiple labels can be selected for one picture, and one label can be selected by multiple pictures
    insert image description here
2.4. Self reference

In the same table, you can refer to your own fields.
For example: employee table (employee number, name, department number, supervisor number)
employee number may also be the supervisor number of subordinate employees
insert image description here

Guess you like

Origin blog.csdn.net/The_girl_wait_me/article/details/124884124