Relational database foundation and application (2) - Oracle operation

Oracle String Operations

String manipulation

CHAR and VARCHAR2 types

Indicates the string data type , which is used to store string information in the table, such as name, occupation, address, etc.;

· CHAR stores fixed-length characters, that is, fills up spaces if it is not enough; VARCHAR2 stores variable-length characters, how much it takes up;

· Example: save the string "HELLOWORLD", 10 English letters;

  - CHAR(100): 10 letters, fill up 90 spaces, actually occupy 100; waste space and save time;

  - VARCHAR2(100): 10 letters, actually occupy 10; waste time and save space;

Sort by the natural order of characters ;

Storage encoding for CHAR and VARCHAR2

The default unit is bytes, which can be specified as characters;

 - CHAR(10), equivalent to CHAR(10 BYTE);

 - The specified unit is character: CHAR(10 CHAR), 20 bytes;

 - VARCHAR2(10), equivalent to VARCHAR2(10 BYTE);

 - The specified unit is character: VARCHAR2(10 CHAR), 20 bytes;

· Each English character occupies one byte, and each Chinese character occupies 2-4 bytes depending on the encoding;

 - ZHS16GBK: 2 bytes;

 - UTF-8: 2-4 bytes;

Maximum length of CHAR and VARCHAR2

The maximum value of CHAR is 2000 bytes ;

 - Save up to 2000 English characters, 1000 Chinese characters (GBK);

The maximum value of VARCHAR2 is 4000 bytes ;

 - Save up to 4000 English characters and 2000 Chinese characters (GBK);

CHAR can not specify the length , the default is 1, VARCHAR2 must specify the length;

LONG and CLOB types

LONG: VARCHAR2 extended version, storing variable - length strings, up to 2GB of string data;

LONG has many restrictions: each table can only have one LONG type column; it cannot be used as a primary key; it cannot be indexed; it cannot appear in query conditions;

CLOB: store fixed - length or variable-length strings, up to 4GB of string data;

· ORACLE recommends using CLOB instead of LONG type in development;

Common String Functions

LENGHT

LENGHT (CHAR): used to return the length of the string;

· If the string type is VARCHAR2, return the actual length of the character, if the character type is CHAR, the length also includes the space after it;

SELECT ename,LENGHT(ename) from emp;
UPPER, LOWER and INITCAP

· Case conversion function, used to convert the upper and lower case of characters;

UPPER(CHAR) is used to convert characters to uppercase;

LOWER(CHAR) is used to convert characters to lowercase;

· INITCAP(CHAR) is used to capitalize the first character of each word in the string, lowercase other characters, and separate words with spaces;

If the input parameter is a NULL value, the NULL value is still returned ;

SELECT UPPER('helloworld'),LOWER('helloworld'),INITCAP('helloworld') FROM DUAL;
TRIM、LTRIM、RTRIM

· Role: intercept substring

· Syntax form:

  - TRIM(c2 FROM c1): intercept c2 from the front and back of c1;

  - LTRIM(c2 FROM c1): intercept c2 from the left of c1;

  - RTRIM(c2 FROM c1): intercept c2 from the right (Right) of c1;

 If there is no c2, remove spaces;

TRIM is often used to remove spaces before and after strings;

LPAD、RPAD

The complement function is used to supplement the left or right end of the string char1 with char2 to n bits, and char2 can be repeated many times;

  - LPAD(char1,n,char2): left complement function;

  - RPAD(char1,n,char2): right complement function;

--Use left padding in the emp table, and fill 6 bits of sal with $
SELECT enmae,LPAD(sal,6,'$') as "salary" from emp;
SUBSTR

SUBSTR(char , m[,n]): used to get the substring of the string, and return n characters starting from m in the char;

· If m=0, start from the first character, if m is negative, start from the tail;

If n is not set, or the length of n exceeds the length of char, it will be taken to the end of the string;

SELECT SUBSTR('Doctor Who travels in TARDIS',8,25) FROM DUAL;

The first count of the string starts from 1 ;

Oracle Numerical Operations

Numeric type

NUMBER(P) represents an integer

· Complete syntax: NUMBER(precision,scale)

 - If scale is not set, the default value is 0, that is, NUMBER(P) represents an integer;

 - P represents the total number of digits, ranging from 1 to 38;

NUMBER(P,S) means floating point

· NUMBER(precision,scale)

 - precision: the maximum number length that NUMBER can store (excluding 0 on the left and right sides);

 - scale: maximum digit length to the right of the decimal point (including 0 to the left)

· If s is specified but p is not specified, the default is 38, such as: column name number(*,s);

Oracle Date Operations

date type

DATE(date)

The  most commonly used date type in ORACLE to save date and time;

The date range represented by  DATE can be from January 1, 4712 BC to December 31, 9999 AD;

The storage of  the DATE type in the database is fixed to 7 bytes , and the format is:

  - 1st byte: century + 100

  - 2nd byte: year

  - 3rd byte: month

  - 4th byte: day

  - 5th byte: hour +1

  - 6th byte: minutes + 1

  - 7th byte: seconds + 1

TIMESTAMP

