Oracle数据库创建用户、表空间、表、索引、序列、触发器

由于公司项目需要,建立以下表及其相关,写此博客,作为学习。


文章参考网上其他资料,如有错误,接受指正,共同进步。


-- 新建表空间 TS_ZW_DXPT_DATA

create tablespace TS_WZ_PTDX_DATA 
datafile 'ts_wz_ptdx_data_001'
size 10M   
autoextend on next 10M;

其中 datafile:数据文件名、size:大小、autoextend:自动增长大小


-- 新建用户 zw_dxpt 默认表空间 TS_WZ_PTDX_DATA

create user wz_ptdx identified by "passw0rd" 

default tablespace TS_WZ_PTDX_DATA;


-- 用户授权

grant create view to wz_ptdx;

  --连接数据库,资源的权限
grant connect,resource to wz_ptdx
  --无限表空间的权限
grant unlimited tablespace to wz_ptdx
  --会话权限  
grant create session to wz_ptdx;


--wz_ptdx下创建ptdx_submitmsg表

   --number类型长度最大为38,number相较于int在Oracle中更为正式,number的第二位不加限制不能保证为一个整数。
create table wz_ptdx.ptdx_submitmsg(
       id number(20,0) not null,
       from_user varchar2(50) not null,
       check_user varchar2(50) not null,
       to_user varchar2(50) not null,
       receive_group number(10,0),
       msg_title varchar2(100) not null,
       msg_text varchar2(255) not null,
       msg_length number(38,0),
       msg_type number(10,0),
       msg_status number(10,0),
       create_time date default sysdate,
       constraint pk_id1 primary key(id)
)


--ptdx_submitmsg添加注释

   --添加表注释:
comment on table wz_ptdx.ptdx_submitmsg IS '提交短信信息表';
   --添加字段注释:
comment on column wz_ptdx.ptdx_submitmsg.id is 'id 主键';
comment on column wz_ptdx.ptdx_submitmsg.from_user is '短信发送者';
comment on column wz_ptdx.ptdx_submitmsg.check_user is '短信审核者';
comment on column wz_ptdx.ptdx_submitmsg.to_user is '短信接收者';
comment on column wz_ptdx.ptdx_submitmsg.receive_group is '短信接受对象';
comment on column wz_ptdx.ptdx_submitmsg.msg_title is '短信主题';
comment on column wz_ptdx.ptdx_submitmsg.msg_text is '短信内容';
comment on column wz_ptdx.ptdx_submitmsg.msg_length is '短信长度';
comment on column wz_ptdx.ptdx_submitmsg.msg_type is '短信类型';
comment on column wz_ptdx.ptdx_submitmsg.msg_status is '短信状态';
comment on column wz_ptdx.ptdx_submitmsg.create_time is '短信创建时间';


-- 创建索引
   --Oracle建表时会根据主键创建同名索引,无需再次创建索引
-- create index index_ptdx_submitmsg on wz_ptdx.pt
dx_submitmsg(id);

-- 创建序列ptdx_submitmsg_seq

create sequence ptdx_submitmsg_seq
minvalue 1
maxvalue 999999999
start with 1
increment by 1
cache 20;


-- 创建触发器

create or replace trigger ptdx_submitmsg_tri
before insert on wz_ptdx.ptwz_submitmsg
for each row
begin
select ptdx_submitmsg_seq.nextval into :new.id from dual;
end;

-- Test insert

insert into wz_ptdx.ptdx_submitmsg(from_user,check_user,to_user,receive_group,msg_title,msg_text,msg_length,msg_type,msg_status) 
       values('张三','李四','王五',0,'节日祝福','中秋快乐',100,2,1);

select * from wz_ptdx.ptdx_submitmsg;



Success;
End;



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值