from Oracle11gBase9787302458227.chapter04 import OptOracle

# 连接 Oracle 数据库
con_scott = cx.connect('scott', 'tiger', '127.0.0.1:1521/orcl')

# exp4_47:
# 【例 4.47】 在 SCOTT 模式下,检索 emp 表中的所有数据,并按照部门编号(deptno)、员工编号(empno)排序
tableHead4_47 = ['deptno', 'empno', 'ename']
sqlCase4_47 = "select deptno, empno, ename from emp order by deptno, empno"

# exp4_48:
# 【例 4.48】 查询 emp 表中员工的年工资,并按照年工资降序排列
tableHead4_48 = ['empno', 'ename', 'Annual Salary']
sqlCase4_48 = "select empno, ename, sal*12 \"Annual Salary\" " \
              "from emp " \
              "order by 3 desc"

# exp4_49:
# 【例 4.49】 使用非选择列表列进行排序的方法,按工资降序显示员工名
tableHead4_49 = ['ename']
sqlCase4_49 = "select ename from emp order by sal desc"

# exp4_50:
# 【例 4.50】 查询 emp 表,按照部门号升序工资降序显示雇员名、部门号和工资
tableHead4_50 = ['ename', 'deptno']
sqlCase4_50 = "select ename, deptno, sal " \
              "from emp " \
              "order by deptno, sal desc"

OptOracle.printData(con_scott, tableHead4_50, sqlCase4_50)  # 执行 exp4_50
""" 章节4.4.2 数字类函数 """

import cx_Oracle as cx
from Oracle11gBase9787302458227.chapter04 import OptOracle

# 连接 Oracle 数据库
con_scott = cx.connect('scott', 'tiger', '127.0.0.1:1521/orcl')  # SCOTT 模式

# exp4_69:
# 【例 4.69】 使用 cell() 函数返回3个指定小数的整数值
tableHead4_69 = ['ceil(7.3)', 'ceil(7)', 'ceil(-7.3)']
sqlCase4_69 = "select ceil(7.3), ceil(7), ceil(-7.3) from dual"

# exp4_70:
# 【例 4.70】 使用 round() 函数返回 PI 为两位小数的值
tableHead4_70 = ['round(3.1415926, 2)']
sqlCase4_70 = "select round(3.1415926, 2) from dual"

# exp4_71:
# 【例 4.71】 使用 power() 函数计算2的3次方的值
tableHead4_71 = ['power(2, 3)']
sqlCase4_71 = "select power(2, 3) from dual"

OptOracle.printData(con_scott, tableHead4_71, sqlCase4_71)  # 执行 exp4_71
# exp4_65:
# 【例 4.65】 在 HR 模式下,在 employees 表中检索雇员名称以字母 “a” 开头的员工信息,并将 first_name 字段的值转换为小写,
# 将 last_name 字段的值转换为大写
tableHead4_65 = ['EMPLOYEE_ID', 'FIRST_NAME', 'LAST_NAME']
sqlCase4_65 = "select EMPLOYEE_ID, lower(FIRST_NAME), upper(LAST_NAME) " \
              "from employees " \
              "where lower(FIRST_NAME) like 'a%'"

# exp4_66:
# 【例 4.66】 使用 LTRIM、LTRIM 和 TRIM 函数分别去掉字符串 “####East####”、“East    ” 和 “####East###”
# 中左侧 “#”、右侧 空格 和左右两侧的 ”#“
tableHead4_66 = [
    'ltrim(\'####East####\', \'#\')', 'rtrim(\'East    \')',
    'trim(\'#\' from \'####East###\')'
]
sqlCase4_66 = "select ltrim('####East####', '#'), rtrim('East    '), trim('#' from '####East###') from dual"

# exp4_67:
# 【例 4.67】 使用 replace() 函数吧字符串 ”Bad Luck Bad Gril“ 中的 ”Bad“ 字符串用 ”Good“ 替换掉
tableHead4_67 = ['replace(\'Bad Luck Bad Gril\', \'Bad\', \'Good\')']
sqlCase4_67 = "select replace('Bad Luck Bad Gril', 'Bad', 'Good') from dual"

# exp4_68:
# 【例 4.68】 使用 substr() 函数在字符串 ”MessageBox“ 中从第8个位置截取长度为3的子字符串
tableHead4_68 = ['substr(\'MessageBox\', 8, 3)']
sqlCase4_68 = "select substr('MessageBox', 8, 3) from dual"

OptOracle.printData(con_hr, tableHead4_65, sqlCase4_65)  # 执行 exp4_65
OptOracle.printData(con_scott, tableHead4_68, sqlCase4_68)  # 执行 exp4_68
Ejemplo n.º 4
0
""" 章节4.4.5 聚合类函数 """

import cx_Oracle as cx
from Oracle11gBase9787302458227.chapter04 import OptOracle

# 连接 Oracle 数据库
con_scott = cx.connect('scott', 'tiger', '127.0.0.1:1521/orcl')  # SCOTT 模式

