Basic operations of MySQL database and tables

insert image description here

1. Basic knowledge of database

background knowledge

MySQL is a client-server program


The side that actively sends data, the side that passively receives data from the client (client) , the server (server)
The data sent by the client to the server is called: request (request)
The data sent by the server to the client is called: response (response)

The client and server communicate over the network
The server is the main body of data storage (the data is stored on the hard disk of the host).
insert image description here
Multiple databases are stored in the server, and multiple tables can be stored in each database, and each table stores its own data. The
prerequisite for the normal use of the database is Client and server are connected

Basic operations of the database

1. Show all databases

show databases;

Here databases is followed by s, because it is all databases
insert image description here
2. Create a database

create database [if not exists] DB_name [charset charset_name] [ collate collate_name]

Here, everything contained in the brackets can be added or not, and you can choose according to the specific situation.
insert image description here
Here we see Query OK, which proves that we successfully created a database named test.
insert image description here
When we created the test database again, the system reported an error (database exists), proving that the database already exists. At this time, we can add a judgment (if not exists)
insert image description here
. Here we create this duplicate database again and find that the system does not report an error, but tells a warning. We can take a look at this warning message
insert image description here
Here we can find that the (database exists) warning is reported. Everyone should pay attention here. Errors and warnings are not a heavyweight. When appropriate, we can add (if not exists) to avoid these errors.
CHARACTER SET: Specify the character set used by the database.
The character set refers to the encoding rules for a certain range of characters.

1. For example, the utf8 character set uses 3 bytes to represent (encode) all Chinese characters, so we call utf8 a character set.
The scope here refers to all Chinese characters.
The encoding rule refers to the use of 3 bytes to represent a Chinese character.
2. For example, the ASCII character set uses 1 byte to represent (encode) all English letters, so we call ASCII a A character set.
The scope here refers to all English letters.
The encoding rule refers to the use of 1 byte to represent a letter.

The utf8 in Mysql is a pseudo-utf8. When some are not satisfied, it can be replaced by a more comprehensive utf8bm4.
insert image description here
COLLATE is usually related to data encoding (CHARSET). Generally speaking, each CHARSET has a variety of COLLATEs it supports, and Each CHARSET specifies a COLLATE as the default value. For example, the default COLLATE of Latin1 encoding is latin1_swedish_ci, the default COLLATE of GBK encoding is gbk_chinese_ci, and the default value of utf8mb4 encoding is utf8mb4_general_ci.
insert image description here
3. Use the database
When we want to operate on a database, then we have to use this database.

use DB_name;

insert image description here

4. Deleting the database
Deleting the database is a very dangerous thing, everyone must be cautious when deleting the database

drop database DB_name;

insert image description here
But if the deleted library does not exist in the server, an error will be reported.
At this time, we can add a judgment in front of the database name

drop database [if exists] test;

At this time, we can find that even if the database server does not exist, only a warning is reported
insert image description here
insert image description here

2. Data type

When we are learning MySQL data types, we will find that some data types are not well designed, and there are some differences from java. This is because the language of MySQL is relatively long. At that time, there was no such language as Java. It's great.

string type

type of data size illustrate Corresponding java type
varchar(size) 0 - 65535 bytes variable length string String
text 0 - 65535 bytes long text data String
mediumtext 0-16777215 bytes medium length text data String
blood 0 - 65535 bytes binary long text data byte[ ]

varchar(size) : The most commonly used type of string, with a parameter parameter, which indicates the maximum storage capacity, varchar(50) indicates that this column can store up to 50 characters, as for the size, it can be determined according to actual needs , not directly allocate as much as the size is ordered, but dynamically allocate, but the maximum will not exceed the size.
text,mediumtext : suitable for relatively long strings, relatively storing a long text, etc., with less application
bold : mainly used to store binary data.

value type

type of data size illustrate Corresponding java type
bit(M) M specifies the number of bits, the default is 1 binary number Commonly used boolean corresponds to bit
tinyint 1 byte byte
smallint 2 bytes short
int 4 bytes Integer
bigint 8 bytes Long
float(M,D) 4 bytes Single precision, M is the length, D is the number of decimal places, and the precision will be lost Float
double(M,D) 8 bytes Double precision, M is the length, D is the number of decimal places, and the precision will be lost Double
decimal(M,D) M/D Max+2 Double precision, M is the length, D is the number of decimal places, exact value BigDecimal
numeric(M,D) M/D Max+2 Double precision, M is the length, D is the number of decimal places, exact value BigDecimal

The numeric type can be specified as unsigned (unsigned), not to take negative numbers.
Try not to use unsigned. For data that may not fit in the int type, int unsigned may also not fit in the data. Instead of this, it is better to upgrade the int type to the bigint type during design.
float, double : Not suitable for data that needs to be stored accurately, because the IEEE 754 standard, the storage of data in memory determines that the data cannot be accurately represented.
decimal : Floating-point numbers can be represented precisely, because it sacrifices space and computing speed in exchange for a more precise representation.
The most commonly used numeric types:int,double,decimal

date type

type of data size illustrate Corresponding java type
datetime 8 bytes The range is from 1000-9999, no search and conversion will be performed java.util.Date、java.sql.Timestamp
timestamp 4 bytes Range from 1970 - 2038, automatically retrieve current time zone and convert java.util.Date、java.sql.Timestamp

The type when inserting into the table is:‘xxxx-xx-xx xx:xx:xx’
timestamp : this type is a bit dangerous and will be exhausted in 2038

Three, the basic operation of the table

When making any database table, you need to use the database first

use DB_name;

create table

create table table_name(
filed1 type [comment xx},
filed2 type [comment xx},
filed3 type [comment xx}
);

comment is an explanation field that can be added
Here we take creating a student table as an example:

 create table student(
     id int comment '学号',
     name varchar(50),
     age int
     );

insert image description here

View table structure

desc table_name;

The full spelling of desc is describle.
insert image description here
Here we can view the structure of the table.

view all tables

Can view all tables under the current database

show tables;

insert image description here
Because we only created a student table in the current database.

delete table

Deleting table structures and deleting database operations are very dangerous. Be careful when using them.

drop table table_name;

insert image description here

Guess you like

Origin blog.csdn.net/buhuisuanfa/article/details/127713348