添加字段
-- Add/modify columns
alter table table_name add column_name varchar2(25);
-- Add comments to the columns
comment on column table_name.column_name
is '字段注释';
修改字段
create table sys_user_bak as select * from sys_user;
create table new_table
as (select 'table_id' as table_id,'remark' as remark_1 from dual);
insert into stu_test_his select * from stu_test;
insert into sys_user_detail (id,user_id,user_name,dept_id,dept_name
,create_time,update_time)
select sys_guid()as id,t.user_id,t.user_name,t.dept_id,t2.dept_name,
(SYSDATE),(SYSDATE+1)
from sys_user t
left join sys_company_dept t2 on t.dept_id= t2.dept_id
where t2.parent_id='B001';



update sys_user_detail t
set t.dept_name
=(
select t2.dept_name from sys_company_dept t2 where t2.dept_id=t.dept_id
);
merge into 解决,sql 如下merge into sys_user_detail d
using (select t.user_id, t.user_name, t.dept_id, t2.dept_name
from sys_user t
left join sys_company_dept t2
on t.dept_id = t2.dept_id) temp
on (d.dept_id = temp.dept_id)
when matched then
update
set d.user_name = temp.user_name, d.dept_name = temp.dept_name
where 1 = 1 and d.user_id=temp.user_id and d.dept_id=temp.dept_id
when not matched then
insert
(id, user_id, user_name, dept_id, dept_name, create_time, update_time)
values
(sys_guid(),
temp.user_id,
temp.user_name,
temp.dept_id,
temp.dept_name,
(SYSDATE),
(SYSDATE + 1));
-- Create table
create table SYS_USER_DETAIL
(
id VARCHAR2(32),
user_id VARCHAR2(20),
user_name VARCHAR2(50),
dept_id VARCHAR2(20),
dept_name VARCHAR2(50),
graduate_school VARCHAR2(50),
graduate_date DATE,
create_time DATE,
update_time DATE
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
-- Add comments to the columns
comment on column SYS_USER_DETAIL.id
is '数据id';
comment on column SYS_USER_DETAIL.user_id
is '用户编号';
comment on column SYS_USER_DETAIL.user_name
is '用户name';
comment on column SYS_USER_DETAIL.graduate_school
is '毕业学校';
comment on column SYS_USER_DETAIL.graduate_date
is '毕业时间';
create sequence seq_id_student_id
minvalue 1
maxvalue 9999999999999999999999999999
start with 1
increment by 1
nocache;
select seq_id_student_id.nextval from dual;

需要如下操作:
-- 首先改成Long类型
alter table table_name modify column_name long;
-- 在Long类型的基础上改成clob类型
alter table table_name modify column_name clob;
直接用下面语句修改不可以:
alter table table_name modify column_name null;
需要用下面这些语句操作即可,如下:
alter table table_name add column_name_temp number(20);
update table_name set column_name_temp=column_name;
alter table table_name drop column column_name;
alter table table_name add column_name number(20);
update table_name set column_name=column_name_temp;
alter table table_name drop column column_name_temp;
即:先新增一个临时字段,存储原有的值,删除原有的字段,新增原有的字段添加值,最后删除新增的临时字段


select to_char(t.enter_time,'yyyy-MM-dd HH24'),count(0) num
from mount_hua_tourist t
group by to_char(t.enter_time,'yyyy-MM-dd HH24');

建表
-- Create table
create table MOUNT_HUA_TOURIST
(
tourist_id VARCHAR2(30),
tourist_name VARCHAR2(30),
enter_time DATE,
out_time DATE
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
-- Add comments to the table
comment on table MOUNT_HUA_TOURIST
is '华山游客记录表';
-- Add comments to the columns
comment on column MOUNT_HUA_TOURIST.tourist_id
is '华山游客id';
comment on column MOUNT_HUA_TOURIST.tourist_name
is '华山游客name';
comment on column MOUNT_HUA_TOURIST.enter_time
is '入山时间';
comment on column MOUNT_HUA_TOURIST.out_time
is '离山时间';
插入数据
insert into mount_hua_tourist (TOURIST_ID, TOURIST_NAME, ENTER_TIME, OUT_TIME)
values ('A001', '游客1', to_date('13-10-2022 07:12:41', 'dd-mm-yyyy hh24:mi:ss'), null);
insert into mount_hua_tourist (TOURIST_ID, TOURIST_NAME, ENTER_TIME, OUT_TIME)
values ('A002', '游客2', to_date('13-10-2022 08:13:41', 'dd-mm-yyyy hh24:mi:ss'), null);
insert into mount_hua_tourist (TOURIST_ID, TOURIST_NAME, ENTER_TIME, OUT_TIME)
values ('A003', '游客3', to_date('13-10-2022 08:34:41', 'dd-mm-yyyy hh24:mi:ss'), null);
insert into mount_hua_tourist (TOURIST_ID, TOURIST_NAME, ENTER_TIME, OUT_TIME)
values ('A004', '游客4', to_date('13-10-2022 08:50:41', 'dd-mm-yyyy hh24:mi:ss'), null);
insert into mount_hua_tourist (TOURIST_ID, TOURIST_NAME, ENTER_TIME, OUT_TIME)
values ('A005', '游客5', to_date('13-10-2022 09:10:41', 'dd-mm-yyyy hh24:mi:ss'), null);
insert into mount_hua_tourist (TOURIST_ID, TOURIST_NAME, ENTER_TIME, OUT_TIME)
values ('A006', '游客6', to_date('13-10-2022 10:13:41', 'dd-mm-yyyy hh24:mi:ss'), null);
insert into mount_hua_tourist (TOURIST_ID, TOURIST_NAME, ENTER_TIME, OUT_TIME)
values ('A007', '游客7', to_date('13-10-2022 10:44:41', 'dd-mm-yyyy hh24:mi:ss'), null);
--先按照 时间 排序,然后累加 num
select to_char(t.enter_time,'yyyy-MM-dd HH24'),count(0) num,
sum(count(0)) over(order by to_char(t.enter_time,'yyyy-MM-dd HH24')) sum_up
from mount_hua_tourist t
where 1=1
--and to_char(t.enter_time,'yyyy-MM-dd')= to_char(sysdate,'yyyy-MM-dd')
and to_char(t.enter_time,'yyyy-MM-dd')= '2022-10-13'
and t.out_time is null
group by to_char(t.enter_time,'yyyy-MM-dd HH24');
效果


count 函数)select t.kennels_num ,t.dog_sex,count(t.dog_sex)
from dog t
group by t.dog_sex,t.kennels_num
order by t.kennels_num,t.dog_sex;

性别已知和未知的数量select
sum(case t.dog_sex when '0' then 1 when '1' then 1 else 0 end)know_sex,
sum(case t.dog_sex when '2' then 1 when '3' then 1 else 0 end)not_know_sex
from dog t;

select t.kennels_num,
sum(case t.dog_sex when '0' then 1 when '1' then 1 else 0 end)know_sex,
sum(case when t.dog_sex in('2','3') then 1 else 0 end)not_know_sex
from dog t
group by t.kennels_num;



select round(1/23,4) r1_1,
trim(concat(to_char(100*round(1/23,4),'990.99'),'%')) r1_2,
round(92/95,4)r2_1,
trim(concat(to_char(100*round(92/95,4),'990.99'),'%'))r2_2
from dual;



extract(fmt from 日期),提取日期中的特定部分select sysdate,
extract(year from sysdate) year,
extract(month from sysdate) month,
extract(day from sysdate) day,
extract(hour from systimestamp) hour,
extract(minute from systimestamp) minute,
extract(second from systimestamp) second
from dual;

to_char()函数,不解释,自己看select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss')d1,
to_char(sysdate,'yyyy-MM-dd HH24:mm:ss')d2,
to_char(sysdate,'YYYY-MM-DD HH24:MI:SS')d3,
to_char(sysdate,'yyyy-MM-dd HH:mm:ss')d4,
to_char(sysdate,'YYYY"年"MM"月"DD"日" HH24:MI:SS')d5
from dual;

select t.enter_time,
to_char(t.enter_time,'yyyy-MM-dd HH24')d1,
to_char(t.enter_time,'yyyy-MM-dd HH24:mi')d2,
to_char(t.enter_time,'yyyy-MM-dd HH24:mi:ss')d3
from mount_hua_tourist t;

to_date()select to_date('2020-11-12 ','yyyy-MM-dd'),
to_date('2020-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss')
from dual;

to_timestamp()select to_timestamp('2020-01-05', 'yyyy-mm-dd hh24:mi:ss:ff'),
to_timestamp('2020-01-05 23:46.24.855000', 'yyyy-mm-dd hh24:mi:ss:ff')
from dual;

add_months()select sysdate,add_months(sysdate,3) from dual;

to_char() 函数 ,如下:select to_char(sysdate,'d') from dual;--周的第几天(周日是第一天的情况)
select to_char(sysdate-1,'d') from dual;--周的第几天(周一是第一天的情况)

select
to_date('2022-12-31','yyyy-MM-dd'),
to_char(to_date('2022-12-31','yyyy-MM-dd'),'d') r_1_0,--'d' 周的第几天(周日是第一天的情况)
to_char(to_date('2022-12-31','yyyy-MM-dd')-1,'d') r_1_1,--'d' 周的第几天(周一是第一天的情况)
to_char(to_date('2022-12-31','yyyy-MM-dd'),'dy') r_2, --'dy' 求出星期几
to_char(to_date('2022-12-31','yyyy-MM-dd'),'day') r_3,--'day' 求出星期几
to_char(to_date('2022-12-31','yyyy-MM-dd'),'dd') r_4,--'dd'月的第几天
to_char(to_date('2022-12-31','yyyy-MM-dd'),'ddd')r_5--'ddd'年的第几天
from dual;
select
sysdate - interval '2' day,
sysdate -2
from dual;

NVL(X,VALUE)NVL(X,VALUE)函数,如果X为空,返回value,否则返回X
nvl2(x,value1,value2)
decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值)
select t.dog_name,
decode(t.dog_sex,'0','男孩','1','女孩','2','绝育','未知')sex,
t.dog_desc
from dog t;

