J2EE开发购物网站解析(4)
[@more@]admin表结构(管理员列表)
字段名称 数据类型 允许空 主键/外键 备注
admin_id INTEGER(自动编号) 否 主键 管理员ID标记
adminname varchar2(20) 否 管理员名称
password varchar2(20) 否 管理员密码 |
设计完表结构后,我们就要开始创建了。创建表我想已经不是什么难事了,那么我们要注意的是product、item、orders这三个表之间的关联,还有自动编号。
下面是完整的SQL语句,在后面我会给出详细的说明,你可以在SQL*Plus里对照着输入,也可以将它存为SQL脚本文件,在SQL*Plus或SQLPlus Worksheet里执行。当然也可以把代码直接拷贝到SQL*Plus里执行!
rem ///BY MAXUAN 开始///
create table item(
type_id integer not null,
type varchar2(30),
constraint item_pk primary key(type_id)
);
create table product(
product_id integer not null,
title varchar2(30) not null,
type_id integer not null,
info varchar2(80),
price number(16,2) not null,
constraint product_pk primary
key (product_id),
constraint product_fk foreign
key(type_id) references item(type_id)
);
create table orders(
order_id integer not null,
name varchar2(20) not null,
address varchar2(100),
tel number(16),
email varchar2(30) not null,
btime date,
product_id integer not null,
uword varchar2(100),
constraint orders_pk primary
key(order_id),
constraint orders_fk foreign
key(product_id) references product(product_id)
);
create table admin(
admin_id integer not null,
adminname varchar2(20) not null,
password varchar2(20) not null,
constraint admin_pk primary key(admin_id)
);
create sequence type_id
increment by 1 start with 1;
create sequence product_id
increment by 1 start with 1;
create sequence order_id
increment by 1 start with 1;
create sequence admin_id
increment by 1 start with 1;
rem ///BY MAXUAN 结束/// |
说明一:建立表之间的关联
product、item、orders三个表通过公共域,通常称为键域(Key Field)进行关联,存在两种类型的键:主键(Primary key)和外部键(Foreign key)。主键使表中的数据行保持唯一,在表product中,product_id为主键,表orders中也包含有product_id,此时的product_id就是外部键。一个表的外部键从其它表中获取信息。
说明二:关于自动编号
在access中有自动编号的数据类型,MSSQL和MYSQL也都有自动增长的数据类型,插入记录时不用操作此字段,会自动获得数据值,而oracle没有自动增长的数据类型,我们需要建立一个自动增长的序列号,插入记录时要把序列号的下一个值赋于此字段,可以预见的是,有此功能,我们可以把数据从ACCESS、MSSQL或MYSQL迁移到oracle了。
create sequence type_id increment
by 1 start with 1; |
这句中,type_id为序列号的名称,每次增长为1,起始序号为1。 好了,咱们的数据库已经建好了,而且从中也了解到一些基本的相关知识。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7839366/viewspace-919928/
,如需转载,请注明出处,否则将追究法律责任。