环境变量设置

  1. SQLPLUS提示符改为SQL=

    sqlplus中输入命令:

1
set sqlprompt "SQL="
image-20220225142139201

​ 若要实现永久修改,需要在shell中输入命令

1
vim $ORACLE_HOME/sqlplus/admin/glogin.sql
·

​ 在其中加入

1
set sqlprompt "SQL="

​ 即可实现永久修改。

  1. 去掉输出结果的列头

​ 在sqlplus中输入

1
set heading off
image-20220225142617923

SQL语句练习

  1. 列出工资在 2000-3000 之间(包括临界值)并且没有提成的员工
1
select * from emp where SAL between 2000 and 3000 and COMM is NULL;
image-20220225143205349
  1. 列出和 JONES 同一部门的员工名字
1
select ENAME from emp where DEPTNO = (select DEPTNO from emp where ENAME = 'JONES');
image-20220225144126767
  1. 列出名字以 S 开头,名字长度为 5 个字符的员工
1
select * from emp where ENAME like 'S%' and LENGTH(ENAME)=5;
image-20220225145136237
  1. 列出在芝加哥工作的员工,并且按工资从高到低排列
1
select * from emp where (DEPTNO = (select DEPTNO from dept where LOC = 'CHICAGO')) ORDER BY SAL desc ;
image-20220225145955750
  1. 列出姓名以 J 开头,总收入超过 2500 的员工名字
1
select ENAME from emp where ENAME like 'J%' and ((SAL+COMM)>2500 or (SAL>2500));
image-20220225151231327

模拟数据导入SQL

​ 用自己熟悉的编程语言(包括并不限于 C++,R,Python 等等),产生一个包含 500 万行 数据的文本文件,每行由两个列组成,第一列是随机产生的长度为 10 字符的字符串,第二 列是随机产生的 8 位数字,两列中间用空格分开(如下)

abfgerjhgg 64758741

awqwyiuusa 10987622

模拟数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import pandas as pd
import random
import string
from tqdm import tqdm

num = 5000000

f = open("simuhw2.txt", 'w')

for i in tqdm(range(num)):
temp_num = ''.join(random.sample(string.digits, 8))
temp_char = ''.join(random.sample(string.ascii_lowercase, 10))
temp_simu = temp_char + " " + temp_num
f.writelines(temp_simu)
f.write('\n')

f.close()
  1. 建表

​ 名字为simuhw2,列名为name和id

1
CREATE TABLE simuhw2(name varchar2(10),id number(8));
  1. 构建文档
1
vi simuhw2.ctl
image-20220225224242587
  1. 导入文件
1
sqlldr userid=scott/tiger control=simuhw2.ctl log=simuhw2.log 
image-20220225173651225
  1. 查看数据
1
SELECT COUNT(*) FROM simuhw2;
image-20220225173915923