oracle数据库:数据的管理维护之一
一、任务
给用户表中的每个用户下一个订单,订单号使用序列生成序号,日期使用默认值,总金额为null,订单状态为1,备注为null,每张订单的订单明细不少于两样商品,单价为所选的商品的单价,其他随意设计。
二、准备
1、添加简单数据
1)商品类型数据的添加到商品类型表中
insert into t_type values('T00001','日用百货');
insert into t_type values('T00002','儿童用品');
insert into t_type values('T00003','女装精品');
insert into t_type values('T00004','男装精品');
insert into t_type values('T00005','电子精品');
2)用户数据添加到用户表中
insert into t_user(uiid,uname) values('000001','系统管理员');
insert into t_user values('000002','李宇',to_date('1989-10-25','yyyy-mm-dd'),'男','佛山禅城区','0757-89999999','');
insert into t_user values('000003','罗华',to_date('1970-5-26','yyyy-mm-dd'),'女','广州天河区','020-68888888','');
insert into t_user values('000004','韩乐',null,'女','深圳宝安','0757-86666666','');
insert into t_user values('000005','孔卿',to_date('1981-7-28','yyyy-mm-dd'),'女','佛山高明','0756-89999999','');
3)商品数据添加到商品信息表中
insert into t_goods values('G01001','修正笔','T00001',5.5,0,0,100,0,null);
insert into t_goods values('G03001','女士衬衣','T00003',92.00,0.95,18,40,1,null);
insert into t_goods values('G04001','领带','T00004',98.00,0,200,500,600,null);
insert into t_goods values('G04002','男装西服','T00004',898,0.92,20,50,0,null);
4)将供应商数据添加到供应商表中
insert into t_supplier values('000001','广州电子','李先生',null,null);
insert into t_supplier values('000002','李宁服装公司','刘小姐',null,null);
insert into t_supplier values('000003','利口福食品公司','王','81111111',null);
insert into t_supplier values('000004','佛山电脑城',null,null,null);
5)为了平衡系统中的数据,将有库存的商品数据做一个虚拟的采购单,采购单数据为:
insert into t_main_procure values('P00000000001',null,sysdate,null,'待审核','初始化系统数据用');
delete from t_main_procure;
insert into t_main_procure values('P00000000001',null,default,null,'待审核','初始化系统数据用');
赋值要求为系统时间,可采用两种方式,第一是使用sysdate系统变量赋值,第二是使用表定义时采购日期的默认值
2、在添加数据中使用序列
1)delete from t_user; 2)create sequence seq_uiid start with 1 increment by 1; insert into t_user(uiid,uname) values(to_char(seq_uiid.nextval,'000000'),'系统管理员');
3、在添加数据中使用子查询
三、实现
1)订单中的订单号使用序列,故创建一个序列用于订单号的列值
create sequence seq_omid;
2)用户编号使用用户表中的所有用户编号,日期,总金额为null,订单状态为"1",备注为null的值
select uiid,sysdate,null,'1',null from t_user;
3) 在select 语句中添加上序列的nextval方法的引用后添加到订单表中,序列值生成后要经过如下的变化才能满足需求:
insert into t_main_order select 'O'|trim(to_char(seq_omid.nextval,'0000000000')),uiid,sysdate,null,'1',null from t_user;
四、验证
1、脚本微调,在全菜中已为表t_user增加备注字段umemo(not null), 先alter table t_user modify umemo varchar2(3) null;
2、脚本微调,如去null外的单引号
3、to_char提示ORA-12899,原来:Oracle中将数字转换为字符串以后,会在字符串的前面预留一个位置存放数字的符号,正的数字符号为空格,负的就是一个"-"号而没有空格,采取trim()去空格函数,也可以使用在格式字符串中加入"fm"的方式避免空格的影响。
4、select OMID,UIID,ODATE from t_main_order;
本文来源 我爱IT技术网 http://www.52ij.com/jishu/5058.html 转载请保留链接。
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
