您的位置:首页 > 数据库 > MySQL

mysql基础

2017-07-03 10:17 369 查看
MySQL基础

1、安装

   1)编码

   2)密码

2、配置文件:my.ini

   1)设置端口号

     port = 3306

   2)设置mysql编码

     default-character-set=utf8

     character-set-server=utf8

   3)最大连接数

     max_connections = 100

   4)设置存储引擎

     default-storage-engine = INNODB

   注意:重启MySQL服务

3、登陆mysql数据库:打开命令提示符窗口

   mysql -h主机地址 -r端口号 -u用户名 -p密码

   错误:"mysql不是系统内部或外部的命令..."

   解决:将mysql的bin目录,配置到系统的path环境变量

   变量名:Path

   变量值:asdfjasdfads;D:\mysql5.7\bin

4、查看mysql数据库的编码

   show variables like 'character%';

   +--------------------------+-----------------------------+

   | Variable_name            | Value                       |

   +--------------------------+-----------------------------+

   | character_set_client     | utf8     (客户端编码)                   |

   | character_set_connection | utf8     (连接数据库的编码)                   |

   | character_set_database   | utf8     (数据库本身的编码)                   |

   | character_set_filesystem | binary   (表文件是什么格式的文件)                   |

   | character_set_results    | utf8     (结果集编码)                   |

   | character_set_server     | utf8     (mysql服务的编码)                   |

   | character_set_system     | utf8     (mysql系统本身的编码)                   |

   | character_sets_dir       | d:\mysql5.7\share\charsets\ |

   +--------------------------+-----------------------------+

5、设置客户端窗口的编码

   set names gbk;

   注意:

   1)MySQL5.5版本或更低的版本:必须执行set names gbk;

   2)MySQL5.7版本:不用执行set names gbk;

6、查看现存的所有库

   show databases;

7、进入库(使用库)

   use 库名;

8、查看库中现存的表

   show tables;

9、查看表结构

   desc 表名;

10、建表

    create table 表名

    (
字段   数据类型    约束,
字段   数据类型    约束,
字段   数据类型    约束

    );

11、数据类型

    int   float   char    varchar    text   timestamp

12、字段约束

    1)主键约束

    2)外键约束

    3)非空约束

    4)空约束

    5)唯一约束

    6)默认值

    7)自增长

13、修改表结构

    1)修改表名

      alter table 原名 rename to 新名;

    2)添加字段

      alter table 表名 add column 字段名 数据类型 约束;

      alter table 表名 add 字段名 数据类型 约束;

    3)删除字段

      alter table 表名 drop column 字段名;

      alter table 表名 drop 字段名;

    4)修改字段

      alter table 表名 change 原字段名 新字段名 数据类型 约束;

14、添加记录

    insert into 表名(字段...)values(值...);

15、修改记录

    update 表名 set 字段=值,字段=值;

    update 表名 set 字段=值,字段=值 where 条件;

16、删除记录

    delete from 表名;

    delete from 表名 where 条件;

17、清空表

    delete from 表名;     不恢复自增长id   慢   恢复数据   支持事务

    truncate table 表名;  恢复自增长id     快   不可恢复   不支持事务

18、普通查询

    select * from 表名;

    select 字段,字段... from 表名;

19、条件查询

    select * from 表名 where 条件;

20、排序

    select * from 表名 order by 字段;

    select * from 表名 order by 字段 desc;

    select * from 表名 where 条件 order by 字段;

21、分组

    select * from 表名 group by 字段;

22、聚合查询(统计查询)

    select count(*) from 表名 where 条件;

    select sum(字段) from 表名 where 条件;

    select avg(字段) from 表名 where 条件;

    select max(字段) from 表名 where 条件;

    select min(字段) from 表名 where 条件;

23、分页查询

    select * from 表名 limit 起始值,条数;

    select * from 表名 where 条件 order by 字段 limit 起始值,条数;

24、模糊查询(搜索)

    %:代表任意长度的任意字符

    _:代表任意一个字符

    select * from 学生表 where 姓名 like '张%';

    select * from 学生表 where 姓名 like '张__';

    select * from 学生表 where 姓名 like '%小%';

25、子查询(嵌套查询)

    select * from 表名 where 字段 in (select语句);

    delete from 表名 where 字段 in (select语句);

    update 表名 set 字段=值 where 字段 in (select语句);

26、内连接

    select * from 表1 inner join 表2 on 关联的字段相等;

27、内连接另一种写法

    select * from 表1,表2,表3 where 关联的字段相等;

28、外连接

    1)左外连接:左表为主、右表为副

      select * from 表1 left outer join 表2 on 关联的字段相等;

    2)右外连接:以右表为主,左表为副

      select * from 表1 right outer join 表2 on 关联的字段相等;

多表查询

1、子查询:查询出来的结果集中只有一个表的数据

2、内连接:合并字段

3、外连接:合并字段

多表查询的使用

1、子查询:被动使用

2、内连接:查询多张表中的数据

3、外连接:不用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql