• 数据库大整合


    Mysql

    为了使用mysql,得先开启mysql的服务

    找到bin/mysqld运行即可(win7以上系统,你要以管理员权限运行)

    请使用cmd命令行登录mysql   输入命令:mysql -uroot -p 就可以root身份登录

    常见面试题:CHAR、VARCHAR的区别

    (今天出现在这份文档里的所有内容语句,都要全部背出来,一个字母都不能错)

    1.创建数据库

    create database aaa;

    2.查看所有数据库

    show databases;

    3.删除数据库

    drop database aaa;

    4.选择数据库

    use aaa;

    5.创建表

    create table students(

        id int,

        name varchar(20)

    );

    6.查看所有表

    show tables;

    7.查看表的结构

    desc students;

    8.删除表

    drop table students;

    9.Insert into插入数据

    Insert into students(id,name) values(1,’zhangsan’);

    Insert into students values(2,’xiaohong’);

    10.查看表中所有数据

    Select * from students;

    Select id,name from students;

    11.根据条件查找(where子句)

    Select * from students where id=1;

    12.Update set改

    Update students set name=’miaomiao’ where id=1;

    13.Delete 删除

    Delete from students where id=1;

    14.两张表的联合查询

    select stu.id,name,age,score from students stu,Scores sc where stu.id=sc.id;

    数据库的导入导出(比如质检时你如何将数据库给老师去批改呢?)

    导出整个数据库

    mysqldump -u 用户名 -p 数据库名 > 导出的文件名

    mysqldump -u dbuser -p dbname > dbname.sql

    数据库约束

     更改UTF8格式

    案例:需要大伙创建两张表

    students,有id(int),name(varchar(20))和age(int)字段

    scores,有id(int),score(int)字段

    向上面两张表中插入数据,得到结果

    Drop table students;

    Create table students(id int,name varchar(20),age int);

    Create table scores(id int,score int);

    Insert into students values(1,'xiaohong',15);

    insert into students values(2,'xiaobao',17);

    insert into students values(3,'xiaoai',16);

    insert into scores values(1,80),(2,90),(3,70);

    Students

    Id    name     age

    1     xiaohong  15

    2     xiaobai   17

    3     xiaoai    16

    Scores

    Id    score  

    1     80

    2     90

    3     70

     mysql> select stu.id,name,age,score from students stu,scores sc where stu.id=sc.id;

    要求这道题,给我查询出如下结果

    Id    name    age   score

    1     xiaohong  15    80

    2     xiaobai   17    90

    3     xiaoai    16    70

    要求这道题,给我查询出如下结果

    s_id    name    age   score

    1     xiaohong  15    80

    2     xiaobai   17    90

    3     xiaoai    16    70

     Create table   test(

      Id  int primary   key   auto_increment

    Name  varchar(20)

    );

    --------------------------------------------------------------------------------------------------------------------------------------------------------

    • 问题引入:

            我们经常会遇到一些向MySQL数据库中插入中文,但是select出来的时候,却发现是乱码的情况。如我们向表a出入这样一段记录:i

    insert into a values('你好helloworld你好','helloworld');

    可能当你访问它的时候,会发现他的结果变成如下图所示:

    image

    那怎么样才能解决这种问题呢?通过下文对MySQL中字符集的一些操作,你将会得到答案!

     

    • 查看库、表字符集命令: 

            要解决字符集的问题,首先要知道现在的系统、数据库、表、客户端等使用什么样的字符集,以及系统支持什么字符集等,下面介绍一些获取相关信息的语句:

    1.查看数据库支持的所有字符集

    show character set;或者show char set;

    Image(42)

    2.查看当前状态,里面当然包括字符集的设置:

    status或者/s

    Image(43)

    其中Db characterset对应的是数据库目录下的db.opt文件内容:

    Image(44)

    3.查看系统字符集设置,包括所有的字符集设置:

    show variables like '%char%';

    得出如何所示结果:

    Image(40)

    其中的含义如下:

    Image(41)

    关于connection相关的字符集的官方文档:

    • What character set is the statement in when it leaves the client?

    The server takes the character_set_client system variable to be the character set in which statements are sent by the client.

     

    • What character set should the server translate a statement to after receiving it?

    For this, the server uses the character_set_connection and collation_connection system variables. It converts statements sent by the client from character_set_client to character_set_connection (except for string literals that have an introducer such as _latin1 or _utf8). collation_connection is important for comparisons of literal strings. For comparisons of strings with column values, collation_connection does not matter because columns have their own collation, which has a higher collation precedence.

     

    • What character set should the server translate to before shipping result sets or error messages back to the client?
    The character_set_results system variable indicates the character set in which the server returns query results to the client. This includes result data such as column values, and result metadata such as column names and error messages.

         从上文中可以看出character_set_connection、character_set_client、character_set_results三个字符集什么时候用到。从实际上可以看到,当客户端连接服务器的时候,它会将自己想要的字符集名称发给mysql服务器,然后服务器就会使用这个字符集去设置character_set_connection、character_set_client、character_set_results这三个值。如cmd是用gbk,而mysql workbench是用utf8.

    CMD:

    image

    MySql WorkBench:

    image

     

    4.查看数据表中字符集设置:

    show full columns from tablename;

    Image(45)

    show create table tablename/G;

    Image(46)

     

    5.查看数据库编码:

    show create database dbname;

    Image(47)

     

    • 创建时指定字符集:

            知道了怎么查找字符集的相关信息之后,我们就要懂得怎么在创建指定对象的时候,为该对象匹配相应的字符集。

    1.服务器级:

    在安装MySQL时可以设置服务器的默认编码格式,也可对my.ini做修改,修改[mysqld]里面的character_set_server=utf8,则可设置character_set_server的值。

    2.数据库级:

    CREATE DATABASE db_name DEFAULT CHARACTER SET utf8;

    Image(48)

    注意,如果不指定默认的字符集,则系统会根据character_set_database的值进行设置,如:

    Image(49)

    3.表级:

    CREATE TABLE  `db_name`.`tb_name` (id VARCHAR(20) NOT NULL,name VARCHAR(20) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    从下图可看出,定义表的默认字符集为utf8,即使character_set_database为gbk,但是表的列都未utf8

    Image(50)

    但要注意,如果没有定义表的默认字符集,则他会按照character_set_database的值来设置,如图所示:

    Image(51)

    4.列级:

    CREATE TABLE  `db_name`.`tb_name` (   id varchar(20) NOT NULL,   name varchar(20) CHARACTER SET utf8 );

    从下图可以看到,整个表的默认字符集为gbk,所以没有指定字符集的列都用默认的字符集,而指定了字符集的列name,则使用指定的字符集utf8。

    Image(52)

     

    • 修改字符集命令

            如果已经是创建好的对象,那又应该如何处理呢。我们就应该对指定对象就行修改字符集的操作。

    1.修改character_set_connection、character_set_client、character_set_results三值:

    对于某一个连接来说,可以使用:

    SET NAMES 'charset_name' [COLLATE 'collation_name']

    image

    命令

    SET NAMES 'charset_name' [COLLATE 'collation_name']

    相当于

    SET character_set_client = charset_name; SET character_set_results = charset_name; SET character_set_connection = charset_name;

    另外、还可以修改配置文件,对[mysql]下增加default-character-set=utf8,配置成你想要的字符集。(个人尝试在my.ini里面配置过,没有成效,不知道是不是被使用的客户端想要的字符集给覆盖掉了呢?)

    2.修改character_set_database字段:

    ALTER DATABASE db_name     [[DEFAULT] CHARACTER SET charset_name]     [[DEFAULT] COLLATE collation_name]

    image

    3.修改character_set_server字段:

    最简单的方法是直接改my.ini配置文件里面[mysqld]的字段,增加character-set-server=gbk,然后重启mysqld,则可改为你想要的字符集。

    4.修改表的字符集:

    ALTER TABLE tbl_name     [[DEFAULT] CHARACTER SET charset_name]     [COLLATE collation_name]

    5.修改列的字符集:

    col_name {CHAR | VARCHAR | TEXT} (col_length)     [CHARACTER SET charset_name]     [COLLATE collation_name]

    show  varibles  like  '%char%';

     set names  utf8;

  • 相关阅读:
    Docker基操—2.MySQL镜像
    Linux基操—13.修改PATH
    nginx关于访问自定义路径php页面问题
    Linux基操—20.制逻辑卷
    Linux装配—18.yum方式安装各类服务
    spring boot实现AOP,Filter,Interceptor以及3个之间的区别和使用场景
    linux内核研究笔记(一) page介绍
    总结一下遇到过的网络同步IO导致服务阻塞的问题
    Chaos网络库(二) Buffer的设计
    Chaos网络库(三) 主循环及异步消息的实现
  • 原文地址:https://www.cnblogs.com/yangshuyuan1009/p/10119790.html
Copyright © 2020-2023  润新知