Database in linux

Table of contents

1.Install MySQL

2.Create database

3. Delete database

4. Query the statement to create the database

5 Statements using database

6. Query the current default database

 7. Query the encoding method and verification rules used

8. Statement to create table

9. Physical storage structure of the table

10.Table data type

11. Summary


Introduction :

Databases are an integral part of modern application development, providing a structured data storage and management environment. In this blog, we will introduce how to create, delete and use a database under the Linux system in a virtual machine, query the current default database and the encoding method verification rules used, and introduce the data types and examples of tables

1.Install MySQL

First, we need to ensure that the MySQL database management system is installed in the virtual machine. You can check if it is installed by entering the following command in the terminal:

 If MySQL is not installed, you can install it on your Linux system with the following command:

sudo apt-get update
sudo apt-get install mysql-server

2.Create database

In Mysql, you can create a new database using the following command:

CREATE DATABASE mydatabase;

3. Delete database

If you need to delete a database, you can use the following command:

DROP DATABASE mydatabase;

4. Query the statement to create the database

If you want to query the statement that created the database, you can use the following command:

 This will display mydatabasethe statement that created the database

5 Statements using database

If you want to use the database, you can use the following command:

use mydatabase;

6. Query the current default database

To query the current default database, you can use the following command:

This will return the current default database name

select database();

 7. Query the encoding method and verification rules used

To query the encoding method and verification rules used by the current database, you can use the following command:

SHOW VARIABLES LIKE 'character_set_database';
SHOW VARIABLES LIKE 'collation_database';

 

The first command will return the encoding of the current database

The second command will return the validation rules of the current database.

8. Statement to create table

CREATE TABLE
简单语法:
    CREATE TABLE 表名(
    列名 列类型,
    列名 列类型
    );


Function: Create a table in the current database

9. Physical storage structure of the table

MyISAM(一种引擎)的表:
[root@node1 ~]# cd /var/lib/mysql/mysql/
[root@node1 mysql]# ls -l user*
-rw-r----- 1 mysql mysql 10816 7月  16 17:39 user.frm  # 描述表结构文件,字段长度等,frame框架
-rw-r----- 1 mysql mysql  384 7月  16 17:52 user.MYD   # 数据信息文件,存储数据信息
(如果采用独立表存储模式) data
-rw-r----- 1 mysql mysql  4096 7月  16 17:54 user.MYI  # 索引信息文件,index


InnoDB(默认的存储引擎)的表:
[root@node2 employess]# ls -l t4*
-rw-r----- 1 mysql mysql  8586 7月  16 20:31 t4.frm
-rw-r----- 1 mysql mysql 98304 7月  16 20:32 t4.ibd

t.frm: stores column-related information, describing table structure files, field lengths, etc.
t.ibd: data rows + indexes. If the independent table storage mode is used, a b.ibd file (storing data information and indexes) will also be generated in data\a Information)
    If the co-storage mode is used, the data information and index information are stored in ibdata1.
    If partitioned storage is used, there will also be a t.par file (used to store partition information)

10.Table data type

In MySQL, there are three main types: text, number, and date/time types

 

 

 

 

Now when you need to define multiple fields in a table, you need to use different data types to accommodate different types of data. In the following example, we will create a table with numeric, text and date fields and use their respective data types

CREATE TABLE my_table (
    id INT,
    name VARCHAR(50),
    age SMALLINT,
    salary DECIMAL(10, 2),
    address TEXT,
    email VARCHAR(100),
    birthdate DATE,
    description_long LONGTEXT,
    content_long LONGBLOB,
    thumbnail MEDIUMBLOB,
    status ENUM('Active', 'Inactive', 'Pending'),
    ratings SET('Excellent', 'Good', 'Average', 'Poor'),
    quantity MEDIUMINT,
    population INT,
    revenue BIGINT,
    rating DOUBLE,
    price DECIMAL(8, 2),
    created_at DATETIME,
    updated_at TIMESTAMP,
    event_time TIME,
    year_field YEAR
);

By using different data types in the table definition, we can ensure that each field can store the corresponding data type. In this way, we can store values ​​of multiple data types such as numbers, text, and dates in the table, and perform corresponding operations and queries in the application.

11. Summary

In this blog, we learned how to create and manage a database under a Linux system in a virtual machine. We learned how to install MySQL, create and delete databases, query the statements to create databases, use databases, and query the current default database and the encoding verification rules used. These basic operations will help you get started using databases and storing and managing data in application development.

If this blog is helpful to you! I hope you can pay attention. We will continue to update database-related knowledge points in the future. If you have any questions or suggestions, please feel free to leave a message below. Thanks!

рекомендация

отblog.csdn.net/weixin_62304542/article/details/131604480