• 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

    1. mysql> create user 'slave'@'192.168.8.%' identified by 'slave_123'';
    2. Query OK, 0 rows affected (0.01 sec)
    3. mysql> grant replication slave on *.* to 'slave'@'192.168.8.%';
    4. Query OK, 0 rows affected (0.01 sec)
    5. mysql> flush privileges;
    6. Query OK, 0 rows affected (0.01 sec)
    7. mysql> flush tables with read lock;
    8. Query OK, 0 rows affected (0.00 sec)
    9. mysql> show master status;
    10. +---------------+----------+--------------+------------------+-------------------+
    11. | File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    12. +---------------+----------+--------------+------------------+-------------------+
    13. | binlog.000004 |     1198 |              |                  |                   |
    14. +---------------+----------+--------------+------------------+-------------------+
    15. 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

    1. mysql> stop slave;
    2. Query OK, 0 rows affected, 2 warnings (0.00 sec)
    3. mysql> reset slave;
    4. Query OK, 0 rows affected, 1 warning (0.00 sec)
    5. mysql> change master to
    6.     -> master_host='192.168.8.139',
    7.     -> master_user='slave',
    8.     -> master_password='slave_123',
    9.     -> master_log_file='binlog.000004',
    10.     -> master_log_pos=3296;
    11. Query OK, 0 rows affected, 8 warnings (0.02 sec)
    12. mysql> start slave;
    13. Query OK, 0 rows affected, 1 warning (0.01 sec)
    14. mysql> show slave status\G;
    3. 主服务器创建表,添加数据
    1. mysql> unlock tables;
    2. Query OK, 0 rows affected (0.01 sec)
    3. mysql> create database if not exists test charset utf8mb4;
    4. Query OK, 1 row affected (0.01 sec)
    5. mysql> use test;
    6. Database changed
    7. mysql> create table student(id int primary key,name varchar(45)not null,gender varchar(4) not null);
    8. Query OK, 0 rows affected (0.02 sec)
    9. mysql> insert into student values(1,'张三','男');
    10. Query OK, 1 row affected (0.02 sec)
    11. mysql> insert into student values(2,'李四','男');
    12. Query OK, 1 row affected (0.00 sec)
    13. mysql> insert into student values(3,'王五','男');
    14. Query OK, 1 row affected (0.00 sec)
    15. mysql> select * from student;
    16. +----+--------+--------+
    17. | id | name   | gender |
    18. +----+--------+--------+
    19. |  1 | 张三   | 男     |
    20. |  2 | 李四   | 男     |
    21. |  3 | 王五   | 男     |
    22. +----+--------+--------+
    4. 从服务器查看同步并写入东西(从服务器不容许写入东西)
    1. mysql> show databases;
    2. +--------------------+
    3. | Database           |
    4. +--------------------+
    5. | information_schema |
    6. | mysql              |
    7. | performance_schema |
    8. | sys                |
    9. | test               |
    10. +--------------------+
    11. 5 rows in set (0.00 sec)
    12. mysql> use test;
    13. Reading table information for completion of table and column names
    14. You can turn off this feature to get a quicker startup with -A
    15. Database changed
    16. mysql> select * from student;
    17. +----+--------+--------+
    18. | id | name   | gender |
    19. +----+--------+--------+
    20. |  1 | 张三   | 男     |
    21. |  2 | 李四   | 男     |
    22. |  3 | 王五   | 男     |
    23. +----+--------+--------+
    24. 3 rows in set (0.00 sec)
    25. mysql> insert into student values(4,'李网','女');
    26. Query OK, 1 row affected (0.00 sec)
    27. mysql> select * from student;
    28. +----+--------+--------+
    29. | id | name   | gender |
    30. +----+--------+--------+
    31. |  1 | 张三   | 男     |
    32. |  2 | 李四   | 男     |
    33. |  3 | 王五   | 男     |
    34. |  4 | 李网   | 女     |
    35. +----+--------+--------+
    36. 4 rows in set (0.00 sec)
    37. mysql> insert into student values(6,'章节','男');
    38. Query OK, 1 row affected (0.01 sec)
    39. mysql> select * from student;
    40. +----+--------+--------+
    41. | id | name   | gender |
    42. +----+--------+--------+
    43. |  1 | 张三   | 男     |
    44. |  2 | 李四   | 男    |
    45. |  3 | 王五  | 男     |
    46. |  6 | 章节   | 男     |
    47. +----+--------+--------+
    48. 4 rows in set (0.00 sec)
    5. 主服务器插入数据
    1. mysql> select * from student;
    2. +----+--------+--------+
    3. | id | name   | gender |
    4. +----+--------+--------+
    5. |  1 | 张三   | 男     |
    6. |  2 | 李四   | 男     |
    7. |  3 | 王五   | 男     |
    8. |  4 | 李网   | 女     |
    9. |  6 | 章节   | 男     |
    10. +----+--------+--------+
    11. 5 rows in set (0.00 sec)
    6. 主服务器查看
    1. mysql> select * from student;
    2. +----+--------+--------+
    3. | id | name   | gender |
    4. +----+--------+--------+
    5. |  1 | 张三   | 男     |
    6. |  2 | 李四   | 男     |
    7. |  3 | 王五   | 男     |
    8. |  4 | 李网   | 女     |
    9. |  6 | 章节   | 男     |
    10. +----+--------+--------+
    11. 5 rows in set (0.00 sec)

    二、SQL语句

    1、新增

            1. insert into库名称.表名

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

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

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

            4. 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. 多表查询  
    1. mysql> select count(*) from student;
    2. +----------+
    3. | count(*) |
    4. +----------+
    5. | 3 |
    6. +----------+
    7. 1 row in set (0.05 sec)
    8. mysql> select count(1) from student;
    9. +----------+
    10. | count(1) |
    11. +----------+
    12. | 3 |
    13. +----------+
    14. 1 row in set (0.06 sec)
    15. mysql> select count(id) from student;
    16. +-----------+
    17. | count(id) |
    18. +-----------+
    19. | 3 |
    20. +-----------+
    21. 1 row in set (0.06 sec)
    22. mysql> select a.*,b.* from student as a,student as b;
    23. +----+--------+--------+----+--------+--------+
    24. | id | name | gender | id | name | gender |
    25. +----+--------+--------+----+--------+--------+
    26. | 3 | 王五 || 1 | 张三 ||
    27. | 2 | 李四 || 1 | 张三 ||
    28. | 1 | 张三 || 1 | 张三 ||
    29. | 3 | 王五 || 2 | 李四 ||
    30. | 2 | 李四 || 2 | 李四 ||
    31. | 1 | 张三 || 2 | 李四 ||
    32. | 3 | 王五 || 3 | 王五 ||
    33. | 2 | 李四 || 3 | 王五 ||
    34. | 1 | 张三 || 3 | 王五 ||
    35. +----+--------+--------+----+--------+--------+
    36. 9 rows in set (0.00 sec)
    5、远程连接数据库
            1. username
            2. password
            3. url   mysql IP地址 数据库名称 端口
    1. mysql> create user 'abc'@'%' identified by '123';
    2. Query OK, 0 rows affected (0.01 sec)
    3. mysql> grant all on *.* to 'abc'@'%';
    4. Query OK, 0 rows affected (0.00 sec)
    5. mysql> flush privileges;
    6. Query OK, 0 rows affected (0.00 sec)
    6、别名
    1. mysql> select id,name,gender from student;
    2. +----+--------+--------+
    3. | id | name | gender |
    4. +----+--------+--------+
    5. | 1 | 张三 ||
    6. | 2 | 李四 ||
    7. | 3 | 王五 ||
    8. +----+--------+--------+
    9. 3 rows in set (0.00 sec)
    10. mysql> select id as 编号,name,gender from student;
    11. +--------+--------+--------+
    12. | 编号 | name | gender |
    13. +--------+--------+--------+
    14. | 1 | 张三 ||
    15. | 2 | 李四 ||
    16. | 3 | 王五 ||
    17. +--------+--------+--------+
    18. 3 rows in set (0.00 sec)

    三、MySQL 函数

    数据分析的基础

    1、排序
            1. max
            2. min
    1. mysql> select max(price) from product group by name;
    2. +------------+
    3. | max(price) |
    4. +------------+
    5. | 8.5 |
    6. | 12.5 |
    7. | 12.4 |
    8. | 18.3 |
    9. +------------+
    10. 4 rows in set (0.00 sec)
    11. mysql> select max(price) from product;
    12. +------------+
    13. | max(price) |
    14. +------------+
    15. | 18.3 |
    16. +------------+
    17. 1 row in set (0.00 sec)
    18. mysql> select min(price) from product;
    19. +------------+
    20. | min(price) |
    21. +------------+
    22. | 8.5 |
    23. +------------+
    24. 1 row in set (0.00 sec)
    2、汇总
            1. count
            2. sum
            3. avg
    1. mysql> select sum(price) from product;
    2. +-------------------+
    3. | sum(price) |
    4. +-------------------+
    5. | 51.69999885559082 |
    6. +-------------------+
    7. 1 row in set (0.00 sec)
    8. mysql> select avg(price) from product;
    9. +--------------------+
    10. | avg(price) |
    11. +--------------------+
    12. | 12.924999713897705 |
    13. +--------------------+
    14. 1 row in set (0.00 sec)
    15. mysql> select *,price*qty as tt from product;
    16. +----+-----------+-------+-----+-------------------+
    17. | id | name | price | qty | tt |
    18. +----+-----------+-------+-----+-------------------+
    19. | 1 | 香蕉 | 8.5 | 200 | 1700 |
    20. | 2 | 苹果 | 12.5 | 400 | 5000 |
    21. | 3 | 菠萝 | 12.4 | 70 | 867.9999732971191 |
    22. | 4 | 哈密瓜 | 18.3 | 400 | 7319.999694824219 |
    23. +----+-----------+-------+-----+-------------------+
    24. 4 rows in set (0.00 sec)
    25. mysql> select sum(tt) from (select *,price*qty as tt from productt) as a;
    26. +--------------------+
    27. | sum(tt) |
    28. +--------------------+
    29. | 14887.999668121338 |
    30. +--------------------+
    31. 1 row in set (0.00 sec)
    3、数制

            1. 二进制

            2. 八进制

            3. 十进制

            4. 十六进制

            5. AA 27

    1. mysql> select * from student order by gender desc;
    2. +----+--------+--------+
    3. | id | name | gender |
    4. +----+--------+--------+
    5. | 1 | 张三 ||
    6. | 2 | 李四 ||
    7. | 3 | 王五 ||
    8. | 4 | 花花 ||
    9. | 5 |||
    10. +----+--------+--------+
    11. 5 rows in set (0.00 sec)
    12. mysql> select * from student order by gender asc;
    13. +----+--------+--------+
    14. | id | name | gender |
    15. +----+--------+--------+
    16. | 4 | 花花 ||
    17. | 5 |||
    18. | 1 | 张三 ||
    19. | 2 | 李四 ||
    20. | 3 | 王五 ||
    21. +----+--------+--------+
    22. 5 rows in set (0.00 sec)
    4、聚合函数

    只有select子句和having子句、order by子句中能使用聚合函数,where子句不能使用聚合函
    数。当使用聚合函数查询以后,不能使用where条件,如果要添加条件,

    1. mysql> select gender,count(gender) from student group by gender;
    2. +--------+---------------+
    3. | gender | count(gender) |
    4. +--------+---------------+
    5. || 3 |
    6. || 2 |
    7. +--------+---------------+
    8. 2 rows in set (0.01 sec)
    9. mysql> select gender as 性别,count(gender) as 人数 from studentgroup by gender;
    10. +--------+--------+
    11. | 性别 | 人数 |
    12. +--------+--------+
    13. || 3 |
    14. || 2 |
    15. +--------+--------+
    16. 2 rows in set (0.00 sec)
    1. mysql> create table product(
    2. -> id int primary key auto_increment,
    3. -> name varchar(45) not null,
    4. -> price float not null,
    5. -> qty int not null);
    6. Query OK, 0 rows affected (0.01 sec)
    7. mysql> desc product;
    8. +-------+-------------+------+-----+---------+----------------+
    9. | Field | Type | Null | Key | Default | Extra |
    10. +-------+-------------+------+-----+---------+----------------+
    11. | id | int | NO | PRI | NULL | auto_increment |
    12. | name | varchar(45) | NO | | NULL | |
    13. | price | float | NO | | NULL | |
    14. | qty | int | NO | | NULL | |
    15. +-------+-------------+------+-----+---------+----------------+
    16. 4 rows in set (0.01 sec)
    17. mysql> insert into product (name,price,qty) values("香蕉",8.5,200);
    18. Query OK, 1 row affected (0.01 sec)
    19. mysql> insert into product (name,price,qty) values("苹果",12.5,4000);
    20. Query OK, 1 row affected (0.00 sec)
    21. mysql> insert into product (name,price,qty) values("菠萝",12.4,700);
    22. Query OK, 1 row affected (0.00 sec)
    23. mysql> insert into product (name,price,qty) values("哈密瓜",18.3,,400);
    24. Query OK, 1 row affected (0.00 sec)
    25. mysql> select * from product;
    26. +----+-----------+-------+-----+
    27. | id | name | price | qty |
    28. +----+-----------+-------+-----+
    29. | 1 | 香蕉 | 8.5 | 200 |
    30. | 2 | 苹果 | 12.5 | 400 |
    31. | 3 | 菠萝 | 12.4 | 70 |
    32. | 4 | 哈密瓜 | 18.3 | 400 |
    33. +----+-----------+-------+-----+
    34. 4 rows in set (0.00 sec)
    35. mysql> select * from product order by qty;
    36. +----+-----------+-------+-----+
    37. | id | name | price | qty |
    38. +----+-----------+-------+-----+
    39. | 3 | 菠萝 | 12.4 | 70 |
    40. | 1 | 香蕉 | 8.5 | 200 |
    41. | 2 | 苹果 | 12.5 | 400 |
    42. | 4 | 哈密瓜 | 18.3 | 400 |
    43. +----+-----------+-------+-----+
    44. 4 rows in set (0.00 sec)
    45. mysql> select * from product order by price;
    46. +----+-----------+-------+-----+
    47. | id | name | price | qty |
    48. +----+-----------+-------+-----+
    49. | 1 | 香蕉 | 8.5 | 200 |
    50. | 3 | 菠萝 | 12.4 | 70 |
    51. | 2 | 苹果 | 12.5 | 400 |
    52. | 4 | 哈密瓜 | 18.3 | 400 |
    53. +----+-----------+-------+-----+
    54. 4 rows in set (0.00 sec)
    55. mysql> select * from (select * from product order by qty) as a oerder by a.price;
    56. +----+-----------+-------+-----+
    57. | id | name | price | qty |
    58. +----+-----------+-------+-----+
    59. | 1 | 香蕉 | 8.5 | 200 |
    60. | 3 | 菠萝 | 12.4 | 70 |
    61. | 2 | 苹果 | 12.5 | 400 |
    62. | 4 | 哈密瓜 | 18.3 | 400 |
    63. +----+-----------+-------+-----+
    64. 4 rows in set (0.00 sec)

     

    1. mysql> select now();
    2. +---------------------+
    3. | now() |
    4. +---------------------+
    5. | 2024-08-07 15:30:50 |
    6. +---------------------+
    7. 1 row in set (0.00 sec)
    8. mysql> select year(now());
    9. +-------------+
    10. | year(now()) |
    11. +-------------+
    12. | 2024 |
    13. +-------------+
    14. 1 row in set (0.00 sec)
    15. mysql> select second(now());
    16. +---------------+
    17. | second(now()) |
    18. +---------------+
    19. | 13 |
    20. +---------------+
    21. 1 row in set (0.00 sec)
    22. mysql> insert into product (name,price,qty) values (now(),7.8,90)
    23. );
    24. Query OK, 1 row affected (0.00 sec)
    25. mysql> select * from product;
    26. +----+---------------------+-------+-----+
    27. | id | name | price | qty |
    28. +----+---------------------+-------+-----+
    29. | 1 | 香蕉 | 8.5 | 200 |
    30. | 2 | 苹果 | 12.5 | 400 |
    31. | 3 | 菠萝 | 12.4 | 70 |
    32. | 4 | 哈密瓜 | 18.3 | 400 |
    33. | 5 | 2024-08-07 15:33:08 | 7.8 | 90 |
    34. +----+---------------------+-------+-----+
    35. 5 rows in set (0.00 sec)

  • 相关阅读:
    数字人民币如何将支付宝钱包余额转入到微信支付钱包余额?
    Git基础
    Docker技术概论(2):Docker环境的搭建
    python函数运行加速
    硬件管理平台-硬件产品库-硬件项目
    openstack 环境配置及基础组件安装
    【Hadoop快速入门】Hdfs、MapReduce、Yarn
    受害者被锤 法官遭殃 背后的它公关赢了?
    深入理解Kafka分区副本机制
    【深度学习】04-01-自注意力机制(Self-attention)-李宏毅老师21&22深度学习课程笔记
  • 原文地址:https://blog.csdn.net/weixin_70751278/article/details/140973850