怎样给oracle视图加一个主键

怎样给oracle视图加一个主键,第1张

oracle是可以为视图加主键,写法: create or replace view mylist(myid,insize,outsize,statrtime,endtime,jobid,imeiid,ip,useragent UNIQUE RELY DISABLE NOVALIDATE,CONSTRAINT id_pk PRIMARY KEY (myid) RELY DISABLE NOVALIDATE)

as

select job.dm_session_id as myid,

http.dm_request_size as insize,

http.dm_response_size as outsize,

job.begin_time_stamp as statrtime,

job.end_time_stamp as endtime,

job.job_id as jobid,

job.device_external_id as imeiid,

http.client_ip as ip,

http.user_agent as useragent

from V_DM_TRACKING_LOG_JOB_SUM job,DM_TRACKING_LOG_HTTP http where job.Dm_Session_Id=http.dm_session_id

1.先清理现有的数据并规划只能一个主键,或者考虑组合主键(即ID列与另一个关键列组合成主键)

2.通过SQL增加主键:alter table tabname add constraint tabname_pk primary key (id) enable validate

组合键:alter table tabname add constraint tabname_pk primary key (id,另一列名) enable validate

1.先建一个序列:

-- Create sequence 

create sequence auto_id

minvalue 1

maxvalue 9999999999999

start with 1

increment by 1

cache 10

2.创建触发器

create or replace trigger auto_col_trigger

  before insert on auto_tab

  for each row

declare

  v_id number := 0

begin

  select auto_id.nextval into v_id from dual

  :new.id := v_id

end

我随便写的,表名和字段名改一下就行了。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11365969.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存