MySQL学习笔记

MySQL
帖子链接,写在前边(MySQL语句优化)
链接一:MySQL语句优化
https://mp.weixin.qq.com/s/7m0ORClmPU5OO4p5PD-wGA
链接二:MySQL查重删重
https://mp.weixin.qq.com/s/FzbDeFheu6Wy1SZNVT5tUQ

  • 进入数据库及查看数据库状态

    • 进入数据库
      • 有密码式本地登录
        • mysql -uroot -p 
          
      • 远程登录
        • mysql -h 192.168.5.116(远程地址) -P 3306 -u root -p123456
          
    • 查看数据库状态
      • sudo /etc/init.d/mysql start
        sudo /etc/init.d/mysql restart
        sudo /etc/init.d/mysql stop
        sudo /etc/init.d/mysql status
        
  • 创建

    • 创建数据库
      • create database 库名 default charset utf8 collate utf8_general_ci;
        
    • 创建表
      • create table 表名 (字段1 类型1 约束,字段2 类型2 约束...);
        
  • 查询

    • 查询库
      • show databases;
        
    • 查询当前所在库是什么
      • select database();
        
    • 查询库里的表
      • show tables;
        
    • 查询表里的信息
      • select * from 表名;
        #指定字段查询
        select * from 表名 where 查询条件;
        
    • 子查询
      • 查询语句包含另一个查询
        • select * from 第一个表名 where 字段名 in (select distinct 字段名 from 第二个表);
          #单表子查询
          select * from 表名 where 字段名 > (select avg(字段名) from 表名);
          #多表子查询
          select * from 表名 where 字段名 in (select distinct 二字段名 from 二表名l where 二字段名 > 5000);
          
    • 模糊查询
      • #以D开头
        select * from acct where acct_name like 'D%';
        #以D结尾
        select * from acct where acct_name like '%D';
        #含有该字符
        select * from acct where acct_name like '%D%';
        
    • 逻辑查询
      • select * from  表名 where 条件一 and 条件二;
        select * from  表名 where 条件一 or 条件二;
        
    • 范围查询
      • select * from 表名 where 字段名 in (范围值);
        select * from 表名 where 字段名 not in (范围值);
        select * from 表名 where 字段名 between 值1 and 值2;
        
    • 排序查询
      • #升序
        select * from acct order by balance asc;
        #降序
        select * from acct order by balance desc;
        #多条件排序
        select * from acct order by balance desc,acct_no asc;
        
    • 聚合操作
      • max/min
        • select max(balance) from acct;
          select min(balance) from acct;
          
      • avg
        • select avg(balance) from acct;
          
      • sum
        • select sum(balance) from acct;
          
      • count
        • select count(*) from acct; null不进行统计
          
    • 分组查询
      • 查询acct_type字段中全部的个数,*可进行修改
      • select count(*),acct_type from acct group by acct_type;
        
    • 过滤查询
      • 查询字段中非空的总数
      • select acct_type,sum(balance) from acct group by acct_type having acct_type is not null; 
        
    • SQL语句执行的过程
      • 第一步:from acct
      • 第二步:where 条件过滤
      • 第三部:group 子句
      • 第四部:sum(balance),acct_type,聚合
      • 第五步:having acct_type is not null ,过滤
      • 第六步:order by acct_type desc ,排序
      • 第七步:limit m,n ,限定
    • 去重查询
      • select distinct(字段名称) from 表名称;
        
    • 限定查询
      • select 字段名 from 表名 order by 字段名 desc limit m,n;
        
  • 修改

    • 修改字段
      • 修改字段类型
        • alter table 表名 modify 字段名 类型(长度) 约束类型;
          
      • 修改字段名称
        • alter table 表名 change 旧字段名 新字段名 类型(宽度);
          
  • 删除

    • 删除库
      • drop database 库名称;
        
    • 删除表
      • drop table 表名称;
        
    • 删除数据
      • delete from 表名称 where 条件表达式;
        
    • 删除字段
      • alter table 表名 drop 字段名;
        
    • 删除外键
      • alter table 表名 drop foreign key 外键名;
        
  • 增加

    • 插入数据
      • insert into 表名(需要插入的字段) values(插入的值);
        
    • 更新数据
      • update 表名 set 字段名=值 where 条件表达式;
        
  • 约束类型

    • 非空约束( not null )
    • 唯一约束( unique )
    • 主键约束( primary key)
    • 默认值(字段值不填写时)
    • 自动约束( primary key auto_increment)
    • 外键约束
      • create table account(
        	  acct_no varchar(32) primary key,
        	  cust_no varchar(32) not null,
        	  -- 在当前表的cust_no上添加外键约束
        	  -- 参照customer表的cust_no字段
        	  constraint foreign key(cust_no)
        	  references customer(cust_no) 
        	);
        
  • 数值类型

    • 数值类型
      • 整数
      • 浮点数
    • 字符串类型
      • char
      • varchar
      • text
    • 日期时间类型
      • now() 当前系统时间
      • sysdate() Oracle下的系统时间
      • curdate() 当前系统日期
      • curtime() 当前系统时间
      • year() 指定日期中的年份
      • month() 指定日期中的月份
      • day() 指定日期中的天
      • date() 指定日期时间中的年月日
      • time() 指定日期时间中的时分秒
    • 枚举类型
      • enum 从给定的范围选取一个
        • create table enum_test(
          		    name varchar(32),
          		    sex enum('boy','girl'),-- 从两者选一
          		    course enum('music', 'dance', 'paint')
          		);
          
      • set 从给定的范围选取一个或多个
        • create table enum_test(
          		    name varchar(32),
          		    sex enum('boy','girl'),-- 从两者选一
          		    course set('music', 'dance', 'paint')
          		);
          

你可能感兴趣的:(MySQL,MySQL,MySQL数据优化,MySQL数据查重)