01-Mysql笔记
1、打开数据库流程
(1)进入mysql命令:
     /usr/local/mysql/bin/mysql -u root -p 
(2)展示数据库:
     show databases ; 
(3)切换数据库:
      use 数据库名称
(4)展示数据库中的表:
      show tables;
(5)创建数据库:
      create database test
(6)创建表: 
     create table test(id int,name varchar(20))
(7)插入数据:
     insert into test values(1,"aaa")
(8)更新数据:
     update test set name="updatename" where id = 2
(9)删除数据库:
     drop database test;
(10)删除表的数据:
     delete from test;
(11)删除表数据和表结构:
      drop table test;
(12)退出数据库:exit
2、常用数据库操作
(1)创建数据库,指定为utf-8格式
     create database 数据库名 character set utf8;
(2)创建数据库,指定为utf-8格式,并添加校对规则;
     create database 数据库名 character set utf8 collate 校对规则;  //校对规则如:utf8_bin
(3)查看数据库定义的语句
     show create database 数据库名称 ;
3、修改数据库操作
  (1)修改数据库字符集
      alter database 数据库名字 character set 字符集;
      alter database test character set gbk;
4、其他数据库操作
   (1)查看一下当前正在使用的数据库
        select database();
5、表的CRUD操作
(1)创建表
     create table 表名 (
              列名  列的类型(长度)约束,
              列名1  列的类型(长度)约束
            );
(2)类型
     java            mysql
     int
     char/string     char/vachar 
                     char:固定长度
                     varchar:可变长度
                     char(3) 
                     varchar(3) 
                     长度代表的是字符的个数
     double          double    
     float           float
     boolean         boolean
     date            date : YYYY-MM-DD
                     time :hh:mm:ss
                     datetime : YYYY-MM-DD hh:mm:ss 默认为null
                     timestamp : YYYY-MM-DD hh:mm:ss 默认使用当前时间
(3)列的约束
     主键约束:primary key
     唯一约束:unique
     非空约束:not null        
(4)查看表;
     --查看所有表
     show tables; 
     --查看表的定义结构
     show create table test;
     -- 查看表的结构
     desc 表名
(5)修改表;
     --添加(add)
     alter table 表名 add 列名 列的类型  列的约束
     alter table student add chengji int not null;
     --修改列(mobify)
     alter table student modify  sex varchar(2);
     --修改列名(change)
     alter table student change sex gender varcher(2);
     --删除列
     alter table student drop chengji;
     --修改表名(rename)
     rename tabe student to heima;
     --修改表的字符集
     alter table heima character set gbk;
(6)删除表
     -- drop table heima;
