1.
1.1属性列运算查询,+ - * /
例: 查询2个字段的乘积,那么sql语句是: select id*age from person;
查询2个字段的和,那么sql语句是: select id+age from person;
查询2个字段的差,那么sql语句是: select id-age from person;
查询2个字段的商,那么sql语句是: select id/age from person;
1.2去掉查询结果中重复行 distinct关键字
select distinct name from student;
1.3比较大小 <,<=,>,>=,=,<>(不等于)
例:查询编号为01的图书名称
select title from book where id='01';
查询年龄大于20的学生的名字
select name from student where age>20;
1.4 确定查询范围,用In(Not In),between and (not between ...and..)来确定(排除)查找的范围
例: 查找年龄在20,21,22岁范围之内的学生姓名
select name from student where age in ('20','21','22');
select name from student where age between 20 and 22;(between包括20和22)
1.5查询空值
例:查询图书馆中库存量为空的图书书名
select title form book where stocks is null;
1.6多条件查询,用and or(and优先级比or高)
例: select name from student where age>20 and grade>90;
1.7对查询结果排序
例:查询学生信息,并按年龄降序排列 (asc表示升序,desc表示降序,缺省(不写)升序 )
select * from student order by age desc;
1.8将查询结果分组
group by一般用于将查询结果分组,多配合聚合函数,sum,count,min,max等一起使用。
例:统计图书馆中各个出版社出版的图书数量
select p#,count(b#) from book group by p#;
1.9模糊查询
%表示任意长度(可以为零)的字符串
_表示任意单个字符
例: 查找以张开头的人名
select name form student where name like '张%';
查找以张开头的3个字的人名
select name from student where name like '张_ _';
2.
use x; //使用x数据库(因为可能存在很多数据库)
show databases; //显示所有数据库
show tables; //显示所有表
show create database x //显示创建数据库x时的代码
show create table x //显示创建表x时的代码
drop database x; //删除名称为x的表
create database x // 创建一个名称为x的数据库
create database x character set gbk; //创建一二使用gbk字符集的数据库,名称为x
desc x //查看x表的结构
rename table x to xx; //将x 表重命名为xx表
alter table x add column name varchar(10) ;//给表x新增一列 属性名为name,数据类型为varchar(10)
3.数据库的数据类型
数值类型:
BIT(M) 位类型,M指定位数,M范围1-64
smallint 2的16次方
int 2的32次方
bigint 2的64次方
float[(m,d)] m指定显示长度,d指定小数位数
double[(m,d)] m指定显示长度,d指定小数位数
char(3) 保存一个"ab",用了3个bit
varchar(3) 保存一个"ab",用了2个bit
text/longtext 保存大文本
blob 存储二进制数据,如图片,音频,视频
4.数据库增删改操作
4.1添加数据
格式:insert into <表名> (属性名,属性名) values (对属性赋值,对属性赋值);
例子:insert into a(name,phone) values('zhangsan','13920394322');
将子查询结果添加到数据库中
例: 求图书馆中每个出版社的图书总数,然后存入新表bookNumber
insert into BookNumber(P# ,Book_Num)
select P#,COUNT(B#) from book group by p#;
4.2 更新数据:
格式: update <表名>
set <列名1>=<表达式>,<列名2>=<表达式>;
例子:update a set salary=salary+100; 对全体成员的数据修改
update a set salary=salary+100 where salary='1000'; 只对salary是1000的进行修改
4.3删除数据:
格式:delete from <表名> where 表达式
例子: delete from a where salary='1000'
delete from a //删除表中所有数据
5.嵌套查询
5.1带In的嵌套查询,其实就是将原来的范围用select字句代替了
例: 查找年龄在20,21,22岁范围之内的学生姓名
select name from student where age in ('20','21','22');
select name from student where age in ( select name from student where age = '20');
5.2带ANY的嵌套查询
将表达式的值与查询返回集的值比较,只要一个满足条件就返回TRUE
例子: 查询书名,这些书的价格比编号为01的书价格低(小于返回集合最大值,比如编号为01的书价格是20,24,25,只要小于25的都可以)
select title from book where price< ANY (select price from book where P# = '01');
5.3带ALL的嵌套查询
将表达式的值与查询返回集中的所有值相比较,所有值都满足条件才返回TRUE
例子:查询书名,这些书的价格比编号为01的所有书价格都低(小于返回集合最小值)
select title from book where price< ALL (select price from book where P# = '01');
5.4带比较运算符的嵌套查询
例子:查询书名,书名的编号='01'
select title from book where bookname = (select bookname from book where id='01');
5.5带EXIST的嵌套查询
EXIST表示一个子查询至少返回一行时条件成立
例子: select title from book where exists (select * from publisher where name='新华出版社');
6.集合查询
主要实现集合的交并差功能,标准sql只提供了并操作,其他操作可以用其他方法来实现
并操作用and来实现,差操作用别的等价查询来实现
6.1并操作的嵌套查询
例:查询图书馆中出版社编号为01出版及单价不大于25元的图书
select * from book where id='01' UNION select * from book where price<25;
7函数查询
7.1 COUNT函数,统计一个表中有多少条记录
例: 查询图书的数目
select count(*) from book;
7.2 SUM函数,计算字段的和
例: 计算所有图书的总价钱
select sum(price) from book;
7.3 AVG函数,用来求某个字段所有值的平均值
例: 计算图书馆中所有图书的平均价格
select avg(price) from book;
7.4 MAX,MIN函数 找出某一列中的最大值,最小值
例: 查询图书中价格最贵是多少
select max(price) from book;
查询图书中价格最贵是多少
select min(price) from book;
8. 数据库视图
基表是实际存储在数据库中的表,视图是由基表或其他视图导出的虚表,对视图的操作会由数据库管理系统转化为对基表的操作。
视图有什么功能呢? 主要是实现安全性方面的考虑,主要作用是不让所有的人都能看到整张表,比如只让HR看到工资,只让一般员工看到联系方式,