一、回顾
1、主服务器
2、从服务器(不用初始化)
3、主从同步
4、主从实现
1. 主服务器
2. 从服务器
3. 主服务器创建表,添加数据
4. 从服务器查看同步并写入东西(从服务器不容许写入东西)
5. 主服务器插入数据
6. 主服务器查看
二、SQL语句
1、新增
2、删除
3、修改
4、查询
1. 单表查询
1.1 select 字段名列表 from表名称,索引
2. 多表查询
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地址 数据库名称 端口
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、别名
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
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
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
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条件,如果要添加条件,
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)
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)
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)