当前位置: 首页 > news >正文

国企网站开发做网站用哪个笔记本

国企网站开发,做网站用哪个笔记本,小说网站代理,阿里云的国际网站建设###数据库之前通过流去操作文件保存数据库的弊端:1.执行效率低2.开发成本高3.一般只能保存小量数据4.只能保存文本数据####什么是DB- DataBase 数据库:代表文件集合####什么是DBMS- DataBaseManagementSystem 数据库管理系统(软件),用于管理保存数据的文件集合,用于和程序员进行…###数据库之前通过流去操作文件保存数据库的弊端: 1.执行效率低 2.开发成本高 3.一般只能保存小量数据 4.只能保存文本数据####什么是DB - DataBase 数据库:代表文件集合 ####什么是DBMS - DataBaseManagementSystem 数据库管理系统(软件),用于管理保存数据的文件集合,用于和程序员进行交互,常 见的DBMS有:Oracle MySQL DB2 SQLServer Sqlite####SQL - Structured Query Language:结构化查询语言,用户程序员和DBMS进行交互,用于程序员告诉DBMS到底对数据进 行什么操作的 ###数据库的分类(了解) - 关系型数据库:经过数据理论验证可以将现实生活中的各种关系保存到数据库,这种就称为关系型数据库.保存数据以表为单位 - 非关系数据库:一般都是为了解决特定场景的问题 比如:缓存,高并发访问,Redis数据库(以key-value形式保存数据)###常见的关系数据库- MySQL:属于Oracle公司的产品, - Oracle:- DB2:- SQLServer:- Sqlite:###打开客户端链接MySQL - 在终端中执行:mysql -uroot -p 回车 如果有密码写密码 回车,如果没有密码则直接回车###和数据库相关的SQL###查询所有的数据库 show databases;###创建数据库 create database db1;###查看数据库详情 show create database db1;###创建数据库指定字符集 create database db2 character set gbk/utf8; ###删除数据库 drop database db2;###和表相关的SQL###查询所有表- show tables;###创建表- create table 表名(字段1名 字段1的类型,字段2名 字段2的类型,...); create table person(name varchar(10),age int);###查看表详情 - show create table 表名;###常见表时指定表的引擎和字符集- create table t1(name varchar(10)) enginemyisam charsetgbk;###表的引擎- innodb :支持数据库的高级操作 如:外键,事务等,默认引擎- myisam:只支持基础的增删改查操作###SQL格式:1.可以有换行2.最后以;结尾3.关键字之间需要有空格(可以写多个空格,建议写一个)###查看表字段- desc 表名;###删除表- drop table 表名;###修改相关表1.修改表名 rename table 原名 to 新名; rename table student to stu;2.修改表的引擎和字符集- alter table 表名 enginemyisam/innodb charsetutf8/gbk; alter table stu enginemyisam charsetgbk;3.添加表字段- 最后面添加:alter table 表名 add 字段名 字段类型;- 最前面添加:alter table 表名 add 字段名 字段类型 first;- xxx的后面添加:alter table 表名 add 字段名 字段类型 after xxx; create table hero(name varchar(10)); alter table hero add age int; alter table hero add id int first; alter table hero add sal int after name;4.删除表字段- alter table 表名 drop 字段名; alter table hero drop sal;5.修改表字段的名字和类型- alter table 表名 change 原字段名 新字段名 新字段类型; alter table hero change name heroname varchar(10);6.修改表字段的类型和位置- alter table 表名 modify 字段名 类型 位置; alter table hero modify age int first(after xxx);###数据相关####插入数据 create table emp(id int,name varchar(10),age int,sal int);-全表插入数据: - insert into emp values(1,tom,18,3000);-指定字段插入数据: - insert into emp (name,age ) values(terry,19); - insert into emp (name ) values(李白);-批量插入数据: - insert into emp values(3,刘备,28,6000),(4,张飞,20,5000),(5,关羽,25,9000); - insert into emp (name,age) values(悟空,500),(八戒,400),(沙僧,300);###查询数据-查询全部数据的全部字段信息 select * from emp;-查询所有员工的姓名和年龄 select name,age from emp;-查询年龄在25岁一下的员工信息 select * from emp where age25;-查询工资3000块钱的员工姓名,年龄,工资 select name,age,sal from emp where sal3000;####修改数据-修改Tom的工资为3333 update emp set sal3333 where nametom;-修改30岁一下的工资为6666 update emp set sal6666 where age30;-修改id等于3的名字为吕布 年龄为55 工资为20000 update emp set name吕布,age55,sal20000 where id3;-修改工资为null的工资为800 update emp set sal800 where salnull;####删除数据-删除id等于1的员工 delete from emp where id1;-删除年龄在25岁以下的员工 delete from emp where age25;-删除全部数据 delete from emp;###主键约束- 主键:用于表示数据唯一性的字段称为主键- 约束:是给表字段添加的限制条件- 主键约束:限制主键字段值不能重复并且非空(唯一且非空) create table t1(id int primary key,name varchar(10)); insert into t1 values(1,Tom); insert into t1 values(1,jerry);//失败,重复 insert into t1 values(null,ABC)//失败,不能为null- 自增: auto_increment create table t2(id int primary key auto_increment,name varchar(10)); insert into t2 values(null,AAA);// insert into t2 values(2,AAA); insert into t2 values(10,mm); insert intp t2 values(null,AAA); 1.当字段值为null的时候值会自己增长 2.自增字段值也可以手动赋值 3.增长规则:从曾经出现过的最大值基础上1 4.自增数值只增不减(delete清空表,自增数值并不清零)###注释 comment- 创建表声明字段的时候给字段添加的介绍 create table t3(id int primary key auto_increment comment 这是一个主键, name varchar(10) comment 这是员工的姓名);###和- :用于修饰表名和字段名 可以省略 create table t4(id int,name varchar(10));- :用于修饰字符串###数据冗余- 如果表设计不够合理,保存大量数据的同时可能随之会出现大量重复数据,这些重复数据的现象就称为数据冗余,通过拆分表的形式解决冗余问题###事务- 什么是事务:事务是数据库中执行SQL语句的最小执行单位,可以保证事务内的多条SQL语句要么全部成功,要么全部失败.- 查看数据库自动提交的状态 show variables like %autocommit%; - 关掉自动提交 0关闭 1开启 set autocommit0;pollk-测试转账: create table person(id int,name varchar(10),money int); inert into person values(1,超人,500),(2,钢铁侠,10000); -关掉自动提交: set autocommit0; 1.先让超人2000; update person set money2500 where id1; 2.开启另一个终端 验证 此时数据库文件的数据并没有改掉 3.让钢铁侠-2000 update person set money8000 where id2; 4.执行提交 commit;-回滚 rollback;-将内存的修改回滚到上次提交的点 update person set money100 where id1; rollback; 保存回滚点 savepoint update person set money100 where id1; savepoint s1; update person set money200 where id1; savepoint s2; rollback to s1;###SQL的分类####DDL Data Definition Language- 数据定义语言包括 create,alter,drop,truncate ,不支持事务####DML Data Manipulation Language- 数据操作语言 包括 insert,delete,update,select(DQL),支持事务####DQL Data Query Language- 数据查询语言只包括select和事务没有关系因为并没有修改数据####TCL Transaction Control Language- 事务控制语言包括 commit,rollback, savepoint,rollback to####DCL Data Control Language- 数据控制语言用于处理分配用户权限相关的操作###truncate - truncate table 表名;- 删除表并且创建一个新表- truncate、drop和delete的区别- delete用于删除数据使用delete清空表时自增数值不清零 执行效率最低- drop 用于删除表 执行效率最高- truncate 用于删除表并创建新的空表执行效率比delete要高而且自增数值会清零###数据库的数据类型####整数- 常用整数有 int(m)和bigint(m)m代表显示长度必须和zerofill结合使用 create table t_int(num int(10) zerofill); insert into t_int values(123); select * from t_int;####浮点数- 常用浮点数double(m,d) m代表总长度 d代表小数长度 23.346 m5 d3 - decimal超高精度浮点数应用场景涉及超高精度运算时使用 create table t_double(num double(5,3)); insert into t_double values(23.5678); 值为23.568 insert into t_double values(23.5); 值为23.500####字符串- char(m): 固定长度 m10 abc 占10效率高最大255- varchar(m):可变长度 m10 abc 占3节省空间最大65535如果超过255建议使用text- text(m):可变长度 最大65535####日期- date:只能保存年月日- time:只能保存时分秒- datetime:保存年月日时分秒默认值为null最大值9999-12-31- timestamp(时间戳距离19700101 080000):保存年月日时分秒默认值为当前系统时间,最大值2038-01-19 create table t_time(t1 date,t2 time,t3 datetime,t4 timestamp); insert into t_time values(2018-10-16,null,null,null); insert into t_time values (null,20:06:32,2008-11-22 18:22:11,null);配置URLjdbc:mysql://localhost:3306/db2?useUnicodetruecharacterEncodingUTF-8三方SQL工具sqlyog###is null和is not null1.查询emp表中没有上级领导mgr的员工编号empno,姓名ename,工资sal select empno ,ename,sal from emp where mgr is null;2.查询emp表中没有奖金comm的员工姓名,工资,奖金 select ename,sal,comm from emp where comm is null;3.查询有奖金的所有员工信息 select * from emp where comm is not null;###别名1.将查询到的员工姓名enamel改为姓名 select ename from emp; select ename as 姓名 from emp; select ename 姓名 from emp; select ename 姓名,sal 工资 from emp;###去重distinct1.查询emp表中出现的所有职位job select distinct job from emp;###比较运算符 ,,,,,!和1.查询工资小于1600的所有员工的姓名和工资 select ename ,sal from emp where sal1600;2.查询部门编号是20的所有员工姓名,职位和部门编号deptno select ename,job,deptno from emp where deptno20;3.查询职位是manager的所有员工姓名和职位 select ename,job from emp where jobmanager;4.查询部门不是10号部门的所有员工姓名和部门编号(两种方法) select ename,deptno from emp where deptno10 or deptno10; select ename,deptno from emp where deptno!10;5.查询商品表t_item中单价price等于23的商品信息 select * from t_item where price23;6.查询商品表中单价不等于8443的商品信息 select * from t_item where price!8443;###and 和 or- and 等效于java中的- or 等效于java中的||- 查询工资大于2000并且是10号部门的员工信息 select * from emp where sal2000 and deptno10;1.查询不是10号部门并且工资大于等于1600的员工姓名,工资,部门编号 select ename,sal,deptno from emp where deptno!10 and sal1600;2.查询部门是30号部门或者上级领导为7698的员工姓名,职位,上级领导和部门编号 select ename,job,mgr,deptno from emp where deptno30 or mgr7698;3.查询有上级领导并且工资低于2000,并且是20号部门的员工信息 select * from emp where mgr is not null and sal2000 and deptno20;###in1.查询emp表中工资是5000,1500,3000的员工信息 select * from emp where sal5000 or sal1500 or sal3000; select * from emp where sal in(5000,1500,3000);###between x and y1.查询工资在2000到3000之间的员工姓名和工资 select ename,sal from emp where sal2000 and sal3000; select ename,sal from emp where sal between 2000 and 3000;###模糊查询 like- _ :代表单个未知字符- % :代表0或多个未知字符- 举例:1.以a开头的字符串: a%2.以m结尾 %m3.包含x %x%4.第二个字符是a _a%5.倒数第三个字母是m %m__6.以a开头并且倒数第二个字母是b a%b_-案例:1.查询名字中包含a的所有员工姓名和工资: select ename,sal from emp where ename like %a%;2.查询标题中包含记事本的商品标题及商品价格 select title,price from t_item where title like %记事本%;3.查询单价低于100的记事本(title包含记事本) select title from t_item where price100 and title like %记事本%;4.查询单价在50到200之间的得力商品(title包含得力) select title from t_item where price between 50 and 200 and title like %得力%; 5.查询商品分类为238和917的商品信息 select * from t_item where category_id238 or category_id917;6.查询有赠品的商品信息(卖点sell_point中包含赠字) select * from t_item where sell_point like %赠%;7.查询有图片的image的得力商品信息 select * from t_item where image is not null and title like %得力%;8.查询和得力无关的商品信息(title不包含得力) select * from t_item where title not like %得力%;9.查询价格在50到100以外的商品信息 select * from t_item where price50 or price100;###排序 order by - order by 关键字,by 后面写排序的字段名称 默认是升序- asc 升序 , desc 降序1.查询所有员工的姓名和工资按照工资升序排列 select ename,sal from emp order by sal ;2.查询10号部门的所有员工信息,按照工资降序排列 select * from emp where deptno10 order by sal desc ;3.查询所有带燃字的商品,按照价格升序排列 select * from t_item where title like %燃% order by price ;4.查询所有dell商品按照分类category_id升序排列 select * from t_item where title like %dell% order by category_id ;5.查询所有员工按照部门升序排列 如果部门一致则按照工资降序排列 select * from emp order by deptno asc , sal desc;6.查询所有商品分类和单价按照分类降序排列,如果分类相同则按照价格升序排列 select category_id,price from t_item order by category_id desc ,price;###分页查询 limit- limit 跳过的条数,请求的数量(每页的数量)limit 0,10;limit 10,10;第五页的20条limit (5-1)*20,20;第八页的4条数据limit (8-1)*4,4;1.查询员工表中的工资降序的前5条 select * from emp order by sal desc limit 0,5;2.查询员工表中工资降序的第3页的4条数据 select * from emp order by sal desc limit 8,4;3.查询商品表中价格升序的前10条数据 select * from t_item order by price limit 0,10;4.查询商品表中价格低于100元的商品信息第三页的三条数据 select * from t_item where price100 order by price limit 6,3;5.查询10号部门和30号部门的员工工资在前三名的员工信息 select * from emp where deptno10 or deptno30 order by sal desc limit 0,3;###concat() 函数- 把concat内部的参数拼接到一起1.查询员工姓名和工资,要求工资单位是元 select ename,concat(sal,元) 工资 from emp; ###数值计算 - * / % (mod(7,2)等效于 7%2)1.查询商品表中每个商品的单价,库存及总价值(单位*库存) select price,num,price*num from t_item;2.查询员工表中每个员工的姓名,工资以及年终奖(五个月的工资) select ename,sal,sal*5 年终奖 from emp;###日期相关函数1.获取当前的年月日时分秒 select new();2.获取当前的日期 current select curdate();3.获取当前的时间 select curtime();4.从年月日时分秒中提取年月日 select date(new());5.从年月日时分秒提取时分秒 select time(new());6.从年月日时分秒提取时间分量 年 月 日 时 分 秒 -extract(year from now()) -extract(month from now()) -extract(day from now()) -extract(hour from now()) -extract(minute from now()) -extract(second from now()) select extract(year from now());-查询员工表中的所有员工姓名和入职的年份 select ename,extract(year from hiredate) from emp;7.日期格式化 date_format(时间,格式) now() 2018年12月31日 18:23:15-%Y:四位年 2018-%y:两位年 18-%m:两位月 05-%c:一位月 5-%d:日-%H:24小时-%h:12小时-%i:分-%s:秒 select date_format(now(),%Y年%m月%d日 %H时%i分%s秒);查询商品名称和商品上传日期(格式:x年x月x日) select title, DATE_FORMAT(created_time,%Y年%m月%d日) 日期 from t_item;-8把非标准的日期字符串转成标准的时间格式 str_to_date(时间字符串,格式) 14.08.2018 08:00:00 select str_to_date(14.08.2018 08:00:00,%d.%m.%Y %H:%i:%s) 标准化日期;###ifnull(x,y)函数-age-ifnull(x,18) 如果x的值为null 则age18 如果不为null则agex1.修改员工表中奖金为null的值为0 update emp set commifnull(comm,0);###聚合函数-用于对多条数据进行统计1.求和 sum(字段名) -查询emp中10号部门的工资总和 select sum(sal) 工资总和 from emp where deptno10;2.平均值 avg(字段名) -查询emp表中所有员工工资的平均值 select avg(sal) 平均工资 from emp; -查询30号部门的员工的最高奖金 select max(comm) 最高奖金 from emp where deptno30;4.最小值 min(字段名) -查询商品表中价格最便宜的商品的单价 select min(price) 最低价格 from t_item ;5.统计数量 count(字段名) 一般使用count(*)3.最大值 max(字段名)###聚合函数练习:1.统计工资在2500以上的员工人数 select count(*) from emp where sal2500;2.查询工资在1000到3000以内的最大奖金 -统计30号部门有多少人 select count(*) from emp where deptno30; select max(comm) from emp where sal in(1000,3000);3.查询30号部门的最大奖金,最高工资,工资平均值,工资总和,并对查询结果起别名 select max(comm) 最大奖金,max(sal) 最高工资,avg(sal) 平均工资,sum(sal) 工资总和 from emp where deptno30;4.查询价格在100元以内的商品数量 select count(*) from t_item where price100;5.查询名字包含a的员工数量 select count(*) from emp where ename like %a%;####和字符串相关函数1.获取字符串的长度 char_length(str); 获取所有员工的姓名和姓名的字符长度 select ename,char_length(ename) from emp;2.获取字符串在另一个字符串中出现的位置 instr(str,substr) select insert(abdcefg,d);3.插入字符串 insert(str,start,length,newStr) select insert(abcdefg,3,2,m);//abmefg4.转大写,转小写 select upper(abc),lower(NBA);5.左边截取和右边截取 select left(abcdefg,2),right(abcdefg,2);6.去两端空白 select trim( a b );7. 截取字符串 select substring(abcdefg,3,2);8. 重复 repeat(str,count) select repeat(ab,2); 9. 替换 replace(str,old,new) select replace(This is mysql,my,your);10. 反转 reverse(str) select reverse(abc);###回顾1. is null 和 is not null2. 别名 3. 去重 distinct4. and 和 or 5. 比较运算符 !和6. in7. between x and y 8. like _单个未知 %0或多个未知9. order by age asc/desc,sal10. limit 12,311. concat(abc,mm)12. 数值运算 - * / %和 mod()13. 日期 now() curdate() curtime() date(now()) time(now()) extract(year/month/day/hour/minute/second from now()) date_format(now(),格式) Y y m c d H h i s , str_to_date(时间字符串格式)14. ageifnull(x,y) 如果xnull agey xnull agex15. 聚合函数 sum() avg() max() min() count(*)16. 字符串 char_length instr insert upper lower left right substring replace repeat reverse trim ###作业 1.案例查询没有上级领导的员工的编号姓名工资 select empno,ename,sal from emp where mgr is null; 2.案例查询emp表中没有奖金的员工的姓名职位工资以及奖金 select enamem,job,sal,comm from emp where comm is null; 3.案例查询emp表中含有奖金的员工的编号姓名职位以及奖金 select empno ,ename,job,comm from emp where comm is not null; 4.案例查询含有上级领导的员工的姓名工资以及上级领导的编号 select ename,sal,mgr from emp where comm is not null; 5.案例查询emp表中名字以‘S’开头的所有员工的姓名 select ename from emp where ename like s%; 6.案例查询emp表中名字的最后一个字符是S的员工的姓名 select ename from emp where ename like %s; 7.案例查询倒数的第2个字符是‘E’的员工的姓名 select ename from emp where ename like %E_; 8.案例查询emp表中员工的倒数第3个字符是‘N’的员工姓名 select ename from emp where ename like %N__; 9.案例查询emp表中员工的名字中包含‘A’的员工的姓名 select ename from emp where ename like %A%; 10.案例查询emp表中名字不是以K开头的员工的所有信息 select * from emp where ename not like K%; 11.案例查询emp表中名字中不包含‘A’的所有员工的信息 select * from emp where ename not like %A%; 12.案例做文员的员工人数job 中 含有 CLERK 的 select count(job) from emp where job like %CLERK%; 13.案例销售人员 job: SALESMAN 的最高薪水 select max(sal) from emp where jobSALESMAN; 14.案例最早和最晚入职时间 select min(HIREdate) 最早入职时间,max(HIREdate) 最晚入职时间 from emp; 15.案例查询类别 163的商品总库存量 select sum(num) 总库存量 from t_item where category_id163; 16.案例查询 类别 163 的商品 select * from t_item where category_id163; 17.案例查询商品价格不大于100的商品名称列表 select title from t_item where price100; 18.案例查询品牌是联想,且价格在40000以上的商品名称和价格 select title,price from t_item where title like 联想 and price40000; 19.案例查询品牌是三木,或价格在50以下的商品名称和价格 select title,price from t_item where title like %三木% or price50; 20.案例查询品牌是三木、广博、齐心的商品名称和价格 select title,price from t_item where title like %三木% or title like %广博% or title like %齐心%; 21.案例查询品牌不是联想、戴尔的商品名称和价格 select title,price from t_item where title not like %联想% and title not like %戴尔%; 22.案例查找品牌是联想且价格大于10000的电脑名称 select title from t_item where title like %联想% and price10000; 23.案例查询联想或戴尔的电脑名称列表 select title from t_item where title like %联想% or title like %戴尔%; 24.案例查询联想、戴尔、三木的商品名称列表 select title from t_item where title like %联想% or title like %戴尔% or title like %三木%; 25.案例查询不是戴尔的电脑名称列表 select title from t_item where title not like %戴尔%; 26.案例查询所有是记事本的商品品牌、名称和价格 select title,item_type,price from t_item where title like %记事本%; 27.案例查询品牌是末尾字符是力的商品的品牌、名称和价格 select title,item_type,price from t_item where title like %力(% or title like %力%; 28.案例名称中有联想字样的商品名称 select title from t_item where title like %联想%; 29.案例查询卖点含有赠产品名称 select title from t_item where sell_point like %赠%; 30.案例查询emp表中员工的编号姓名职位工资并且工资在1000~2000之间。 select empno,ename,job,sal from emp where sal1000 and sal2000; 31.案例查询emp表中员工在10号部门并且含有上级领导的员工的姓名职位上级领导编号以及所属部门的编号 select ename,job,mgr,deptno from emp where deptno10 and mgr is not null; 32.案例查询emp表中名字中包含E并且职位不是MANAGER的员工的编号姓名职位以及工资。 select empno,ename,job,sal from emp where ename like %E% and job!MANAGER; 33.案例查询emp表中10号部门或者20号部门中员工的编号姓名所属部门的编号 select empno,ename,deptno from emp where deptno10 or deptno20; 34.案例查询emp表中没有奖金或者名字的倒数第2个字母不是T的员工的编号姓名职位以及奖金 select empno,ename,job,comm from emp where comm is null or ename not like %T_; 35.案例查询工资高于3000或者部门编号是30的员工的姓名职位工资入职时间以及所属部门的编号 select ename,job,sal,hiredate,deptno from emp where sal3000 or deptno30; 36.案例查询不是30号部门的员工的所有信息 select * from emp where deptno!30; 37.案例查询奖金不为空的员工的所有信息 select * from emp where comm is not null; 38.案例查询emp表中所有员工的编号姓名职位根据员工的编号进行降序排列 select empno,ename,job from emp order by empno desc; 39.案例查询emp表中部门编号是10号或者30号中所有员工姓名职务工资根据工资进行升序排列 select ename,job,sal from emp where deptno10 or deptno30 order by sal; 40.案例查询emp表中所有的数据然后根据部门的编号进行升序排列如果部门编号一致根据员工的编号进行降序排列 select * from emp order by deptno ,empno desc; 41.案例查询emp表中工资高于1000或者没有上级领导的员工的编号姓名工资所属部门的编号以及上级领导的编号根据部门编号进行降序排列如果部门编号一致根据工资进行升序排列。 select empno,ename,sal,deptno,mgr from emp where sal1000 or mgr is null order by deptno desc,sal ; 42.案例查询emp表中名字中不包含S的员工的编号姓名工资奖金根据工资进行升序排列如果工资一致根据编号进行降序排列 select empno,ename,sal,comm from emp where ename not like %S% order by sal ,empno desc; 43.案例统计emp表中员工的总数量 select count(*) from emp; 44.案例统计emp表中获得奖金的员工的数量 select count(comm) from emp; 45.案例求出emp表中所有的工资累加之和 select sum(sal) from emp; 46.案例求出emp表中所有的奖金累加之和 select sum(comm) from emp; 47.案例求出emp表中员工的平均工资 select avg(sal) from emp; 48.案例求出emp表中员工的平均奖金 select avg(comm) from emp; 49.案例求出emp表中员工的最高工资 select max(sal) from emp; 50.案例求出emp表中员工编号的最大值 select max(empno) from emp; 51.案例查询emp表中员工的最低工资。 select min(sal) from emp; 52.案例查询emp表中员工的人数工资的总和平均工资奖金的最大值奖金的最小值,并且对返回的列起别名。 select count(*) 总人数,sum(sal) 工资总和,avg(sal) 平均工资,max(comm) 奖金最大值,min(comm) 奖金最小值 from emp; 53.案例查询emp表中每个部门的编号人数工资总和最后根据人数进行升序排列如果人数一致根据工资总和降序排列。 select deptno,count(*) 人数,sum(sal) 工资总和 from emp group by deptno order by 人数,工资总和 desc; 54.案例查询工资在1000~3000之间的员工信息每个部门的编号平均工资最低工资最高工资根据平均工资进行升序排列。 select deptno,avg(sal) 平均工资,min(sal) 最低工资,max(sal) 最高工资 from emp where sal1000 and sal3000 group by deptno order by 平均工资; 55.案例查询含有上级领导的员工每个职业的人数工资的总和平均工资最低工资最后根据人数进行降序排列如果人数一致根据平均工资进行升序排列 select count(job) 每个职业人数,sum(sal) 工资总和,avg(sal) 平均工资,min(sal) 最低工资 from emp where mgr is not null group by job order by 每个职业人数 desc,平均工资; 56.案例查询工资在1000~3000之间每一个员工的编号姓名职位工资 select empno,ename,job,sal from emp where sal between 1000 and 3000; 57.案例查询emp表中奖金在500~2000之间所有员工的编号姓名工资以及奖金 select empno,ename,sal,comm from emp where comm between 500 and 2000; 58.案例查询员工的编号是73697521 select * from emp where empno7369 or empno7521; 59.案例查询emp表中职位是ANALYST select * from emp where jobANALYST; 60.案例查询emp表中职位不是ANALYST, select * from emp where job!ANALYST;###数学相关函数1.向下取整 floor(num) select floor(3.84);2.四舍五入 round(num) select round(3.84); -round(num,m) m代表小数位数 select round(3.1415936,3);//3.1423.非四舍五入 truncate(num,m) select truncate(3.1415926,3);//3.1414.随机数 rand(); 0-1 5-10 0-5; select floor(rand()*6)5;//5-10 select floor(rand()*6)3;//3-8###分组查询-分组查询通常和聚合函数结合使用,以组为单位进行统计-一般情况下,题目中每个xxx就在group by 后面写xxx1.查询每个部门的最高工资 select max(sal) from emp group by dep tno;2.查询每个部门的平均工资 select avg(sal) from emp group by deptno;3.查询每个分类下商品的最低价格 select min(price) from t_item group by category_id;4.查询每个部门中工资大于1500的人数 select deptno 部门编号,count(*) 人数 from emp where sal1500 group by deptno;5.查询每个领导的手下人数 select mgr 领导, count(*) 人数 from emp where mgr is not null group by mgr;6.查询每个商品分类的库存数量 select category_id 商品分类,sum(num) 库存数量 from t_item group by category_id; --如果需要使用多个字段进行分组 直接在group by 后面写多个字段名 通过逗号分隔 --having 后面可以写普通字段条件也可以写聚合函数,但是推荐在having后面只写聚合函数 ------固定的顺序:select * from 表名 where .....group by xxx having ..... order by ..... limit...;1.查询每个部门下每个领导的手下人数 select deptno,mgr,count(*) from emp where mgr is not null group by deptno,mgr;2.查询每个部门的平均工资,要求平均工资大于2000 select avg(sal) a from emp group by deptno having a2000;3.查询每个分类的平均单价,过滤掉平均单价低于100的 select category_id,avg(price) a from t_item group by category_id having a100;####练习:1.查询每个分类商品的库存总量,要求总量高于19999; select category_id, sum(num) s from t_item group by category_id having s19999;2.查询分类id为238和917的两个分类的平均单价各是多少 select category_id, avg(price) 平均单价 from t_item where category_id238 or category_id917 group by category_id;3.查询emp表中每个部门的平均工资高于2000的部门编号,部门人数,平均工资,最后根据平均工资降序排列 select deptno,count(*) 部门人数,avg(sal) 平均工资 from emp group by deptno having 平均工资2000 order by 平均工资 desc;4.查询emp表中工资在1000-3000之间的员工,每个部门的编号,工资总和,平均工资,要求过滤掉平均工资低于2000的部门,按照工资总和降序排列 select deptno,sum(sal) 工资总和,avg(sal) 平均工资 from emp where sal between 1000 and 3000 group by deptno having 平均工资2000 order by 工资总和 desc;5.查询emp表中不是以s开头每个职员的名字,人数,工资总和,最高工资,过滤掉平均工资是3000的职位,根据人数升序排序,如果一致根据工资总和降序排列6.查询emp表中每年入职的人数 select extract(year from hiredate) y,count(*) from emp group by y; 7.查询emp表工资最高的员工信息 select * from emp where sal(select max(sal) from emp); ###子查询 1.查询emp表工资的最高的员工信息 select max(sal) from emp; select * from emp where sal5000;-子查询 select * from emp where sal(select max(sal) from emp);2.查询emp表中工资高于平均工资的员工信息3.查询和Jones相同工作的员工信息 select * from emp where job(select job from emp where enamejones) and ename!Jones;4.查询工资最低的员工的所在部门同事信息 select * from emp where deptno(select deptno from emp where sal(select min(sal) from emp)) and sal!(select min(sal) from emp);5.查询最后入职的员工信息 select * from emp where hiredate(select max(hiredate) from emp); -having 要写在group by 后面6.查询king的部门编号和部门名称 select deptno,dname from dept where deptno(select deptno from emp where enameking);7.查询有员工的部门信息 select * from dept where deptno in(select distinct deptno from emp);8.查询平均工资最高的部门信息 -得到每个部门的平均工资 select deptno, avg(sal) from emp group by deptno; -得到最高的平均工资 select deptno, avg(sal) a from emp group by deptno order by desc limit 0,1; -通过最高的平均工资得到对应的部门编号 select deptno from emp group by deptno having avg(sal)(select avg(sal) a from emp group by deptno order by a desc limit 0,1); -通过部门编号得到部门信息 select * from dept where deptno(select deptno from emp group by deptno having avg(sal)(select avg(sal) a from emp group by deptno order by a desc limit 0,1)); -子查询可以写在什么位置 1.写在where或having后面 当做查询条件的值 2.写在创建表语句的后面的时候 把查询结果保存成一张新的表 create table emp_20 as(select * from emp where deptno20); 3.写在from的后面 当成一个虚拟表 **必须有别名** select * from emp where deptno20; select ename,sal from (select * from emp where deptno20) newtabte; ###关联查询 -同时查询多张表的数据成为关联查询 1.查询每一个员工的姓名和对应的部门名称 select e.ename,d.ename from emp e,dept d where e.deptnod.deptno and d.locnew york; 2.查询在new york工作的员工信息 select * from emp e,dept d where e.deptnod.deptno and d.locnew york;3.查询商品标题和所对应的分类名称 select i.title,c.name from t_item i,t_item_category c where i.category_idc.id;###笛卡尔积-如果关联查询不写关联关系则结果为两张表的乘积,这个乘积称为笛卡尔积-笛卡尔积为一种错误的查询结果,切记工作中不要出现###等值连接和内连接- 等值连接和内连接查询到的内容一样都为两张表中有关联关系的数据(交集部分)-等值连接: select * from A,B where A.xB.x and A.age18;-内连接: select * from A join B on A.xB.x where A.age18;1.查询每一个员工的姓名和对应的部门名称 select e.ename,d.dname from emp e join dept d on e.deptnod.deptno; ###外连接 - 内连接和等值连接查询到的都是交集部分的数据,外链接查询到的是某一张表的全部数据另外一张表的交集数据 -左外连接: select * from A left/right join B on A.xB.x where A.age18; insert into emp (empno,ename) values(10010,Tom);1.查询所有员工姓名和对应的部门名称 select e.ename,d.dname from emp e left join dept d on e.deptnod.deptno; select e.ename,d.dname from emp e right join dept d on e.deptnod.deptno;###关联查询总结- 关联查询的查询方式 等值连接 内连接和外连接- 如果想查询的数据为两张表的交集数据使用等值连接或内连接(推荐)- 如果查询的数据是一张表的全部数据和另外一张表的交集数据则使用外连接###作业1. 每个部门的人数,根据人数排序 select deptno 部门, count(*) 人数 from emp group by deptno order by 人数 desc;2. 每个部门中每个主管的手下人数 select deptno 部门,mgr 主管, count(*) 人数 from emp where mgr is not null group by 部门,主管; --另一种方法: select d.deptno,e.mgr,count(e.ename) from emp e right join dept d on e.deptnod.deptno where e.mgr is not null group by e.mgr,d.deptno; --查询每个部门的人数(结果中包含40号部门) select d.deptno,count(e.name) from emp e right join dept d on e.deptnod.deptno group by d.deptno;3. 每种工作的平均工资 select job 职位, avg(sal) 平均工资 from emp group by 职位;4. 每年的入职人数 select extract(year from hiredate) 年份, count(*) 人数 from emp group by 年份;5. 少于等于3个人的部门信息 select * from dept where deptno in(select deptno from emp group by deptno having count(*)3); --另一种方法: select d.*,count(e.ename) c from emp e right join dept d on e.deptnod.deptno group by d.deptno having c3;6. 拿最低工资的员工信息 select * from emp having min(sal)7. 只有一个下属的主管信息 select * from emp where empno in (select mgr from emp where mgr is not null group by mgr having count(*)1); 8. 平均工资最高的部门编号 -得到最高的平均工资 select avg(sal) a from emp group by deptno order by a desc limit 0,1; -通过平均工资值去查询对应的部门编号 select deptno from emp group by deptno having avg(sal)(select avg(sal) c from emp group by deptno order by c desc limit 0,1);9. 下属人数最多的人查询其个人信息 select * from emp where mgr(select mgr from emp group by mgr having count(*)(select count(*) c from emp group by mgr order by c desc limit 0,1));10. 拿最低工资的人的信息 select * from emp having min(sal);11. 最后入职的员工信息 select * from emp having max(hiredate);12. 工资多于平均工资的员工信息 select * from emp having sal(select avg(sal) from emp);13. 查询员工信息部门名称 select e.*,d.dname from emp e,dept d where e.deptnod.deptno;14. 员工信息部门名称所在城市 select e.*,d.dname,d.loc from emp e,dept d where e.deptnod.deptno;15. DALLAS 市所有的员工信息 select e.* from emp e join dept d on e.deptnod.deptno and d.locDALLAS;16. 按城市分组计算每个城市的员工数量 select d.loc ,count(e.ename) from emp e right join dept d on e.deptnod.deptno group by d.loc ;17. 查询员工信息和他的主管姓名 select e1.*,e2.ename 主管姓名 from emp e1 join emp e2 on e1.mgre2.empno;18. 员工信息员工主管名字部门名 select e1.*,e2.ename,d.ename from emp e1,emp e2,dept d where e1.mgre2.empno,e1.deptnod.deptno;19. 员工和他所在部门名 select e.ename,d.dname from emp e,dept d where e.deptnod.deptno;20. 案例查询emp表中所有员工的姓名以及该员工上级领导的编号姓名职位工资 select e1.ename,e2.empno,e2.ename,e2.job,e2.sal from emp e1,emp e2 where e1.mgre2.empno;21. 案例查询emp表中名字中没有字母K的所有员工的编号姓名职位以及所在部门的编号名称地址 select e.empno,e.ename,e.job,e.deptno,d.dname,d.loc from emp e,dept d where e.ename not like %k% and e.deptnod.deptno;22. 案例查询dept表中所有的部门的所有的信息以及与之关联的emp表中员工的编号姓名职位工资 select d.*,e.empno,e.ename,e.job,e.sal from emp e LEFT join dept d on d.deptnoe.deptno;###表设计之关联关系####一对一 - 什么是一对一关系: 有AB两张表,其中A表的一条数据对应B表的一条数据,同时B表的一条数据也对应A表中的一条数据 - 应用的场景:用户表和用户信息扩展表 -- 用户名 密码 昵称 头像 性别 手机号 地址 邮箱.... --外键:表中用于建立关系的字段称为外键,一张表有可能有多个外键,但只能有一个主键 -- 如何建立关系:在从表中添加外键指向主表的主键 --练习:创建表保存以下数据 用户名wukong 密码abcd 昵称悟空 性别男 地址大唐 wzt admin 武则天 女 大陆 Superman 123456 超人 男 铁岭 create table user(id int primary key auto_increment,username varchar(10),password varchar(10)); create table userinfo(userid int,nick varchar(10),gender varchar(5), loc varchar(20)); insert into user values(null,wukong,abcd),(null,wzt,admin),(null,superman,123456); insert into userinfo values(1,悟空,男,花果山),(2,武则天,女,大陆),(3,超人,男,铁岭);1.查询每个用户对应的昵称 select u.username,ui.nickname from user u join userinfo ui on u.idui.id;2.查询超人的用户名 select ui.nickname, u.username from user u join userinfo ui on u.idui.id where ui.nickname超人;3.查询性别是男的用户名和密码 select u.username,u.pwd from user u join userinfo ui on u.idui.id where ui.gavider男;4.查询是否存在 用户名:wukong 密码:abc 的用户(查询符合条件的数据条数) select count(*) from user where usernamewukong and pwdabc;####一对多 --什么是一对多:有AB两张表,A表中的一条数据对应B表中的多条数据,同时B表中的一条数据对应A表中的一条 --应用场景:用户表和部门表,商品表和分类表 --如何创建关系:在多的一端添加外键指向另外一张表的主键 --练习:创建emp表和dept表 create table emp(id int primary key auto_increment,name varchar(15),deptno int ); create table dept(id int primary key auto_increment,name varchar(15)); insert into dept values(null ,神仙),(null,妖怪); insert into emp values(null,猪八戒,1),(null,白骨精,2),(null,蜘蛛精,2); 1.查询每个员工的姓名和对应的部门名称 select e.name,d.name from emp e join dept d on e.deptnod.id; 2.查询妖怪部的员工姓名 select d.name, e.name from dept d join emp e on e.deptnod.id where d.name妖怪;####多对多 --什么是多对多:有AB两张表,A表中的一条数据对应de --应用场景:老师表和学生表 --如何建立关系:通过第三章关系表保存两张主表的关系 --练习: 创建老师表,学生表和关系表 create table teacher(id int primary key auto_increment,name varchar(20)); create table student(id int primary key auto_increment,name varchar(20)); create table t_s(tid int,sid int); 1.往以上表中保存苍老师的学生小刘和小丽传奇老师的学生小刘小王和小丽 insert into teacher values(null,苍老师); insert into teacher values(null,传奇老师); insert into student values(null,小刘); insert into student values(null,小王); insert into student values(null,小丽); insert into t_s value(1,1); insert into t_s value(1,2); insert into t_s value(2,1); insert into t_s value(2,2); insert into t_s value(2,3); 2.查询每个学生姓名和对应的老师姓名 select s.*,t.name from student s join teacher t join t_s ts on s.idts.sid and t.idts.tid; 或者:select s.name,t.name from student s join t_s ts on s.idts.sid join teacher t on t.idts.tid; 3.查询苍老师的学生都有谁 select t.name,s.name from teacher t join t_s ts on t.idts.tid join student s on s.idts.sid where t.name苍老师; 4.查询小丽的老师是谁 select s.name,t.name from student s join t_s ts on s.idts.sid join teacher t on t.idts.tid where s.name小丽; ###自关联 - 在当前表中添加外键,外键的值指向当前表的主键,这种关联方式称为自关联 create table person(id int primary key auto_increment,name varchar(15),mgr int); 保存以下数据:如来-唐僧-悟空-猴崽子 insert into person values(null,如来,null),(null,唐僧,1),(null,悟空,2),(null,猴崽子,3); 1.查询每个人的名字和和上级的名字 select p.name,m.name 上级 from person p left join person m on p.mgrm.id;###表设计案例:权限管理--实现权限管理功能需要准备三张主表和两张关系表--创建表: create table user(id int,name varchar(10)); create table role(id int,name varchar(10)); create table module(id int,name varchar(10)); create table u_r(uid int,rid int); create table r_m(rid int,mid int);--插入数据: insert into user values(1,刘德华),(2,凤姐); insert into role values(1,男游客),(2,男会员),(3,女游客),(4,女管理员); insert into module values(1,男浏览),(2,男发帖),(3,女浏览),(4,女发帖),(5,女删帖); -保存角色和权限的关系: insert into r_m values(1,1),(2,1),(2,2),(3,3),(4,3),(4,4),(4,5); -保存用户和角色的关系 刘德华男会员和女游客 凤姐:女管理员和男游客 insert into u_r values(1,2),(1,3),(2,1),(2,4); 1.查询每个用户的权限有哪些 select u.name 用户名, m.name 权限 from user u join u_r ur on u.idur.uid join r_m rm on ur.ridrm.rid join module m on rm.midm.id; 2.查询凤姐的权限 select u.name 用户名 ,m.name 权限 from user u join u_r ur on u.idur.uid join r_m rm on ur.ridrm.rid join module m on rm.midm.id where u.name凤姐; 3.查询拥有男浏览权限的用户有谁 select m.name 权限,u.name 用户名 from module m join r_m rm on m.idrm.mid join u_r ur on rm.ridur.rid join user u on ur.uidu.id where m.name男浏览; -------------------------------------------------------------------------------------------------- create table trade(tradeyid int primary key auto_increment,tradedate datetime,aomey int,directid int,way varchar(10)); create table direction(id int primary key auto_increment, relationship varchar(15),name varcahr(15),gender varchar(5)); insert into trade values(null,2018-02-15,150,1,微信红包); insert into trade values(null,2018-01-11,120,2,现金红包); insert into trade values(null,2017-08-15,-130,3,微信红包); insert into trade values(null,2018-05-15,66,4,支付宝红包); insert into trade values(null,2018-04-15,-88,6,现金红包); insert into trade values(null,2018-01-15,99,5,微信红包); insert into trade values(null,2019-02-15,88,7,现金红包); insert into trade values(null,2018-09-15,166,11,支付宝红包); insert into trade values(null,2018-04-15,-88,8,现金红包); insert into trade values(null,2018-01-15,99,9,微信红包); insert into trade values(null,2017-02-15,88,10,现金红包); insert into trade values(null,2018-09-15,-23,12,支付宝红包);insert into direction values(null,亲戚,妹妹,女);insert into direction values(null,同事,同事1,女);insert into direction values(null,朋友,朋友1,男);insert into direction values(null,同事,同事2,男);insert into direction values(null,朋友,朋友2,女);insert into direction values(null,亲戚,哥哥,男);insert into direction values(null,亲戚,弟弟,男);insert into direction values(null,朋友,朋友3,女);insert into direction values(null,同事,同事3,男);insert into direction values(null,亲戚,姐姐,女);insert into direction values(null,朋友,朋友4,男);insert into direction values(null,同事,同事4,男);insert into direction values(null,朋友,朋友5,女);insert into direction values(null,亲戚,表哥,男);insert into direction values(null,亲戚,表弟,男);insert into direction values(null,亲戚,嫂子,女);insert into direction values(null,同事,同事5,男); 3.统计从2018年春节(2月15日)到现在收益(收益收入-支出) 多少红包? select sum(aomey) from trade where tradedate2018-02-15; 4.查询从2018年(2月15日)到现在红包大于100元的所有女性亲戚的名字和对应的红包金额 select d.name,t.aomey from trade t join direction d on t.directidd.id where t.aomey100 and d.relationship亲戚; select d.name,t.aomey from trade t join direction d on t.directidd.id where timestr_to_date(2018年2月15号,%Y年%m月%d号) and t.money not between -100 and 100 and d.gender女 and d.relationship亲戚; 5.查询统计 现金 ,支付宝,微信三个平台分别收到的红吧总金额 select way, sum(aomey) from trade where aomey0 group by way ; 1. 建立一个员工信息表employee表中id员工代码、sex员工性别、name姓名、departmentid部门代码、address地址、birthdate生日、postcode邮编、salary薪水、workdate入职日期、remark备注信息其中postcode、remark 可以为空薪水需要伟number类型生日、入职日期为date类型以员工代码主键 create table employee( id int primary key auto_increment, sex char(4), name vaechar(15), departmentid int, address varchar(20), birthdate date, postcode int null, salary int, workdate date, remark text null ); ####视图 -数据库中包含多种对象,表和视图都是数据库中的对象,视图可以理解成一张虚拟的表,视图本质就是取代了一段sql查询语句 -为什么使用视图:因为有些数据的查询需要些大量的sql查询语句,每次书写比较麻烦,通过使用视图相当于把大量的sql查询语句进行保 存,下次从视图中查询就不用再次书写大量sql语句,从而提高开发效率 -视图格式:create view 视图名 as (子查询); create view v_emp_10 as (select * from emp where deptno10); delete from emp where sal1300; select * from v_emp_10; 1.创建一个没有工资的视图 create view v_emp_nosal as (select empno,ename,comm,mgr from emp);2.创建视图,视图中显示每个部门的工资总和,平均工资,最高工资,最低工资 create view v_emp_sum as (select sum(sal),avg(sal),max(sal),min(sal) from emp group by deptno);-视图的分类:1.简单视图:创建视图的时候不包含:去重,分组,函数,关联查询的视图称为简单视图,可以对视图中的数据进行增删改查2.复杂视图:和简单视图相反,只能进行查询操作-简单视图的增删改操作 操作方式和操作table一样-插入数据 insert into v_emp_10 (empno,ename,deptno) values (10011,悟空,10); insert into v_emp_10 (empno,ename,deptno) values (10012,八戒,20);(数据污染)-数据污染:往视图中插入一条在视图中不实现但是在原表中显示的数据,称为数据污染-通过with check option 关键字解决数据污染问题 create view v_emp_20 as (select * from emp where deptno20) with check option; -测试: insert into v_emp_20 (empno,ename,deptno) values (10013,刘备,20);//成功 insert into v_emp_20 (empno,ename,deptno) values (10014,刘备,30);//失败 -删除和修改:只能操作视图中存在的数据 -别名:如果创建视图时使用别名 则操作视图时只能使用别名 create view v_emp_30 as (select ename name from emp where deptno30); select * from v_emp_30 where enamejames;###约束-约束:约束是创建表时给字段添加的限制条件####非空约束 not null-字段值不能为null create table t1(id int ,age int not null); insert into t1 values(1,18);//成功 insert into t1 values(2,null)//失败####唯一约束 unique-字段的值不能重复 create table t2(id int,age int unique); insert into t2 values(1,20);//成功 insert into t2 values(2,20);//失败####主键约束-字段的值唯一且非空-创建表时添加主键约束:create table t3(id int primary key ,age int);-创建表之后添加主键约束: create table t3(id int,name varchar(10)); alter table t3 add primary key(id);-删除主键约束 alter table t3 drop primary key;####自增-数值只增不减-从历史最大值的基础上1-字段值赋值为null的时候自动1-使用dalete 删除全表数据 自增数值不变-使用truncate 自增清零####默认约束-给字段添加默认值 create table t4(id int,age int default 10); insert into t4 values(1,20);//默认值不生效 insert into t4 values(2,null);//默认值不生效 insert into t4 (id) values(3);//默认值生效####检查约束 check-语法支持,但是没有效果 create table t5(id int,ahe int check(age10)); insert into t5 values (1,5);//依然成功,check没有效果####外键约束-外键约束的作用:为了保证两个表之间的关系正确建立1.插入数据时外键值可以为null,可以重复,但是不能是另外一张表不存在的数据2.被依赖的表不能被先删除3.被依赖的数据不能被先删除-如何使用外键:1.创建部门表 create table dept(id int primary key auto_increment,name vaechar(10));2.创建员工表 create table emp(id int primary key auto_increment,name varchar(10),deptid int ,constraint fk_dept foreign key(deptid) references dept(id));-介绍: constraint 约束名称 foreign key(外键字段名) references 表名(字段名)-测试: insert into dept values(null,神仙),(null,妖怪); insert into dept values(null,八戒,1);//成功 insert into dept values(null,悟空,1);//成功 insert into dept values(null,超人,3);//失败,没有3 drop table dept;//失败 delete from dept where id2;//成功 delete from dept where id1;//失败###索引-什么是索引:索引是数据库中提高查询效率的技术,类似于字典的目录-为什么使用索引:如果不使用索引数据会零散的保存在每一个磁盘块当中,查询数据时需要挨个的遍历每一个磁盘块查找数据,如果数据量超级大,遍 历每一个磁盘块是件非常耗时的事件,添加索引后,会将磁盘块以树状结构进行保存,查询数据时会会有目的性的访问部分磁盘块,因为访问的磁盘块 数量降低所以能起到提高查询效率的作用-索引是越多越好么? 不是,因为索引会占磁盘空间,通过某个字段创建的索引可能永远用不上,则这个索引完全没有存在的意义,只需要对查询时频繁使用的字段创建索引-有索引就一定好吗? 不一定,如果数据量小使用索引反而会降低查询效率.-索引的分类(了解)1.聚集索引(聚簇索引):通过主键创建的索引为聚集索引,添加了主键约束的表会自动添加聚集索引,聚集索引的树桩结构中保存了数据2.非聚集索引:通过非主键字段创建的索引叫作非聚集索引,树桩结构中只保存了数据所在磁盘块的地址并没有数据-创建索引的格式: create index 索引名 on 表名(字段名[(字符长度)]); create index i_item_title on item2(title); -创建完后继续执行 select * from item2 where title100; //看一下耗时 0.02秒 -查看索引: show index from item2;-删除索引 drop index 索引名 on 表名; drop index i_item_title on item2;-复合索引 通过多个字段创建的索引称为复合索引 -格式:create index 索引名 on 表名(字段1,字段2); 频繁使用多个字段进行数据查询时为了提高查询效率可以创建复合索引 select * from item2 where title100 and price100; create index i_item_title_price on item(title,price);-总结:1.索引是用于提高查询效率的技术,类似目录2.索引会占用磁盘空间不是越多越好3.如果数据量小的话,添加索引会降低查询效率4.尽量不要在频繁改动的表上添加索引###事务-数据库中执行sql语句的最小工作单元,保证事务中的多条sql全部成功或全部失败-事务的ACID特性:1.Atomicity:原子性, 最小不可拆分 保证全部成功或全部失败2.Consistency:一致性, 从一个一致状态到另外一个一致状态3.Isolation:隔离性, 多个事务之间互相隔离互不影响4.Durability:持久性, 事务提交后数据持久保存到数据库文件中-事务相关指令:-查看自动提交状态:show variables like %autocommit%-修改自动提交:set autocommit0/1;-提交:commit;-回滚:rollback;-保存回滚点:savepoint s1;-回滚到指定回滚点:rollback to s1;###group-concat() 分组连接函数1.查询员工表中 每个部门的所有员工工资 select deptno,group_concat(sal) from emp group by deptno;2.查询员工表中,每个部门的员工姓名和对应的工资,要求显示到一条数据中 select deptno,group-concat(ename,sal) from emp group by deptno;###面试题:有个学生表student (id,name,subject,score)保存以下数据:张三 语文 66 , 张三 数学 77 , 张三 英语 55 , 张三 体育 77李四 语文 59 , 李四 数学 88 , 李四 英语 78 , 李四 体育 95王五 语文 75 , 王五 数学 54 , 王五 英语 98 , 王五 体育 88 create table student(id int primary key auto_increment,name varchar(15),subject varchar(5),score int not null); insert into student values(null,张三,语文,66); insert into student values(null,张三,数学,77); insert into student values(null,张三,英语,55); insert into student values(null,张三,体育,77); insert into student values(null,李四,语文,59); insert into student values(null,李四,数学,88); insert into student values(null,李四,英语,78); insert into student values(null,李四,体育,95); insert into student values(null,王五,语文,75); insert into student values(null,王五,数学,54); insert into student values(null,王五,英语,98); insert into student values(null,王五,体育,88);1.查询每个人的平均分 从大到小排序 select name, avg(score) 平均分 from student group by name order by 平均分 desc;2.每个人的姓名 科目 成绩 一行显示出来 select name,group_concat(subject,score) from student group by name;3.查询每个人的最高和最低分 select name,max(score) 最高分,min(score) 最低分 from student group by name;4.查询每个人不及格的科目以及分数和及格的科目数量 select name,group_concat(subject,:,score) , count(*) from student where score60 group by name;####JDBC- JDBC:Java DataBase Connectivity.java数据库连接,实际上jdbc是java中的一套和数据库进行交互的api(application program interface 应用程序编程接口)- 为什么使用JDBC:因为Java程序员需要连接多种数据库,为了避免每一种数据库都学习一套新的api,Sun公司提出了一个JDBC的接口,各个数据库的厂商根据此 接口写实现类驱动这样java程序员只需要掌握JDBC接口的调用即可访问任何数据库。###如何使用JDBC连接MySQL数据库1. 创建Maven工程2. 登录maven私服的网站 maven.tedu.cn 外网 仓库服务 首页搜索MySQL 找到 5.1.6版本 把坐标复制到pom.xml中 3. 创建Demo01.java类 添加main方法-通过以下代码连接数据库并执行sql语句//1.注册驱动 Class.forName(com.mysql.jdbc.Driver);//2.获取链接对象 Connection connDriverManager.getConnection (jdbc:mysql://localhost:3306/db1, root,root);//3.创建SQL执行对象 Statement statconn.createStatement();//4.执行SQL String sqlcreate table jdbc(id int,name varchar(10)); stat.execute(sql); System.out.println(创建完成!);//5.关闭资源 stat.close(); conn.close();###JUnit Test单元测试-在无参无返回值的方法上面添加Test注解通过右键 run as 执行 绿色代表执行成功红色执行失败###执行SQL的方法-execute();此方法可以执行任意的SQl 但是推荐执行DDL (数据定义语言 create,drop,alter,truncate) 返回值为boolean值 返回值代表的是是否有结果集(只有查询语句有结果集)-executeUpdate(); 增删改操作全部使用此方法 返回值为int 表示生效的行数-executeQuery(); 此方法执行查询操作 返回ResultSet 通过while循环遍历代码如下 //执行查询 得到的结果封装在了resultset中 ResultSet rsstat.executeQuery(sql); //遍历结果集 while(rs.next()){ int empnors.getInt(empno); String namers.getString(ename); double salrs.getDouble(sal); System.out.println(empno,name,sal); }-从ResultSet中获取数据的两种方式:1. 通过表字段的名称获取2. 通过结果中字段的位置获取 以本次查询的字段顺序为准并非表字段的顺序 ####自定义模板代码 window-preferense-java-editor-templates-new ###数据库连接池 DBCP- 为什么用连接池如果没有连接池一万次请求会对应一万次和数据库服务器的连接和断开连接操作使用连接池之后可以将连接池中的连接复用从而提高执行效率 - 使用方式 //创建数据源对象 BasicDataSource dataSource new BasicDataSource(); //设置数据库连接信息 dataSource.setDriverClassName(com.mysql.jdbc.Driver); dataSource.setUrl(jdbc:mysql://localhost:3306/db1); dataSource.setUsername(root); dataSource.setPassword(root); //设置初始连接数量 dataSource.setInitialSize(3); //设置最大连接数量 dataSource.setMaxActive(5); //从连接池中获取连接 Connection conn dataSource.getConnection(); System.out.println(conn);###PreparedStatement预加载的SQL执行对象- 好处1. 代码更直观简洁2. 可以避免SQL注入,因为在预编译时已经把sql逻辑固定锁死不会被之后替换进去的值改变原有逻辑###批量操作batch 因为每次sql的执行都需要和数据库服务器进行数据传输如果执行的sql太多每次和数据库进行交互浪费资源执行效率低使用批量操作可以把多条SQL语句合并到一次交互中这样可以提高执行效率- Statement //添加到批量操作 stat.addBatch(sql1); stat.addBatch(sql2); stat.addBatch(sql3); //执行批量操作 stat.executeBatch();- PreparedStatement stat conn.prepareStatement(sql); stat.setString(1, 刘备); stat.setString(2, aaa); //添加到批量处理 stat.addBatch(); stat.setString(1, 关羽); stat.setString(2, bbb); //添加到批量处理 stat.addBatch(); stat.setString(1, 张飞); stat.setString(2, ccc); //添加到批量处理 stat.addBatch(); //执行 stat.executeBatch();###事务1. 关闭自动提交 conn.setAutoCommit(false/true);2. 提交 conn.commit();3. 回滚 conn.rollback();- 案例 参见代码Demo11.java###获取自增主键的值 rsstat.getGeneratedKeys();create table team(id int primary key auto_increment,name varchar(10));create table player(id int primary key auto_increment,name varchar(10),teamid int);- 球队和球员案例代码参见 Demo11.java####数据库的元数据和表的元数据 代码参见Demo12.java create table city(id int primary key auto_increment,cityname varchar(10),Pid int); create table provice(id int primary key auto_increment,provicename varchar(20),Iid int); create table user(id int primary key auto_increment,email varchar(32),pwd varchar(16),Pid int,Iid int); create table information(id int primary key auto_increment,nickname varcahr(12),forumnickname varchar(12),gender varchar(2),birthday date,Pid int,Cid int);select say 单价,num 库存,say*num 总金额 from t_item; html中的高度坍塌
http://www.huolong8.cn/news/139752/

