Skip to content

Commit 5c34078

Browse files
committed
doc: docs/oracle.md (#617)
1 parent 4e21fd0 commit 5c34078

File tree

1 file changed

+99
-30
lines changed

1 file changed

+99
-30
lines changed

docs/oracle.md

Lines changed: 99 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@ Oracle 备忘清单
88
### SELECT 语句
99

1010
```sql
11-
SELECT * FROM beverages WHERE field1 = 'Kona' AND field2 = 'coffee' AND field3 = 122;
11+
SELECT *
12+
FROM beverages
13+
WHERE field1 = 'Kona'
14+
AND field2 = 'coffee'
15+
AND field3 = 122;
1216
```
1317
<!--rehype:className=wrap-text-->
1418

1519
### SELECT INTO 语句
1620

1721
```sql
18-
SELECT name,address,phone_number INTO v_employee_name,v_employee_address,v_employee_phone_number FROM employee WHERE employee_id = 6;
22+
SELECT name, address, phone_number
23+
INTO v_employee_name, v_employee_address, v_employee_phone_number
24+
FROM employee
25+
WHERE employee_id = 6;
1926
```
2027
<!--rehype:className=wrap-text-->
2128

@@ -25,24 +32,35 @@ SELECT name,address,phone_number INTO v_employee_name,v_employee_address,v_emplo
2532
使用 VALUES 关键字插入
2633

2734
```sql
28-
INSERT INTO table_name VALUES ('Value1', 'Value2', ... );
29-
INSERT INTO table_name(Column1, Column2, ... ) VALUES ( 'Value1', 'Value2', ... );
35+
INSERT INTO table_name
36+
VALUES ('Value1', 'Value2', ... );
37+
38+
INSERT INTO table_name (Column1, Column2, ... )
39+
VALUES ( 'Value1', 'Value2', ... );
3040
```
3141
<!--rehype:className=wrap-text-->
3242

3343
使用 SELECT 语句插入
3444

3545
```sql
36-
INSERT INTO table_name(SELECT Value1, Value2, ... from table_name );
37-
INSERT INTO table_name(Column1, Column2, ... ) ( SELECT Value1, Value2, ... from table_name );
46+
INSERT INTO table_name
47+
SELECT Value1, Value2, ...
48+
FROM table_name;
49+
50+
INSERT INTO table_name (Column1, Column2, ...)
51+
SELECT Value1, Value2, ...
52+
FROM table_name;
3853
```
3954
<!--rehype:className=wrap-text-->
4055

4156
### DELETE 语句
4257

4358
```sql
44-
DELETE FROM table_name WHERE some_column=some_value
45-
DELETE FROM customer WHERE sold = 0;
59+
DELETE FROM table_name
60+
WHERE some_column = some_value;
61+
62+
DELETE FROM customer
63+
WHERE sold = 0;
4664
```
4765

4866
### UPDATE 语句
@@ -384,7 +402,8 @@ CREATE TABLE table_name
384402
column1 datatype null/not null,
385403
column2 datatype null/not null,
386404
...
387-
CONSTRAINT constraint_name CHECK (column_name condition) [DISABLE]
405+
CONSTRAINT constraint_name
406+
CHECK (column_name condition) [DISABLE]
388407
);
389408
```
390409

@@ -410,7 +429,8 @@ CREATE TABLE table_name
410429
column1 datatype null/not null,
411430
column2 datatype null/not null,
412431
...
413-
CONSTRAINT constraint_name UNIQUE (column1, column2, column_n)
432+
CONSTRAINT constraint_name
433+
UNIQUE (column1, column2, column_n)
414434
);
415435
```
416436

