-- 建立表空間
create tablespace waterboss
datafile 'C:\waterboss.dbf'
size 100M
autoextend on next 10M;
-- 建立用戶
create user wateruser
identified by root
default tablespace waterboss;
-- 給wateruser賦予權限
grant dba to wateruser;
-- 建立業主表
create table t_owners(
id number primary key, -- 業主id
name varchar2(30), -- 業主姓名
addressid number, -- 地址
housenumber varchar2(30), -- 門牌號
watermeter varchar2(30), -- 水錶編號
adddate date, -- 添加日期
ownertypeid number -- 業主類型
);
-- 業主類型
create table t_ownertype(
id number primary key,
name varchar2(30) -- 類型名稱
);
-- 價格表
create table t_pricetable(
id number primary key,
price number(10,2), -- 價格
ownertypeid number, -- 業主類型id
minnum number(10,2), --區間數開始值
maxnum number(10,2) -- 區間數截止值
);
-- 區域表
create table t_area(
id number primary key,
name varchar2(30)
);
-- 收費員表
create table t_operator(
id number primary key,
name varchar2(30) -- 操做員表
);
-- 地址表
create table t_address(
id number primary key,
name varchar2(30), -- 地址名稱
areaid number, -- 區域id
operatorid number -- 操做員表
);
alter table t_address modify name varchar(100);
-- 收費臺帳
create table t_account(
id number primary key,
ownerid number, --業主編號
ownertypeid number, -- 業主類型
areaid number, -- 所在區域
year char(4), -- 財務年份
month char(2), -- 財務月份
num0 number, -- 上月累計數
num1 number, -- 本月累計數
usenum number, -- 本月使用數
meteruserid number, -- 抄表員
meterdate DATE, -- 抄表日期
money number(10,2), -- 應該繳費金額
isfee char(1), --是否繳費
feedate DATE, -- 繳費日期
feeuserid number -- 收費員
);
-- 查詢表
select * from t_owners where id=6;
-- 插入數據
insert into t_owners values(2,'向問天',1002,'1-302','w002',sysdate,2002);
insert into t_owners values(3,'風清揚',1003,'1-303','w003',sysdate,2003);
insert into t_owners values(4,'左冷禪',1004,'1-304','w004',sysdate,2004);
insert into t_owners values(5,'張無忌',1005,'1-305','w005',sysdate,2005);
-- 更新數據
update t_owners set name='周芷若',addressid=1007, housenumber='1-307',watermeter='w-007',adddate=sysdate,ownertypeid=2007 where id=6;
-- 刪除數據
delete from t_owners where id in(1,2,3,4,5); -- 刪除數據能夠rollback,可能產生碎片,而且不釋放空間
truncate table t_owners; -- 先摧毀表結構,在重構表結構
-- 數據庫自增加
create sequence seq_account;
--業主類型
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 usenum=num1-num0;
update t_account set money=usenum*2.45;
commit;
數據庫