# exp4_76:
# 【例 4.76】 在 SCOTT 模式下,使用 count() 函数计算员工总数,使用 avg() 函数计算平均工资
tableHead4_76 = ['员工总数', '平均工资']
sqlCase4_76 = "select count(EMPNO) as 员工总数, round(avg(sal), 2) as 平均工资 from EMP"

OptOracle.printData(con_scott, tableHead4_76, sqlCase4_76)  # 执行 exp4_76
Ejemplo n.º 5
0
""" 章节4.5.4 关联子查询 """

import cx_Oracle as cx
from Oracle11gBase9787302458227.chapter04 import OptOracle

# 连接 Oracle 数据库
con_scott = cx.connect('scott', 'tiger', '127.0.0.1:1521/orcl')  # SCOTT 模式

# exp4_82:
# 【例 4.82】 在 emp 表中,使用 “关联子查询” 检索工资大于同职位的平均工资的员工信息
tableHead4_82 = ['EMPNO', 'ENAME', 'SAL']
sqlCase4_82 = "select EMPNO, ENAME, SAL " \
              "from EMP e " \
              "where SAL > (select avg(SAL) from EMP where JOB = e.JOB) " \
              "order by JOB"

OptOracle.printData(con_scott, tableHead4_82, sqlCase4_82)  # 执行 exp4_82
""" 章节4.5.1 什么是子查询 """

import cx_Oracle as cx
from Oracle11gBase9787302458227.chapter04 import OptOracle

# 连接 Oracle 数据库
con_scott = cx.connect('scott', 'tiger', '127.0.0.1:1521/orcl')  # SCOTT 模式

# exp4_77:
# 【例 4.77】 在 SCOTT 模式下,使用 emp 表中查询部门名称(dname)为 “RESEARCH” 的员工信息
tableHead4_77 = ['empno', 'ename', 'job']
sqlCase4_77 = "select empno, ename, job from EMP " \
              "where deptno=(select deptno from DEPT " \
              "where dname='RESEARCH')"

OptOracle.printData(con_scott, tableHead4_77, sqlCase4_77)  # 执行 exp4_77
""" 章节4.5.2 单行子查询 """

import cx_Oracle as cx
from Oracle11gBase9787302458227.chapter04 import OptOracle

# 连接 Oracle 数据库
con_scott = cx.connect('scott', 'tiger', '127.0.0.1:1521/orcl')  # SCOTT 模式

# exp4_78:
# 【例 4.78】 在 emp 表中,查询出既不是最高工资,也不是最低工资的员工信息
tableHead4_78 = ['EMPNO', 'ENAME', 'SAL']
sqlCase4_78 = "select EMPNO, ENAME, SAL from EMP " \
              "where SAL > (select min(SAL) from EMP) " \
              "and SAL < (select max(SAL) from EMP)"

OptOracle.printData(con_scott, tableHead4_78, sqlCase4_78)  # 执行 exp4_78
Ejemplo n.º 8
0
""" 章节4.4.3 时间日期类函数 """

import cx_Oracle as cx
from Oracle11gBase9787302458227.chapter04 import OptOracle

# 连接 Oracle 数据库
con_scott = cx.connect('scott', 'tiger', '127.0.0.1:1521/orcl')  # SCOTT 模式

# exp4_72:
# 【例 4.72】 使用 sysdate() 函数返回系统当前的日期
tableHead4_72 = ['系统时间']
sqlCase4_72 = "select sysdate as 系统时间 from dual"

# exp4_73:
# 【例 4.73】 使用 ADD_MONTHS 函数在当前的日期下加上6个月,并显示其值
tableHead4_73 = ['add_months(sysdate, 6)']
sqlCase4_73 = "select add_months(sysdate, 6) from dual"

OptOracle.printData(con_scott, tableHead4_73, sqlCase4_73)  # 执行 exp4_73
""" 章节4.4.4 转换类函数 """

import cx_Oracle as cx
from Oracle11gBase9787302458227.chapter04 import OptOracle

# 连接 Oracle 数据库
con_scott = cx.connect('scott', 'tiger', '127.0.0.1:1521/orcl')  # SCOTT 模式

# exp4_74:
# 【例 4.74】 使用 to_char() 函数转换系统日期为 “YYYY-MM-DD” 格式
tableHead4_74 = ['默认格式日期', '转换后日期']
sqlCase4_74 = "select sysdate as 默认格式日期, to_char(sysdate, 'YYYY-MM-DD, hh24:mi:ss') as 转换后日期 from dual"

# exp4_75:
# 【例 4.75】 使用 to_number() 函数把十六进制数 “18f” 转换为十进制数
tableHead4_75 = ['to_number(\'18f\', \'xxx\')']
sqlCase4_75 = "select to_number('18f', 'xxx') as 十进制数 from dual"

OptOracle.printData(con_scott, tableHead4_75, sqlCase4_75)  # 执行 exp4_75