暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

MySQL 系列(二) 你不知道的数据库操作

Linux Windows server学习交流 2020-05-20
94

本章内容:


  • 查看\创建\使用\删除 数据库

  • 用户管理及授权实战

  • 局域网远程连接法

  • 查看\创建\使用\删除\清空\修改 数据库表(是否可空,默认值,主键,自增,外键)

  • 表内容的增删改查

  • where条件、通配符_%、限制limit、排序desc\asc、连表join、组合union

  • 查看建表语句、查看表结构、查看是否走索引

  • 数据类型

  • 索引!

一、数据库操作

1、查看数据库

    SHOW DATABASES;




    # 默认数据库:
      mysql - 用户权限相关数据
      test - 用于用户测试数据
      information_schema - MySQL本身架构相关数据
    复制

     2、创建数据库

      # utf-8 编码
      CREATE DATABASE 数据库名称 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

      # gbk 编码
      CREATE DATABASE 数据库名称 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
      复制

      3、使用数据库

        USE db_name;




        # 可以不使用分号
        复制

         4、用户管理

          # 创建用户
          create user '用户名'@'IP地址' identified by '密码';
          # 删除用户
          drop user '用户名'@'IP地址';
          # 修改用户
          rename user '用户名'@'IP地址' to '新用户名'@'IP地址';;
          # 修改密码
          set password for '用户名'@'IP地址' = Password('新密码')

          PS:用户权限相关数据保存在mysql数据库的user表中,所以也可以直接对其进行操作(不建议)
          # 查看当前用户
          select user();
          # 查看所有用户
          select host,user from mysql.user;
          # 人性化显示所有用户
          SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
          # 查看用户的所有权限
          show grants for 'nick'@'%';
          复制
            mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
            +---------------------------+
            | query |
            +---------------------------+
            | User: 'nick'@'%'; |
            | User: 'root'@'localhost'; |
            +---------------------------+
            2 rows in set (0.00 sec)


            mysql>
            mysql>
            mysql>
            mysql>
            mysql> select host,user from mysql.user;
            +-----------+------+
            | host | user |
            +-----------+------+
            | % | nick |
            | localhost | root |
            +-----------+------+
            2 rows in set (0.00 sec)


            mysql> show grants for 'nick'@'%';
            +-----------------------------------------------------------------------------------------------------+
            | Grants for nick@% |
            +-----------------------------------------------------------------------------------------------------+
            | GRANT USAGE ON *.* TO 'nick'@'%' IDENTIFIED BY PASSWORD '*ECE7D02DCD7D4EF7CFE8E3B249FD1D5062A821F7' |
            | GRANT ALL PRIVILEGES ON `kaoshi`.* TO 'nick'@'%' |
            | GRANT ALL PRIVILEGES ON `xxxxx`.* TO 'nick'@'%' |
            | GRANT ALL PRIVILEGES ON `xxxxxx`.`chouti` TO 'nick'@'%' |
            +-----------------------------------------------------------------------------------------------------+
            4 rows in set (0.00 sec)


            mysql>
            复制

            5、授权管理

              # 查看权限
              show grants for '用户'@'IP地址'
              # 授权
              grant 权限 on 数据库.表 to '用户'@'IP地址'
              # 取消权限
              revoke 权限 on 数据库.表 from '用户'@'IP地址'
              复制
                常用权限:
                all privileges 除grant外的所有权限
                select 仅查权限
                select,insert 查和插入权限
                usage 无访问权限




                对于目标数据库以及内部其他:
                数据库名.* 数据库中的所有
                数据库名.表 指定数据库中的某张表
                数据库名.存储过程 指定数据库中的存储过程
                *.* 所有数据库




                对于用户和IP:
                用户名@IP地址 用户只能在改IP下才能访问
                用户名@192.168.1.% 用户只能在改IP段下才能访问(通配符%表示任意)
                用户名@% 用户可以再任意IP下访问(默认IP地址为%)
                复制
                 更多权限
                复制
                  all privileges  除grant外的所有权限
                  select 仅查权限
                  select,insert 查和插入权限
                  ...
                  usage 无访问权限
                  alter 使用alter table
                  alter routine 使用alter proceduredrop procedure
                  create 使用create table
                  create routine 使用create procedure
                  create temporary tables 使用create temporary tables
                  create user 使用create userdrop userrename userrevoke all privileges
                  create view 使用create view
                  delete 使用delete
                  drop 使用drop table
                  execute 使用call和存储过程
                  file 使用select into outfileload data infile
                  grant option 使用grantrevoke
                  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 masterkilllogspurgemasterset global。还允许mysqladmin􏵗􏵘􏲊􏲋调试登陆
                  replication client 服务器位置的访问
                  replication slave 由复制从属使用


                  复制
                   添加额外管理员


                     添加额外管理员
                    简单示例
                    创建用户一般流程mysql> delete from mysql.user;
                    Query OK, 2 rows affected (0.00 sec)
                    mysql> grant all privileges on *.* to system@'localhost' identified by 'oldsuo' with grant option;
                    Query OK, 0 rows affected (0.00 sec)
                    mysql> flush privileges;
                    Query OK, 0 rows affected (0.00 sec)
                    mysql> select user,host from mysql.user;
                    +--------+-----------+
                    | user | host |
                    +--------+-----------+
                    | system | localhost |
                    +--------+-----------+
                    1 row in set (0.00 sec)
                    mysql>
                    复制
                     简单示例
                                  grant all privileges on db1.tb1 TO '用户名'@'IP'


                      grant select on db1.* TO '用户名'@'IP'


                      grant select,insert on *.* TO '用户名'@'IP'


                      revoke select on db1.tb1 from '用户名'@'IP'
                      复制
                       创建用户一般流程
                        create user oldsuo@localhost identified by 'oldsuo123';
                        GRANT ALL ON test.* TO 'oldsuo'@'localhost'; #授权
                        show grants for oldsuo@localhost;
                        flush privileges;




                        mysql> help grant
                        CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass';
                        GRANT ALL ON db1.* TO 'jeffrey'@'localhost';
                        GRANT SELECT ON db2.invoice TO 'jeffrey'@'localhost';
                        GRANT USAGE ON *.* TO 'jeffrey'@'localhost' WITH MAX_QUERIES_PER_HOUR 90;
                        #创建nick用户,对test库具备所有权限,允许从localhost主机登陆管理数据库,密码为#nick23。
                        grant all privileges on test.* to 'nick'@'localhost' identified by 'nick123';
                        flush privileges;


                        mysql> show grants for nick@localhost;
                        +---------------------------------------------------------------------------------------------------------------+
                        | Grants for nick@localhost |
                        +---------------------------------------------------------------------------------------------------------------+
                        | GRANT USAGE ON *.* TO 'nick'@'localhost' IDENTIFIED BY PASSWORD '*7495041D24E489A0096DCFA036B166446FDDD992' |
                        | GRANT ALL PRIVILEGES ON `test`.* TO 'nick'@'localhost' |
                        +---------------------------------------------------------------------------------------------------------------+
                        2 rows in set (0.00 sec)
                        复制


                         6、授权局域网内主机远程连接数据库

                          #百分号匹配法
                          grant all on *.* to 'test'@'192.168.200.%' identified by 'test123';
                          #子网掩码配置法
                          grant all on *.* to 'test'@'192.168.200.0/255.255.255.0' identified by 'test123';
                          #刷新权限
                          flush privileges;
                          #远程登陆连接
                          mysql -utest -ptest123 -h 192.168.200.96
                          复制

                           


                          二、表操作

                          1、创建表

                            # 基本语法:
                            create table 表名(
                            列名 类型 是否可以为空 默认值 自增 主键,
                            列名 类型 是否可以为空
                            )ENGINE=InnoDB DEFAULT CHARSET=utf8




                            not null # 不可以为空
                            default 1 # 默认值为1
                            auto_increment # 自增
                            primary key # 主键
                            constraint 外键名 foreign key (从表字段’自己‘) references 主表(主键字段) # 外键
                            复制
                                      是否可空,null表示空,非字符串


                              not null - 不可空
                              null - 可空
                              复制
                                默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值


                                create table tb1(
                                nid int not null defalut 2,
                                num int not null
                                )
                                复制

                                       

                                  自增,如果为某列设置自增列,插入数据时无需设置此列,默认将自增(表中只能有一个自增列)


                                  create table tb1(
                                  nid int not null auto_increment primary key,
                                  num int null
                                  )

                                  create table tb1(
                                  nid int not null auto_increment,
                                  num int null,
                                  index(nid)
                                  )
                                  注意:1、对于自增列,必须是索引(含主键)。
                                  2、对于自增可以设置步长和起始值
                                  show session variables like 'auto_inc%';
                                  set session auto_increment_increment=2;
                                  set session auto_increment_offset=10;




                                  shwo global variables like 'auto_inc%';
                                  set global auto_increment_increment=2;
                                  set global auto_increment_offset=10;
                                  复制

                                       

                                    主键,一种特殊的唯一索引,不允许有空值,如果主键使用单个列,则它的值必须唯一,如果是多列,则其组合必须唯一。


                                    create table tb1(
                                    nid int not null auto_increment primary key,
                                    num int null
                                    )

                                    create table tb1(
                                    nid int not null,
                                    num int not null,
                                    primary key(nid,num)
                                    )
                                    复制

                                           

                                      外键,一个特殊的索引,只能是指定内容


                                      creat table color(
                                      nid int not null primary key,
                                      name char(16) not null
                                      )




                                      create table fruit(
                                      nid int not null primary key,
                                      smt char(32) null ,
                                      color_id int not null,
                                      constraint fk_cc foreign key (color_id) references color(nid)
                                      )
                                      复制

                                       2、删除表

                                        drop table 表名
                                        复制

                                        3、清空表

                                          # 表还存在,表内容清空




                                          delete from 表名
                                          truncate table 表名
                                          复制

                                          4、修改表

                                            # 添加列:
                                            alter table 表名 add 列名 类型
                                            # 删除列:
                                            alter table 表名 drop column 列名
                                            # 修改列:
                                            alter table 表名 modify column 列名 类型; -- 类型
                                            alter table 表名 change 原列名 新列名 类型; -- 列名,类型
                                            # 添加主键:
                                            alter table 表名 add primary key(列名);
                                            # 删除主键:
                                            alter table 表名 drop primary key;
                                            alter table 表名 modify 列名 int, drop primary key;
                                            # 添加外键:
                                            alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
                                            # 删除外键:
                                            alter table 表名 drop foreign key 外键名称
                                            # 修改默认值:
                                            ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
                                            # 删除默认值:
                                            ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
                                            # 更改表名
                                            rename table 原表名 to 新表名;
                                            复制

                                             增删改表的字段

                                             

                                              #增加表字段,altertable法。
                                              1> 语法: altertable 表名 add 字段 类型 其他;
                                              2> 插入列,名为sex。
                                              mysql> alter table student add sex char(4);
                                              Query OK, 3 rows affected (0.01 sec)
                                              Records: 3 Duplicates: 0 Warnings: 0


                                              mysql> select * from student;
                                              +----+----------+-----+------+------+
                                              | id | name | age | dept | sex |
                                              +----+----------+-----+------+------+
                                              | 2 | oldsuo | 0 | NULL | NULL |
                                              | 3 | kangknag | 0 | NULL | NULL |
                                              | 4 | kangkang | 0 | NULL | NULL |
                                              +----+----------+-----+------+------+
                                              3 rows in set (0.00 sec)
                                              3> 插入名为suo列在name后面。
                                              mysql> alter table student add suo int(4) after name;
                                              Query OK, 6 rows affected (0.00 sec)
                                              Records: 6 Duplicates: 0 Warnings: 0
                                              4> 插入名为qq列在第一。
                                              mysql> alter table student add qq varchar(15) first;
                                              Query OK, 6 rows affected (0.00 sec)
                                              Records: 6 Duplicates: 0 Warnings: 0


                                              #更改表名字,rename法。
                                              1> 语法: rename table 原表名 to 新表名;
                                              2> 更改oldsuo表为oldning。
                                              mysql> rename table oldsuo to oldning;
                                              Query OK, 0 rows affected (0.00 sec)
                                              mysql> show tables;
                                              +--------------------------+
                                              | Tables_in_nick_defailt |
                                              +--------------------------+
                                              | oldning |
                                              | student |
                                              +--------------------------+
                                              2 rows in set (0.00 sec)


                                              #删除表
                                              1> 语法:drop table <表名>;
                                              2> 删除表名为oldsuo表。
                                              mysql> drop table oldsuo;
                                              Query OK, 0 rows affected (0.00 sec)
                                              复制
                                              文章转载自Linux Windows server学习交流,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                                              评论