oracle中创建数据表入门篇

php中文网
发布: 2016-06-07 17:46:19
原创
1416人浏览过

使用 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 表空间。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
	  <tr>
		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy6350')">复制代码</td>
	  </tr>
	  <tr>
		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6350><pre class="brush:php;toolbar:false">drop user phpuser cascade;<br /><br />create user phpuser identified by phpuserpw;<br /><br />grant connect, resource to phpuser;<br /><br />alter user phpuser default tablespace users temporary tablespace temp account unlock;
登录后复制
设计样例数据库的结构

要排列和存储所需的所有数据,您需要使用两个表:

  • 一个是 wishers 表,用于存储注册用户的名称和口令
  • 另一个是 wishes 表,用于存储心愿说明

样例数据库的结构:两个表通过 wisher_id 相关联

wishers 表包含三个字段:

  1. id - 许愿者的唯一 ID。该字段用作主键
  2. name
  3. 口令

wishes 表包含四个字段:

  1. id - 心愿的唯一 ID。该字段用作主键
  2. wisher_id - 心愿所属的许愿者的 ID。该字段用作外键
  3. description
  4. due_date - 请求心愿时的日期

这些表通过许愿者的 ID 相关联。除了 wishes 表中的 due_date 以外,所有字段都是必需的。

创建 Oracle 数据库架构

  1. 以创建的用户身份登录到数据库。

    如果通过 NetBeans IDE 进行连接,请使用新用户的名字和口令创建一个连接。确保选择的架构具有与用户相同的名称。(请参见“连接到 Oracle 数据库”教程的建立到 Oracle DB 的连接部分。)

  2. 要创建 wishers 表,请运行以下 SQL 查询:
    <table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
    	  <tr>
    		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
    		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy1070')">复制代码</td>
    	  </tr>
    	  <tr>
    		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1070><pre class="brush:php;toolbar:false">create table wishers (<br /> id number not null,<br /> name varchar2(50) unique not null,<br /> password varchar2(50) not null,<br /> constraint wishers_pk primary key(id)<br />);
    登录后复制
    要创建 wishes 表,请运行以下 SQL 查询。请注意,将创建一个外键以将心愿与许愿者相关联。
  3. <table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
    	  <tr>
    		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
    		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy4378')">复制代码</td>
    	  </tr>
    	  <tr>
    		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4378><pre class="brush:php;toolbar:false">create table wishes (<br /> id number not null,<br /> wisher_id number not null,<br /> description varchar2(255) not null,<br /> due_date date,<br /> constraint wishes_pk primary key(id),<br /> constraint wishes_fk1 foreign key(wisher_id) references wishers(id)<br />);
    登录后复制
    验证是否将新表添加到数据库中。如果使用 NetBeans IDE 连接到数据库,请转至“服务”窗口中的 jdbc:oracle:thin:@localhost:1521:XE [PHPUSER 上的 phpuser] 连接节点。将在“表”节点中列出新表。(如果未显示这些表,请右键单击连接,然后选择“刷新”。)
    NetBeans IDE 的“服务”窗口中显示的数据库表

注意:您可以在此处下载一组 SQL 命令以创建 Oracle 数据库表。

添加序列和以增加 ID 值

在使用 Oracle 数据库时,您必须指定一个序列以增加值。要在表中添加新成员时增加值,请添加一个触发器。

  1. 要为 wisher 表添加序列,请运行以下 SQL 命令:
    <table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
    	  <tr>
    		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
    		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy2323')">复制代码</td>
    	  </tr>
    	  <tr>
    		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2323><pre class="brush:php;toolbar:false">create sequence wishers_id_seq start with 1 increment by 1;
    登录后复制
    要在添加新的许愿者时在 wishers 表的 ID 列上触发序列,请运行以下 SQL 命令:
  2. <table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
    	  <tr>
    		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
    		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy7818')">复制代码</td>
    	  </tr>
    	  <tr>
    		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7818>create or replace trigger wishers_insert<br />before insert on wishers<br />for each row<br />begin<br /> wishers_id_seq.nextval into :new.id from dual;<br />end;<br /></td>
    	  </tr>
    	</table>/
    登录后复制
  3. 为 wishes 表添加一个序列。
    <table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
    	  <tr>
    		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
    		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy6982')">复制代码</td>
    	  </tr>
    	  <tr>
    		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6982><pre class="brush:php;toolbar:false">create sequence wishes_id_seq start with 1 increment by 1;
    登录后复制
    添加一个触发器,以在添加新的心愿时在 wishes 表的 ID 列上运行序列。
  4. <table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
    	  <tr>
    		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
    		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy2791')">复制代码</td>
    	  </tr>
    	  <tr>
    		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2791>create or replace trigger wishes_insert<br />before insert on wishes<br />for each row<br />begin<br />select wishes_id_seq.nextval into :new.id from dual;<br />end;<br /></td>
    	  </tr>
    	</table>/
    登录后复制

注意:您可以在此处下载一组 SQL 命令以创建 Oracle 数据库表,包括序列和触发器。

输入测试数据

要测试应用程序,您需要使用数据库中的某些数据。下面的示例说明了如何添加两个许愿者和四个心愿。

  1. 添加一个名为 Tom 且口令为 tomcat 的许愿者。
    <table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
    	  <tr>
    		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
    		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy5640')">复制代码</td>
    	  </tr>
    	  <tr>
    		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5640><pre class="brush:php;toolbar:false">insert into wishers (name, password) values ('Tom','tomcat');
    登录后复制
    添加一个名为 Jerry 且口令为 jerrymouse 的许愿者。
  2. <table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
    	  <tr>
    		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
    		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy3063')">复制代码</td>
    	  </tr>
    	  <tr>
    		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3063><pre class="brush:php;toolbar:false">insert into wishers (name, password) values ('Jerry', 'jerrymouse');<br />commit;
    登录后复制
    添加心愿。
  3. <table width="620" align="center" border="0" cellpadding="1" cellspacing="1"  style="background:#FB7">
    	  <tr>
    		<td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td>
    		<td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onClick="doCopy('copy6589')">复制代码</td>
    	  </tr>
    	  <tr>
    		<td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6589><pre class="brush:php;toolbar:false">insert into wishes (wisher_id, description, due_date) <br /> values (1, 'Sausage', to_date('2008-04-01', 'YYYY-MM-DD');<br /><br />insert into wishes (wisher_id, description) <br /> values (1, 'Icecream');<br /><br /><br />insert into wishes (wisher_id, description, due_date) values (2, 'Cheese', to_date('2008-05-01', 'YYYY-MM-DD'));<br /><br />insert into wishes (wisher_id, description)<br /> values (2, 'Candle');<br />commit;
    登录后复制
    验证是否添加了测试数据。如果使用 NetBeans IDE 查看测试数据,请在相关表上单击鼠标右键,然后从上下文菜单中选择“查看数据”。
    使用 NetBeans IDE 界面查看输入的测试数据
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号