select t.dog_name,
(case t.dog_sex when '0' then '男孩' when '1' then '女孩' when '2' then '绝育'else '未知'end)sex,
t.dog_desc
from dog t;


select t.table_name,t.column_name,t.data_type
from all_tab_columns t
where t.table_name='DOG_TEST';

select t.table_name,
max(decode(t.column_name,'ID',t.data_type)) ID,
max(decode(t.column_name,'DOG_NAME',t.data_type)) DOG_NAME,
max(decode(t.column_name,'DOG_SEX',t.data_type)) DOG_SEX,
max(decode(t.column_name,'DOG_DESC',t.data_type)) DOG_DESC
from all_tab_columns t
where t.table_name='DOG_TEST'
group by table_name;
select t.table_name,
max(case t.column_name when 'ID' then t.data_type end)ID,
max(case t.column_name when 'DOG_NAME' then t.data_type end)DOG_NAME,
max(case t.column_name when 'DOG_SEX' then t.data_type end)DOG_SEX,
max(case t.column_name when 'DOG_DESC' then t.data_type end)DOG_DESC
from all_tab_columns t
where t.table_name='DOG_TEST'
group by table_name;

create table tab_columns_test as
select t.table_name,
max(case t.column_name when 'ID' then t.data_type end)ID,
max(case t.column_name when 'DOG_NAME' then t.data_type end)DOG_NAME,
max(case t.column_name when 'DOG_SEX' then t.data_type end)DOG_SEX,
max(case t.column_name when 'DOG_DESC' then t.data_type end)DOG_DESC
from all_tab_columns t
where t.table_name='DOG_TEST'
group by table_name;

