Oracle建表,建序列,建触发器,自动生成唯一主键。dual解释

--根据数据库设计说明书创建表
--GOODS
create table goods1
(
gid number(11) primary key,
gname varchar2(20) not null unique,
gprice number(18,1) not null,
gnum number(11) not null
);


--实现GOODS1表中主键自动生成(需要使用序列和触发器)
--为GOODS1表创建序列:生成唯一一系列的数
create sequence goods_seq
start with 1     --从1开始
increment by 1  --每次增1
minvalue 1      --最小值
maxvalue 999999999999999   --最大值
nocycle           --不循环取数
cache 10;       --缓存


--为GOODS1表创建触发器,用于自动从序列取值给GOODS1表中GID赋值
create trigger goods_trigger
before insert on goods1         --触发条件:在向GOODS1表中插入数据之前自动触发此触发器
for each row                    --行级触发器:插入的每一行数据都会触发
begin
   select goods_seq.nextval into :NEW.GID from dual;
end;



--向GOODS1表中插入测试数据
INSERT INTO goods1(gname,gprice,gnum) VALUES('computer',3000,100);
INSERT INTO goods1(gname,gprice,gnum) VALUES('computer2',3500,100);
INSERT INTO goods1(gname,gprice,gnum) VALUES('iphone6',4000,200);
INSERT INTO goods1(gname,gprice,gnum) VALUES('iphone6s',5000,300);
commit;
---------------------------------------------------------------------------------------------------------------------------------------------------------------
dual 的解释
1. dual 确实是一张表.是一张只有一个字段,一行记录的表. 
2.习惯上,我们称之为'伪表'.因为他不存储主题数据.
3. 他的存在,是为了操作上的方便.因为select 都是要有特定对象的.
如:select * from mytable ;
select * from myview;
等等.
但如果我们不需要从具体的表来取得表中数据,而是单纯地为了得到一些我们想得到的信息,并要通过select 完成时,就要借助一个对象,这个对象,就是dual;
如我们要计算 999*999 的值,可以用:
select 999*999 from dual;
来实现;
要拼接一个电话信息:
select concat('010-','88888888')||'转23' 高乾竞电话 from dual;

就变成了我们想要的格式输出.
4.dual是系统表,它包含了调用系统各种函数的功能,dual中永远只显示一条记录,我们不能创建和dual类似的表(普通表),即便创建了,也只能实现普通表具有的功能
---------------------------------------------------------------------------------------------------------------------------------------------------------------
以下摘自网络:

--建表t_account

create table t_account(  accountID number(8) not null,  accountName varchar(100) not null )tablespace portalone; alter table t_account add constraint pk_account primary key(accountID) using index tablespace portaloneindx;

 

--建序列 create sequence s_account minvalue 1 maxvalue 99999999 start with 1 increment by 1 cache 20;

 

--建触发器 create or replace trigger trigger_t_account before insert on t_account for each row when(new.accountID is null) declare begin    --从序列中获取新的序号并赋给新行的ID字段    select s_account.nextval into :new.accountID from dual;    --获取系统的guid/uuid生成主键    --select sys.guid() into :New.accountID from dual;    --获取系统时间作为表的某个字段值    --select to_char(sysdate, "YYYY-MM")into :New.YEAR_MONTH from dual; end trigger_t_account;   

 

--建表t_order drop table t_order; create table t_order(  orderID number(8) not null,  accountID number(8) not null,  orderName varchar(100) not null )tablespace portalone; alter table t_order add constraint pk_order primary key(orderID) using index tablespace portaloneindx; alter table t_order add constraint fk_order foreign key(accountID) references t_account(accountID);

 

--建立序列 create sequence s_order minvalue 1 maxvalue 99999999 start with 1 increment by 1 cache 20;

 

--建触发器 create or replace trigger trigger_t_order before insert on t_order for each row when (new.orderID is null) declare begin      select s_order.nextval into :new.orderID from dual;     select s_order.currval into :new.accountID from dual; end trigger_t_order;

 

--建表t_orderItem drop table t_orderItem; create table t_orderItem(  orderItemID number(8) not null,  orderID number(8) not null,  orderItemName varchar(100) not null )tablespace portalone; alter table t_orderItem add constraint pk_orderItem primary key(orderItemID) using index tablespace portaloneindx; alter table t_orderItem add constraint fk_orderItem foreign key(orderID) references t_order(orderID);

 

