MYSQL 笔记(一)

你们好,今天咱们来聊一聊什么是数据库,以及怎么操做?面试

1、什么是数据库,它有什么做用? 数据库(Database)是按照 数据结构来组织、 存储和管理数据的仓库。数据库

2、数据库的基本操做 ---------增删改查 增:新建数据库: create database 数据库的名字; 删: drop database 数据库的名字; 改:通常不得修改数据库 查:show databases; //查看全部的数据库 实例 : show create database day07;数据结构


切换数据库, use 数据库名字 查看当前数据库: select database();函数

3、表的操做 增:新建数据库:create table 表名( 列名 列的类型(长度) 约束) 实例:create table student( sid int primary key, sname varchar(10), sex int, age int); 删:drop table 表名; 删除列:alter table student drop chengji; 改: 添加列:alter table 表名 add 列名 列的类型 列的约束 add :alter table student add sex varchar(2);
modify: alter table student modify sex varchar(2); 通常不要动他:修改列名:alter table student change kaijian daishiqi int; 修改表名: rename table student to daishiqi; 修改表的字符集: alter table daishiqi character set gbk;排序

查:show tables; //查看全部的表 show create database day06;//查看 desc student;ci

列的约束: 主键约束:primary key 惟一约束 : umique 非空约束: not nullrem

4、表中数据的插入产品

  1. 标准写法:insert into 表名 (列名1,列名2) vaules(值1,值2); ps:插入的是全列名,能够不用写列名table

  2. 批量插入;
    insert into 表名 vaules (2,11),(3,5),(5,5); //注意中间的是逗号乱码

  3. 查看表中数据 select*from student;

5、表中数据的删除

  1. delete from 表名 [where 条件];
    实例:delete from student where T_name = "张三"; 2.面试题 delete 和 truncate 删除有什么区别? delete : DML 一条一条删除表中的数据. -- 适用于数据比较少的时候 truncate : DDL 先删除表,再重建表中数据. -- 适用于数据比较多的时候.

6、解决乱码问题

7、更新表中结构 1.update 表名称 set 列名称=新值 where 更新条件; 实例:将 id 为 5 的手机号改成默认的 - : update students settel=default where id=5; 将全部人的年龄增长 1: update students set age=age+1; 将手机号为 13288097888 的姓名改成 "小明", 年龄改成 19: update students setname="小明", age=19 wheretel="13288097888";

若是不指定条件,就会把表里全部的所有给改了

8、select 的简单查询 (字段就是类型) 1.先创建一个表,插入数据 create table category( cid int primary key autoincrement, cname varchar(18), cdesc varchar(31) ); //autoincrement id 自增加

insert into category values(null,'手机数码' , '电子产品,黑马生产'); insert into category values(null,'鞋子箱包' , '江南皮革厂,倾情打造'); insert into category values(null,'酸奶饼干' , '安慕希,娃哈哈'); insert into category values(null,'餐追进是' , '辣条,宝批龙'); insert into category values(null,'餐追进是' , '辣条,宝批龙'); select cname , cdesc from category; //只有后面两列的数据 简单查询: select*from category;

上面是例子,说明了 select 的用法 ,便是 select 列名,列名 from 表名;

  1. 别名查询 列别名:select price as 商品名称,pname as 商品价格 from product; 表别名:select p.price,p.pname from product as p;

3.去掉重复值 --- distinct 实例: select distinct price from product;

  1. select 运算查询 select *,price *1.5 as 折后价 from product; 效果:在cno 后面多了一列,折后价,下面是新运算好的数据。

  2. 条件查询(关键字) 查询价格大于 60 : select * from product where price > 60; 同理:有<> != 两种方式的不等于 between and select * from product where price between 10 and 100; 逻辑运算: and or not 实例:select * from product where price > 60 and price < 100;

  3. like 模糊查询 (where 关键字) _ : 表明的是一个字符 %: 表明的是多个字符

--查询出名字中带有饼的全部商品 '%饼%' select * from product where pname like '%饼%'; 查询第二个名字带熊的全部商品 '熊' select * from product where pname like '饼%'; in 在某个范围中得到值 查询出商品分类ID在1,4,5 里面的商品 select * from product where cno (1,4,5);

  1. 排序查询: ( order by 关键字) asc : ascend 升序 desc : descend 降序

  2. 查询全部商品,按照价格排序 select * from product order by price;

  3. 降序排序 select * from product order by price desc; desc 能够换成 asc 2.升序排序 查询名称中带有 '小' 商品 select * from product where pname like '%小%'; 进行排序得出结果 select * from product where pname like '%小%' order by price asc; 上面出来后能够排序

8.聚合函数 sum() avg() 求平均值 count() 统计数量 max() min() count:统计指定列不为NULL的记录行数;

  1. 统计商品表中价格大于50的有多少条记录 SELECT COUNT(*) FROM products WHERE price>50;
  2. 得到商品价格的总和 select sum(price) from product;
  3. select avg(price) from product;
  4. select count(price) from product;

注意: where 后面不能接聚合函数

select * from product where peice > avg (price); // 这时错的!!!!,不能这样使用

查出商品价格大于平均价格的全部商品: 子查询:
select * from product where price > ( select avg(price) from product);

9.分组: ( 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 ; // by 后面是分组, select 后面是先显示的东西 having 关键字 能够接在聚合函数的 出如今分组以后,做用和 where 差很少 where 关键字 他是不能够接聚合函数 ,出如今分组以前

编写顺序: s … f …w .g ….h… o… select … from… where …group by … having ..order by

执行顺序: f…w…g…h…s…o from … where …group by …having…select …order by

相关文章
相关标签/搜索