· Date types commonly used by ORACLE;

The difference from DATE is that not only the date and time can be saved, but also fractional seconds can be saved, and the highest precision can be ns (nanoseconds);

The database is stored in 7 or 11 bytes internally, the precision is 0, and it is stored in 7 bytes, which is the same as the DATE function. If the precision is greater than 0, it is stored in 11 bytes;

·The format is:

 - 1st byte - 7th byte: same as DATE;

 - Bytes 8-11: nanoseconds, stored in 4 bytes, and the internal operation type is integer;

CREATE TABLR test(
    c1 DATE,
    c2 TIMESTAMP
);

date keyword

SYSDATE

Its essence is an internal function of Oracle, returning the current system time, accurate to seconds;

·The default display format is DD-MON-RR;

SYSTIMESTAMP

Internal function that returns the current system date and time, accurate to milliseconds;

Date Summons Function

TO_DATE

TO_DATE(char[ , fmt[,nlsparams]]): Convert the string to a date type in a custom format;

  - char: the string to convert;

  - fmt: format;

  - nlsparams: specify the date language;

The common date formats are as follows:


Date common functions

LAST_DAY

LAST_DAY(date): Returns the last day of the month in which the date date is located ;

Useful: It is useful when calculating some business logic according to the natural month, or arranging periodic activities at the end of the month;

SELECT LAST_DAY(SYSDATE)FROM DUAL;
ADD_MONTHS

ADD_MONTHS (date i): Returns the date after date plus i months;

 - The parameter i can be any number, most of the time it takes a positive integer;

 - If i is a decimal, it will be truncated to an integer before participating in the operation;

 - If i is a negative number, the date value after subtracting i months is obtained;

-- 20th Anniversary of Computing Resource Onboarding
SELECT ename,ADD_MONTHS(hiredate,20*12)as '20周年' from emp;
MONTH_BETWEEN

· MONTH_BETWEEN(date1, date2): Calculate the number of months between date1 and date2;

The actual operation is date1-date2, if date2 is later than date1, it will get a negative value;

· Unless the interval between two dates is a whole number of months, you will get a result with decimal places, such as calculating the number of months between September 1, 2009 and October 10, 2009, you will get 1.29 months

NEXT_DAY

· NEXT_DATE(date,char): Returns the next day of the week of date data, the day of the week is determined by the parameter char;

· In the Chinese environment, use the form of "Wednesday" directly. In the English environment, you need to use "WEDNESDAY" which is the day of the week in Chinese and English; in order to avoid trouble, you can directly use the numbers 1-7 to indicate Sunday-Saturday

· NEXT_DAY is not tomorrow;

LEAST(least)、GREATEST(greatest)

· GREATEST(expr1[,expr2[,expr3]]...)

· LEAST(expr1[,expr2[,expr3]]...)

Also known as a comparison function , it can have multiple parameters, and the return result is the largest or smallest value in the parameter list;

The parameter types must be the same;

· Before the comparison, the second and subsequent parameters in the parameter list will be implicitly converted to the data type of the first parameter, so if it can be converted, continue the comparison, if it cannot be converted, an error will be reported;

EXTRACT(extract)

· EXTRACT(date FROM datetime): Extract the data specified by the parameter date from the parameter datetime, such as extracting the year, month, and day

Null operation

NULL meaning

Important concepts in the database: NULL, that is, empty value;

Sometimes some field values ​​in the table, the data is unknown or temporarily does not exist, the value is NULL ;

· Any data type can take the value NULL;

NULL operation

insert NULL
CREATE TABLE student(id NUMBER(4),NAME CHAR(20),gender CHAR(1))
-- insert normally
INSERT INTO student VALUES(1000,'Zhang San','F');
--Display insert NULL value
INSERT INTO student VALUES(1001,'Li Si',NULL);
-- Implicitly insert NULL values
INSERT INTO student(id,name) VALUES(1001,'王五');
Update to NULL value
UPDATE student SET gender = NULL;

· Note that this update can only be performed if the column has no not-null constraint;

If a column has a non - null constraint, it cannot be updated to a NULL value, and the above statement will report an error;

NULL condition query

NULL is not equal to any value

-- Note that IS is not an = sign
SELECT * FROM student WHERE gender IS NULL;
Not Null Constraint

The NOT NULL constraint is used to confirm that the field value is not empty;

· By default, any column is allowed to have null values, but the business logic of the system may require that some columns cannot have null values;

· When a field is set with a non-null constraint, there must be a valid value in this field. That is: when performing an operation of inserting data, the data of this column must be provided, and when performing an update operation, this column cannot be set to NULL;

Null function

NVL

NVL(expr1 , expr2): Convert NULL to non-NULL value

 - If expr1 is NULL, the value expr2 is taken, and expr2 is the actual value;

 - expr1 and expr2 can be any data type, but the data types of the two parameters must be the same;

NVL2

· NVL2 (expr1, expr2, expr3): and NVL functions, both convert NULL into actual values;

· NVL2 is used to judge whether expr1 is NULL, if not, return expr2, if it is NULL, return expr3; similar to ternary operation;

Guess you like

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