码迷,mamicode.com
首页 > 数据库 > 详细

MySQL学习笔记

时间:2017-03-30 13:36:56      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:group by   birt   定义   base   卡尔   常用sql   reference   保留   表达   

1、启动

  net start(stop) mysql

  mysql -h localhost -u root -p

2、数据库的操作

  create database 数据库名;

  show databases;

  use 数据库名;  -->选择数据库使用

  drop database 数据库名;  -->删除数据库

3、数据表的操作

  1).CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition...)] [table_options] [select_statement]

    create_definition:

      col_name type [not null|null] [default default_value] [auto_increment][unique_key][primary_key][comment ‘string‘][reference_definition]

  2).show tables from 数据库名;

    show columns from 数据表名 from 数据库名;

  3).describe 数据库名.数据表名 字段名;    -->查看某个指定字段的信息

  4).修改表的结构

    alter table 原数据表名 rename 新表名;

    alter table 数据表名 change 原字段名 新字段定义;  

    alter table 数据表名 add 增加的字段定义;

    alter table 数据表名 drop 字段名;

  5).删除数据表

    drop table 数据表名,...;

  6)备份与恢复

    a.备份 mysqldump --opt 数据库名 -h localhost -u root -p -r 备份路径

    b.恢复 mysql -h localhost -u root -p 数据库名 < 备份路径

4、常用SQL语句

  1)查询语句(SELECT)

    a.简单SELECT查询

      select 字段名 from 数据表名

      where 条件表达式

            select name from student
          -> where birthday >= ‘1990-01-01‘;

    b.带结果排序的SELECT查询

      select 字段名 from 数据表名

      where 条件表达式

      order by 字段名 ASC(DESC) 

          select name,birthday from student
        -> where year(birthday) >= 1990
        -> order by birthday desc;

    c.限定结果条数的SELECT查询    -->常与order by 一起使用

     select 字段名 from 数据表名

      where 条件表达式

      limit [offset,] row_count    -->从offset(默认0)开始返回row_count条记录

          select name,birthday from student
        -> where year(birthday) >= 1990
        -> limit 3;

  2)插入语句(INSTER)

    a.insert ..values 语句

      insert  into 数据表名(字段名,...)    -->若没有缺省字段,则字段名可以省略

      values(字段值,...),    -->要一一对应

        (字段值...);

    b.insert..set语句

      insert into 数据表名

      set 字段名=字段值,

      ...;

  3)更新语句(UPDATE)

    update 数据表名

    set 字段名=字段值,...

    [where 查询条件]    -->可以接order by 和limit 语句

  4)删除语句(DELETE)

    delete from 数据表名

    where 查询条件

5、高级查询语句

  1)聚合函数  -->count,max,min,sum,avg

    a.count  -->返回统计结果的行数

      select count(*) from stu_mark

        where mark >= 90;

      select count(distinct mark) from stu_mark

        where mark >= 90;      -->删除字段值重复的记录,使用distinct关键字

    b.max(min)

      select *,max(mark) from stu_mark

        where mark >= 90;

    c.sum(avg)

      select sum(mark) from stu_mark

        where mark >= 90;

  2)分组查询    -->对表中的记录按照字段进行分组,然后对每个分组用聚合函数进行查询计算

    select 聚合函数 字段名,... from 表名

    where 查询条件

    group by 字段名

    having 过滤条件;    -->分组后对记录进行过滤,where在分组前对记录进行过滤,having可以使用聚合函数

  3)联合查询    -->使用union,将两个及以上的select查询结果合并为一个结果集显示

    select 语句

    union[all]    -->all省略,则联合结果中重复行只保留一行,否则保留所有行

    select 语句

    union[all] select 语句...    -->联合查询时查询结果字段名为第一个表的字段名

  4)连接查询    -->查询存放在多张数据表中的信息,连接类型分为交叉、内、外三种

    select 字段名,... from 数据表1名

    连接类型 数据表2名

    [on 连接条件]

    a.交叉连接(cross join) --> 返回连接表中所有数据行的笛卡尔积

      select stu_info.name,stu_info.major_id from stu_info

      cross join major;

    b.内连接(inner join)  -->返回连接表中与连接条件相匹配的行

      select stu_info.name,major.major_name from stu_info  -->数据表名.字段名

      inner join major

      on stu_info.major_id = major.major_id;  -->等值、不等和自然连接三种

    c.外连接    -->left join  right join   full join

      不仅列出符合连接条件匹配的行,还列出了左表(右,全)中所有符合搜索条件的数据行

  5)子查询    -->进行查询的条件是另一个select语句的结果,使用子查询,in、not in、=、!=、exists、not exists等

    a.in子查询

      select * from stu_info

      where major_id in

      (select major_id from major);

    b.exists子查询    -->查询条件为exists后面的select语句查询结果不为空

      select * from stu_info

      where exists

      (select major_id from major

      where major_id = 1);

      

MySQL学习笔记

标签:group by   birt   定义   base   卡尔   常用sql   reference   保留   表达   

原文地址:http://www.cnblogs.com/bigger-class/p/6624644.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!