[MySQL] Basic database operations, table operations

1. Database

1. What is a database

Just use files to store data, why get a database?

File storage data has the following disadvantages:
file security issues,
files are not conducive to data query and management,
files are not conducive to storing massive data
, files are inconvenient to control in the program

Database storage medium:
1. Disk
2. Memory

In order to solve the above problems, experts have designed software that is more conducive to managing data - database, which can manage data more effectively. The database can provide remote services, that is, use the database through a remote connection, so it is also called a database server.

数据库It is a type of software whose function is to "manage data". To implement the internal database software, various data structures will be widely used, and it is often convenient to store data on the hard disk (persistent storage) for adding, deleting, modifying and checking
. Typical representatives: MySQL, Oracle, SQL Server

MySQLThis database is a program with a " client-server " structure ~
any command entered on the console essentially interacts with the server through the network.
Client-server: During network communication, communication the two sides, respectively named

  • Server : The party that passively accepts the request
    Client : The party that actively initiates the request
    Request : The data sent by the client to the server
    Response : The data returned by the server to the client

What we use, MySQL is probably like this:
insert image description here

For MySQL, you have installed MySQL, which is equivalent to installing both the client and the server. The communication between the client and the server is through the "network"

Operate the "database" through SQL . The database we are talking about here refers to an independent data collection managed by a MySQL server. A MySQL server can manage multiple groups of such data collections at the same time~

insert image description here


2. Basic database operations

2.1. Display database

show databases;
Show which databases (which data collections) are currently on the server

insert image description here


2.2, create a database

create database 数据库名;

insert image description here

insert image description here

2.3, select the database

use 数据库名;

Before performing further operations on the database, you need to select the database first, and then operate.

2.4, delete the database

drop database数据库名;

(Very dangerous operation) Once deleted, the data is very likely to be unrecoverable , especially some important data in the company (data in the production environment )

Is there a way to avoid data deletion in the production environment:

  • Permission control Not just anyone can delete, only individual "administrators" can perform delete operations
  • Backups Once deleted, there are still backup databases available.

3. Database classification

Databases can be roughly divided into relational databases and non- relational databases

Relational Database (RDBMS): 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. Based on standard SQL, only some internal implementations are different. Common relational databases are:

  1. Oracle: Oracle products are suitable for large-scale projects and 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:
(Understand) SQL-based implementation is not specified. Now more referring to NoSQL databases like:

  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: like hbase
  4. Graph based: like neo4j

The difference between relational database and non-relational database:

Relational Database non-relational database
using SQL Yes Not mandatory, generally not implemented based on SQL
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 system based on statistical analysis
  • Note: OLTP (On-Line Transaction Processing) refers to online transaction processing, OLAP (On-Line Analytical
    Processing) refers to online analytical processing

4. MySQL installation

insert image description here

  • environment variable
    insert image description here

MySQL server install
entos install maria


Second, the use of data sheets

1. Common data types

1.1. Numerical type

type of data size illustrate Corresponding java type
BIT[ (M) ] M specifies the number of digits, the default is 1 Binary number, M ranges from 1 to 64, and stores values ​​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 same as DECIMAL BigDecimal

DECIMALCan accurately represent decimals , correspondingly, a BigDecimalclass is also provided in Java to implement the corresponding function~
insert image description here

Extended information:

The value type can be specified as unsigned (unsigned), which means that
1 byte (bytes) = 8bit does not take a negative number.
For ranges of integer types:

  1. Signed range: -2^ (type byte number 8-1) to 2^ (type byte number 8-1)-1, if int is 4 bytes, it
    is -2^31 to 2^31-1
  2. Unsigned range: 0 to 2^ (type bytes * 8)-1, such as int is 2^32-1

Try not to use unsigned. For the data that the int type may not be able to store, the int unsigned may also not be able to store it. Instead, it is better to upgrade the int type to the bigint type when designing.


1.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[]

varcharYou can specify how much space to take up according to your actual needs (the unit of SIZE is characters).
A character may be composed of multiple bytes~

BLOBUsed to represent binary data. mp3 files, jpg files; .clas… all belong to binary files
txt, java, .c belong to text files~~


1.3. Date type

type of data Large and small illustrate Corresponding java type
DATETIME 8 bytes The range is from 1000 to 9999, and no time zone retrieval and conversion will be performed. java.util.Date、 java.sql.Timestamp
TIMESTAMP 4 bytes The range is from 1970 to 2038, the current timezone is automatically retrieved and converted. java.util.Date、 java.sql.Timestamp

If you use varchar, you will lose the verification function of the date (to determine whether the date is legal or not.


2, the operation of the table

  • How MySQL Manages Data
    insert image description here

It can be written on IDEA
insert image description here
In cmd, ctrl + c means to interrupt the current input (a sql input is half, you don't want it, re-enter it.) If
you want to copy, you need to select it first, then enter to copy


2.1, create a table

create table表名(列名类型,列名类型....);

In SQL, you can use "–space + description" to indicate comment description,
insert image description here
you can use comment to add field description

create table stu_test (
	id int,
	name varchar(20) comment '姓名',
	password varchar(50) comment '密码', 
	age int,
	sex varchar(1),
	birthday timestamp,
	amout decimal(13,2),
	resume text
);

Before performing table operations, you must first select the database (use)

insert image description here
When specifying a column, the column name comes first and the type comes after

In most programming languages, the type is first, followed by the variable name, and some languages ​​define variables with the variable name first and the type last (Python, Go)


2.2. View table

show tables
The premise of this operation is to select the database first.

insert image description here


2.3, view the table structure

View how many columns there are in a table, the type, name, and other supplementary information of each column

desc 表名;
describe

insert image description here


2.4, delete table

drop table 表名

It is also a very dangerous operation. Once deleted, it cannot be recovered.


3. Practice

There is a store's data, which records customers and shopping, and consists of the following three tables:

  • Commodity goods (product number goods_id, commodity name goods_name, unit price unitprice, commodity category category, supplier provider)
  • Customer customer (customer number customer_id, name, address, email, gender sex, ID card_id)
  • Purchase purchase (purchase order number order_id, customer number customer_id, commodity number goods_id, purchase quantity nums)
    insert image description here
    insert image description here
    insert image description here

Guess you like

Origin blog.csdn.net/qq_56884023/article/details/123214618