Oracle's basic syntax

Oracle's basic syntax

A Select statement and filtering

(1) Grammar:

SELECT *|[DISTINCT] 字段列表 FROM 表名 [WHERE 筛选条件][ORDER BY ASC/DESC];

(2) Note that
case is not sensitive
② SQL can be written in one or more lines
③ Keywords cannot be abbreviated or divided into lines
④ Each clause should generally be written in separate lines
Use indentation to improve the readability of the statement
(3) Use numeric operator
numbers And date can use arithmetic operators
(4) Operator precedence
① The precedence of multiplication and division is higher than that of addition and subtraction
② Operators of the same precedence are executed from left to right
③ Operators in parentheses are executed first
Mathematical expressions with null values ​​are all
The alias of the empty value (5) column
is a renamed column, which is convenient for calculation. It can also be followed by the column name. You can also add ' AS ' between the column name and the alias . The alias uses double quotation marks to include spaces or Special characters and case sensitive

SELECT last_name AS name FROM employees;
或者
SELECT last_name "name" FROM employees;

(6) Connecting character
Use "||" to connect column to column, column to character.
For example, to connect name and job id:

SELECT last_name||job_id AS "Employees" FROM employees;

Insert picture description here
(7) Comparison operation
① The basic comparison operator
"== 、> ,>=,<,<=,<>is not equal to, :=assignment symbol"
②Other comparison operators
BETWEEN…AND,IN(),LIKE,IS NULL
③ Escape character To
avoid the use of escape character for special symbols, just add'\' before the special symbol and then ESCAPE''.
(8) Logic operation
① AND logical AND, all true is true
② OR logical OR, if one true is true
③ NOT logical negation, inverted
(9) Priority

算数运算符>连接符>比较符>IN(),LIKE,IS NULL>BETWEEN....AND>NOT>AND>OR

(10) Character
string A character string can be a character, number, or date in the SELECT list. The date and character can only appear in single quotation marks .
(11) Remove duplicate rows
Use the keyword " DISTINCT " in the SELECT clause to delete duplicate rows and records
(12) Display the table structure
Use the DESCRIBE command to indicate the table structure

DESC[RIBE] 表名;

Two functions

1 Single-line functions are
used to manipulate data objects, accept parameters and return a result, transform only one line, and return one result per line. The data type can be converted, and the parameter can be nested. The parameter can be a column or a value.
(1) Character function
① Case control functions
LOWER, UPPER, INITCAP
Insert picture description here
② Character control functions
CONCAT, SUBSTR, LENGTH, INSTR, LPAD, RPAD, TRIM, REPLACE
Insert picture description here
③ Number functions
ROUND, TRUNC, MOD
Insert picture description here
④ Date functions
are added to the date Or subtract a number and the result is still a date. Subtracting the two dates returns the number of days between the dates.
You can divide the number by 24 to add or subtract the number of days to the date.
Insert picture description here
Insert picture description here

Three-type conversion

1 Implicit data type conversion
Oracle automatically completes the following conversions.
Insert picture description here
2 Explicit conversion
(1) The date conversion of the TO_CHAR function
must be enclosed in single quotation marks and is case sensitive. It can include any valid date format. The dates are separated by commas. , Use double quotes to add characters to the date

TO_CHAR(date,'转换格式');
案例:
SELECT TO_CAHR(sysdate,'yyyy-mm-dd hh:mi:ss') FROM dual;

(2) Conversion of TO_DATA function to characters

TO_DATA(char,[,'转换格式']);
案例:
TO_DATA(20121029日’,'yyyy “年” mm "月" dd "日"') FROM dual;

(3) Conversion of TO_CHAR function to numbers.
The following are several formats often used in TO_CHAR function
Insert picture description here

TO_CHAR(number,'转换格式');
案例:
SELECT TO_CHAR(salary,'$99,999,00') SALARY FROM employee WHERE last_name = 'Ernst';

(4) TO_NUMBER function to convert characters

TO_NUMBER(char,'转换格式');
案例:
TO_NUMBER('¥1,234,567,890.00','L999,999,999,999.99') FROM dual;

Four general functions

1 NVL function
converts a null value into a known value
. Data types such as date, character and number can be used
NVL (expr1, expr2)
NVL (expr1, expr2, expr3) is similar to the ternary conditional operator
Insert picture description here
2 NULLIF function
NULLIF(expr1 ,expr2); Equal returns null, otherwise it returns the data value under the expr1 column.
Insert picture description here
3
Compared with NVL, the COALESCE function COALESCE can handle multiple alternating values ​​at the same time. If the first expression is empty, the next expression is returned. Other parameters are COALESCE.

Five-condition logic control

1 CASE expression
syntax:

CASE expr WHEN 条件1 THEN 语句一
[WHEN 条件2 THEN 语句一 
......
ELSE 语句]
END
结果作为一个字段

Case:
Insert picture description here
2 DECODE function
Syntax:

DECODE(字段|表达式,search1,result1,
			     [search1,result1,..],
			     [,default])

Case:
Insert picture description here

Guess you like

Origin blog.csdn.net/hcyxsh/article/details/114897634