[MySQL] Basic knowledge of database + basic + operation (easy to understand)

I am a directory ~~~

First, the basic understanding of the database

        1. Why do you need a database?

        2. What is the database?

         3. What types of databases are there?

        4. What exactly is MySQL?

Second, the database foundation

        1. What are the basic operations of the database?

                1. Display the current database:

                2. Create the database: 

                3. Use the database: 

                4. Delete the database:  

         2. What are the common data types in the database?

                1. Numerical type:

                2. String type:

                3. Date type:

3. Basic operation of data table

        1. What are the basic operations of the data table?

                1. Create a data table:

                2. View the data table structure:

                3. Display data table:

                4. Delete the data table:


First, the basic understanding of the database

        1. Why do you need a database?

        With the continuous development of our Internet technology, our demand for the Internet has also increased. I vaguely remember that ten years ago, it was only required that the game downloaded on the mobile phone should not exceed 50M, or the time to be cached for watching videos and movies on the web was short. A few are enough, but the current computer network speed and hard disk memory capacity are already sky-high and underground compared to before.

        With it, there is also a huge amount of network data, and a large amount of data is generated rapidly. So how do we store, retrieve, and manage them more safely and effectively? Therefore, the issues of effective storage, efficient access, convenient sharing and security control of data have become a very important issue in the information age.

        Then some people may say, don't we have files or folders for storing data? Don't these store data? Yes, these things do store data, however, using files to store data has some disadvantages:

* The security of the file;
* The file is not conducive to data query and management;
* The file is not conducive to storing massive data;
* The file is inconvenient to control in the program.

        So in order to solve the above problem of storing data in files, we use a database. It can manage data more efficiently. The database
can provide remote services, that is, use the database through a remote connection, so it is also called a database server.

        2. What is the database?

        In short, a database is a piece of software, or a class of software. The function of this software is also very clear, that is, it is used to manage data (a warehouse used to load data). As for the data structure we learned before, do you think it's just used to brush questions and prepare for exams? Big mistake! In fact, it is used to discuss how to organize and manage data specifically! The purpose of our management is to "add, delete, check and modify" to manage the data in our library.

        So where is the data in our database generally stored? Generally, we save it on the hard disk. Here is the basic component of the next computer. If you imagine it as a human being, it will look like this:

        As its head, the CPU is used to process all kinds of strange information, and the memory and external memory are used as its storage devices to store the information in its small head, which can be imagined as its body and belly. The input device can be used as the hand it eats, mainly used to instill information and knowledge into the head and stomach. The output device can be used as the hand that writes the homework, this hand, used to output the result of its thought processing, write it on paper, and show it to others.

        However, there are also significant differences between memory and external memory, mainly as follows:

        1. The memory storage space is small and the external memory is large.
        2. The memory access speed is fast, and the external memory is full.
        3. The cost of memory is high and the cost of external memory is low.
        4. The data in the memory disappears after the power is turned off, and the external memory still exists after the power is turned off.

         3. What types of databases are there?

        We often say that the database can be roughly divided into two categories:

Relational Database:

        A database that uses a relational model to organize data. Simply put, a relational model refers to a two-dimensional table model, and a relational database is a data organization composed of two-dimensional tables and the connections between them.
Both are based on standards-based SQL, but some internal implementations are different. Common relational databases such as:

1. Oracle : Oracle products, suitable for large-scale projects, suitable for complex business logic, such as ERP, OA and other enterprise information systems. TOLL.
2. MySQL : belongs to Oracle, not suitable for complex business. Open source is free.
3. SQL Server : Microsoft's product, installed and deployed on the windows server, suitable for medium and large projects. TOLL.

 Non-relational database:

        SQL-based implementation is not specified. Now more refers to NoSQL databases, such as:

1. Based on key-value pairs (Key-Value): such as memcached, redis
2. Based on document type: such as mongodb
3. Based on column family: such as hbase
4. Based on graph: such as neo4j

        Let's talk about the specific differences between the two types of databases, but first of all, relational databases and non-relational databases are quite different in usage scenarios, so there is no such thing as strong or weak. Advantages of class databases: 

Relational Database non-relational database
using SQL Yes Not mandatory, generally not eager for SQL implementation
Transaction support support not support
complex operation support not support
Massive read and write operations low efficiency efficient
basic structure Based on tables and columns, the structure is fixed high flexibility
scenes to be used OLTP systems for business For data caching, or OLAP systems based on statistical analysis

        4. What exactly is MySQL?

        MySQL, the database software, is a program with a "client-server" structure. "Client" and "server" are connected through the network when connecting. These two nouns also refer to the two parties during network communication respectively.

        The party that initiates the network request actively becomes the "client", and the party that passively accepts the request becomes the "server". The data sent by the "client" to the "server" becomes the "request", and the data returned by the "server" to the "client" becomes the "response". For the MySQL software, as long as we install MySQL, it is equivalent to installing both the "client" and the "server".

