美文网首页
13-MySQL操作数据库

13-MySQL操作数据库

作者: 紫荆秋雪_文 | 来源:发表于2021-05-11 14:03 被阅读0次

    一、创建数据库

    1.1、查看当前 MySQL 存在的数据库

    show databases;
    
    查看当前 MySQL 存在的数据库.png

    1.2、创建数据库语法格式一

    CREATE DATABASE database_name
    

    1.3、创建数据库语法格式二

    CREATE DATABASE IF NOT EXISTS database_name
    
    • 如果MySQL中不存在相关的数据库,则创建数据库,如果MySQL中已经存在相关的数据库,则忽略创建语句,不再创建数据库。
    • 推荐使用 CREATE DATABASE IF NOT EXISTS database_name 语句创建数据库

    二、查看数据库

    2.1、查看MySQL中存在的数据库

    show databases;
    

    2.2、查看MySQL命令行所在的数据库

    select database();
    
    查看MySQL命令行所在的数据库.png

    2.3、查看数据库的创建信息

    show create database goods;
    或
    show create database goods \G
    
    image.png
    • 创建名称为 goods
    • 使用的字符编码为 utf8mb4
    • 使用的校对规则为 utf8mb4_0900_ai_ci
    • DEFAULTENCRYPTION='N'表示默认没有使用MySQL的加密技术

    三、修改数据库名称

    • MySQL5.1.7中修改数据库名称 SQL 语句,可能造成数据丢失
    RENAME DATABASE database_name TO new_database_name
    

    3.1、通过重命名数据表修改数据库名称

    3.1.1、创建测试数据库test_old

    create database if not exists test_old;
    

    3.1.2、在test_old数据库中创建table_test数据表

    create table if not exists table_test(id int)
    
    image.png

    3.1.3、创建新数据库test_new

    create database if not exists test_new;
    
    image.png

    3.1.4、将数据库test_old下的数据表重命名到 test_new数据库下

    • test_new数据库下的表


      test_new数据库中表.png
    rename table test_old.table_test to test_new.table_test;
    
    • test_new数据库下的表


      image.png

    3.1.5、删除表

    drop table if exists test_old ;
    

    3.1.6、删除数据库

    • 删除前


      image.png
    • 删除test_old
    drop database if exists test_old ;
    
    • 删除后


      image.png

    3.2、通过导入 / 导出 数据修改数据库名称

    3.2.1、test_old数据库 image.png

    3.2.2、test_old数据库中表

    image.png

    3.2.3、将名称为test_old的数据库导出

    mysqldump -uroot -p test_old > testOld.sql
    
    • 因为我使用的是 docker 安装的MySQL,所以需要进入 MySQL容器中执行上面的命令


      image.png

    3.2.4、重新创建test_new数据库

    image.png
    • 导入sql文件
    source sql文件
    source /testOld.sql
    

    3.3、通过创建数据表修改数据库名称

    3.3.1、创建新数据库test_new

    create database if not exists test_new;
    

    3.3.2、在test_new数据库中创建 table_test数据表,使其按照 test_old 数据库中的数据表进行创建

    create table if not exists test_new.table_test like test_old.table_test;
    

    四、数据库编码

    在MySQL中,会为创建的每个数据库指定一个字符编码。在创建数据库时没有指定字符编码时,会默认指定一个字符编码,这个默认的字符编码在MySQL的配置文件 my.cnf 中进行配置

    • my.cnf 在CentOS7 中映射文件路径
    /home/app/mysql/config
    
    • my.cnf 内容
    # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
    #
    # This program is free software; you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation; version 2 of the License.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with this program; if not, write to the Free Software
    # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
    
    #
    # The MySQL  Server configuration file.
    #
    # For explanations see
    # http://dev.mysql.com/doc/mysql/en/server-system-variables.html
    
    [mysqld]
    pid-file        = /var/run/mysqld/mysqld.pid
    socket          = /var/run/mysqld/mysqld.sock
    datadir         = /var/lib/mysql
    secure-file-priv= NULL
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    
    # Custom config should go here
    !includedir /etc/mysql/conf.d/
    

    4.1、创建数据库时指定字符编码

    create database [if not exists] database_name default character set character_name collate collate_name [default encryption='N'];
    
    create database if not exists test_characher default character set utf8 collate utf8_unicode_ci default encryption='N';
    
    • 创建数据库test_character
    • 数据库的字符编码 utf-8
    • 校验规则为 uft8_unicode_ci
    • 不使用 MySQL 的加密技术
    • 查看 数据库的字符编码指令
    show create database test_characher \g
    

    4.2、修改数据库的字符编码

    4.2.1、语法格式

    alter database database_name character set character_name collate collate_name
    

    4.2.2、修改test_character数据库的字符编码为 utf8mb4,校验规则修改为 utf8mb4_0900_ai_ci

    alter database test_characher character set utf8mb4 collate utf8mb4_0900_ai_ci;
    

    相关文章

      网友评论

          本文标题:13-MySQL操作数据库

          本文链接:https://www.haomeiwen.com/subject/srtidltx.html