你们好,今天咱们来聊一聊什么是数据库,以及怎么操做?面试
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、表中数据的插入产品
标准写法:insert into 表名 (列名1,列名2) vaules(值1,值2); ps:插入的是全列名,能够不用写列名table
批量插入;
insert into 表名 vaules (2,11),(3,5),(5,5); //注意中间的是逗号乱码
查看表中数据 select*from student;
5、表中数据的删除
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 表名;
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的记录行数;
注意: where 后面不能接聚合函数
select * from product where peice > avg (price); // 这时错的!!!!,不能这样使用
查出商品价格大于平均价格的全部商品: 子查询:
select * from product where price > ( select avg(price) from product);
9.分组: ( group by)
编写顺序: 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