【Mysql】用户权限操作、处理重复数据

用户权限操作

登陆进入数据库 输入用户名和密码

#mysql -uroot -p;

创建一个名字为“mei_db” 编码为utf-8的数据库

create database if not exists `mei_db` default character set utf8 collate utf8_general_ci;
# 创建用户,用户可以通过特定ip的客户端访问数据库 
CREATE USER '用户名'@'ip' IDENTIFIED BY '密码';
实例:
# 创建用户 mei,密码是123456 ;  @'%' 表示用户可以通过任意ip的客户端访问数据库
CREATE USER 'mei'@'localhost' identified by '123456';  
CREATE USER 'mei'@'%' IDENTIFIED BY '123456';

# 赋予用户权限,让其拥有权限1和权限2去操作特定数据库的所有表
GRANT 权限1, 权限2 ON '数据库名'.* TO '用户名'@'ip';

# 赋予权限给用户testUser去操作数据库dbtest的所有表
# 如果要赋予所有权限,可以用GRANT ALL
GRANT SELECT, INSERT, UPDATE, REFERENCES, DELETE, CREATE, DROP, ALTER, INDEX, TRIGGER, CREATE VIEW, SHOW VIEW, EXECUTE, ALTER ROUTINE, CREATE ROUTINE, CREATE TEMPORARY TABLES, LOCK TABLES, EVENT ON `mei_db`.* TO 'mei'@'%';
GRANT ALL privileges  ON `mei_db`.* TO `mei`@`%`;

# 让该用户可以再授权给其他人
GRANT GRANT OPTION ON `dbtest`.* TO `testUser`@`%`;

#刷新权限
flush privileges;

#退出mysql
exit;

处理重复数据

1.防止重复数据 (UNIQUE)

create table dong1
(
   first_name CHAR(20) NOT NULL,
   last_name CHAR(20) NOT NULL,
   sex CHAR(10),
   UNIQUE (last_name, first_name)       //创建first_name、last_name的唯一性
);

注意:除了unique来设置字段的唯一性,也可以通过primary 来设置双主键,但是,后者多用于索引,而且要求not null
1、primary key = unique + not null
2、唯一约束和主键一样都是约束的范畴,而且都可以作为外键的参考,不同的是,一张表只能有一个主键
3、主键和唯一约束的创建需要依靠索引,如果在创建主键或唯一约束的时候没有已经建好的索引可以使用的话,Oracle会自动建立一个唯一的索引。

2.统计重复数据

mysql> select * from yan3;
+----+----------+-----------+------------+
| id | book     | zuozhe    | shijian    |
+----+----------+-----------+------------+
|  1 | meimei00 | xiaowen   | 2022-01-02 |
|  2 | meimei01 | xiaowen   | 2022-05-19 |
|  3 | meimei02 | xiaowen02 | 2022-05-19 |
|  4 | meimei03 | xiaowen03 | 2022-05-19 |
|  5 | dongdong | yanyan    | 2022-05-19 |
+----+----------+-----------+------------+
5 rows in set (0.00 sec)

mysql> select count(*) as nu,zuozhe from yan3   
    -> group by zuozhe
    -> having nu>2;          //统计重复2次以上的数据
+----+---------+
| nu | zuozhe  |
+----+---------+
|  2 | xiaowen |
+----+---------+
1 row in set (0.01 sec)

3.过滤重复数据

select distinct zuozhe from yan3;             //方法一:通过distinct来过滤统计数据
select zuozhe from yan3 group by zuozhe;      //方法二:通过group by 来过滤统计数据

4.删除重复数据

原理:将过滤后的数据,重新建表后,删除原来旧表,将新表名改为旧表名

//原始表数据
mysql> select * from yan3;
+----+-----------+-----------+------------+
| id | book      | zuozhe    | shijian    |
+----+-----------+-----------+------------+
|  1 | meimei00  | xiaowen   | 2022-01-02 |
|  2 | meimei01  | xiaowen   | 2022-05-19 |
|  3 | meimei02  | xiaowen02 | 2022-05-19 |
|  4 | meimei03  | xiaowen03 | 2022-05-19 |
|  5 | dongdong  | yanyan    | 2022-05-19 |
| 23 | dongdong2 | yanyan    | NULL       |
| 24 | dongdong3 | yanyan    | NULL       |
+----+-----------+-----------+------------+
7 rows in set (0.00 sec)

 //查询出的结果,重新建表

mysql> create table new_yan3 select book,zuozhe,shijian from yan3 group by zuozhe;
Query OK, 4 rows affected (0.04 sec)
Records: 4  Duplicates: 0  Warnings: 0

//删除原来表   

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

 //将新表重新命名为原来表名字 

mysql> alter table new_yan3 rename to yan3;
Query OK, 0 rows affected (0.00 sec)

 //查看结果,达到删除重复数据的目的

mysql> select * from yan3;
+----------+-----------+------------+
| book     | zuozhe    | shijian    |
+----------+-----------+------------+
| meimei00 | xiaowen   | 2022-01-02 |
| meimei02 | xiaowen02 | 2022-05-19 |
| meimei03 | xiaowen03 | 2022-05-19 |
| dongdong | yanyan    | 2022-05-19 |
+----------+-----------+------------+
4 rows in set (0.00 sec)

你可能感兴趣的:(Mysql,mysql,数据库)