zoukankan      html  css  js  c++  java
  • 深入MySQL(一)

    一、MySQL基础语句

    create table student_info(
    id int primary key auto_increment,
    name varchar(32),
    age int,
    register_date date

    ) engine=innodb;

    insert into student_info(name,age,register_date) values('huhuijun',22,'2018-03-01');
    insert into student_info(name,age,register_date) values('yanggang',21,'2016-09-01');
    insert into student_info(name,age,register_date) values('maojunxian',23,'2017-08-01');
    insert into student_info(name,age,register_date) values('zhangliang',33,'2018-06-21');

    create table study(
    id int auto_increment primary key,
    stu_id int not null,
    day int,
    status varchar(32) not null,
    foreign key(stu_id) references student_info(id)
    ) engine=innodb;
    insert into study(stu_id,day,status) values(1,1,'YES');
    insert into study(stu_id,day,status) values(2,1,'YES');
    insert into study(stu_id,day,status) values(3,1,'NO');

    #查询不重复的记录
    select name distinct from student_info;

    #内连接仅选出两张表中互相匹配的记录,而外连接会选出其他不匹配的记录
    #内链接
    select
    st.name,
    st.age,
    st.register_date,

    sd.day,
    sd.status
    from student_info as st
    inner join study as sd
    on st.id=sd.stu_id;

    #左外连接
    select
    st.name,
    st.age,
    st.register_date,

    sd.day,
    sd.status
    from student_info as st
    left outer join study as sd
    on st.id=sd.stu_id;

    #右外连接
    select
    st.name,
    st.age,
    st.register_date,

    sd.day,
    sd.status
    from student_info as st
    right outer join study as sd
    on st.id=sd.stu_id;


    #分组查询
    select day,count(day) from study group by day;

    #with rollup表示是否对分类聚合的结果再汇总;
    select day, count(day) as '每天总共上课人数' from study group by day with rollup;

    #having 关键字表示对分类后的结果再进行条件的过滤;
    select day, count(day) as '每天总共上课人数' from study group by day with rollup having count(day)>3;

    #联合查询 把查询结果合并在一起
    select id from student_info union all select stu_id from study;
    select id from student_info union select stu_id from study;(去重后的查询结果)
    #

    #修改表字段类型
    alter table study modify day int not null default 1;
    #增加表字段
    alter table student_info add classic default 'Linux运维';
    #删除表字段
    alter table student_info drop sex;
    #修改字段名
    alter table student_info change classic 班级 varchar(12);

    #修改表名
    alter table student_info rename student;

    #修改表数据
    update student_info set age=27 where id=3;

    #分页查询(下面两个语句查询的结果一样)
    select * from study limit 3 offset 0;
    select * from study limit 0,3;

    #DCL语句
    #创建用户
    create user 'tanghu'@'localhost' identified by 'admin123';
    #赋予用户权限
    grant select,update,insert on student.* to 'tanghu'@'localhost' identified by 'admin123';

    revoke update,insert on student.* from 'tanghu'@'localhost' identified by 'admin123';


    create table t1(id1 int,id2 int(5));

    二、MySQL的数据类型

    对于小数的表示方式,MySQL分为两种方式:浮点数和定点数。浮点数包括float和double,而定点数只有decimal一种表示,
    定点数在MySQL内部以字符串形式存放,比浮点数更精确,适合用来表示货币等精度高的数据;

    #查看数据库时区
    show variables like 'time_zone';
    #设置时区
    set time_zone='+9:00';

    #timestamp 和时区相关,当插入日期时,会先转换为本地时区后存放;而从数据库里面取出时,
    也同样需要将日期转换为本地时区后显示;这样两个不同时区的用户看到的同一个日期可能是不一样的;


    varchar 和 char 类型:
    都用来保存MySQL中较短的字符串。二者主要的区别在于存储的方式不同;char列的长度固定为创建表
    时声明的长度,长度可以为从0-255的任何值;而varchar 列中的值为可变长字符串,长度可以指定为
    0-255或者65535之间的值。检索的时候,char列删除了尾部的空格,而varchar则保留了这些空格;enum 和 set 类型

    enum 它的值的范围需要在创建表时通过枚举方式显示指定,对1-255个成员的枚举需要1个字节存储;对于255-65536个成员,需要2个字节存储。最多允许有65535个成员

    enum的类型时忽略大小写的

    enum 类型只允许从值集合中选取单个值,而不能一次取多个值;

    set 和 enum 类似,也是一个字符串对象,里面可以包含0-64个成员。8个成员占一个字节.
    set 和 enum 的区别除了存储之外,最主要的区别在于set类型一次可以选取多个成员,而enum只能选取一个。

  • 相关阅读:
    操作winrar
    lucene.NET详细使用与优化详解
    js实现记住帐号或密码(js读写COOKIE)
    jQuery对select操作小结
    XMLHelper
    AJAX2用法
    as3Crypto and php, what a fun ride!
    linux文件描述符导致squid拒绝服务
    Centos 增加硬盘
    安装Squid log analyzer分析工具
  • 原文地址:https://www.cnblogs.com/tanghu/p/9644897.html
Copyright © 2011-2022 走看看