Oracle basic statements and functions

1.. Handling NULL
NVL function can convert NULL into other types of symbols
Programming skills: NVL function is useful in multi-condition fuzzy query
NVL function can return multiple data types:
return date NVL(start_date,'2002-02- 01')
returns the string NVL(title,'no title')
returns the number NVL(salary,1000)
2. Sort by the specified rules
SELECT expr FROM table [ORDER BY {column, expr} [ASC | DESC] ];
default The sorting is ASC ascending (from small to large)
and you can also ORDER BY the position of the field name [1]| [2] ASC| DESC;
3. LIKE operation
% zero to any number of characters _ one character
For example : field name like ' M%' field name like '%m%' field name like 'job_'
If you want to find characters with underscores, you need to add backslashes For example: field name like '%X/_Y%' escape '/'
4. Date field
Example :
Date field between to_date('2001-12-12','YYYY-MM-DD') and to_date('2002-02-01','YYYY-MM-DD')
date field> to_date('2001-12-12','YYYY-MM-DD') and date field<=
to_date('2002-02-01','YYYY-MM-DD');
5. The digital function
ABS takes the absolute value POWER The power LN 10 is the base and takes 0
SQRT The square root EXP e nth power LOG(m,n ) m is the base n takes 0
Mathematical operation function: ACOS ATAN ATAN2 COS COSH SIGN SIN SINH TAN TANH
CEIL greater than or equal to take an integer
FLOOR less than or equal to take an integer
MOD take the remainder
ROUND(n,m) rounded according to the number of digits of m If round(date): After 12 noon it will be tomorrow's date.    
round(sysdate,'Y') is the first day of the year
TRUNC(n,m) Take the value after the decimal point by m digits if trunc(date) , The saving is to remove the time
. 6. The character function
CHR returns the characters from numbers according to the character set of the database.
CONCAT(c1,c2) ​​Combines two characters c1,c2 into one character, which is the same as ||
REPLACE(c,s,r ) Replace the character with s in character c with r, and return a new character
SUBSTR(c,m,n) m is greater than 0, character c takes n characters from the previous m, m is equal to 0 and 1 is the same,
m is smaller than 0, the character c takes n characters from the back m
TRANSLATE(c,f1,t1) The character c is converted into a new string according to the rules from f1 to t1
INITCAP The first letter of the character is uppercase, and other characters are lowercase
LOWER Characters are all lowercase
UPPER All uppercase characters
LTRIM(c1,c2) ​​Remove the character c2 appearing to the left of the character c1
RTRIM(c1,c2)
​​TRIM(c1,c2) ​​Remove the character c2 on the left and right sides of the character
c1 LPAD(c1,n,c2) The character c1 is as specified The number of digits n shows that the number of digits is insufficient. Replace the left space with the c2 string.
RPAD(c1,n,c2)
7. Date function
ADD_MONTHS(d,n) Date value plus n months
LAST_DAY returns the date of the last day of the month
MONTHS_BETWEEN( d1,d2) The month between two date values, d1<d2 returns a negative number
NEXT_DAY returns the date of the next day of the date value
SYSDATE The current system time
DUAL is the next empty table for the SYS user, it has only one field dummy
8. Conversion function (1 )
TO_CHAR(date,'date display format')
TO_CHAR(number) format alignment for display or report
TO_DATE(char,'date display format')
TO_LOB convert long field to lob field
TO_NUMBER(char) for calculation or comparison Size
9. Conversion function (2)
The date display format in to_date is
YYYY year YEAR YYY YY Y
Q quarter
MM month MONTH MON
W Week (week of month) WW, IW (week of year)
(Note: Week count is based on ISO standards, from the week number on January 1 to the next seven days as a week, not necessarily from Monday to Sunday)
DD Day DAY DY
HH24 hours HH12 HH
MI minutes
SS seconds
If you      want to fix the display format of the date, you can write a new line parameter
NLS_DATE_FORMAT=yyyy-mm-dd
hh24:mi:ss in the initorasid.ora parameter file of the database, which can be in the UNIX environment variable or NT The setting in the registry of NLS_DATE_FORMAT=yyyy-mm-dd hh24:mi:ss
10. Input character, return number function
instr(c1,c2) ​​Character c2 appears in the position of c1, does not appear, returns 0, often used for fuzzy Query
length Calculate the length of character c according to the character set of the database, which is related to the character set of the database. The length of a Chinese character is 1.
6. Function with logical comparison NVL(EXPR1, EXPR2) function
Explanation : IF EXPR1=NULL RETURN EXPR2
ELSE RETURN EXPR1
DECODE(AA0V10R10V20R2....) function
Explanation : IF AA=V1 THEN RETURN R1
IF AA=V2 THEN RETURN R2
..…
ELSE
RETURN NULL
Example: decode(id,1,'dept sale',2,'dept tech')