相关文章:

  • 青岛黄岛区网站开发html网页代码大全的阅读
  • 网站建设与管理规划书网站营销活动
  • 定制网站建设服务公司html编辑器中文版
  • 网站建设服务哪里便宜国内专业的seo机构
  • 哪些网站可以做宣传十个程序员必备的网站
  • 郑州网站推品牌设计是做什么的
  • 小地方的旅游网站怎么做网上引流推广
  • wordpress怎样设置会员免费seo优化方法
  • 上海千途网站建设wordpress数据转移
  • 小松建设官方网站小制作小发明手工图片
  • 上海网站建设制作公司大连天健网大连
  • 桃城区网站制作公司利尔化学股票
  • 淘宝网站怎么做适配灵台门户网站建设
  • 住房与城乡建设部网站特色小镇建设管理部门网站查询
  • 朔州城市建设网站网页设计需要学什么专业陪护工
  • 网站开发 视频存储长治个人网站建设
  • 中文域名可以做网站吗石家庄商城网站建设
  • 长春做商业平台网站鲜花网站建设图片
  • 城乡和住房建设部网站深圳网站制作开发
  • 新手学做网站 pdf下载wordpress连接自建数据库
  • 网站批量上传服务器抖音的商业营销手段
  • 重庆网站建设微信开发自动推广引流app
  • 海南网站建设推广公司哪家好网站建设分析图
  • 自己做的博客网站吗建筑工程网上报建网站诚信手册
  • 学校的网站是怎么建设的河北省城乡住房建设厅网站
  • 贵州省建设监理协会网站做网站原创要多少钱
  • 中国建设银行官网站保本理财包头外贸网站建设
  • 陕西建设主管部门网站医院线上预约
  • 网站下载音乐3d网页游戏平台
  • 如何查看一个网站的所有二级域名建个人网上银行登录入口