select table_name,col_key,col_value
from tab_columns_test
unpivot(col_value for col_key in(ID, DOG_NAME, DOG_SEX,DOG_DESC));


select dept_id,dept_name,
LISTAGG(user_name, ',') WITHIN GROUP(order by user_name) as all_user_names
from sys_user_detail where 1=1
and dept_id='D001'
group by dept_id,dept_name;

select dept_id,WM_CONCAT(user_name) all_user_names
from sys_user_detail
WHERE dept_id ='D001'
GROUP BY dept_id;


hi@ip1:1521/orclpdb1,数据库2是hello@ip2:1521/orcl,现在想在数据库1上访问数据库2,如何实现,请往下看select * from user_sys_privs where privilege like upper('%DATABASE LINK%');
grant create public database link,drop public database link to hi;
create any tablegrant create any table to hi
select * from user_sys_privs

创建语句如下:
create public database link hi_to_hello_DBLINK
connect to hello identified by hello123
using '(DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL = TCP)(HOST = ip2)(PORT = 1521)))
(CONNECT_DATA =(SERVICE_NAME = orcl)))';

查看创建的dblink
下面两个查询语句都可,如下:
select * from dba_db_links;
select * from dba_objects where object_type='DATABASE LINK';


查看创建的dblink(plsql里)
如下:

select * from test_table@hi_to_hello_DBLINK;
select * from hello.test_table@hi_to_hello_DBLINK;


ip2下orcl里其他用户的数据,只有hello用户原本可访问即可
--1.查询是否有创建 dblink 的权限
select * from user_sys_privs where privilege like upper('%DATABASE LINK%');
--2.如果没有权限,则授权
grant create public database link,drop public database link to hi;
grant create any table to hi;
--3.创建dblink
create public database link hi_to_hello_DBLINK
connect to hello identified by hello123
using '(DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL = TCP)(HOST = ip2)(PORT = 1521)))
(CONNECT_DATA =(SERVICE_NAME = orcl)))';
--4.查看你创建的dblink
select * from dba_db_links;
select * from dba_objects where object_type='DATABASE LINK';
--5.访问数据库
select * from test_table@hi_to_hello_DBLINK;
select * from hello.test_table@hi_to_hello_DBLINK;
select * from hello2.sys_company_dept@hi_to_hello_DBLINK;