Home  >  Article  >  Database  >  Summarize the basic knowledge of using Oracle database

Summarize the basic knowledge of using Oracle database

WBOY
WBOYforward
2022-03-08 17:39:262397browse

This article brings you relevant knowledge about Oracle. It mainly summarizes and introduces the related issues of database use, including installation, uninstallation, table space, data type, etc. I hope it will be helpful to you. Everyone is helpful.

Summarize the basic knowledge of using Oracle database

Recommended tutorial: "Oracle Tutorial"

Uninstall

Execute deinstall. xml file for automatic uninstallation. Enter Enter or yes during the execution. Directories that cannot be deleted will be deleted manually after the uninstallation is completed.
Summarize the basic knowledge of using Oracle database

Introduction

System user

sys, system (Permissions sys>system>scott)

sys needs to have system administrator rights
system can log in directly
sysman is used to operate the enterprise manager, administrator level
scott Oracle founder name, the default password is tiger

Login

Use system user to log in
[username/password] [@server] [as sysdba|sysper]

Summarize the basic knowledge of using Oracle database
Note: If you log in to the local database, you don’t need @serverSummarize the basic knowledge of using Oracle database show user View the current logged in user

dba_users Data dictionary (desc dba_users)

Table space overview

Table space: the logical storage space of the database, a database contains multiple table spaces
  • Permanent table space: Table information, views, stored procedures and other files that need to be permanently stored
  • Temporary table space: In the middle of database operations During the execution process, the execution is completed and the
  • UNDO table space is released: it stores the data before the data is modified and can be rolled back
View the user table Space:

Administrator data dictionary: dba_tablespaces, dba_users
Ordinary user data dictionary: user_tablespaces, user_users
Summarize the basic knowledge of using Oracle database

Set the user's default or temporary table space
ALTER USER username DEFAULT|TEMPOPRRY TABLESPACE tablespace_name
Create a permanent table space
CREATE  TABLESPACE tablespace_name DATAFILE 'XX.dbf'  SIZE 10m
Create a temporary table space
CREATE TEMPORARY TABLESPACE tablespace_name TEMPFILE 'XX.dbf'  SIZE 10m
View the data file dictionary
desc dba_data_files

View the table space file storage pathSummarize the basic knowledge of using Oracle database

select file_name from dba_data_files where tablespace_name = 'tablespace_name';

Modify table space statusSet online or offline status:
ALTER TABLESPACE tablespace_name ONLINE|OFFLINE; Set read-only or read-write status :

ALTER TABLESPACE tablespace_name READ ONLY|READ WRITE;

Table space modification data fileAdd data file:
ALTER TABLESPACE tablespace_nama ADD DATAFILE 'xx.dbf' SIZE xx; Delete data files:

ALTER TABLESPACE tablespace_nama DROP DATAFILE 'xx.dbf';

(The first data file cannot be deleted unless the tablespace is deleted)

Delete table space

DROP  TABLESPACE tablespace_name [INCLUDING CONTENTS]

Data type

Character type



CHAR(n): MAX-2000
NCHAR(n): MAX-1000, unicode format, stores more Chinese characters

VARCHAR2(n): MAX-4000

NVARCHAR2(n): MAX-2000, unicode format

Numeric type


NUMBER(p, s): p-number of significant digits, s-number of decimal places to be retained

FLOAT(n): 1~126 bits of binary data (*0.30103 gets decimal data)

Date type


DATE: Accurate to seconds

TIMESTAMP: Accurate to milliseconds

Other types of large files


BLOB: 4G Binary

CLOB: 4G String

Manage Table

Create TableSummarize the basic knowledge of using Oracle database

Add Field
ALTER TABLE table_name add  column_name data_type;

Change field data type (when there is no data)

ALTER TABLE table_name MODIFY column_name data_type;

Delete field

ALTER TABLE table_name DROP COLUMN column_name;

Modify field name

ALTER TABLE table_name RENAME COLUMN column_name TO new_column_name;

Modify table name

RENAME table_name TO new_table_name;

Delete table

It is faster than delete and deletes all data without deleting the table structure.

TRUNCATE TABLE table_name;
###Delete table structure###
DROP TABLE table_name;

在创建时复制表

CREATE TABLE new_table AS SELECT column1,...|* FROM old_table;

在添加时复制表

INSERT INTO new_table [(column1,...)] SELECT column1,...|* FROM old_table;

约束 :定义规则和确保完整性

非空约束:数据不能是NULL值,如用户名、密码等(设置非空约束之前表中不能有空数据)
主键约束:唯一标识,不能为空,加快查询速度,自动创建索引。一张表只能设计一个,可以由多个字段构成(联合或复合主键)。
Summarize the basic knowledge of using Oracle database
Summarize the basic knowledge of using Oracle database
Summarize the basic knowledge of using Oracle database
Summarize the basic knowledge of using Oracle database
启用|禁用当前约束

DISABLE | ENABLE CONSTARINT constraint_name;

删除当前约束

ALTER TABLE table_name DROP CONSTRAINT constraint_name;
DROP PRIMARY KEY [CASCADE] ;     [CASCADE] :外键约束关系

外键约束:主表的字段必须是主键,主从表中响应的字段是同一个数据类型,从表外键字段值必须来自主表中相应字段值,或者为null值。

创建表时添加外键约束

CREATE TABLE table2 (column_name datatype REFERENCES table1(column_name));

Summarize the basic knowledge of using Oracle database

CONSTRAINT constraint_name FOREIGN KEY(column_name) REFERENCES table_name(column_name) [ON DELETE CASCADE]

