大家好,今天我们来聊一聊什么是数据库,以及怎么操作?
一、什么是数据库,它有什么作用? 数据库(Database)是按照 数据结构来组织、 存储和管理数据的仓库。
二、数据库的基本操作 ---------增删改查 增:新建数据库: create database 数据库的名字; 删: drop database 数据库的名字; 改:一般不得修改数据库 查:show databases; //查看所有的数据库 实例 : show create database day07;
切换数据库, use 数据库名字 查看当前数据库: select database();
三、表的操作 增:新建数据库: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;
列的约束: 主键约束:primary key 唯一约束 : umique 非空约束: not null
四、表中数据的插入
标准写法:insert into 表名 (列名1,列名2) vaules(值1,值2); ps:插入的是全列名,可以不用写列名
批量插入;
insert into 表名 vaules (2,11),(3,5),(5,5); //注意中间的是逗号查看表中数据 select*from student;
五、表中数据的删除
- delete from 表名 [where 条件];
实例:delete from student where T_name = "张三"; 2.面试题 delete 和 truncate 删除有什么区别? delete : DML 一条一条删除表中的数据. -- 适用于数据比较少的时候 truncate : DDL 先删除表,再重建表中数据. -- 适用于数据比较多的时候.
六、解决乱码问题
七、更新表中结构 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";
如果不指定条件,就会把表里所有的全部给改了
八、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 表名;
- 别名查询 列别名:select price as 商品名称,pname as 商品价格 from product; 表别名:select p.price,p.pname from product as p;
3.去掉重复值 --- distinct 实例: select distinct price from product;
select 运算查询 select *,price *1.5 as 折后价 from product; 效果:在cno 后面多了一列,折后价,下面是新运算好的数据。
条件查询(关键字) 查询价格大于 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;
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);
排序查询: ( order by 关键字) asc : ascend 升序 desc : descend 降序
查询所有商品,按照价格排序 select * from product order by price;
降序排序 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的记录行数;
- 统计商品表中价格大于50的有多少条记录 SELECT COUNT(*) FROM products WHERE price>50;
- 获得商品价格的总和 select sum(price) from product;
- select avg(price) from product;
- 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)
- 根据商品中的 cno 字段,进行分组,再统计出商品的个数 select cno ,count(*) from product group by cno; // 分组的意思是将
- 根据 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