Mysql question summary & learning record 1: simple sql statements; library, table operations and basic data types of Mysql

Mysql problem summary & learning record 1

What is the difference between utf8 and utf8mb4? - Zhihu(zhihu.com)

Problem with setting Mysql permanent unified encoding: (2 messages) MySQL5.7 permanent unified encoding/character set is utf8 (solve garbled code)_mysql 5.7.29 utf8 garbled code_NekoSheep's blog-CSDN blog

An article on understanding the MySQL character set encoding configuration and modification methods - Nuggets (juejin.cn)

I haven't modified it here yet, because some bloggers said there is no need to modify it, and there are some problems with utf8, so I haven't changed it yet.

I found that the Command Line Client that comes with the latest version of MySQL comes with two versions, one without suffix - Unicode version, and one with built-in - Unicode version. It turns out that the Unicode version has helped us unify the encoding. So we don't need to consider this issue now. (Comparison of the two pictures below)

Insert image description here
Insert image description here
From this we can know that in subsequent use, we can directly use the Unicode version of the console to avoid possible character garbled problems.


sql statement:

Action folder (library)

​ 增:create database db1 charset utf8;

​ 查:show create database db1;

​ 改:alter database db1 charset gbk;

​ Delete: drop database db1;

Operation file (table)

​ Switch folders: use db1;

View the current folder: select database();

​ 增:create table t1(id int,name char);

​ 查:show create table t1;

​ show tables;#View all

​ desc t1;

​ 改:alter table t1 modify name char(6);

​ alter table t1 change name NAME char(7);

​Delete: drop table t1;

Operation file content (record)

​Added: insert t1(id,name)values(1,'egon1'),(2,'egon2'),(3,'egon3');#One-to-one correspondence in order

​ 查:select id,name from db2.t1;

​ select * from db2.t1;#Query all files, but not recommended

​ 改:update db2.t1 set name=‘SB’;

​ update db2.t1 set name='DUMP’where id = 2;

​ Check: delete from t1;

​ delete from t1 where id = 2;


library operations

SQL language is divided into three types:

1. DDL statement database definition language: database, table, view, index, stored procedure, such as CREATE DROP ALTER

2. DML statement database manipulation language: insert data INSERT, delete data DELETE, update data UPDATE

3. DCL statement database control language: for example, controlling user access rights GRANT and REVOKE

System database

information_schema: virtual library, which does not occupy disk space and stores some parameters after the database is started, such as user table information, column information, permission information, character information, etc.

performance_schema: Collect database server performance parameters and record various events, locks and other phenomena that occur when processing query requests.

mysql: Authorization library, mainly stores system user permission information

test: A test database automatically created by the MySQL database system

Create database

create database database name charset utf8

Rules: Available letters, numbers, underscores, @, #, &; case sensitive; unique; cannot use keywords; cannot use numbers alone; up to 128 characters (similar to python rules)

Database related operations: SQL statements in the previous section

*help xxx: You can view related operation names; usage rules, etc.


Table operations

storage engine

The storage engine is the type of table

View the storage engines supported by MySQL: show engines;

Insert image description here

Specify table type/storage engine:

create table t1(id int)engine = innodb; #The default is innodb

create table t2(id int)engine = memory;#Exists in memory, it will be gone after restarting

create table t3(id int)engine = blackhole;#Throw junk data in and it will be gone

create table t4(id int)engine = myisam;

Insert table value

insert into t1 values(1);

insert into t2 values(1);

insert into t3 values(1);

insert into t4 values(1);

View table data: select * from tx;

Add, delete, modify and query tables

Create table:

create table table name(

Field name 1 type [(width) constraint],

Field name 2 type [(width) constraint],

);#Field names in the same table cannot be the same, width and constraints are optional, field names and types must be

desc file name: view table information;

show create table t1\G;#To view the detailed structure of the table, you can add \G to facilitate display line by line.

Modify table structure

Insert image description here
Copy table

eg. create table t1 select host,user from mysql.user;#Put the data containing the host and user fields in mysql.user into the t1 table (both table structure and records are required)

create table t2 select host,user from mysql.user where 1>5;#Give a wrong logic, you can only copy the table structure but not the content.

Another method: create table t3 like mysql.user;#Can directly copy the table structure without copying the content

Delete table: drop table table name;


type of data

integer type

Insert image description here

If not specified, the default is a signed range.

create table t2(x tinyint unsigned);#bit unsigned range

The old version will directly display the maximum value if it exceeds the range, while the new version will report an error if it exceeds the range.

Insert image description here

create table t3(id int(1) unsigned);#(1)This width refers to the display width, not the storage width. The storage width is fixed as shown in the previous table.

Display width: If the amount of data exceeds the display width data, the controlled display width is invalid.
Insert image description here

floating point

float double decimal has different precisions

Guess you like

Origin blog.csdn.net/weixin_47723114/article/details/131731718