table of Contents
- 5 Oracle's basic query, filtering and sorting query
-
- 5.1 Solve the problem of Chinese garbled characters in SqlPlus
- 5.2 Oracle basic query statement
-
- spool save sql to file
- host cls clear screen
- show user shows the currently logged in user
- select * from tab; Display the table under the current user
- desc query table structure [desc emp;]
- show linesize show line width
- set linesize 150 set line width
- col ename for a8 Set the width of ename column, display 8 characters, a represents characters
- col sal for 9999 set sal column to display 4 numbers, 9 means number
- / Means to execute the previous statement
- c Change command, use when typing a wrong sentence [eg: c /form/from]
- * + Multiplication and addition operations
- Query the null value in the table
- alias of as column
- ed into the write cache file file afiedt.buf [convenient to modify long SQL statements]
- distinct 1. Remove duplicates 2. Act on all subsequent columns
- concat string concatenation
- dual represents a virtual table
- || also means string concatenation
- 5.3 Oracle's filtering and sorting query
5 Oracle's basic query, filtering and sorting query
5.1 Solve the problem of Chinese garbled characters in SqlPlus
- Previously used hr login, now use another login set
- Use scott account to log in [sqlplus scott/[email protected]:1521/orcl]
- Chinese garbled problems were found when creating aliases
first step
- Execute select userenv('language') from dual in sqlplus;
- View the character set of the current database: AMERICAN_AMERICA.ZHS16GBK
- We only need to set both the client character set and operating system character set to: SIMPLIFIED CHINESE_CHINA.ZHS16GBK.
Second step
- Find an environment variable named "NLS_LANG" in the system environment variables. If it is not found, create a new one and assign the variable value to: "SIMPLIFIED CHINESE_CHINA.ZHS16GBK"
- Close the dos terminal, rewrite and open (Chinese garbled problem solved)
5.2 Oracle basic query statement
spool save sql to file
- Save the sql statement to the file [spool C:\Users\shuyy\Desktop\a.txt]
- spool off write content to the file [to write the SQL that needs to be saved to the file between spool C:\Users\shuyy\Desktop\a.txt and spool off, after the spool off is closed, the SQL written again will not Write to specified file]
host cls clear screen
show user shows the currently logged in user
select * from tab; Display the table under the current user
- Unlike the MySQL database, Oracle does not display the database like MySQL, and then use
- The Oracle database will directly display the tables that can be seen by the current user permissions [the tables here are some of the tables provided by Oracle by default]
desc query table structure [desc emp;]
show linesize show line width
- Query the emp table and found that the display is not beautiful because of the row width problem
set linesize 150 set line width
- Much more beautiful
col ename for a8 Set the width of ename column, display 8 characters, a represents characters
col sal for 9999 set sal column to display 4 numbers, 9 means number
- 999 means that 3 numbers are displayed, and the data of 4 numbers is found to become ####
/ Means to execute the previous statement
c Change command, use when typing a wrong sentence [eg: c /form/from]
* + Multiplication and addition operations
#查询员工信息: 员工号 姓名 月薪
select empno "员工号", ename "名字",sal "月薪" from emp;
#查询员工信息: 员工号 姓名 月薪 年薪 奖金 年收入
select empno "员工号", ename "名字",sal "月薪", sal*12 "年薪",comm "奖金", sal*12+comm "年收入" from emp;
- [It is found that there is a problem with this query. The result of adding the annual salary and bonus data to null is null, obviously this does not meet the requirements]
#让comm里面的null变成0【nvl(comm,0)】
select empno "员工号", ename "名字",sal "月薪", sal*12 "年薪",comm "奖金", sal*12+nvl(comm,0) "年收入" from emp;
Query the null value in the table
- MySQL can be written like this [writing like this in Oracle will report an error]: select * from emp where comm = null;
- select * from emp where comm is null;
alias of as column
select empno as "员工号", ename "名字",sal "月薪", sal*12 "年薪",comm "奖金", sal*12+nvl(comm,0) "年收入" from emp;
ed into the write cache file file afiedt.buf [convenient to modify long SQL statements]
- After the modification is completed, save with Ctrl+s, then close the file, and enter / to indicate the execution of the last SQL statement
distinct 1. Remove duplicates 2. Act on all subsequent columns
#去除重复
select distinct deptno from emp;
#作用于后面所有的列
select distinct deptno, job from emp;
concat string concatenation
- dual represents a virtual table
select concat('abc','def') from dual;
dual represents a virtual table
select 3+2 from dual;
|| also means string concatenation
select 'abc'||'aaa' from dual;
select ename|| '的工资是'||sal from emp;
5.3 Oracle's filtering and sorting query
Character case sensitivity
#如查询员工为SMITH的信息
select * from emp where ename='SMITH';
select * from emp where ename='SMITh';
Date format is sensitive
#查看当前的日期格式
select * from v$nls_parameters;
#修改当前会话的日期格式【下一次打开还是原来的格式】
#java中是yyyy-MM-dd
alter session set NLS_DATE_FORMAT='yyyy-mm-dd';
#查询日期
select * from emp where hiredate='17-12月-80';
select * from emp where hiredate='1980-12-17';
between and
#查询薪水1000~2000之间的员工,结果是包含1000和2000的
select * from emp where sal between 1000 and 2000;
#注:含有边界 小值在前 大值在后
in is in the set, not in is not in the set
#查询10和20号部门的员工
select * from emp where deptno in(10,20);
#查询不在10和20号部门的员工
select * from emp where deptno not in(10,20);
#注:如果集合中含有null,不能使用not in; 但可以使用in
like fuzzy query [similar to mysql]
#查询名字以S打头的员工
select * from emp where ename like 'S%';
#查询名字是4个字的员工,4个下划线即可
select * from emp where ename like '____';
#查询名字中含有下划线的员工 需要转义字符
select * from emp where ename like '%\_%' escape '\';
rollback
- Oracle starts transactions automatically, unlike mysql, which needs to be started manually [start transaction;]
order by
#1.查询员工信息 按照月薪排序【默认升序】
select * from emp order by sal;
#2.按年薪降序排序【order by后面 + 列,表达式,别名,序号】
select empno,ename,sal,sal*12 from emp order by sal*12 desc;
#根据别名排序
select empno,ename,sal,sal*12 年薪 from emp order by 年薪 desc;
#根据列数排序
select empno,ename,sal,sal*12 年薪 from emp order by 9 desc;
#3.按部门升序然后按工资降序排序员工信息
select * from emp order by deptno,sal desc;
#4.按部门降序然后按工资降序排序员工信息
select * from emp order by deptno desc,sal desc;
#注:order by 作用于后面所有的列;desc只作用于离它最近的列
#6.查询员工信息 按照奖金降序排序,null值是最大,所以会排在最前面
select * from emp order by comm desc;
select * from emp order by comm desc nulls last;
set pagesize set page size
#设置分页大小
set pagesize 20;