Oracle Database basics: data query operation

SELECT uname FROM TUser WHERE uname=‘admin’

SELECT list of field names FROM table WHERE condition;

 

In an Oracle database, an object belonging to the pattern, a pattern corresponding to each account, the name of the user is the model name. In front of the table to add the name of the pattern, the pattern between tables and table names with "." Separated.

When we are in a different account to log database, to enter different modes, such as log on to the STUDENT account, entered STUDENT mode.

In the STUDENT table belong to query mode SCOTT mode, you need to write: SELECT * FROM SCOTT.EMP; but if the user logged-on user access model itself belongs to the table, you can omit the name of the table in front of the name patterns. SELECT * FROM emp.

 

In the query may have an arithmetic expression, it will form a new row, for displaying the result of the calculation, commonly referred to as a computed column. Expression can contain column names, arithmetic operators, and parentheses. Brackets are used to change the priority order of operation. Common arithmetic operators include: + *: addition operator. * -: subtraction operator. * *: Multiplication operator. * /: Division operator.

SELECT ename,sal,sal*(1+20/100) FROM emp;

 

We can play an alias for the list of its benefits is that you can change the display header. Especially for computing column, the column can be from a simple alias for it instead of calculating the expression shown in the header.

SELECT ename AS name, sal salary FROM emp;

 

If there are duplicate rows in the results display, you can use the DISTINCT keyword to eliminate duplicate display.

SELECT DISTINCT job FROM emp;

 

If you want to sort results appear while the query, you can use the following statement: SELECT field list FROM table WHERE condition ORDER BY field name 1 [ASC | DESC] [, field name 2 [ASC | DESC] ...];

Followed by the ORDER BY clause to sort the column. ORDER BY clause appears at the end of the SELECT statement. Sorting has ascending and descending points, ASC indicates ascending order, DESC indicates descending order. If not specified sort order, the default sort order is ascending. If you are in descending order, the DESC keyword must be written. If omitted ASC and DESC, the default is ASC, i.e., in ascending order

In descending order: the SELECT ename, the HireDate the FROM emp the HireDate the ORDER BY DESC ;

Sorting multiple columns: it can be sorted by a multi-row, first column press, and then press the second column, third column ....... SELECT ename, deptno, hiredate FROM emp ORDER BY deptno, hiredate;

If you want to calculate the sorting columns may alias the calculated column, and then sorted by the alias. T table_name SELECT, tablespace_name S from USER_TABLES Order by T

Priority operation is NOT, AND, OR. If you want to change the order of precedence, use parentheses.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The same connection there is a question: If an employee of the department also did not fill that blank, then the employee will not appear in the query; or a department has no employees, the department will not appear in the query. To solve this problem may be connected with the outer, i.e. in addition to the display of the records that meet the conditions the same connection, the connection line also shows that the condition is not satisfied, and does not satisfy the connection conditions shown in the last rows. Even outside the operator of (+), which can occur in the left or right side of the same connection conditions. Appears in the left or right different meaning herein and illustrated by the following example.

Display name of employees, wages and department name and where no sector employees. Execute the following query: SELECT ename, sal, dname FROM emp, dept WHERE emp.deptno (+) = dept.deptno; left outer join i.e. left outer connection: 

SELECT ename,sal,dname FROM emp left outer join dept on emp.deptno=dept.deptno;

最后是一个自连接的训练实例,自连接就是一个表,同本身进行连接。对于自连接可以想像存在两个相同的表(表和表的副本),可以通过不同的别名区别两个相同的表。

 

 

子查询一般出现在SELECT语句的WHERE子句中,Oracle也支持在FROM或HAVING子句中出现子查询。 子查询比主查询先执行,结果作为主查询的条件,在书写上要用圆括号扩起来,并放在比较运算符的右侧。 子查询可以嵌套使用,最里层的查询最先执行

例子:

SELECT ename,sal FROM emp WHERE sal<(SELECT sal FROM emp WHERE empno=7788) AND deptno=(SELECT deptno FROM emp WHERE empno=7788);

如果子查询返回多行的结果,则我们称它为多行子查询。多行子查询要使用不同的比较运算符号,它们是IN、ANY和ALL。

例子:

SELECT * FROM emp WHERE sal < all (SELECT sal FROM emp WHERE job = 'MANAGER');

==SELECT * FROM emp WHERE sal < (select min(sal) from (SELECT sal FROM emp WHERE job = 'MANAGER'));

如果子查询返回多列,则对应的比较条件中也应该出现多列,这种查询称为多列子查询。

例子:

SELECT * FROM emp WHERE (job,deptno) =(SELECT job,deptno FROM emp WHERE empno=7788);

同于上面语句: SELECT * FROM emp WHERE job =(SELECT job FROM emp WHERE empno=7788) and deptno =(SELECT deptno FROM emp WHERE empno=7788) ;

 

 

 

Guess you like

Origin www.cnblogs.com/kxykxykxy/p/11877209.html