11. Connection between data tables
Simple connection syntax:
SELECT field name 1, field name 2, ... FROM table name 1, [table name2, ……]
WHERE table name 1. field name = table name 2. field name [ AND ……] ;
SELECT field name 1, field name 2, …… FROM table name 1, [table name 2, ……]
WHERE table name 1. field name = table name 2. field name (+) [ AND ...] ;
the field position with (+) sign automatically fills in the empty value
Connection classification:
equal connection =
not equal connection!= BETWEEN …AND … IN Note that IN and OR cannot be used together. The
outer join has a field name (+), and the empty value is filled if the conditions are not met.
Self-joining the same table and self-joining with themselves, such as finding duplicate records
12. Example of joining between data tables
Delete Records with duplicate field name email in table_name table:
SQL>delete from table_name t1
where t1.rowid >
(select min(rowid) from table_name t2
where t1.email = t2.email
group by email
having count(email) > 1);
Find the service area of ​​mobile phone users:
SQL> select a.handphoneno,nvl(c.name,'null'),a.totalscore
from topscore a,chargeoperator cc,chargeoperatorinfo c
where substr(a.handphoneno,1,7)=cc .hpnohead(+)
and cc.chargetype=c.chargetype(+)
order by a.totalscore desc;
Aggregate functions
are often used with group by


2. Aggregate function list
AVG (DISTINCT | ALL | N) Average
COUNT (DISTINCT | ALL | N | expr | * ) Number of statistics
MAX (DISTINCT | ALL | N) Maximum value
MIN (DISTINCT | ALL | N) Minimum value
SUM (DISTINCT | ALL | N) Total value
STDDEV (DISTINCT | ALL | N) Take the deviation value, if the selected content in the group is the same, the result is 0
VARIANCE (DISTINCT | ALL | N) Take the square deviation value

Use the syntax of the aggregate function
SELECT column, group_function FROM table
WHERE condition GROUP BY group_by_expression
HAVING group_condition ORDER BY column;


3. Precautions when using count
SELECT COUNT(*) FROM table;
SELECT COUNT(constant) FROM table;
both count the number of records in the table, if there is no PK, the latter is better
SELECT COUNT(all field name) FROM table;
SELECT COUNT(field name) FROM table;
will not count the number of NULL fields
SUM, AVG will ignore NULL fields


4. Restrictions when using group by
SELECT field name It cannot be arbitrarily included in the fields of
GROUP BY. When ORDER BY after GROUP BY, position symbols and aliases cannot be used to
limit the display results of GROUP BY. Use HAVING condition


5. Example
SQL> select title,sum(salary) payroll from s_emp
where title like 'VP%' group by title
having sum(salary)>5000 order by sum(salary) desc;
find out the number of duplicate records in a field in a table, and display
SQL> select (duplicate field names) from table_name
group by (list out fields) having count(*)>1;

6. Take out 80 to 100 SQL statements of the
result set ORACLE processes each result set with only one ROWNUM field to indicate its Logical position,
and only ROWNUM<100 can be used, ROWNUM>80 cannot be used. The following are the two SQL statements (ID is the field name of the unique keyword)
with the result set between 80 and 100 obtained by ORACLE after analysis . SQL>select * from ( ( select rownum as numrow, c.* from ( select [field_name,...] from table_name where condition 1 order by condition 2) c) where numrow > 80 and numrow <= 100 ) order by condition 3; 7. Example of bind variable SQL statement (1) SQL> select id, last_name, salary from s_emp where dept_id = &department_number; Enter value for department_number: 10










old 1: select id, last_name, salary from s_emp where dept_id=&department_number;
new 1: select id, last_name, salary from s_emp where dept_id= 10
SQL> SET VERIFY OFF | ON; can close and open the prompt confirmation message old 1 and new Display of 1.

select id, last_name, salary
from s_emp
where title = '&job_title';
Enter value for job_title: Stock Clerk
SQL> select id, last_name, salary
from s_emp
where hiredate >to_date( '&start_hire_date','YYYY-MM- DD');
Enter value for start_hire_date : 2001-01-01
When binding string and date type variables, single quotation marks should be added outside the variables. You can
also bind variables to query different field names
. Do not add them when entering variable values; and other symbols
8.DEFINE syntax and examples
SQL> DEFINE variable = value
Description: variable refers to the variable name value refers to the variable value
After the variable value is defined, the SQL statement that binds the variable will not prompt for the input variable. Example
using DEFINE:
SQL> DEFINE dname = sales
SQL> DEFINE dname
DEFINE dname = “sales” (CHAR)
SQL> select name from dept where lower(name)='&dname';
NAME
-------------------------
sales
sales
SQL> UNDEFINE dname
SQL> DEFINE dname
Symbol dname is UNDEFINED


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326409423&siteId=291194637