2024.8.7(SQL语句)

一、回顾

1、主服务器

[root@slave-mysql ~]# yum -y install rsync
[root@master-mysql ~]# yum -y install rsync

[root@master-mysql ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar
[root@master-mysql ~]# ls
[root@master-mysql ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz
[root@master-mysql ~]# ls
[root@master-mysql ~]# vim mysql-8.0.33-linux-glibc2.12-x86_64/support-files/mysql.server
[root@master-mysql ~]# cp -r mysql-8.0.33-linux-glibc2.12-x86_64 /usr/local/mysql
[root@master-mysql ~]# cd /usr/local/mysql/
[root@master-mysql mysql]# ls


[root@master-mysql mysql]# mkdir mysql-files
[root@master-mysql mysql]# ll

[root@master-mysql mysql]# useradd -r -s /sbin/nologin mysql
[root@master-mysql mysql]# id mysql
uid=997(mysql) gid=995(mysql) 组=995(mysql)
[root@master-mysql mysql]# chown mysql:mysql ./mysql-files/
[root@master-mysql mysql]# ll

[root@master-mysql mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql
[root@master-mysql mysql]# ls

[root@master-mysql mysql]# ./bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data
[root@master-mysql mysql]# cp support-files/mysql.server /etc/init.d/mysql8
[root@master-mysql mysql]# vim my.cnf


[root@master-mysql mysql]# service mysql8 start

[root@master-mysql mysql]# ./bin/mysql -P 3306 -p

[root@master-mysql mysql]# vim my.cnf

[mysqld]
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
port=3306
log-error=/usr/local/mysql/data/err.log
log-bin=/usr/local/mysql/data/binlog
charactor_set_server=utf8mb4

server_id=10

[root@master-mysql mysql]# service mysql8 restart
[root@master-mysql mysql]# ls data

2、从服务器(不用初始化)

[root@slave-mysql ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar
[root@slave-mysql ~]# tar -xf mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz
[root@slave-mysql ~]# cp -r mysql-8.0.33-linux-glibc2.12-x86_64 /usr/local/mysql
[root@slave-mysql ~]# useradd -r -s /sbin/nologin mysql
[root@slave-mysql ~]# mkdir /usr/local/mysql/mysql-files
[root@slave-mysql ~]# chown mysql:mysql /usr/local/mysql/mysql-files/
[root@slave-mysql ~]# ll /usr/local/mysql/


[root@slave-mysql ~]# rm -rf /etc/my.cnf
[root@slave-mysql ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql8

3、主从同步

[root@master-mysql mysql]# rm -rf /usr/local/mysql/data/auto.cnf

[root@master-mysql mysql]# ls /usr/local/mysql/data/
[root@master-mysql mysql]# rsync -av /usr/local/mysql/data root@192.168.8.128:/usr/local/mysql/

[root@slave-mysql ~]# vim /usr/local/mysql/data/my.cnf


[root@slave-mysql ~]# service mysql8 start

[root@slave-mysql ~]# /usr/local/mysql/bin/mysql -pN6s9znjbL*/s

4、主从实现
1. 主服务器

[root@master-mysql mysql]# vim /etc/profile


[root@master-mysql mysql]# source /etc/profile

[root@master-mysql mysql]# mysql -p

sql 复制代码
mysql> create user 'slave'@'192.168.8.%' identified by 'slave_123'';

Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave on *.* to 'slave'@'192.168.8.%';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000004 |     1198 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
2. 从服务器

[root@slave-mysql ~]# vim /etc/profile


[root@slave-mysql ~]# source /etc/profile

[root@slave-mysql ~]# mysql -h192.168.8.139 -uslave -pslave_123 --get-server-public-key

[root@slave-mysql ~]# mysql -p123456 -P3306

sql 复制代码
mysql> stop slave;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> reset slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to
    -> master_host='192.168.8.139',
    -> master_user='slave',
    -> master_password='slave_123',
    -> master_log_file='binlog.000004',
    -> master_log_pos=3296;
Query OK, 0 rows affected, 8 warnings (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> show slave status\G;
3. 主服务器创建表,添加数据
sql 复制代码
mysql> unlock tables;
Query OK, 0 rows affected (0.01 sec)
mysql> create database if not exists test charset utf8mb4;
Query OK, 1 row affected (0.01 sec)

mysql> use test;
Database changed
mysql> create table student(id int primary key,name varchar(45)not null,gender varchar(4) not null);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into student values(1,'张三','男');
Query OK, 1 row affected (0.02 sec)

mysql> insert into student values(2,'李四','男');
Query OK, 1 row affected (0.00 sec)

mysql> insert into student values(3,'王五','男');
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;
+----+--------+--------+
| id | name   | gender |
+----+--------+--------+
|  1 | 张三   | 男     |
|  2 | 李四   | 男     |
|  3 | 王五   | 男     |
+----+--------+--------+
4. 从服务器查看同步并写入东西(从服务器不容许写入东西)
sql 复制代码
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from student;
+----+--------+--------+
| id | name   | gender |
+----+--------+--------+
|  1 | 张三   | 男     |
|  2 | 李四   | 男     |
|  3 | 王五   | 男     |
+----+--------+--------+
3 rows in set (0.00 sec)

mysql> insert into student values(4,'李网','女');
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;
+----+--------+--------+
| id | name   | gender |
+----+--------+--------+
|  1 | 张三   | 男     |
|  2 | 李四   | 男     |
|  3 | 王五   | 男     |
|  4 | 李网   | 女     |
+----+--------+--------+
4 rows in set (0.00 sec)

mysql> insert into student values(6,'章节','男');
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+--------+--------+
| id | name   | gender |
+----+--------+--------+
|  1 | 张三   | 男     |
|  2 | 李四   | 男    |
|  3 | 王五  | 男     |
|  6 | 章节   | 男     |
+----+--------+--------+
4 rows in set (0.00 sec)
5. 主服务器插入数据
sql 复制代码
mysql> select * from student;
+----+--------+--------+
| id | name   | gender |
+----+--------+--------+
|  1 | 张三   | 男     |
|  2 | 李四   | 男     |
|  3 | 王五   | 男     |
|  4 | 李网   | 女     |
|  6 | 章节   | 男     |
+----+--------+--------+
5 rows in set (0.00 sec)
6. 主服务器查看
sql 复制代码
mysql> select * from student;
+----+--------+--------+
| id | name   | gender |
+----+--------+--------+
|  1 | 张三   | 男     |
|  2 | 李四   | 男     |
|  3 | 王五   | 男     |
|  4 | 李网   | 女     |
|  6 | 章节   | 男     |
+----+--------+--------+
5 rows in set (0.00 sec)

二、SQL语句

1、新增
  1. insert into库名称.表名

(id,username,password)values(1,"abc","123")

  1. insert into 表名称 values(1, "name","word")

  2. insert into 表名称 select* from 其他表

  3. insert into 表 values (),()

2、删除
  1. delete from 表名

  2. deletefrom表名称 where id=3

  3. delete from 表名称 where age>8

  4. delete from 表 where name on ("a","b","c")

3、修改
  1. update mysql.user set host='%' where name='root'

  2. update user set password='abc' where username="zhangsan"

4、查询
1. 单表查询

1.1 select 字段名列表 from表名称,索引

2. 多表查询
sql 复制代码
mysql> select count(*) from student;
+----------+
| count(*) |
+----------+
|        3 |
+----------+
1 row in set (0.05 sec)

mysql> select count(1) from student;
+----------+
| count(1) |
+----------+
|        3 |
+----------+
1 row in set (0.06 sec)

mysql> select count(id) from student;
+-----------+
| count(id) |
+-----------+
|         3 |
+-----------+
1 row in set (0.06 sec)

mysql> select a.*,b.* from student as a,student as b;
+----+--------+--------+----+--------+--------+
| id | name   | gender | id | name   | gender |
+----+--------+--------+----+--------+--------+
|  3 | 王五   | 男     |  1 | 张三   | 男     |
|  2 | 李四   | 男     |  1 | 张三   | 男     |
|  1 | 张三   | 男     |  1 | 张三   | 男     |
|  3 | 王五   | 男     |  2 | 李四   | 男     |
|  2 | 李四   | 男     |  2 | 李四   | 男     |
|  1 | 张三   | 男     |  2 | 李四   | 男     |
|  3 | 王五   | 男     |  3 | 王五   | 男     |
|  2 | 李四   | 男     |  3 | 王五   | 男     |
|  1 | 张三   | 男     |  3 | 王五   | 男     |
+----+--------+--------+----+--------+--------+
9 rows in set (0.00 sec)
5、远程连接数据库
1. username
2. password
3. url mysql IP地址 数据库名称 端口
sql 复制代码
mysql> create user 'abc'@'%' identified by '123';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all on *.* to 'abc'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
6、别名
sql 复制代码
mysql> select id,name,gender from student;
+----+--------+--------+
| id | name   | gender |
+----+--------+--------+
|  1 | 张三   | 男     |
|  2 | 李四   | 男     |
|  3 | 王五   | 男     |
+----+--------+--------+
3 rows in set (0.00 sec)

mysql> select id as 编号,name,gender from student;
+--------+--------+--------+
| 编号   | name   | gender |
+--------+--------+--------+
|      1 | 张三   | 男     |
|      2 | 李四   | 男     |
|      3 | 王五   | 男     |
+--------+--------+--------+
3 rows in set (0.00 sec)

三、MySQL 函数

数据分析的基础

1、排序
1. max
2. min
sql 复制代码
mysql> select max(price) from product group by name;
+------------+
| max(price) |
+------------+
|        8.5 |
|       12.5 |
|       12.4 |
|       18.3 |
+------------+
4 rows in set (0.00 sec)

mysql> select max(price) from product;
+------------+
| max(price) |
+------------+
|       18.3 |
+------------+
1 row in set (0.00 sec)

mysql> select min(price) from product;
+------------+
| min(price) |
+------------+
|        8.5 |
+------------+
1 row in set (0.00 sec)
2、汇总
1. count
2. sum
3. avg
sql 复制代码
mysql> select sum(price) from product;
+-------------------+
| sum(price)        |
+-------------------+
| 51.69999885559082 |
+-------------------+
1 row in set (0.00 sec)

mysql> select avg(price) from product;
+--------------------+
| avg(price)         |
+--------------------+
| 12.924999713897705 |
+--------------------+
1 row in set (0.00 sec)

mysql> select *,price*qty as tt from product;
+----+-----------+-------+-----+-------------------+
| id | name      | price | qty | tt                |
+----+-----------+-------+-----+-------------------+
|  1 | 香蕉      |   8.5 | 200 |              1700 |
|  2 | 苹果      |  12.5 | 400 |              5000 |
|  3 | 菠萝      |  12.4 |  70 | 867.9999732971191 |
|  4 | 哈密瓜    |  18.3 | 400 | 7319.999694824219 |
+----+-----------+-------+-----+-------------------+
4 rows in set (0.00 sec)

mysql> select sum(tt) from (select *,price*qty as tt from productt) as a;
+--------------------+
| sum(tt)            |
+--------------------+
| 14887.999668121338 |
+--------------------+
1 row in set (0.00 sec)
3、数制
  1. 二进制

  2. 八进制

  3. 十进制

  4. 十六进制

  5. AA 27

sql 复制代码
mysql> select * from student order by gender desc;
+----+--------+--------+
| id | name   | gender |
+----+--------+--------+
|  1 | 张三   | 男     |
|  2 | 李四   | 男     |
|  3 | 王五   | 男     |
|  4 | 花花   | 女     |
|  5 | 花     | 女     |
+----+--------+--------+
5 rows in set (0.00 sec)

mysql> select * from student order by gender asc;
+----+--------+--------+
| id | name   | gender |
+----+--------+--------+
|  4 | 花花   | 女     |
|  5 | 花     | 女     |
|  1 | 张三   | 男     |
|  2 | 李四   | 男     |
|  3 | 王五   | 男     |
+----+--------+--------+
5 rows in set (0.00 sec)
4、聚合函数

只有select子句和having子句、order by子句中能使用聚合函数,where子句不能使用聚合函

数。当使用聚合函数查询以后,不能使用where条件,如果要添加条件,

sql 复制代码
mysql> select gender,count(gender) from student group by gender;

+--------+---------------+
| gender | count(gender) |
+--------+---------------+
| 男     |             3 |
| 女     |             2 |
+--------+---------------+
2 rows in set (0.01 sec)

mysql> select gender as 性别,count(gender) as 人数 from studentgroup by gender;
+--------+--------+
| 性别   | 人数   |
+--------+--------+
| 男     |      3 |
| 女     |      2 |
+--------+--------+
2 rows in set (0.00 sec)
sql 复制代码
mysql> create table product(
    -> id int primary key auto_increment,
    -> name varchar(45) not null,
    -> price float not null,
    -> qty int not null);
Query OK, 0 rows affected (0.01 sec)

mysql> desc product;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int         | NO   | PRI | NULL    | auto_increment |
| name  | varchar(45) | NO   |     | NULL    |                |
| price | float       | NO   |     | NULL    |                |
| qty   | int         | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

mysql> insert into product (name,price,qty) values("香蕉",8.5,200);
Query OK, 1 row affected (0.01 sec)

mysql> insert into product (name,price,qty) values("苹果",12.5,4000);
Query OK, 1 row affected (0.00 sec)

mysql> insert into product (name,price,qty) values("菠萝",12.4,700);
Query OK, 1 row affected (0.00 sec)

mysql> insert into product (name,price,qty) values("哈密瓜",18.3,,400);
Query OK, 1 row affected (0.00 sec)

mysql> select * from product;
+----+-----------+-------+-----+
| id | name      | price | qty |
+----+-----------+-------+-----+
|  1 | 香蕉      |   8.5 | 200 |
|  2 | 苹果      |  12.5 | 400 |
|  3 | 菠萝      |  12.4 |  70 |
|  4 | 哈密瓜    |  18.3 | 400 |
+----+-----------+-------+-----+
4 rows in set (0.00 sec)

mysql> select * from product order by qty;
+----+-----------+-------+-----+
| id | name      | price | qty |
+----+-----------+-------+-----+
|  3 | 菠萝      |  12.4 |  70 |
|  1 | 香蕉      |   8.5 | 200 |
|  2 | 苹果      |  12.5 | 400 |
|  4 | 哈密瓜    |  18.3 | 400 |
+----+-----------+-------+-----+
4 rows in set (0.00 sec)

mysql> select * from product order by price;
+----+-----------+-------+-----+
| id | name      | price | qty |
+----+-----------+-------+-----+
|  1 | 香蕉      |   8.5 | 200 |
|  3 | 菠萝      |  12.4 |  70 |
|  2 | 苹果      |  12.5 | 400 |
|  4 | 哈密瓜    |  18.3 | 400 |
+----+-----------+-------+-----+
4 rows in set (0.00 sec)
mysql> select * from (select * from product order by qty) as a oerder by a.price;
+----+-----------+-------+-----+
| id | name      | price | qty |
+----+-----------+-------+-----+
|  1 | 香蕉      |   8.5 | 200 |
|  3 | 菠萝      |  12.4 |  70 |
|  2 | 苹果      |  12.5 | 400 |
|  4 | 哈密瓜    |  18.3 | 400 |
+----+-----------+-------+-----+
4 rows in set (0.00 sec)
sql 复制代码
mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-08-07 15:30:50 |
+---------------------+
1 row in set (0.00 sec)

mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
|        2024 |
+-------------+
1 row in set (0.00 sec)

mysql> select second(now());
+---------------+
| second(now()) |
+---------------+
|            13 |
+---------------+
1 row in set (0.00 sec)

mysql> insert into product (name,price,qty) values (now(),7.8,90)
);
Query OK, 1 row affected (0.00 sec)

mysql> select * from product;
+----+---------------------+-------+-----+
| id | name                | price | qty |
+----+---------------------+-------+-----+
|  1 | 香蕉                |   8.5 | 200 |
|  2 | 苹果                |  12.5 | 400 |
|  3 | 菠萝                |  12.4 |  70 |
|  4 | 哈密瓜              |  18.3 | 400 |
|  5 | 2024-08-07 15:33:08 |   7.8 |  90 |
+----+---------------------+-------+-----+
5 rows in set (0.00 sec)
相关推荐
BeyondESH2 分钟前
Linux线程同步—竞态条件和互斥锁(C语言)
linux·服务器·c++
hanniuniu134 分钟前
详细解读,F5服务器负载均衡的技术优势
运维·服务器·负载均衡
鱼饼6号18 分钟前
Prometheus 上手指南
linux·运维·centos·prometheus
Asher Gu24 分钟前
Linux系统编程入门 | 模拟实现 ls -l 命令
linux
c无序41 分钟前
【Linux进程控制】进程程序替换
linux
m0_609000422 小时前
向日葵好用吗?4款稳定的远程控制软件推荐。
运维·服务器·网络·人工智能·远程工作
小安运维日记3 小时前
Linux云计算 |【第四阶段】NOSQL-DAY1
linux·运维·redis·sql·云计算·nosql
CoolTiger、6 小时前
【Vmware16安装教程】
linux·虚拟机·vmware16
m0_741768856 小时前
使用docker的小例子
运维·docker·容器
学习3人组7 小时前
CentOS 中配置 OpenJDK以及多版本管理
linux·运维·centos