-- 《自来水公司收费系统》
-- 1 业主类型表
create table t_ownertype(
id number primary key,
name varchar2(30) not null
);
-- 2 价格表
-- 2.1 创建价格表
create table t_pricetable(
id number primary key,
price number(10,2) not null,
ownertypeid number not null,
minnum number(10,2) not null,
maxnum number(10,2) not null
);
-- 2.2 业主类型 和 价格表 主外键关系
alter table t_pricetable add constraint pricetable_ownertype_fk foreign key (ownertypeid) references t_ownertype (id);
-- 3 区域表
create table t_area(
id number primary key,
name varchar2(30) not null
);
-- 4 收费员表
create table t_operator(
id number primary key,
name varchar2(30) not null
);
-- 5 地址表
-- 5.1 创建地址表
create table t_address(
id number primary key,
name varchar2(30) not null,
areaid number not null,
operatorid number not null
);
-- 5.2 创建主外键关系:区域表和地址表
alter table t_address add constraint address_area_fk foreign key (areaid) references t_area (id);
-- 5.3 创建主外键关系:收费员表和地址表
alter table t_address add constraint address_operator_fk foreign key (operatorid) references t_operator (id);
-- 6 业主表
-- 6.1 创建业主表
create table t_owners(
id number primary key,
name varchar2(30) not null,
addressid number not null,
housenumber varchar2(30) not null,
watermeter varchar2(30) not null,
adddate date not null,
ownertypeid number not null
);
-- 6.2 创建主外键关系:业务类型表和业主表
alter table t_owners add constraint owners_ownertype_fk foreign key (ownertypeid) references t_ownertype (id);
-- 6.3 创建主外键关系:地址表和业主表
alter table t_owners add constraint owners_address_fk foreign key (addressid) references t_address (id);
-- 7 收费台账表
-- 7.1 创建台账表
create table t_account(
id number primary key,
ownerid number not null,
ownertypeid number not null,
areaid number not null,
year char(4) not null,
month char(2) not null,
num0 number ,
num1 number ,
userunm number,
meteruserid number,
meterdate date,
money number(10,2),
isfee char(1) not null,
feedate date,
feeuserid number
);
-- 7.2 创建主外键关系:业主表和台账表
alter table t_account add constraint account_owners_fk foreign key (ownerid) references t_owners (id);
-- 7.3 创建主外键关系:业主类型表和台账表
alter table t_account add constraint account_ownertype_fk foreign key (ownertypeid) references t_ownertype (id);
-- 7.4 创建主外键关系:区域表和台账表
alter table t_account add constraint account_area_fk foreign key (areaid) references t_area (id);
-- 7.5 创建主外键关系:收费员表和台账表
alter table t_account add constraint account_operator_fk foreign key (feeuserid) references t_operator (id);
--业主类型
insert into t_ownertype values(1,'居民');
insert into t_ownertype values(2,'行政事业单位');
insert into t_ownertype values(3,'商业');
--地址信息--
insert into t_address values( 1,'明兴花园',1,1);
insert into t_address values( 2,'鑫源秋墅',1,1);
insert into t_address values( 3,'华龙苑南里小区',2,2);
insert into t_address values( 4,'河畔花园',2,2);
insert into t_address values( 5,'霍营',2,2);
insert into t_address values( 6,'回龙观东大街',3,2);
insert into t_address values( 7,'西二旗',3,2);
--业主信息
insert into t_owners values(1,'范冰',1,'1-1','30406',to_date('2015-04-12','yyyy-MM-dd'),1 );
insert into t_owners values(2,'王强',1,'1-2','30407',to_date('2015-02-14','yyyy-MM-dd'),1 );
insert into t_owners values(3,'马腾',1,'1-3','30408',to_date('2015-03-18','yyyy-MM-dd'),1 );
insert into t_owners values(4,'林小玲',2,'2-4','30409',to_date('2015-06-15','yyyy-MM-dd'),1 );
insert into t_owners values(5,'刘华',2,'2-5','30410',to_date('2013-09-11','yyyy-MM-dd'),1 );
insert into t_owners values(6,'刘东',2,'2-2','30411',to_date('2014-09-11','yyyy-MM-dd'),1 );
insert into t_owners values(7,'周健',3,'2-5','30433',to_date('2016-09-11','yyyy-MM-dd'),1 );
insert into t_owners values(8,'张哲',4,'2-2','30455',to_date('2016-09-11','yyyy-MM-dd'),1 );
insert into t_owners values(9,'昌平区中西医结合医院',5,'2-2','30422',to_date('2016-10-11','yyyy-MM-dd'),2 );
insert into t_owners values(10,'美廉美超市',5,'4-2','30423',to_date('2016-10-12','yyyy-MM-dd'),3 );
--操作员
insert into t_operator values(1,'马小云');
insert into t_operator values(2,'李翠花');
--地区--
insert into t_area values(1,'海淀');
insert into t_area values(2,'昌平');
insert into t_area values(3,'西城');
insert into t_area values(4,'东城');
insert into t_area values(5,'朝阳');
insert into t_area values(6,'玄武');
--价格表--
insert into t_pricetable values(1,2.45,1,0,5);
insert into t_pricetable values(2,3.45,1,5,10);
insert into t_pricetable values(3,4.45,1,10,null);
insert into t_pricetable values(4,3.87,2,0,5);
insert into t_pricetable values(5,4.87,2,5,10);
insert into t_pricetable values(6,5.87,2,10,null);
insert into t_pricetable values(7,4.36,3,0,5);
insert into t_pricetable values(8,5.36,3,5,10);
insert into t_pricetable values(9,6.36,3,10,null);
--账务表--
insert into t_account values( seq_account.nextval,1,1,1,'2012','01',30203,50123,0,1,sysdate,34.51,'1',to_date('2012-02-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,1,1,1,'2012','02',50123,60303,0,1,sysdate,23.43,'1',to_date('2012-03-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,1,1,1,'2012','03',60303,74111,0,1,sysdate,45.34,'1',to_date('2012-04-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,1,1,1,'2012','04',74111,77012,0,1,sysdate,52.54,'1',to_date('2012-05-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,1,1,1,'2012','05',77012,79031,0,1,sysdate,54.66,'1',to_date('2012-06-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,1,1,1,'2012','06',79031,80201,0,1,sysdate,76.45,'1',to_date('2012-07-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,1,1,1,'2012','07',80201,88331,0,1,sysdate,65.65,'1',to_date('2012-08-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,1,1,1,'2012','08',88331,89123,0,1,sysdate,55.67,'1',to_date('2012-09-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,1,1,1,'2012','09',89123,90122,0,1,sysdate,112.54,'1',to_date('2012-10-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,1,1,1,'2012','10',90122,93911,0,1,sysdate,76.21,'1',to_date('2012-11-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,1,1,1,'2012','11',93911,95012,0,1,sysdate,76.25,'1',to_date('2012-12-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,1,1,1,'2012','12',95012,99081,0,1,sysdate,44.51,'1',to_date('2013-01-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,2,1,3,'2012','01',30334,50433,0,1,sysdate,34.51,'1',to_date('2013-02-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,2,1,3,'2012','02',50433,60765,0,1,sysdate,23.43,'1',to_date('2013-03-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,2,1,3,'2012','03',60765,74155,0,1,sysdate,45.34,'1',to_date('2013-04-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,2,1,3,'2012','04',74155,77099,0,1,sysdate,52.54,'1',to_date('2013-05-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,2,1,3,'2012','05',77099,79076,0,1,sysdate,54.66,'1',to_date('2013-06-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,2,1,3,'2012','06',79076,80287,0,1,sysdate,76.45,'1',to_date('2013-07-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,2,1,3,'2012','07',80287,88432,0,1,sysdate,65.65,'1',to_date('2013-08-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,2,1,3,'2012','08',88432,89765,0,1,sysdate,55.67,'1',to_date('2013-09-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,2,1,3,'2012','09',89765,90567,0,1,sysdate,112.54,'1',to_date('2013-10-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,2,1,3,'2012','10',90567,93932,0,1,sysdate,76.21,'1',to_date('2013-11-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,2,1,3,'2012','11',93932,95076,0,1,sysdate,76.25,'1',to_date('2013-12-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,2,1,3,'2012','12',95076,99324,0,1,sysdate,44.51,'1',to_date('2014-01-14','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,100,1,3,'2012','12',95076,99324,0,1,sysdate,44.51,'1',to_date('2014-01-01','yyyy-MM-dd'),2 );
insert into t_account values( seq_account.nextval,101,1,3,'2012','12',95076,99324,0,1,sysdate,44.51,'1',to_date('2015-01-01','yyyy-MM-dd'),2 );
update t_account set usernum=num1-num0;
update t_account set money=usernum*2.45;
commit;
select distinct * | 列名 as 别名, 列表2 as 别名2... | 聚合函数
from 表名 as 别名, 表名2 as 别名2 ,....
where 查询条件
group by 分组字段 having 分组条件
order by 排序字段 asc | desc,....
-- 1 查询水表编号为30408的业主记录
select * from t_owners where watermeter = '30408';
-- 使用表的别名
select * from t_owners ow where ow.watermeter = '30408';
-- 2查询业主名称包含“刘”的业主记录
--- like 语句 %匹配多个字符 _匹配1个字段
select * from t_owners where name like '%刘%';
-- 3查询业主名称包含“刘”的并且门牌号包含5的业主记录
select * from t_owners where name like '%刘%' and housenumber like '%5%';
-- 4查询业主名称包含“刘”的或者门牌号包含5的业主记录
select * from t_owners where name like '%刘%' or housenumber like '%5%';
--5查询业主名称包含“刘”的或者门牌号包含5的业主记录,并且地址编号为3的记录。
--- and 优先级 高于 or,如果先执行or,需要使用小括号
select * from t_owners where (name like '%刘%' or housenumber like '%5%') and addressid = 3;
-- 6查询台账记录中用水字数大于等于10000,并且小于等于20000的记录
--- 关系运算符: > >= < <= == <>
select * from t_account where usenum >= 10000 and usenum <= 20000;
--- 字段 between ... and ...
select * from t_account where usenum between 10000 and 20000;
-- 7 查询T_PRICETABLE表中MAXNUM为空的记录
select * from t_pricetable where maxnum is null;
--8 查询T_PRICETABLE表中MAXNUM不为空的记录
select * from t_pricetable where maxnum is not null;
-- 去重复
-- 需求:查询业主表中的地址ID,不重复显示
select distinct addressid from t_owners;
select distinct(addressid) from t_owners;
-- 排序
-- select ... order by 字段 asc|desc ,字段2 asc|desc,....
--需求:对T_ACCOUNT表按使用量进行升序排序
select * from t_account order by usenum asc
--需求:对T_ACCOUNT表按使用量进行降序排序
select * from t_account order by usenum desc
--需求:对T_ACCOUNT表 按照month降序,如果相同按照usenum进行升序
select * from t_account order by month desc , usenum asc ;
常见伪列 | 描述 |
---|---|
rowid | 物理文件上唯一区别记录的唯一标识 用途:用于区分重复数据 |
rownum | 在查询的结果集中,ROWNUM为结果集中每一行标识一个行号 用途:在Oracle进行分页 |
rowid
聚合函数 | 描述 |
---|---|
sum | 求和 |
avg | 平均 |
max | 最大值 |
min | 最小值 |
count | 计数 |
-- 聚合函数
--1 统计2012年所有用户的用水量总和
select sum(usenum) from t_account where year = '2012'
--2 统计2012年所有用水量(字数)的平均值
insert into t_account values( seq_account.nextval,2,1,3,'2012','12',95076,99324,null,1,sysdate,44.51,'1',to_date('2014-01-14','yyyy-MM-dd'),2 );
select avg(usenum) from t_account where year = '2012'
--3统计2012年最高用水量(字数)
select max(usenum) from t_account
4统计2012年最低用水量(字数)
select min(usenum) from t_account
5统计记录个数 count
select count(id) from t_account
笛卡尔积:两个表乘积,所有的数据最大集(开发无用)
select * from A , B;
内连接
隐式内连接
select * from A , B where a.id = b.aid;
显示内连接
select * from A
inner join B on a.id = b.aid;
外链接
左外连接:查询左表(A)所有数据,如果条件成立显示右边(B)的数据,否则显示null
select * from A
left outer join B on a.id = b.aid
右外连接:查询右表(B)所有数据,如果条件成立显示左边(A)的数据,否则显示null
select * from A
right outer join B on a.id = b.aid
--连接查询
-- 1查询显示业主编号,业主名称,业主类型名称
--- 隐式内连接
select ow.id,ow.name,ot.name from t_owners ow , t_ownertype ot
where ow.ownertypeid = ot.id;
--- 显示内连接
select ow.id,ow.name,ot.name from t_owners ow
inner join t_ownertype ot on ow.ownertypeid = ot.id;
-- 2查询显示业主编号,业主名称、地址和业主类型
--- 隐式内连接
select ow.id,ow.name,ad.name,ot.name from t_owners ow , t_ownertype ot , t_address ad
where ow.ownertypeid = ot.id and ow.addressid = ad.id;
select ow.id,ow.name as 业主名称,ad.name 地址名称,ot.name 业主类型名称 from t_owners ow , t_ownertype ot , t_address ad
where ow.ownertypeid = ot.id and ow.addressid = ad.id;
--- 显示内连接
select ow.id,ow.name,ad.name,ot.name from t_owners ow
inner join t_address ad on ow.addressid = ad.id
inner join t_ownertype ot on ow.ownertypeid = ot.id;
-- 3查询显示业主编号、业主名称、地址、所属区域、业主分类
--- 隐式内连接
select ow.id,ow.name 业主名称,ad.name 地址,ar.name 所属区域,ot.name 业主分类 from t_owners ow , t_ownertype ot , t_address ad , t_area ar
where ow.ownertypeid = ot.id and ow.addressid = ad.id and ad.areaid = ar.id
--- 显示内连接
select ow.id,ow.name 业主名称,ad.name 地址,ar.name 所属区域,ot.name 业主分类 from t_owners ow
inner join t_ownertype ot on ow.ownertypeid = ot.id
inner join t_address ad on ow.addressid = ad.id
inner join t_area ar on ad.areaid = ar.id;
-- 左外链接
--需求:查询业主的账务记录,显示业主编号、名称、年、月、金额。如果此业主没有账务记录也要列出姓名。
select ow.id,ow.name,ac.year,ac.month,ac.money from t_owners ow
left outer join t_account ac on ow.id = ac.ownerid
-- Oracle 班级簿左外连接
select ow.id,ow.name,ac.year, ac.month ,ac.money from t_owners ow , t_account ac where ow.id = ac.ownerid (+);
--需求:查询业主的账务记录,显示业主编号、名称、年、月、金额。如果账务记录没有对应的业主信息也要列出记录。
--- 准备数据,修改将 t_account 表 ownerid 非空约束去掉
insert into t_account values( seq_account.nextval,null,1,3,'2012','12',95076,99324,0,1,sysdate,44.51,'1',to_date('2014-01-14','yyyy-MM-dd'),2 );
select ow.id,ow.name,ac.year,ac.month,ac.money from t_owners ow
right outer join t_account ac on ow.id = ac.ownerid
子查询:一个select语句,作为另一条select语句语法的一部分。
select语句语法:
select distinct * | 字段 from 表名
where 查询条件
group by 分组字段 having 分组条件
order by 排序字段 asc | desc
-- 查询2012年1月用水量大于平均值的台账记录
-- 1 用水量平均值
select avg(usenum) from t_account where year='2012' and month = '01'
-- 2 查询2012年1月所有用水量
select * from t_account where year='2012' and month = '01'
-- 3 合并
select * from t_account where year='2012' and month = '01' and usenum > 20009.5
select * from t_account where year='2012' and month = '01' and usenum > (
select avg(usenum) from t_account where year='2012' and month = '01'
)
--查询2012年台账中,使用量大于2012年3月最大使用量的台账数据
-- 方式1:求最大值
-- 1. 求2012年3月最大使用量
select max(usenum) from t_account where year = '2012' and month = '03'
-- 2. 查询2012年台账 大于 13808
select * from t_account where year = '2012' and usenum > 13808
-- 3. 整合
select * from t_account where year = '2012' and usenum > (
select max(usenum) from t_account where year = '2012' and month = '03'
)
-- 方式2:使用all运算符
-- 1. 求2012年3月所有使用量
select usenum from t_account where year = '2012' and month = '03'
-- 2. 查询2012年台账 大于 3月所有使用量
select * from t_account where year = '2012' and usenum > all (13808,13390)
-- 3. 整合
select * from t_account where year = '2012' and usenum > all (
select usenum from t_account where year = '2012' and month = '03'
)
-- 查询在海淀区的小区名字中含有花园的业主记录
-- 1. 查询区域id,名称为“海淀” --多个海淀
select id from t_area where name = '海淀'
-- 2. 查询地址id,条件:名称含“花园” 和 区域id
select id from t_address where name like '%花园%' and areaid in (1)
-- 3 组合 1+2
select id from t_address where name like '%花园%' and areaid in (
select id from t_area where name = '海淀'
)
-- 4. 查询业主,条件:一组地址id
select * from t_owners where addressid in (1)
-- 5. 组合
select * from t_owners where addressid in (
select id from t_address where name like '%花园%' and areaid in (
select id from t_area where name = '海淀'
)
)
--- 只有一个海淀
select id from t_address where name like '%花园%' and areaid = (
select id from t_area where name = '海淀'
)
-- 查询台账表中的用户年用水量的总和 以及 年平均用水量
-- 1. 查询用水量总和 137868
select sum(usenum) from t_account
-- 2. 查询用水量平均值 5514.72
select avg(usenum) from t_account
-- 3. 将两个不相关的数据,使用虚表 dual 组合在一起
select (137868) as 总和,(5514.72) as 平均 from dual
select (
select sum(usenum) from t_account
) as 总和,(
select avg(usenum) from t_account
) as 平均 from dual
-- 查询显示业主编号,业主名称、地址和业主类型
-- 1. 查询业主编号,名称
select id,name,addressid,ownertypeid from t_owners
-- 2. 根据地址id,查询地址
select name from t_address where id = 1
-- 3. 根据类型id,查询业主类型
select name from t_ownertype where id = 1
-- 4. 组合
select ow.id,ow.name,(
select name from t_address where id = ow.addressid
) 地址名称 ,ownertypeid from t_owners ow
select ow.id,ow.name,(
select name from t_address where id = ow.addressid
) 地址名称 ,(
select name from t_ownertype where id = ow.ownertypeid
) from t_owners ow
-- 查询显示业主编号、业主名称、地址、所属区域、业主分类
-- 1. 查询业主编号,名称
select id,name,addressid,ownertypeid, ar.name from t_owners
-- 2. 根据地址id,查询地址
select name from t_address where id = 1
-- 3. 根据类型id,查询业主类型
select name from t_ownertype where id = 1
-- 4. 根据区域id,查询区域
select ar.name from t_area ar, t_address ad where ar.id = ad.areaid and ad.id = 1
-- 5 组合
select ow.id, ow.name,(
select name from t_address where id = ow.addressid
) ,(
select name from t_ownertype where id = ow.ownertypeid
) , (
select ar.name from t_area ar, t_address ad where ar.id = ad.areaid and ad.id = ow.addressid
) from t_owners ow
-- 分页
--- 基本分页
--1.查询所有台账表
select * from t_account
--2.查询所有台账表,含rownum
select rownum r, a.* from t_account a
--3.第一页
select rownum, a.* from t_account a where rownum <=10
--4.第二页 , rownum 在处理每行数据时生产,不能使用大于号>
--select t.* from A t
--where t.r > 10 and t.r <=20
select t.* from ( select rownum r, a.* from t_account a ) t
where t.r > 10 and t.r <=20
--- 排序 + 分页
--1. 查询所有 + 排序
select * from t_account order by usenum desc
--2. 结果 --> + rownum
-- select rownum r, t.* from () t
select rownum r , t.* from (
select * from t_account order by usenum desc
) t
--3. 结果 --> 分页条件
-- select * from B t2
-- where t2.r > 10 and t2.r <= 20
select * from (
select rownum r , t.* from (
select * from t_account order by usenum desc
) t
) t2
where t2.r > 10 and t2.r <= 20
select rownum, t.* from t_account t
select rownum, t.* from t_account t order by usenum desc