运行oracle服务是启动脚本(所有账户密码都是LIPUlipu521)
sqlplus / as sysdba;(超级用户,没有密码)
命令行输入 sqlplus
用户名 system(管理员账户)
密码 LIPUlipu521
Oracle
表空间 == 数据库(MySQL)
用户操作
###创建用户
###create user 用户名 identified by "密码";
create user c##root identified by "LIPUlipu521";
create user c##root identified by "LIPUlipu521"
default tablespace root_data
temporary tablespace root_temp;
grant create session to c##ROOT;
grant create table to c##ROOT;
grant create tablespace to c##ROOT;
grant create view to c##ROOT;
###删除用户
drop user c##root cascade;
###解锁Oracle账户
ALTER USER c##municipal ACCOUNT UNLOCK;
###设置密码失败次数为3次锁定账户
alter profile default limit failed_login_attempts 3;
###取消输入失败锁定账户功能
alter profile default limit failed_login_attempts unlimited;
### 设置新密码
alter user 用户名 identified by 新密码;
alter user c##municipal identified by Municipal;
###用户 有权限和角色
###新建用户应当赋予
### 角色权限 connect resource
### 系统权限 create session create table create tablespace create view unlimited tablespace
### 以sysdba身份登录Oracle 并解锁 SYSTEM账户
sqlplus / as sysdba
alter user SYSTEM account unlock;
创建数据表空间 (数据库文件会自动创建)
create tablespace MYORACLE
datafile 'G:\Database\Oracle\Oracle\Oracle\oradata\ORCL\MYORACLE.DBF'
size 50m
autoextend on
next 50m maxsize unlimited
extent management local
segment space management auto;
删除表空间
###drop tablespace 表空间名 including contents and datafiles
drop tablespace TEST including contents and datafiles
授予用户使用表空间的权限
grant connect,resource to c##ROOT;
grant create any sequence to c##ROOT;
grant create any table to c##ROOT;
grant delete any table to c##ROOT;
grant insert any table to c##ROOT;
grant select any table to c##ROOT;
grant unlimited tablespace to dec##ROOTmo;
grant execute any procedure to c##ROOT;
grant update any table to c##ROOT;
grant create any view to c##ROOT;
###alter user 用户名 quota unlimited on 表空间;
alter user SYSTEM quota unlimited on MYORACLE;
数据类型
### Nvarchar2 字符串 varchar (MySQL)
### Number 数字类型 int decimal
### TimeStamp(时间戳) 日期类型 datetime
关键词
### ROWNUM
###
常用函数
select sysdate from dual; ###sysdate 获取当前系统时间
select next_day(sysdate,1) from dual; ###查询当前系统时间下一星期一的日期
select trunc() from dual; ###trunc()函数
trunc(value,n) ###对value进行截断,如果n>0,保留n位小数;n<0,则保留-n位整数位;n=0,则去掉小数部分
select trunc(555.666) from dual; ###--返回结果为555,不加n时默认去掉小数部分
select trunc(555.666,2) from dual; ###--返回结果为555.66
select trunc(555.666,-2) from dual; ###--返回结果为500
to_char(x[,format]) ###将x转化为字符串。 format为转换的格式,可以为数字格式或日期格式
select to_char('12345.67') from dual; ###返回结果为12345.67
select to_char('12345.67','99,999.99') from dual; ###返回结果为12,345.67
to_date('2021-12-08 16:00:00','yyyy-mm-dd hh24:mi:ss')
lower(rawtohex(sys_guid())) ###生成uuid函数
select * from (select * from table_name order by rowNum desc) where rowNum=1 ### 灵活变换rowNum 实现limit功能
select chr(ascii('A') + level - 1) from dual connect by rownum <= 26 ### Oracle 查询26个英文字母
decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 等于 case when then ### decode函数只能在Oracle中使用
IF 条件=值1 THEN
RETURN(翻译值1)
ELSIF 条件=值2 THEN
RETURN(翻译值2)
......
ELSIF 条件=值n THEN
RETURN(翻译值n)
ELSE
RETURN(缺省值)
END IF
ROUND(number[,decimals]) ###number 待做截取处理的数值 decimals 指明需保留小数点后面的位数。截取小数位数
Oracle 中 || 是字符串+号
###大于小于 等于转义字符
< <= > >= &
< <= > >= &
###时间格式胡
yyyy-MM-dd hh24:mi:ss
时间
### 'Q' 季度 1,2,3,4
### 'MM' 月份 01,07
### 'WW' 一年中的第几周 29,52
### 'W' 本月第几周 1,2,3,4
### 'DD' 当月第几天 01,30
### 'D' 一周内第几天 1,7
### 'DY' 星期一 - 星期七
### last_day(sysdate) 本月最后一天 2022-07-31 13:06:11
### add_months(sysdate,2) 当前日期后第 2个月 2023-03-21 13:08:09
### months_between(sysdate,to_date(‘2012-11-12’,’yyyy-mm-dd’)) 计算相差月数
### select sysdate+1 from dual; 查询当前时间+一天
select to_char(sysdate,‘Q’) from dual;—指定日期的季度
Oracle配置操作
show parameter sga; ###显示内存分配情况
### 设置内存占用小点并不会明显降低查询速度,最关键的因素是有没有给所有表设置主键 或者 关键字段加索引
alter system set sga_max_size=1024m scope=spfile; ###修改最大占用内存的大小 一定要和 sga_target 同步更改
alter system set sga_target=1024m scope=spfile; ###修改 目标占用内存大小
### 通过 转化 pfile和spfile来对oracle配置文件进行修改
create pfile='D:\app\Administrator\product\12.1.0\dbhome_2\dbs\init2.ora' from spfile;
create pfile='F:\init3.ora' from spfile;
create spfile from pfile='F:\init3.ora';
startup pfile='D:\app\Administrator\product\12.1.0\dbhome_2\dbs\init2.ora';
###
alter system set spfile='+DG_ORA/${ORACLE_SID}/spfile${SID}.ora';
###
create spfile from pfile='D:\app\Administrator\product\12.1.0\dbhome_2\dbs\init2.ora';
shutdown immediate;
startup;
###查看当前数据库 SID
select instance_name from V$instance;
###重要配置文件 pfile(初始化参数文件,文本文件)、spfile(服务器参数文件,二进制文件,不可修改)和init.ora
###Oracle 9i以前,Oracle是经过pfile启动和初始化数据库。在9i以后,默认经过加载spfile来启动和初始化数据库。
###在SQL命令窗口,输入show parameter spfile,若是结果显示spfile的文件路径,即为经过spfile启动的,若是没有,就是pfile。
###init.ora: 是pfile文件的例子,可设置,可不设置