Summarize the basic knowledge of using Oracle database
修改表时添加外键约束

ALTER TABLE table_name ADD CONSTRAINT constraint_name FOREIGN KEY(column_name) REFERENCES table_name(column_name) [ON DELETE CASCSDE] ;  
[ON DELETE CASCSDE]:级联删除

唯一约束:字段值不能重复
唯一约束和主键约束的区别
主键必须是非空,唯一约束允许有一个空值。主键在每张表中只能有一个,唯一约束在每张表中可以有多个。
Summarize the basic knowledge of using Oracle databaseSummarize the basic knowledge of using Oracle database

修改表时添加唯一约束

ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE(column_name);

检查约束:使表当中的值具有实际意义。
Summarize the basic knowledge of using Oracle database
Summarize the basic knowledge of using Oracle database

修改表时添加检查约束

ALTER TABLE table_name ADD CONSTRAINT constraint_name CHECK(column_name > 0);

查询

替换列的显示名称

Summarize the basic knowledge of using Oracle database

设置数据格式

更改字符长度(字符类型)
Summarize the basic knowledge of using Oracle database
== 数值类型格式(“9”代表一个数字)==

Summarize the basic knowledge of using Oracle database
Summarize the basic knowledge of using Oracle database
Summarize the basic knowledge of using Oracle database
== 清除设置的格式==

COLUMN column_name CLEAR;

函数

函数的作用

  • 方便数据统计
  • 处理查询结果

函数的分类Summarize the basic knowledge of using Oracle database数值函数

四舍五入: ROUND(n,[,m]) ; 省略m : m = 0 取整; m>0 : 保留小数点后m位;m 取整函数:CEIL(n) - 整数最大值
FLOOR(n) - 整数最小值
常用计算:ABS(n) - 绝对值
MOD(m,n) 取余数 m/n ,m和n有一个值为null,结果返回NULL
POWER(m,n) 返回m的n次幂,m和n有一个值为null,结果返回NULL
SORT(n) 平方根
三角函数:…

字符函数

大小写转换:UPPER(char)
LOWER(char)
INITCAP(char) : 首字母大写
获取子字符串:n可以省略,截取到最后;m
获取字符串长度:LENGTH(char)
字符串连接:CONCAT(char1,char2) 与 || 操作符作用一样
去除字串:TRIM(c2 FROM c1) 从c1当中去除c2字符串
LTRIM(c1 [, c2]) 从头部开始去除一个c2 ,c2 为空去除左边空格
LTRIM(c1 [, c2]) 从尾部开始去除一个c2,c2 为空去除右边空格
TRIM(c1) 去除空格
替换函数:REPLACE(char,s_string [,r_string]) r_string为空默认替换为空串

日期函数

系统时间: SYDATE 默认格式 DD-MON-YY
Summarize the basic knowledge of using Oracle database
ADD_MONTHS(date,i)
NEXT_DAY(date,char)
LAST_DAY(date)
MONTHS_BETWEEN(date1.date2) 两个日期之间间隔的月份,计算间隔多少天直接日期相减
EXTRACT(date FROM datetime)
Summarize the basic knowledge of using Oracle database
Summarize the basic knowledge of using Oracle database

转换函数

日期>>字符:TO_CHAR(date[,format[,params]]) date:将要转换的日期 ; format:转换的格式; params: 日期的语言,通常不写;
Summarize the basic knowledge of using Oracle database
字符>>日期:TO_DATE(date[,format[,params]]) --------只能输出默认日期格式
数字>>字符:TO_CHAR(number[,format])
9: 显示数字并忽略前面的0
0:显示数字,位数不足,用0补齐
. 或D 显示小数点
, 或G 显示千位符
$:美元符号
S:加正负号(前后都可以)
Summarize the basic knowledge of using Oracle database
字符>>数字:TO_NUMBER(char[,format])
Summarize the basic knowledge of using Oracle database

decode函数(都不满足返回null)

decode(column_name, value1,result1,... , defaultValue)

Summarize the basic knowledge of using Oracle database

你问我答:

联合索引比单索引的效率高么?
如果联合索引中的多个字段都在where谓词中出现了,则联合索引效率比单列索引高。因为通过多个条件可以从索引中过滤得到更少的记录条数,也就减少了需要回表扫描的次数,甚至可以直接在联合索引中得到所查的所有结果,则不再需要回表。
但是由于多列的联合索引肯定要比单列索引大,也就是说同样的索引需要存储的物理块要多于单列索引,所以,如果查询中只出现了联合索引中的某一列,则其效率不如单列索引。
前导列的作用?
前导列的概念是这样的,如果建立了f1,f2上的联合索引,则在查询时必须要用到f1,也就是所谓的前导列,该索引才会有效,因为索引是按照前导列排序的,如果where条件谓词中没有前导列,则需要执行索引扫描才能得到想要的结果,这种情况下其效率往往较差。
如果不需要前导列的话,reverse 这个反转又起到什么作用呢?
鉴于前面描述的前导列的概念,我们考虑如下表存储table(f1,f2);
aa 1
ab 2
ac 3
ad 4
ae 5
如果我们对表table建立f1上的普通索引,由于按照f1进行排序,所以针对where f1=ad则需要遍历所有的a开始的索引,而如果对f1建立reverse索引,则由于da只有一个,则可以更快的得到需要的结果。

推荐教程:《Oracle视频教程

The above is the detailed content of Summarize the basic knowledge of using Oracle database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete