The fifth lesson of MySQl database -------- simple commands in SQl -------- learning and learning

Author's Foreword

 Welcome little cuties to come and learn from my gtiee Qin Boss (qin-laoda) - Gitee.com

————————————————————————————

Table of contents

A brief introduction to the database
        1. Data Storage
        2. Database type
                (1). Relational database
                 (2). Non-relational database
        3. Data type
        4. Constraints
        5.SQL

————————————————————————————

author small nonsense

        In the blog in the previous chapters, I briefly introduced the simple commands of linux. These commands allow us to perform simple operations in the terminal, such as modifying files, creating folders, files, copying files or folders. Moving files or folders, searching for file names, searching for file contents, packing and compressing files (including decompression), granting permissions, obtaining administrator permissions, viewing users, and some commands to download mysql, etc. Below we operate in the environment that will step into the database

Interstitial knowledge

1. Do not design mysql password-free login when entering /etc/my.cnf, which may prevent us from connecting to the graphical interface

A brief introduction to the database

data storage

     From knotting and keeping records in ancient times to modern data storage. Data has always been a non-important existence in daily life. From as small as the bookkeeping after buying breakfast to as large as the collection and arrangement of data during the war. Even today's machine learning or AI requires massive amounts of data as the basis for computing

In daily life, when we look at JD.com and Taobao, most of the data is displayed in this form, with both pictures and texts. This is a feature that is convenient for customers to view the product
As a qualified programmer, viewing data from the database is the most basic operation. Through SQL statements, call out logical data for use by yourself and your colleagues.

 The database itself is just a kind of file

We can see what mysql looks like

 You can see the layout in the mysql file

 When we enter the mysql environment to view the library inside (which can be understood as a database), what is displayed is the name of the library

In the Linux terminal, we can find the corresponding data directory in /var/lib/mysql . At the same time, the corresponding library name can also be found in the database. Corresponding to a specific table, the presentation form of the data is very similar to the Excel file
1. Select a library to enter and view the number of tables

2. Select a table to enter, view the structure of the table,

 3. View the contents of the table

 database type

    1. Relational database:

         In a database, associations between data are allowed. That is, to put data into tables, which can be related to each other. At its core is its Structured Query Language (SQL ). Among them, oracle , mysql and sqlite are typical relational databases and are widely used. In order to meet different scenarios and computing requirements, different enterprises will also optimize and manufacture their own databases according to their own needs, such as hive, which facilitates big data computing, and Alibaba Cloud's general-purpose database ADB .
      MySQL
       Pros: Free. MySQL supports multiple operating systems, has a wide range of usage scenarios, and can be accepted by most graphical interfaces. Use standard SQL language forms. It can run on multiple systems and supports multiple languages.
A relational database is a database that can be displayed in Excel  
   2. Non-relational database:
Data ( databases that cannot be simply displayed in Excel). Non-relational databases are also interpreted as NoSQL . Although there are many types of NoSQL databases, the common feature is to remove the relational characteristics of relational databases. There is no relationship between data, so it is very easy to expand. The longest used non-relational databases are Redis and MongoDB . The representation of data is also different from relational databases, key-value pairs, columns, documents, etc.
Redis
Pros: Free. Supports data persistence, can save the data in the memory to the disk, and can be loaded again for use when restarting. Support data backup. It not only supports data of key-value pair key-value type, but also provides storage of data structures such as list list , set set , ordered set zset, and hash hash .

RDBMS (Relational Database Management System)

Relational Database Management System : A relational database management system that expresses relational data through tables.

 RDBMS-server can be understood as a server, RDBMS-ckient can be understood as a client 

Operating principle:

The user has used RDBMS-ckient to send sql statements to RDBMS-server, and then RDBMS-server returns the data that meets the requirements to RDBMS-ckient

Through SQL statements, the data can be organized in a relational and structured manner, and then displayed on the user's client.

type of data

The data types of Python language include int, str, list..., etc., and the data types of C language include int, char float double, etc.

Of course mysql also has its own data type

data type

 Among them, we commonly use tinyint, int (integer), double, and other smallint, mediumint, blgint, float, and decimal are used later to search

time type

 date, time, year, datetime, timestamp (timestamp) we can simply remember date and datetime, the precision of data is up to days, and the precision of datetime is up to seconds (commonly used in transaction time)

character type

In char, varchar, tinyblob, tinytext, blob, text, medlumblob, medlumtext, longblob, longtext, we first simply remember varchar, text (long text)

Notice

For example, char(20) and varchar(20) indicate that 20 characters can be stored instead of 20 bytes. CHAR and VARCHAR types are similar, but they are stored and retrieved differently. No case conversion is done during storage or retrieval.
BINARY and VARBINARY are similar to CHAR and VARCHAR , except that they contain binary strings instead of non-binary strings. They contain byte strings rather than character strings . This means they have no character set, and sorting and comparisons are based on the numeric value of the column value bytes.
A BLOB is a binary large object that can hold variable amounts of data. There are 4 BLOB types: TINYBLOB , BLOB , MEDIUMBLOB, and LONGBLOB . They differ in that they can accommodate different storage ranges.
There are 4 TEXT types : TINYTEXT , TEXT , MEDIUMTEXT and LONGTEXT . The corresponding four BLOB types have different maximum storage lengths, which can be selected according to the actual situation
When processing data, it is not the most correct way, but the most applicable way

SQL

Structured Query Language is a database language used to operate RDBMS . In oracle , for example , there will be individual syntax differences, and current relational databases support operations using the SQL language ( SQL statements are not case-sensitive).
SQL statements are mainly divided into:
DQL : Data Query Language, used to query data
DML : data manipulation language, add, modify, delete data
DDL : data definition language, database, table management, etc.
DCL : data control language, authorization and authority recovery
CCL : pointer control language, through the control pointer to complete the operation of the table
For a programmer, the addition, deletion, modification and query of the database must be mastered. Namely insert , delete , update , select
Check (select) is the most important when processing data

database operation

enter the database

mysql -u root -p

view all libraries

database: database  

Add s databases: all databases

show databases;

select database
use 数据库的名称;

create database

create database  库名;或者CREATE DATABASE 库名;
create database 库名  charset=utf8;

 Generally, the default is utf-8 encoding format

delete library

drop database 库名;或者DROP DATABASE 库名;

View the location of the current library

select database();或者SELECT DATABASE();

If we forget this command, we can select the library again (USE library name)

USE 库名

Data Table Operations

select library

use 库名 或者USE 库名

 View all tables in the current Curry

show tables;或者SHOW TABLES;

View the structure of the current table

desc 表名;或者 DESC 表名;

 The content under the field is the header name. It can be seen that there are three headers in this table: id name pid

The data type in the id is int and cannot be left blank. The default value of the primary key is empty.  

constraint
Primary key primary key : The order of physical storage (there is a real sort), the primary key is non-empty and unique
not null : this field does not allow to be empty
Unique : the value of this field does not allow duplicates
Default default : When this field is not filled in, the default value will be used
Foreign key foreign key : Constrains the relational field. When filling in the value for the relational field, it will go to the associated table to query whether the value is
It exists, if it exists, it will be filled successfully, if it does not exist, it will fail to be filled and an exception will be thrown.
Although foreign key constraints can ensure the validity of data, it will reduce the performance of the database when adding, modifying, deleting, and checking data.
Can, so it is not recommended to use.
view table contents
select  * from 表名;
select *  from 表名 limit  20; //查看表内容的前20行

 Summarize:

Here is a brief introduction to some values ​​and names of the database, as well as some simple commands, and more commands will be introduced later

Guess you like

Origin blog.csdn.net/m0_69984273/article/details/131606114