Database programming (constants and variables + commonly used system functions)

1. Constants and Variables

1. Constant

  1. String constant : A  string constant refers to a sequence of characters enclosed in single or double quotes. Single quotes are recommended in MySQL
  2. Numeric constants : Numeric constants can be divided into integer constants and decimal constants.
  3. Date and time constants : Date and time constants are represented by character date values ​​in a specific format, enclosed in single quotes.
  4. Boolean constants : Boolean values ​​have only two values, true and false. The SQL command execution result uses 1 to represent true and 0 to represent false.
  5. NULL value : Applicable to various field types, usually means "uncertain value", the operation of NULL value participation, the result is still NULL value.

[Example 3-1] Employee information with the ename value of SCOTT in the query table emp. 

SELECT  *  FROM  emp WHERE  ename='SCOTT';

[Example 3-2] Change the comm value of SCOTT employees in the table emp to 1250 (required to use scientific notation).

UPDATE emp  SET  COMM=1.25E+3 WHERE ename='SCOTT';

[Example 3-3] The ename and hiredate information of employees hired after 1981 in the query table emp.

SELECT ename,hiredate  FROM  emp WHERE  hiredate>'1981/12/31';

[Example 3-4] Query whether the name ename and salary sal of all employees in the table emp is greater than or equal to 2000.

SELECT  ename,sal>2000  FROM  emp;

2. Variables

   1. Local Variables

      (1) Definition and assignment of local variables

          SET @local variable name = expression 1

           [, @ Local variable name = expression 2, ...]

      (2) Display of local variables

           SELECT @local variable name [, @ local variable name, ……]

[Example 3-7] The job and hired values ​​of the employee 'SMITH' in the query table emp are assigned to the variables job_v and hired_v, and the values ​​of the two variables are displayed

SELECT  job,hiredate  INTO  @job_v,@hiredate_v

FROM  emp  WHERE  ename='SMITH';

#查看变量
SELECT  @job_v,@hiredate_v;

[Example 3-8] Query the information of the specified employee based on the value given by the name variable.

#定义变量并赋值

SET  @name='SCOTT';

#变量@name的调用

SELECT  *  FROM  emp  WHERE  ename=@name;

     2. Global Variables

           Global variables are variables provided and assigned by the MySQL system. Users cannot define global variables, they can only use them.

 

Second, commonly used system functions

1. String functions

    1) Function for calculating the number of characters in a string and function for the length of a string

          CHAR_LENGTH (str) returns the number of characters contained in the string str.

          The return value of LENGTH (str) is the length in bytes of the string. A Chinese character is 3 bytes, and a number or letter is 1 byte.

SELECT  CHAR_LENGTH('中国') 字符数,LENGTH('中国') 字符串长度;

result:

     2) Merge string function

           CONCAT (s1, s2, ...) returns the result is the string generated by the connection parameters, if any one parameter is NULL, the return value is NULL.

[Example 3-11] Examples.

SELECT CONCAT('MySQL版本:',@@version)  版本号;

result:

     3) String case conversion function

          LOWER (str) is to convert all alphabetic characters in the string str to lowercase letters.

          UPPER (str) is to convert all alphabetic characters in the string str to uppercase letters.

SET @name='alice';
SELECT * FROM emp WHERE UPPER(ename)=UPPER(@name);

result:

     4) Delete space function

          LTRIM (str) returns the string str with leading spaces removed;

          RTRIM (str) returns the string str with trailing spaces removed;

          TRIM (str) returns the string str that deletes spaces on both sides.

SET @name='  alice  ';
SELECT  *  FROM  emp  WHERE  UPPER(ename)=TRIM(UPPER(@name));

result:

     5) Take substring function

          SUBSTRING (str, start, length) returns a substring of length str from start to length.

  [Example 3-14] Return the employee information whose ename value starts with 'A' in emp.

#第一种写法
SELECT * FROM emp WHERE SUBSTRING(ename,1,1)='A';
#第二种写法
SELECT * FROM emp  WHERE ename LIKE 'A%';

result:

2. Mathematical functions

      ABS (x) returns the absolute value of x.

      PI () returns the value of pi.

      SQRT () returns the square root of a non-negative number.

      MOD (m, n) returns the remainder after m is divided by n.

      ROUND (x, y) returns x rounded to the precision specified by y. If y is negative, the value of x will be retained to the y digit to the left of the decimal point.

 

3. Date and time functions

      1) Get the current system date and the date, month, and day functions

           CURDATE () returns the current system date in the format 'YYYY-MM-DD'.

           YEAR (d), MONTH (d), and DAY (d) return the year, month, and day values ​​of the date or date and time d, respectively.

SELECT  CURDATE() 当前日期,YEAR(CURDATE()) 年,MONTH(CURDATE()月,DAY(CURDATE()) 日;

result:

       2) Get the current system date and time function

            The CURRENT _ TIMESTAMP (), LOCALTIME (), the NOW (), SYSDATE () action of the same four functions, both return the current system date and time format 'YYYY-MM-DD HH: MM: SS'.

4. System information function

       USER () returns the currently logged in user name.

       DATABASE () returns the name of the currently used database.

       VERSION () returns the MySQL server version number.

SELECT  CONCAT('当前数据库:',DATABASE(),';用户:',USER()) AS 登录信息;

result:

5. Conditional control function

      1) IF () function

           IF (conditional expression, v1, v2) If the conditional expression is true, the function returns the value of v1, otherwise returns the value of v2.

      2) CASE () function

          CASE expression

            WHEN  v1  THEN  r1

            WHEN  v2  THEN  r2

             ……

           [ELSE   rn]

        END

SELECT ename 姓名,
   CASE deptno
    WHEN  10  THEN  'ACCOUNTING'
    WHEN  20  THEN  'RESEARCH'
    WHEN  30  THEN  'SALES'
    WHEN  40  THEN  'OPERATIONS'
ELSE 'DESIGN'
   END  部门名称
  FROM  emp
  WHERE  ename='Alice';

6. Data type conversion function

        The two functions of CAST (x AS new type) and CONVERT (x new type) have the same effect, converting the value of one type to the value of another type.

SELECT ename,sal INTO @empname,@salary FROM  emp  WHERE  ename='Alice';
SELECT  CONCAT(@empname,'的工资是',
CAST(@salary AS CHAR(7)))  信息;

result:

 

Published 75 original articles · praised 164 · 110,000 views

Guess you like

Origin blog.csdn.net/qq_41679818/article/details/105628658