After we install MySQL, we have both "server" and "client" in our computer, namely:

We can also use our own computer to communicate with other people's servers:

 Or in complex scenarios, multiple clients correspond to one server:

Second, the database foundation

        1. What are the basic operations of the database?

        After we install the MySQL software and enter the password, we will enter the MySQL operation interface:

        When the prompt "mysql>" appears, we can enter the sql statement to operate the database.

                1. Display the current database:

        show databases;

 Note: Use spaces to separate keywords and keywords, and it doesn't matter how many.
            When entering SQL statements, both uppercase and lowercase are acceptable, depending on personal habits.
            At the end of the statement, you must use English quotation marks ";" to mark the end of a statement.
            The punctuation marks in the sentence are all in English, not Chinese! ! !

                2. Create the database: 

        create database database name;

        In this way, we have created a new database named mynewdatabase. 

Note: When creating your own database name, the name can be arbitrarily named, but it cannot be a keyword in SQL. If a keyword appears in the database name you created, you can use "` `" to cause it.

                3. Use the database: 

        use the database name to use;

        Here I use the newly created database mynewdatabase. When it prompts Database changed, it means that the database has been successfully switched. When performing further operations on the database, you need to select the database first, and then perform the operation.

Note: Individual SQL statements do not need to end with an English semicolon ";", but we still recommend that everyone add a semicolon to the end to facilitate less mistakes in the future.

                4. Delete the database:  

        drop database The name of the database to be deleted;

 Note: This operation is very dangerous, because once you delete it successfully, it is very likely that it cannot be recovered! ! ! When we want to delete it in the future, we need to be cautious and cautious! ! !

         2. What are the common data types in the database?

                1. Numerical type:

type of data size illustrate Corresponding Java type
BIT[ (M) ]

M specifies the number of digits, the default is 1

Binary numbers, M ranges from 1 to 64, and stored values ​​range from 0 to 2^M-1

Commonly used Boolean corresponds to BIT. At this time, the default is 1 bit, that is, only 0 and 1 can be stored.

TINYINT 1 byte Byte
SMALLINT 2 bytes Short
INT 4 bytes Integer
BIGINT 8 bytes Long
FLOAT(M, D) 4 bytes

Single precision, M specifies the length, and D specifies the number of decimal places.

Loss of precision will occur

Float
DOUBLE(M,D) 8 bytes Double
DECIMAL(M,D) M/D max +2 Double precision, M specifies the length, and D represents the number of decimal places. exact value BigDecimal
NUMERIC(M,D) M/D max +2 Double precision, M specifies the length, and D represents the number of decimal places. exact value BigDecimal

Note: decimal can represent decimals exactly (M means effective length, D means the number of digits after the decimal point). 

                2. String type:

type of data size illustrate Corresponding Java type
VARCHAR (SIZE) 0-65,535 bytes variable length string String
TEXT 0-65,535 bytes long text data String
MEDIUMTEXT 0-16 777 215 bytes medium length text data String
BLOB 0-65,535 bytes long text data in binary form Byte[ ]

Note: varchar can specify how much space to occupy according to actual needs. (The unit of size is character, a character may consist of multiple bytes) 

                3. Date type:

type of data size illustrate Corresponding Java type
DATETIME 8 bytes Range from 1000 to 9999, no time zone
retrieval and conversion
java.util.Date、
java.sql.Timestamp
TIMESTAMP 4 bytes Range from 1970 to 2038, automatically retrieve current time
zone and convert
java.util.Date、
java.sql.Timestamp

3. Basic operation of data table

         Before operating the data table, you need to select a database first , because the data table is stored in the database. If you do not select the database first, then the computer cannot know which database the table you want to operate is in.

        Here we create a new database, select it and use it:

        1. What are the basic operations of the data table?

                1. Create a data table:

        create table requires a new table name (column name type, column name type...);

        Here I have created a new student table, which has an id (student number) column with type int; name (name) column with type varchar (50); age (age) column with type int; score (grade) column, The type is double(3, 1).

注意:varchar为字符串类型,初始容量为50;double为浮点数型,后面3表示有效数字为3个,1为小数点后保留1位。

                2、查看数据表结构:

        desc 需要查看的表名;

        可以查看表内每个列的名字和类型。

                3、显示数据表:

        show tables;

        可以显示当前数据库中所有的表。 

                4、删除数据表: 

        drop table 需要删除的表名;

        这里我删去了之前就在库中的aaa表,再次显示数据表时,aaa就不会再出现了。

 

End~~~ 

Guess you like

Origin blog.csdn.net/weixin_56960711/article/details/123398795