oracle database study notes (b)

The second chapter select query

1.select basic query syntax
to use at least two keywords:
1) is used to specify the SELECT query field, content
2) from the query is used to specify which tables
the basic syntax: select field from table;
three different kinds of forms:
1) a query tables in a specific field
select the field name from the table name;
for example:
query id for all employees?
select id from s_emp;
2) a query tables in multiple fields?
select field names 1, 2 field names, field names ...... 3
from table name;
Note: Be sure to use English symbols of symbols
such as:
query all employees id, last_name?
the above mentioned id the SELECT, last_name
from s_emp;
suggestion: one row for each key position.

Every select query results are the equivalent of a table.
3) a query tables in all fields
a) all the field names in the table list them all
b) use the wildcard * symbol

For example: query the department table all the fields?
ID SELECT, name, the region_id
from s_dept;

It is equivalent to:
the SELECT *
from s_dept;
Note: * only learn, practice, time tested use.
When the project development should all fields are listed.
Run Process:
A) directly define the field name queries
directly from the given field name query field values
b) using the * wildcard
to query the tables in which fields there?
And fetches a corresponding field value according to the result of the query.

In the real project development, sql command does not need every
hand to knock. We can directly bring some common sql commands
predefined in the program. Then each time after receiving the request,
direct the saved sql command to call it.
just write a sql command.
password from the Customer the SELECT
the WHERE username =;?

DISTINCT keyword:
Role: to eliminate duplicates.
Location: on the back of select keywords
such as:
query all the department name?
Select name
from s_dept;
what kinds of queries sector companies there?
DISTINCT name SELECT
from s_dept;

2. arithmetic
process a query, you can make arithmetic query results.
Not really change the data values in the table.

1) numeric type operations
include addition, subtraction
+ - * /
for example:
Query sum of employees' wages 12 months?
* 12 is the salary SELECT
from s_emp;

Exercise:
Each employee salary rose 200 yuan,
at the end there are 500 yuan year-end awards.
Year to employees 14 months salary.
Discover all employees of the total annual income?
SELECT (200 is the salary +) * 14 + 500
from s_emp;

2) date type operation
only addition and subtraction.
On behalf of the addition in the next few days from the specified date.
Subtraction represents the number of days from a specified date to the past.

Exercise:
Queries three days before the entry time of all employees?
START_DATE. 3-SELECT
from s_emp;

3. Processing null
query id for all employees, commission?
ID SELECT, a commission_pct
from s_emp;

Query id for all employees, salary + commission sum?
ID SELECT, the salary + a commission_pct
from s_emp;

Processing null values: nvl function
Syntax: nvl (null fields may occur, the default value)
NVL (a commission_pct, 0)

SELECT ID, the salary + NVL (a commission_pct, 0)
from s_emp;

4. string concatenation
query the full names of all employees?
Full name = name + name
first_name + last_name
the SELECT first_name, last_name
from s_emp;

FIRST_NAME || last_name SELECT
from s_emp;
string concatenation: Use symbol ||

Exercise:
Query full name of all employees
required between the first and last name "-" split?
FIRST_NAME || SELECT '-' || last_name
from s_emp;

the concat function
concat (string1, string2)

5. Results alias to
every select query results

Query 12 total monthly income of all employees?
Required to display the results in the field called Year_Sal?
ID SELECT, the salary * Year_Sal 12 is
from s_emp;

Four ways:
. 1) SELECT field aliases s_emp from;
2) s_emp SELECT field as aliases from;
. 3) SELECT field "Alias" from s_emp;
. 4) SELECT field as "alias" from s_emp;

The first and second no difference.
The difference between first and third: the
results of the default field names are all uppercase.
Double quotes will be added in accordance with the contents of the string is output.
SQL: the SELECT the above mentioned id, salary * 12 "Year_Sal"
from s_emp;

6. Set the session query result display format
SELECT FIRST_NAME, last_name
from s_emp;

column
may be abbreviated as col

Syntax: column / col field to be modified format format;
for example: the last_name display the results revised to 15 characters long?
A15 last_name the format column;
a: representative of the type of characters to be processed is a character type can handle only
15: to display the representative length

 

 

Guess you like

Origin www.cnblogs.com/DennySmith/p/12153535.html