4.表关系(一对一,一对多,多对多)和授权

1.表关系

1.1 单表

单独一张表可以保存信息,之前新建表都是单表

create table depart(
	id int not null auto_increment primary key,
    title varchar(16) not null
)default charset=utf8;

1.2 一对多

  • 包含外键,通过外键关系将表连接起来
    创建时添加外键关联:
create table depart(
	id int not null auto_increment primary key,
    title varchar(16) not null
)default charset=utf8;


create table info(
	id int not null auto_increment primary key,
    name varchar(16) not null,
    email varchar(32) not null,
    age int,
    depart_id int not null,
    constraint fk_info_depart foreign key (depart_id) references depart(id)
)default charset=utf8;

通过添加constraint fk_info_depart foreign key (depart_id) references depart(id) #关联了depart的ID键
如果表已经建好了,额外添加外键就是通过alter方式修改表结构这样:
alter table info add constraint fk_info_depart foreign key info(depart_id) references depart(id);
删除外键:

alter table info drop foreign key fk_info_depart;

插入数据:

1.3 多对多

包含内容:创建外键关联,新增外键关联,删除外键关联
如果一个表A的多个字段,每个字段都会对应表B中的多个字段,需要通过设置中间表,在中间表设置多个关联关系,同时关联A和B的字段。示例:

create table boy(
	id int not null auto_increment primary key,
    name varchar(16) not null
)default charset=utf8;

create table girl(
	id int not null auto_increment primary key,
    name varchar(16) not null
)default charset=utf8;


create table boy_girl(
	id int not null auto_increment primary key,
    boy_id int not null,
    girl_id int not null,
    constraint fk_boy_girl_boy foreign key boy_girl(boy_id) references boy(id),
    constraint fk_boy_girl_girl foreign key boy_girl(girl_id) references girl(id)
)default charset=utf8;

如果表结构已创建好了,额外想要增加外键:

alter table boy_girl add constraint fk_boy_girl_boy foreign key boy_girl(boy_id) references boy(id);
alter table boy_girl add constraint fk_boy_girl_girl foreign key boy_girl(girl_id) references girl(id);

删除外键:

alter table info drop foreign key fk_boy_girl_boy;
alter table info drop foreign key fk_boy_girl_girl;

2.授权

2.1 查看权限

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| day26              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
10 rows in set (0.00 sec)
mysql> select user,authentication_string,host from  mysql.user;
+----------------------------------+-------------------------------------------+-------------------------------+
| user                             | authentication_string                     | host                          |
+----------------------------------+-------------------------------------------+-------------------------------+
| root                             | *FAAFFE644E901CFAFAEC7562415E5FAEC243B8B2 | localhost                     |
| mysql.session                    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost                     |
| mysql.sys                        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost                     |
+----------------------------------+-------------------------------------------+-------------------------------+
3 rows in set (0.00 sec)

2.2 创建和删除用户

create user ‘用户名’@‘连接者的IP地址’ identified by ‘密码’;


```sql
create user wupeiqi1@127.0.0.1 identified by 'root123';
drop user wupeiqi1@127.0.0.1;

create user wupeiqi2@'127.0.0.%' identified by 'root123';
drop user wupeiqi2@'127.0.0.%';

create user wupeiqi3@'%' identified by 'root123';
drop user wupeiqi3@'%';

create user 'wupeiqi4'@'%' identified by 'root123';
drop user 'wupeiqi4'@'%';

## 2.3 修改用户信息
- 修改用户

rename user ‘用户名’@‘IP地址’ to ‘新用户名’@‘IP地址’;


```sql
rename user wupeiqi1@127.0.0.1 to wupeiqi1@localhost;

rename user 'wupeiqi1'@'127.0.0.1' to 'wupeiqi1'@'localhost';
  • 修改密码

    set password for '用户名'@'IP地址' = Password('新密码')
    
    set password for 'wupeiqi4'@'%' = Password('123123');
    

2.4 授权管理

创建好用户之后,就可以为用户进行授权了。

  • 授权

    grant 权限 on 数据库.表 to   '用户'@'IP地址'
    
    grant all privileges on *.* TO 'wupeiqi'@'localhost';         -- 用户wupeiqi拥有所有数据库的所有权限
    grant all privileges on day26.* TO 'wupeiqi'@'localhost';     -- 用户wupeiqi拥有数据库day26的所有权限
    grant all privileges on day26.info TO 'wupeiqi'@'localhost';  -- 用户wupeiqi拥有数据库day26中info表的所有权限
    
    grant select on day26.info TO 'wupeiqi'@'localhost';          -- 用户wupeiqi拥有数据库day26中info表的查询权限
    grant select,insert on day26.* TO 'wupeiqi'@'localhost';      -- 用户wupeiqi拥有数据库day26所有表的查询和插入权限
    
    grant all privileges on day26db.* to 'wupeiqi4'@'%';
    
    
    注意:flush privileges;   -- 将数据读取到内存中,从而立即生效。
    
    • 对于权限

      all privileges  除grant外的所有权限
      select          仅查权限
      select,insert   查和插入权限
      ...
      usage                   无访问权限
      alter                   使用alter table
      alter routine           使用alter procedure和drop procedure
      create                  使用create table
      create routine          使用create procedure
      create temporary tables 使用create temporary tables
      create user             使用create user、drop user、rename user和revoke  all privileges
      create view             使用create view
      delete                  使用delete
      drop                    使用drop table
      execute                 使用call和存储过程
      file                    使用select into outfile 和 load data infile
      grant option            使用grant 和 revoke
      index                   使用index
      insert                  使用insert
      lock tables             使用lock table
      process                 使用show full processlist
      select                  使用select
      show databases          使用show databases
      show view               使用show view
      update                  使用update
      reload                  使用flush
      shutdown                使用mysqladmin shutdown(关闭MySQL)
      super                   􏱂􏰈使用change master、kill、logs、purge、master和set global。还允许mysqladmin􏵗􏵘􏲊􏲋调试登陆
      replication client      服务器位置的访问
      replication slave       由复制从属使用
      
    • 对于数据库和表

      数据库名.*            数据库中的所有
      数据库名.表名          指定数据库中的某张表
      数据库名.存储过程名     指定数据库中的存储过程
      *.*                  所有数据库
      
  • 查看授权

    show grants for '用户'@'IP地址'
    
    show grants for 'wupeiqi'@'localhost';
    show grants for 'wupeiqi4'@'%';
    
  • 取消授权

    revoke 权限 on 数据库.表 from '用户'@'IP地址'
    
    revoke ALL PRIVILEGES on day26.* from 'wupeiqi'@'localhost';
    
    revoke ALL PRIVILEGES on day26db.* from 'wupeiqi4'@'%';
    注意:flush privileges;   -- 将数据读取到内存中,从而立即生效。
    

一般情况下,在很多的 正规 公司,数据库都是由 DBA 来统一进行管理,DBA为每个项目的数据库创建用户,并赋予相关的权限。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值