6、SQL完成对表中数据的CRUD的操作
(1)插入数据库
     --简单写法
      insert into 表名 (列1,列2,列3) values(值1,值2,值3);//注:如果插入是全列名数据,表名后面的列名可以省略
      insert into student(sid,sname,sex,age)values(2,'zhangsan',1,26);
     -- 如果插入部分列的话,列名不能省略
      insert into student VALUES(3,'huangliang',1,23),(4,'huangliang',1,24),(5,'lisi',1,23),(6,'wangwu',1,28);
     -- 批量插入
     insert into student values(4,'zhangsan' ,1,26),(5,'zhangsan' ,1,23),(6,'zhangsan' ,1,28),(7,'zhangsan’,1,29),
     (8,'zhangsan’,1,24);
     -- 单条插入和批量插入的效率
(2)删除记录
     delete from 表名 [where 条件]
     delete from student where  sid=10;
     delete from student;//如果没有指定条件,会将表中数据一条一条全部删除
     -- 区分delete 删除数据 和truncate 删除数据有什么差别
     delete:DML 一条一条删除表中的数据
     truncate:DDL 先删除表再重建表
     -- 关于哪条执行效率高:具体要看表中的数据量
         如果数据量比较少,delete比较高效
         如果是数据比较多,truncate比较高效;
(3)更新表记录
     update 表名 set 列名=列的值,列名2=列的值2 [whert 条件]
     -- 讲sid为3的名字改成李四
     update student set sname='李四' where sid = 3;
     update student set sname='李四',sex=0;
(4)查看表中数据;
        select *from student;
     -- 去除重复的数据
           distinct 
     -- 商品分类:手机数码,鞋靴箱包
     1.分类ID
     2.分类名称
     3.分类描述
     create table category(
     cid int primary  key auto_increment,
     cname varchar(10),
     cdesc  varchar(30),
     );
     -- 所有商品
          1.商品ID
     2.商品名称    
     3.商品价格
     4.生产日期    
     5.商品分类ID
     -- 商品和商品分类:所属关系
     create table product(
         pid int primary key auto_increment,
         pname varchar(10),
         price double,
         pdate timestamp,
         cno int
     );
     -- 插入数据
     insert into product values(null,'小米8',998,'2020-09-17 16:11:11',1);
     insert into product values(null,'锤子',2888,'2020-09-17 16:11:11',1);
     insert into product values(null,'阿迪王',99,'2020-09-17 16:11:11',2);
     insert into product values(null,'老村长',88,'2020-09-17 16:11:11',3);
     insert into product values(null,'四特酒',55,'2020-09-17 16:11:11',3);
     insert into product values(null,'小熊饼干',1,'2020-09-17 16:11:11',4);
     insert into product values(null,'卫龙辣条',2,'2020-09-17 16:11:11',5);
     insert into product values(null,'旺旺大饼',1,'2020-09-17 16:11:11',5);
     -- 查询所有数据
      select *from product;
     -- 查询商品名称+商品价格
     select pname,price from product;
     -- 别名查询. as 关键字,as 关键词可以省略
        -- 表别名:select p.name, p.price from product p;(主要用在多表查询);
        select p.name,p.price from pruduct as p;
     -- 列别名:select pname as 商品名称,price as 商品价格from product;
     select pname as 商品名称,price as 商品价格 from product;
     -- 去掉重复的值
       -- 查询商品所有价格
       select price from product;
       select distinct price from product;
     -- select运算查询
       select *,price*1.5 from product;
       select *,price*1.5 as 折后价 from product;    
     -- 查询商品价格>60元的所有商品信息
       select * from product where price>60;
     -- where 后的条件写法
         -- 关系运算符:> >= < <= = != <>
           <>:不等于:标准SQL语法
           !=:不等于:非标准SQL语法
     select *from product where price <>2;
     -- 逻辑运算:and ,or,not
     -- 查询商品价格在10 到 100之间
     select *from product whert price >10 and price <100;
     between….and….
     select * from product where price beween 10 and 100;
     -- 查询出商品价格小于 35 或者商品价格大于900
     select *from product where price <35 or price >900;
     -- like模糊查询
           _ :代表的是一个字符
       %:代表的是多个字符
     -- 查询出名字中带有饼的所有商品 ( '%饼%' )
     select *from product where pname like '%饼%';
     -- 查询第二名字是”熊”的所有商品  
     select *from product where pname like '_熊%’;
     -- in 在某个范围中获得值
         --查询出商品分类ID1,4,5里面的所有商品
     select *from product where cuo in(1,4,5);
     -- 排序查询:order by 关键字
         asc:ascend 升序(默认排序方式)
         desc:descend 降序
         --(0)查询所有商品,按照价格进行降序排序(asc-升序  desc-降序)
            select  *from product order by price;
         --(1)查询所有商品,按照价格进行降序排序
            select *from product order by price desc;
         --(2)查询名称有“小”的商品,按价格升序
               1.查询名称有”小”的商品
                select *from product where pname like '%小%';
               2.进行排序得出结果
                select *from product where pname like '%小%' obder by price desc;
      -- 聚合函数(注意:where条件后面不能接聚合函数)
         sum():求和
         avg():求平均
         count():统计数据量
         max():最大值
         min():最小值
      --查询所有商品的求和
        select sum(price) from product;
      --查询所有商品的平均价格
        select  avg(price) from product;
      --查询商品价格大于平均价格的所有商品
       select *from product where price > (select avg(price) from product);
      --分组:group by
       1.根据cno字段分组,分组后统计商品的个数
       select cno,count(*) from product group by cno;
       2.根据cno分组,分组统计每组商品的平均价格 并且商品平均价格>60
       select cno,avg(price)
       from product group by cno
       having avg(price) > 60; 
      --having 关键词可以接聚合函数,出现分组之后
      --where 关键字它是不可以接聚合函数,出现在分组之前
      —编写顺序 S..F..W..G..H..O
        select .. from ..where ..group by ..having ..order by
      —执行顺序 F..W..G..H..S..O
        from.. where.. gruop by ..having ..select .. order by
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号