数据库基础操作

数据库操作:
1. 新增表

create table inpcase.progress_note_abc(
rec_sn varchar2(36),
ipid varchar2(36),
patient_name varchar2(100),
tpl_code varchar2(100),
tpl_name varchar2(100),
vreate_time varchar2(14),
modify_time varchar(14),
constraint PK_progress_note_abc primary key (rec_sn)
)

2. 新增字段
alter table inpcase.progress_note_abc add (name varchar(20) default '', pass varchar2(36) default '');

3. 修改表字段
名称:
ALTER TABLE inpcase.progress_note_abc RENAME COLUMN name1 TO name2;
长度(类型):
alter table inpcase.progress_note_abc modify (name2 varchar2(300) default '');

4. 删除表字段
alter table inpcase.progress_note_abc drop column name;

5.表重命名
rename inpcase.progress_note_abc to inpcase.progress_note_abcdefg; -- ???

6. 插入数据
一条数据:
insert into inpcase.progress_note_abc(rec_sn, ipid, patient_name) values('4','2','12')
多条数据:
insert all
into inpcase.progress_note_abc(rec_sn, ipid, patient_name) values('1','3','121')
into inpcase.progress_note_abc(rec_sn, ipid, patient_name) values('2','3','1221')
into inpcase.progress_note_abc(rec_sn, ipid, patient_name) values('3','2','12222')
into inpcase.progress_note_abc(rec_sn, ipid, patient_name) values('4','2','12123')
select 1 from dual
7. 清空表数据
TRUNCATE TABLE inpcase.progress_note_abc;

8. 删除表结构
DROP TABLE inpcase.progress_note_abc;

9. 查询

select i.*,rowid from inpcase.progress_note_abc i

分页:
select * from (select i.*,rownum as px from inpcase.progress_note_abc i) where px>2 and px<=4

根据值条件判断 -- case when then else end
select case ipid when '2' then 'cs' else '' end, patient_name from inpcase.progress_note_abc
select case ipid when '2' then 'cs' when '3' then 'cs1' else '' end as result, patient_name from inpcase.progress_note_abc

分组排序--ROW_NUMBER()over()

不分组排序(patient_name 排序)

select ROW_NUMBER()over(order by patient_name desc) rn,rec_sn, ipid, patient_name from inpcase.progress_note_abc

分组排序(ipid分组, patient_name 排序)

select ROW_NUMBER()over(partition by ipid order by patient_name desc) rn,rec_sn, ipid, patient_name from inpcase.progress_note_abc

分组排序取第一
select * from
(select ROW_NUMBER()over(partition by ipid order by patient_name desc) rn,rec_sn, ipid, patient_name from inpcase.progress_note_abc)
where rn = '1'

是否存在(根据是否存在确定是否执行sql)
select * from inpcase.progress_note_abc where exists (select * from inpcase.progress_note_abc where ipid = '2')

左连接/右连接/内连接
左连接:查出左表数据,右表存在对应数据显示,不存在显示未空
右连接:同左连接,以右表为基准
内连接:左右表都有数据才显示


10.视图

新增视图
create view v_i_p(ipid, patient_name) as select ipid, patient_name from inpcase.progress_note_abc

查询视图:
select * from v_i_p;

删除视图:
drop view v_i_p;

posted on 2021-12-16 17:41  java-go  阅读(47)  评论(0)    收藏  举报

导航