Chapter Four · MySQL client tools and SQL explain

I. Introduction client command

1.mysql

1, for connection management database

  • 1) is connected (omitted)
  • 2) Management:
#MySQL接口自带的命令
\h 或 help 或?      查看帮助
\G                  格式化查看数据(key:value)
\T 或 tee            记录日志
\c(5.7可以ctrl+c)   结束命令
\s 或 status         查看状态信息
\. 或 source         导入SQL数据
\u或 use             使用数据库
\q 或 exit 或 quit   退出
  • 3) receive the user's SQL statement

2, sends the user's SQL statements to the server

2.mysqladmin

1, the command-line management tool

3.mysqldump

Contents 1, the backup database and tables

Use 4.help command

mysql> help
mysql> help contents
mysql> help select
mysql> help create
mysql> help create user
mysql> help status
mysql> help show

Use 5.source command

#在MySQL中处理输入文件:
#如果这些文件包含SQL语句则称为:
#1.脚本文件
#2.批处理文件
mysql> SOURCE /data/mysql/world.sql
#或者使用非交互式
mysql</data/mysql/world.sql

Use 6.mysqladmin command

01)“强制回应 (Ping)”服务器。
02)关闭服务器。
03)创建和删除数据库。
04)显示服务器和版本信息。
05)显示或重置服务器状态变量。
06)设置口令。
07)重新刷新授权表。
08)刷新日志文件和高速缓存。
09)启动和停止复制。
10)显示客户机信息。
#查看MySQL存活状态
[root@db01 ~]# mysqladmin -uroot -p123 ping
#查看MySQL状态信息
[root@db01 ~]# mysqladmin -uroot -p123 status
#关闭MySQL进程
[root@db01 ~]# mysqladmin -uroot -p123 shutdown
#查看MySQL参数
[root@db01 ~]# mysqladmin -uroot -p123 variables
#删除数据库
[root@db01 ~]# mysqladmin -uroot -p123 drop DATABASE
#创建数据库
[root@db01 ~]# mysqladmin -uroot -p123 create DATABASE
#重载授权表
[root@db01 ~]# mysqladmin -uroot -p123 reload
#刷新日志
[root@db01 ~]# mysqladmin -uroot -p123 flush-log
#刷新缓存主机
[root@db01 ~]# mysqladmin -uroot -p123 reload
#修改口令
[root@db01 ~]# mysqladmin -uroot -p123 password

II. Receives the user's SQL statement

1. What is SQL

Structured Query Language

2.SQL species

2.1 DDL: Data Definition Language

Object Library: library name, the library property
development specifications: library name lowercase

2.1.1 Create a library: create database | schema

#创建oldboy数据库
mysql> create database oldboy;
#创建OLDBOY数据库
mysql> create database OLDBOY;
#查看数据库
mysql> show databases;
#查看oldboy的创建语句(DQL)
mysql> show create database oldboy;
#查看创建数据库语句帮助
mysql> help create database
#创建oldboy数据库添加属性
mysql> create database testa charset utf8;

2.1.2 delete library: drop database

#删除oldboy数据库
mysql> drop database oldboy;

2.1.3 modify the definition library: alter database

#修改oldboy数据库属性
mysql> alter database oldboy charset gbk;
#查看oldboy的创建语句(DQL)
mysql> show create database oldboy;

Table objects: Column name, column property, constraints

2.1.4 Create a table: create table (developers do)

#查看创建表语句帮助
mysql> help create table
#创建表
mysql> create table student(
sid INT,
sname VARCHAR(20),
sage TINYINT,
sgender ENUM('m','f'),
cometime DATETIME);

2.1.5 Data Types

