关于Mysql数据库的权限控制

发布时间 2023-03-24 14:48:29作者: 璀璨如歌

DCL-权限控制

权限 说明
all,all privileges 所有权限
select 查询数据
insert 插入数据
update 修改数据
delete 删除数据
alter 修改表
drop 删除数据库/表
create 创建数据库/表

基本使用

进入数据库

  • mysql -uroot -p

创建数据库

  • create database [if exists] 库名;

使用数据库

  • use 库名;

创建表

  • create table 表名(字段1 数据类型,字段2 数据类型......);

关于创建的权限

  • create作用及权限:创建新库/表

    创建库:create database [if exists] 库名;

    • 指定字符集:create database [if exists] 库名 [default character set = 字符集名字];

    创建表:create table 表名(字段1 数据类型,字段2 数据类型......);

    • 设置主/外/唯/默/非约束:create table 表名 (字段名 数据类型 [constraint 约束名] primary key | foreign key 字段名 references 主表(主表字段名) | unique | default 默认值 | not null);

关于查询的权限

  • select作用及权限:查询数据

​ 查询/限制查询表数据:select * from 表名 [where 条件 | limit 0,n];

​ 查询时过滤重复数据:select distinct 字段名 from 表名;

​ 查询当前使用库:select 库名();

  • show作用及权限:查询库/表结构、字符集

    查询当前系统所有库:show databases;

    查询当前数据库所有表:show tables;

    • 查询表结构:show create table 表名;show columns from 表名;desc 表名;(describe)

    查询字符集:show variables like 'character%';

关于修改的权限(重点!!!)

  • alter作用及权限:修改表的结构、重新命名表

    更改表名:alter table 旧表名 rename 新表名;

    更改表字符集:alter database 库名 [default] character set[=]字符集名字;

    • change权限:数据类型、字段

      • 修改字段名及数据类型:alter table 表名 change 旧字段名 新字段名 新数据类型;
    • modify权限:数据类型、约束/自动增长、移动字段名

      • 更改数据类型:alter table 表名 modify 字段名 新数据类型;

      • 删除非空/默认约束/自动增长:alter table 表名 modify 字段名 数据类型;

      • 增加主键/唯一/默认/非空约束/自动增长:alter table 表名 modify 字段名 数据类型 primary key | unique | default 默认值 | not null | auto_increment;

      • 移动字段名:alter table 表名 modify 需要移动的字段名 需要移动的字段数据类型 after 字段名;

    • add权限:主键、字段

      • 添加主键/外键约束:alter table 表名 add [constraint 约束名] primary key (字段名) | foreign key (外键) references 主表(主键) | unique(字段名);
      • 添加字段:alter table 表名 add 新字段名 数据类型 [first 字段 | after 字段]
    • drop权限:主键/外键/唯一、字段

      • 删除主键/外键/唯一约束:alter table 表名 drop primary key | foreign key 外键 | key/index 约束名;
      • 删除字段:alter table 表名 drop 字段1[,drop 字段2,...];
  • update作用及权限:更新表数据

    语法:update 表名 set 字段名1=值1,字段名2 = 值2,… [where 条件];

关于删除的权限

  • drop作用及权限:删除库/表

    删库:drop database [if exists] 库名;

    删表:drop table [if exists] 表名,表名,...;

  • delete作用及权限:删除表数据

    删除表数据:delete from 表名 [where 条件];

关于插入的权限

  • insert作用及权限:插入字段数据

    增加字段数据:insert into 表名 (列名) values (值);