使用 oracle 的工具,或者将 netbeans ide 连接到数据库并使用 ide 的 sql 编辑器。netbeans 7.0 目前仅提供 beta 或开发版本,可以改进到 oracle 数据库的连接。要了解如何将 netbeans ide 连接到 oracle 数据库以及如何在数据库中创建用户
使用 oracle 的工具,或者将 netbeans ide 连接到数据库并使用 ide 的 sql 编辑器。netbeans 7.0 目前仅提供 beta 或开发版本,可以改进到 oracle 数据库的连接。要了解如何将 netbeans ide 连接到 oracle 数据库以及如何在数据库中创建用户
通过使用所选的工具,创建以下用户:
用户名 user
口令 phpuserpw
系统权限 create table
代码如下 复制代码
create view
create sequence
create trigger
角色 (oracle database 10.x) connect
代码如下 复制代码
resource
下面是一组用于创建该用户的示例 sql 命令。这些命令假定具有 users 和 temp 表空间。
代码如下 复制代码
drop user phpuser cascade;
create user phpuser identified by phpuserpw;
grant connect, resource to phpuser;
alter user phpuser default tablespace users temporary tablespace temp account unlock;
设计样例数据库的结构要排列和存储所需的所有数据,您需要使用两个表:
一个是 wishers 表,用于存储注册用户的名称和口令 另一个是 wishes 表,用于存储心愿说明
wishers 表包含三个字段:
id - 许愿者的唯一 id。该字段用作主键 name 口令wishes 表包含四个字段:
id - 心愿的唯一 id。该字段用作主键 wisher_id - 心愿所属的许愿者的 id。该字段用作外键 description due_date - 请求心愿时的日期这些表通过许愿者的 id 相关联。除了 wishes 表中的 due_date 以外,所有字段都是必需的。
创建 oracle 数据库架构
以创建的用户身份登录到数据库。 如果通过 netbeans ide 进行连接,请使用新用户的名字和口令创建一个连接。确保选择的架构具有与用户相同的名称。(请参见“连接到 oracle 数据库”教程的建立到 oracle db 的连接部分。)
要创建 wishers 表,请运行以下 sql 查询:
代码如下 复制代码
create table wishers (
id number not null,
name varchar2(50) unique not null,
password varchar2(50) not null,
constraint wishers_pk primary key(id)
);
要创建 wishes 表,请运行以下 sql 查询。请注意,将创建一个外键以将心愿与许愿者相关联。
代码如下 复制代码
create table wishes (
id number not null,
wisher_id number not null,
description varchar2(255) not null,
due_date date,
constraint wishes_pk primary key(id),
constraint wishes_fk1 foreign key(wisher_id) references wishers(id)
);
验证是否将新表添加到数据库中。如果使用 netbeans ide 连接到数据库,请转至“服务”窗口中的 jdbc:oracle:thin:@localhost:1521:xe [phpuser 上的 phpuser] 连接节点。将在“表”节点中列出新表。(如果未显示这些表,请右键单击连接,然后选择“刷新”。)
注意:您可以在此处下载一组 sql 命令以创建 oracle 数据库表。
添加序列和以增加 id 值
在使用 oracle 数据库时,您必须指定一个序列以增加值。要在表中添加新成员时增加值,请添加一个触发器。
要为 wisher 表添加序列,请运行以下 sql 命令:
代码如下 复制代码
create sequence wishers_id_seq start with 1 increment by 1;
要在添加新的许愿者时在 wishers 表的 id 列上触发序列,请运行以下 sql 命令:
代码如下 复制代码
create or replace trigger wishers_insert
before insert on wishers
for each row
begin
wishers_id_seq.nextval into :new.id from dual;
end;
/ 为 wishes 表添加一个序列。
代码如下 复制代码
create sequence wishes_id_seq start with 1 increment by 1;
添加一个触发器,以在添加新的心愿时在 wishes 表的 id 列上运行序列。
代码如下 复制代码
create or replace trigger wishes_insert
before insert on wishes
for each row
begin
select wishes_id_seq.nextval into :new.id from dual;
end;
/ 注意:您可以在此处下载一组 sql 命令以创建 oracle 数据库表,包括序列和触发器。
输入测试数据
要测试应用程序,您需要使用数据库中的某些数据。下面的示例说明了如何添加两个许愿者和四个心愿。
添加一个名为 tom 且口令为 tomcat 的许愿者。
代码如下 复制代码
insert into wishers (name, password) values ('tom','tomcat');
添加一个名为 jerry 且口令为 jerrymouse 的许愿者。
代码如下 复制代码
insert into wishers (name, password) values ('jerry', 'jerrymouse');
commit;
添加心愿。
代码如下 复制代码
insert into wishes (wisher_id, description, due_date)
values (1, 'sausage', to_date('2008-04-01', 'yyyy-mm-dd');
insert into wishes (wisher_id, description)
values (1, 'icecream');
insert into wishes (wisher_id, description, due_date) values (2, 'cheese', to_date('2008-05-01', 'yyyy-mm-dd'));
insert into wishes (wisher_id, description)
values (2, 'candle');
commit;
验证是否添加了测试数据。如果使用 netbeans ide 查看测试数据,请在相关表上单击鼠标右键,然后从上下文菜单中选择“查看数据”。