int: 整数 -231 ~ 231 -1
varchar:字符类型 (变长)
char: 字符类型 (定长)
tinyint: 整数 -128 ~ 128
enum: 枚举类型
datetime: 时间类型 年月日时分秒
#创建表加其他属性
mysql> create table student(
sid INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT ‘学号’,
sname VARCHAR(20) NOT NULL COMMENT ‘学生姓名’,
sage TINYINT UNSIGNED COMMENT ‘学生年龄’,
sgender ENUM('m','f')  NOT NULL DEFAULT ‘m’ COMMENT ‘学生性别’,
cometime DATETIME NOT NULL COMMENT ‘入学时间’)chatset utf8 engine innodb;
#查看建表语句
mysql> show create table student;
#查看表
mysql> show tables;
#查看表中列的定义信息
mysql> desc student;

2.1.6 Data Properties

not null: 非空
primary key: 主键(唯一且非空的)
auto_increment: 自增(此列必须是:primary key或者unique key)
unique key: 单独的唯一的
default: 默认值
unsigned: 非负数
comment: 注释

2.1.7 Delete table

#删除表
mysql> drop table student;

2.1.8 modify a table definition: alter table (developers do)

#修改表名
mysql> alter table student rename stu;
#添加列和列定义
mysql> alter table stu add age int;
#添加多个列
mysql> alter table stu add test varchar(20),add qq int;
#指定位置进行添加列(表首)
mysql> alter table stu add classid varchar(20) first;
#指定位置进行添加列(指定列)
mysql> alter table stu add phone int after age;
#删除指定的列及定义
mysql> alter table stu drop qq;
#修改列及定义(列属性)
mysql> alter table stu modify sid varchar(20);
#修改列及定义(列名及属性)
mysql> alter table stu change phone telphone char(20);

2.2 DCL: Data Control Language

Control permissions

2.2.1 grant

#授权[email protected]用户所有权限(非炒鸡管理员)
mysql> grant all on *.* to root@'10.0.0.51' identified by 'oldboy123';
#怎么去授权一个炒鸡管理员呢?
mysql> grant all on *.* to root@'10.0.0.51' identified by 'oldboy123' with grant option;
#其他参数(扩展)
max_queries_per_hour:一个用户每小时可发出的查询数量
max_updates_per_hour:一个用户每小时可发出的更新数量
max_connetions_per_hour:一个用户每小时可连接到服务器的次数
max_user_connetions:允许同时连接数量

2.2.2 revoke

#收回select权限
mysql> revoke select on *.* from root@'10.0.0.51';
#查看权限
mysql> show grants for root@'10.0.0.51';

2.3 DML: Data Manipulation Language

Line information table operation data

2.3.1 insert

#基础用法,插入数据
mysql> insert into stu values('linux01',1,NOW(),'zhangsan',20,'m',NOW(),110,123456);
#规范用法,插入数据
mysql> insert into stu(classid,birth.sname,sage,sgender,comtime,telnum,qq) values('linux01',1,NOW(),'zhangsan',20,'m',NOW(),110,123456);
#插入多条数据
mysql> insert into stu(classid,birth.sname,sage,sgender,comtime,telnum,qq) values('linux01',1,NOW(),'zhangsan',20,'m',NOW(),110,123456),
('linux02',2,NOW(),'zhangsi',21,'f',NOW(),111,1234567);

2.3.2 update

#不规范
mysql> update student set sgender='f';
#规范update修改
mysql> update student set sgender='f' where sid=1;
#如果非要全表修改
mysql> update student set sgender='f' where 1=1;

2.3.3 delete

#不规范
mysql> delete from student;
#规范删除(危险)
mysql> delete from student where sid=3;
#DDL删除表
mysql> truncate table student;
  • 1, the use of pseudo-deleted
    using update instead of delete

1) adding an extra status bar

mysql> alter table student add status enum(1,0) default 1;

2) Use update

mysql> update student set status='0' where sid=1;

3) the existence of application data query

mysql> select * from student where status=1;
  • 2, using a trigger (understand)
trigger

2.4 DQL: Data Query Language

select: Basic usage

