半天搞定MySQL(全)六

半天搞定MySQL(全)

半天搞定MySQL(全)一
半天搞定MySQL(全)二
半天搞定MySQL(全)三
半天搞定MySQL(全)四
半天搞定MySQL(全)五
博主用的是8.0版本的MySQL,储存引擎是InnoDB,关于InnoDB这里不详细解释,需要的话推荐了解这篇博文(或者自行百度)https://www.jianshu.com/p/519fd7747137

12. 序列,重复数据处理
序列
MySQL其实是没有序列的,只有一个自增长的“序列”,这种自增只能实现自动加一的操作,而且针对于数字类型的主键,同时只能有一个表自增长,不能多表共用。
那么,如果我们要实现随心所欲的自增长怎么操作呢?只能在自定义函数中实现了!
主键自增

auto_increment
CREATE TABLE `city_play` (
  `city_name` varchar(20) NOT NULL,
  `city_thing` varchar(50) NOT NULL,
  `cost` int DEFAULT NULL,
  `journey_ID` varchar(20) DEFAULT NULL,
  `thing_score` int DEFAULT NULL,
  `play_ID` int NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`play_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

自增主键为 play_ID ,从12开始自增。

函数自增
1.删掉同名函数

mysql> drop function if exists currval;
Query OK, 0 rows affected (0.02 sec)
mysql> DROP FUNCTION IF EXISTS nextval;$
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> DROP FUNCTION IF EXISTS setval;$
Query OK, 0 rows affected, 1 warning (0.01 sec)

2.创建取当前值的函数curral:
先创建函数,在声明确定性和读取sql数据,最后定义函数

mysql> create function currval (seq_name varchar(20))
    -> returns integer
    -> language sql
    -> deterministic
    -> contains sql
    -> sql security definer
    -> begin
    -> declare value int;
    -> set value = 0;
    -> select play_ID into value from city_play where city_name=seq_name;
    -> return value;
    -> end
    -> $ delimiter;
Query OK, 0 rows affected (0.01 sec)

2.创建 计算(获取)下一个值的函数 nextval
先创建函数,在声明确定性,最后定义函数,函数返回的是currval(seq_name),也就是替换后的值。
设置增加值为10:

mysql> create function nextval (seq_name varchar(20))
    -> returns int
    -> deterministic
    -> begin
    -> update city_play set play_ID=play_ID+10
    -> where city_name = seq_name;
    -> return currval(seq_name);
    -> end
    -> $
Query OK, 0 rows affected (0.01 sec)

测试:
插入数据,及查看结果:

mysql> insert into city_play values('浅川','香樟树',10000,15,82,5);
mysql> select * from city_play;
+-----------+------------+-------+------------+-------------+---------+
| city_name | city_thing | cost  | journey_ID | thing_score | play_ID |
+-----------+------------+-------+------------+-------------+---------+
| 海南      | 潜水       | 15000 | 1          |          95 |       1 |
| 夏威夷    ||  NULL | 2          |          88 |       2 |
| 香港      | 潜水       |  NULL | 10         |          95 |      10 |
| Janpan    | woman      |  NULL | 11         |          90 |      5 |
+-----------+------------+-------+------------+-------------+---------+
4 rows in set (0.00 sec)

运行函数:

mysql> select currval('浅川');
+-----------------+
| currval('浅川') |
+-----------------+
|                5 |
+-----------------+
1 row in set (0.00 sec)
mysql> select nextval('浅川')
+-----------------+
| nextval('浅川') |
+-----------------+
|              15 |
+-----------------+
1 row in set (0.01 sec)

结果:

mysql> select * from city_play;$
+-----------+------------+-------+------------+-------------+---------+
| city_name | city_thing | cost  | journey_ID | thing_score | play_ID |
+-----------+------------+-------+------------+-------------+---------+
| 海南      | 潜水       | 15000 | 1          |          95 |       1 |
| 夏威夷    ||  NULL | 2          |          88 |       2 |
| 香港      | 潜水       |  NULL | 10         |          95 |      10 |
| Janpan    | woman      |  NULL | 11         |          90 |      11 |
| 浅川      | 香樟树     | 10000 | 15         |          82 |      15 |
+-----------+------------+-------+------------+-------------+---------+
5 rows in set (0.00 sec)

处理重复数据
防止表中出现重复数据

指定字段为primary key 或者unique。
同时有primary key 和unique:

mysql> alter table city_play modify journey_ID int unique;
mysql> desc city_play;
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| city_name   | varchar(20) | NO   |     | NULL    |                |
| city_thing  | varchar(50) | NO   |     | NULL    |                |
| cost        | int         | YES  |     | NULL    |                |
| journey_ID  | int         | YES  | UNI | NULL    |                |
| thing_score | int         | YES  |     | NULL    |                |
| play_ID     | int         | NO   | PRI | NULL    | auto_increment |
+-------------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

当然,你也可以在建表的时候就指定unique

统计重复数据
其中xxx为统计次数

mysql> select count(*) as xxx,journey_ID,city_name,times,data from city_journey group by city_name,data having times=1;$
+-----+------------+-----------+-------+------------+
| xxx | journey_ID | city_name | times | data       |
+-----+------------+-----------+-------+------------+
|   1 |          2 | 夏威夷    |     1 | 2020-09-09 |
|   2 |          9 | 香港      |     1 | 2020-06-18 |
|   1 |         11 | Janpan    |     1 | 2021-06-04 |
|   1 |         12 | America   |     1 | 2022-06-06 |
|   1 |         13 | Iceland   |     1 | 2023-06-05 |
+-----+------------+-----------+-------+------------+
5 rows in set (0.00 sec)

过滤重复数据
使用distinct过滤重复数据

mysql> select distinct * from city_journey;$
+------------+-----------+-------+------------+
| journey_ID | city_name | times | data       |
+------------+-----------+-------+------------+
|          1 | 海南      |     2 | 2020-06-18 |
|          2 | 夏威夷    |     1 | 2020-09-09 |
|          9 | 香港      |     1 | 2020-06-18 |
|         10 | 香港      |     1 | 2020-06-18 |
|         11 | Janpan    |     1 | 2021-06-04 |
|         12 | America   |     1 | 2022-06-06 |
|         13 | Iceland   |     1 | 2023-06-05 |
+------------+-----------+-------+------------+
7 rows in set (0.00 sec)

删除重复数据
其实就是复制到新表,在删除旧表,最后重命名新表
要注意的是,删除的列为city_name中重复的列。

mysql> create table temp select journey_ID,city_name, data,times  from city_journey group by (city_name);
Query OK, 7 rows affected (0.04 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> drop table city_journey;
Query OK, 0 rows affected (0.01 sec)

mysql> alter table temp rename to city_journey;
Query OK, 0 rows affected (0.03 sec)

未完待续。。。。。
半天搞定MySQL(全)一
半天搞定MySQL(全)二
半天搞定MySQL(全)三
半天搞定MySQL(全)四
半天搞定MySQL(全)五

猜你喜欢

转载自blog.csdn.net/lovedingning/article/details/106884629