@@ -421,7 +441,8 @@ CREATE TABLE customer
421441
(
422442
id integer not null,
423443
name varchar2(20),
424-
CONSTRAINT customer_id_constraint UNIQUE (id)
444+
CONSTRAINT customer_id_constraint
445+
UNIQUE (id)
425446
);
426447
```
427448

@@ -431,14 +452,18 @@ CREATE TABLE customer
431452

432453
```sql
433454
ALTER TABLE [table name]
434-
ADD CONSTRAINT [constraint name] UNIQUE( [column name] ) USING INDEX [index name];
455+
ADD CONSTRAINT [constraint name]
456+
UNIQUE([column name])
457+
USING INDEX [index name];
435458
```
436459

437460
例如:
438461

439462
```sql
440463
ALTER TABLE employee
441-
ADD CONSTRAINT uniqueEmployeeId UNIQUE(employeeId) USING INDEX ourcompanyIndx_tbs;
464+
ADD CONSTRAINT uniqueEmployeeId
465+
UNIQUE(employeeId)
466+
USING INDEX ourcompanyIndx_tbs;
442467
```
443468

444469
### 添加外部约束
@@ -447,14 +472,19 @@ foregin 约束的语法是:
447472

448473
```sql
449474
ALTER TABLE [table name]
450-
ADD CONSTRAINT [constraint name] FOREIGN KEY (column,...) REFERENCES table [(column,...)] [ON DELETE {CASCADE | SET NULL}]
475+
ADD CONSTRAINT [constraint name]
476+
FOREIGN KEY (column,...)
477+
REFERENCES table [(column,...)]
478+
[ON DELETE {CASCADE | SET NULL}];
451479
```
452480

453481
例如:
454482

455483
```sql
456484
ALTER TABLE employee
457-
ADD CONSTRAINT fk_departament FOREIGN KEY (departmentId) REFERENCES departments(Id);
485+
ADD CONSTRAINT fk_departament
486+
FOREIGN KEY (departmentId)
487+
REFERENCES departments(Id);
458488
```
459489

460490
### 删除约束
@@ -482,13 +512,17 @@ INDEXES
482512

483513
```sql
484514
CREATE [UNIQUE] INDEX index_name
485-
ON table_name (column1, column2, . column_n)
515+
ON table_name (
516+
column1,
517+
column2,
518+
.
519+
column_n
520+
)
486521
[ COMPUTE STATISTICS ];
487522
```
488523

489-
`UNIQUE` 表示索引列中值的组合必须是唯一的
490-
491-
`COMPUTE STATISTICS` 告诉 Oracle 在创建索引期间收集统计信息。 然后优化器使用这些统计信息来选择执行语句时的最佳执行计划。例如:
524+
- `UNIQUE` 表示索引列中值的组合必须是唯一的
525+
- `COMPUTE STATISTICS` 告诉 Oracle 在创建索引期间收集统计信息。然后优化器使用这些统计信息来选择执行语句时的最佳执行计划。例如:
492526

493527
```sql
494528
CREATE INDEX customer_idx
@@ -604,7 +638,8 @@ DBA 相关
604638
创建用户的语法是:
605639

606640
```sql
607-
CREATE USER username IDENTIFIED BY password;
641+
CREATE USER username
642+
IDENTIFIED BY password;
608643
```
609644

610645
例如:
@@ -642,53 +677,87 @@ ALTER USER brian IDENTIFIED BY brianpassword;
642677
```
643678

644679
### 查看表空间的名称以及大小
680+
<!--rehype:wrap-class=col-span-2-->
681+
645682
```sql
646-
select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size from dba_tablespaces t, dba_data_files d where t.tablespace_name = d.tablespace_name group by t.tablespace_name;
683+
SELECT t.table_name,
684+
ROUND(SUM(bytes / (1024 * 1024)), 0) AS ts_size
685+
FROM dba_tablespaces t,
686+
dba_data_files d
687+
WHERE t.table_name = d.table_name
688+
GROUP BY t.table_name;
647689
```
648690

649691
### 查看还没提交的事务
692+
650693
```sql
651694
select * from v$locked_object;
652695
select * from v$transaction;
653696
```
654697

655698
### 查看数据库库对象
699+
<!--rehype:wrap-class=col-span-2-->
700+
656701
```sql
657-
select owner, object_type, status, count(*) count# from all_objects group by owner, object_type, OJIB status;
702+
SELECT owner, object_type, status, COUNT(*) AS count#
703+
FROM all_objects
704+
GROUP BY owner, object_type, status;
658705
```
659706

660707
### 查看数据库的版本
708+
661709
```sql
662-
Select version FROM Product_component_version where SUBSTR(PRODUCT,1,6) = 'Oracle';
710+
SELECT version
711+
FROM Product_component_version
712+
WHERE SUBSTR(PRODUCT, 1, 6) = 'Oracle';
663713
```
664714

665715
### 查看数据库的创建日期和归档方式
716+
666717
```sql
667-
select created, Log_Mode, Log_Mode From v$Database;
718+
SELECT created, Log_Mode, Log_Mode
719+
FROM v$Database;
668720
```
669721

670722
### 查看控制文件
723+
671724
```sql
672725
select name from v$controlfile;
673726
```
674727

675728
### 查看日志文件
729+
676730
```sql
677731
select member from v$logfile;
678732
```
679733

680734
### 查看表空间的使用情況
735+
<!--rehype:wrap-class=col-span-2-->
736+
681737
```sql
682-
select sum (bytes)/(1024*1024) as free_space, tablespace_name from dba_free_space group by tablespace_name;
738+
SELECT SUM(bytes)/(1024*1024) AS free_space,
739+
tablespace_name
740+
FROM dba_free_space
741+
GROUP BY tablespace_name;
683742
```
684743

685744
### 捕捉运行很久的SOL
686-
```sql
687-
column username format a12
688-
column opname format a16
689-
column progress format a8
690745

691-
select username,sid,opname,round(sofar* 100 / totalwork,0) || '%' as progress,time_remaining,sqL_text from v$session_longops,v$sql where time_remaining <> 0 and sql_address = address and sql_hash_value = hash_value
746+
```sql
747+
COLUMN username FORMAT A12
748+
COLUMN opname FORMAT A16
749+
COLUMN progress FORMAT A8
750+
751+
SELECT username,
752+
sid,
753+
opname,
754+
ROUND(sofar * 100 / totalwork, 0) || '%' AS progress,
755+
time_remaining,
756+
sql_text
757+
FROM v$session_longops, v$sql
758+
WHERE time_remaining <> 0
759+
AND sql_address = address
760+
AND sql_hash_value = hash_value;
692761
```
693762

694763
另见

0 commit comments

Comments
 (0)