--建序列 create sequence s_orderItem minvalue 1 maxvalue 99999999 start with 1 increment by 1 cache 20;

 

--建触发器 create or replace trigger trigger_t_orderItem before insert on t_orderItem for each row when (new.orderItemID is null) declare begin     select s_orderItem.nextval into :new.orderItemID from dual;     select s_orderItem.currval into :new.orderID from dual; end trigger_t_orderItem;

----------------------------------------------------------------------------------------------------
以下摘自网路:
-建立表空间(oracle中的tablespace(表空间)就相当于sqlserver的database) 
CREATE TABLESPACE data01
DATAFILE 'D:\oracle\ora92\oradata\db\DATA01.dbf' SIZE 200M
UNIFORM SIZE 128k;
#指定区尺寸为128k,如不指定,区尺寸默认为64k 
--建立临时表空间
CREATE TEMPORARY TABLESPACE temp_data
TEMPFILE 'D:\TEMP_DATA.dbf' SIZE 100M

--建立用户
CREATE USER peter IDENTIFIED BY peter
DEFAULT TABLESPACE data01 
TEMPORARY TABLESPACE temp_data;

--给用户授权
grant connect,resource,dba to peter;

-- 从 '建立表空间' 到 '建立临时表空间' 到 ’建立用户‘ 到 ’给用户授权’ , 
-- 到此就可以用建立的用户进行登陆,然后建立table了
-- 并且以某个用户的身份进行登陆,进行备份与还原了

一、建立表空间

CREATE TABLESPACE data01
DATAFILE '/oracle/oradata/db/DATA01.dbf' 
SIZE 500M
UNIFORM SIZE 128k; #指定区尺寸为128k,如不指定,区尺寸默认为64k
(注意,必须先写datafile才能写size和uniform size,因为只有先指定了文件才能够指定文件的大小,这是一个因果关系)

二、建立UNDO表空间

CREATE UNDO TABLESPACE UNDOTBS02
DATAFILE '/oracle/oradata/db/UNDOTBS02.dbf' SIZE 50M

#注意:在OPEN状态下某些时刻只能用一个UNDO表空间,如果要用新建的表空间,必须切换到该表空间:

ALTER SYSTEM SET undo_tablespace=UNDOTBS02;

三、建立临时表空间

CREATE TEMPORARY TABLESPACE temp_data
TEMPFILE '/oracle/oradata/db/TEMP_DATA.dbf' SIZE 50M

四、改变表空间状态

1.使表空间脱机

ALTER TABLESPACE game OFFLINE;

如果是意外删除了数据文件,则必须带有RECOVER选项

ALTER TABLESPACE game OFFLINE FOR RECOVER;

2.使表空间联机

ALTER TABLESPACE game ONLINE;

3.使数据文件脱机

ALTER DATABASE DATAFILE 3 OFFLINE;

4.使数据文件联机

ALTER DATABASE DATAFILE 3 ONLINE;

5.使表空间只读

ALTER TABLESPACE game READ ONLY;

6.使表空间可读写

ALTER TABLESPACE game READ WRITE;

五、删除表空间(删除临时表空间也是同样的写法)

DROP TABLESPACE data01 INCLUDING CONTENTS AND DATAFILES;
drop tablespace temp_data including contents and datafiles;(删除临时表空间)

六、扩展表空间

首先查看表空间的名字和所属文件

select tablespace_name, file_id, file_name,
round(bytes/(1024*1024),0) total_space
from dba_data_files
order by tablespace_name;

1.增加数据文件
ALTER TABLESPACE game
ADD DATAFILE '/oracle/oradata/db/GAME02.dbf' SIZE 1000M;

2.手动增加数据文件尺寸
ALTER DATABASE DATAFILE '/oracle/oradata/db/GAME.dbf'
RESIZE 4000M;

3.设定数据文件自动扩展
ALTER DATABASE DATAFILE '/oracle/oradata/db/GAME.dbf
AUTOEXTEND ON NEXT 100M
MAXSIZE 10000M;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值