#常用用法
mysql> select countrycode,district from city;
#查询单列
mysql> select countrycode from city;
#行级查询
mysql> select countrycode,district from city limit 2;
mysql> select id,countrycode,district from city limit 2,2;
#条件查询
mysql> select name,population from city where countrycode='CHN';
#多条件查询
mysql> select name,population from city where countrycode='CHN' and district='heilongjiang';
#模糊查询
mysql> select name,population,countrycode from city where countrycode like '%H%' limit 10;
#排序查询(顺序)
mysql> select id,name,population,countrycode from city order by countrycode limit 10;
#排序查询(倒叙)
mysql> select id,name,population,countrycode from city order by countrycode desc limit 10;
#范围查询(>,<,>=,<=,<>)
mysql> select * from city where population>=1410000;
#范围查询OR语句
mysql> select * from city where countrycode='CHN' or countrycode='USA';
#范围查询IN语句
mysql> select * from city where countrycode in ('CHN','USA');

III. Character set definition

1. What is the character set (Charset)

Character sets: the set of all abstract character of a system of support. Character is the general term for a variety of text and symbols, including the national characters, punctuation marks, graphic symbols, numbers, and so on.

2.MySQL database character set

1)字符集(CHARACTER)
2)校对规则(COLLATION)

3.MySQL common character set

1)UTF8
2)LATIN1
3)GBK

4. Common collation

1)ci:大小写不敏感
2)cs或bin:大小写敏感

5. We can use the following command to view

mysql> show charset;
mysql> show collation;

IV. Character Set

1. The operating system level

[root@db01 ~]# source /etc/sysconfig/i18n
[root@db01 ~]# echo $LANG
zh_CN.UTF-8

2. The client operating system level (SSH)

3.MySQL instance level

Method 1: the designation of the character set as the server installed at compile time.

cmake . 
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \

Method 2: Set the character set in the configuration file

[mysqld]
character-set-server=utf8

4. building a database level

mysql> create database oldboy charset utf8 default collate = utf8_general_ci;

The construction of the table level

mysql>  CREATE TABLE `test` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

Thinking: how to deal with inappropriate if in a production environment, not enough character set or the character set?

production environment changes to the database (including data) character set method

mysql> alter database oldboy CHARACTER SET utf8 collate utf8_general_ci;
mysql> alter table t1 CHARACTER SET utf8;

Five .select Advanced usage (Extended)

1. multi-table join query (even-table query)

set:

[zhang3,li4,wang5]

[50,70,80]

t1:

sid 1 2 3
sname zhang3 li4 wang5

t2:

sid 1 2 3
mark 50 70 80

Paradigm: reduce data redundancy and prevent consistency problems, as an atom to a table, the table to a split can not be split up. (Design specification development stage)

Example: Joe Smith found the results based on the contents of the two tables

select t1.sname,t2.mark from t1,t2 where t1.sid=t2.sid and t1.sname=’zhang3’;

1.1 the conventional connector (the connector can only be intersected)

#世界上小于100人的人口城市是哪个国家的?
select city.name,city.countrycode,country.name 
from city,country 
where city.countrycode=country.code 
and city.population<100;

1.2 NATURAL JOIN (from joined tables have a common column name)

SELECT city.name,city.countrycode ,countrylanguage.language ,city.population
FROM  city NATURAL  JOIN  countrylanguage 
WHERE population > 1000000
ORDER BY population;

1.3 enterprise multi-table join query (en)

select city.name,city.countrycode,country.name 
from city join country on city.countrycode=country.code 
where city.population<100;

Recommendation: use the join statement, the former small table, a large table in the post.

1.4 an outer connector

select city.name,city.countrycode,country.name 
from city left join country 
on city.countrycode=country.code 
and city.population<100;

1.5 UNION (merger inquiry)

#范围查询OR语句
mysql> select * from city where countrycode='CHN' or countrycode='USA';
#范围查询IN语句
mysql> select * from city where countrycode in ('CHN','USA');
替换为:
mysql> select * from city where countrycode='CHN' 
union  all
select * from city where countrycode='USA' limit 10
union:去重复合并
union all :不去重复
使用情况:union<union all

Guess you like

Origin www.cnblogs.com/Forever-x/p/10992551.html