MySQL database programming, built-in functions, stored procedures (loop Insert)

Source: Internet
Author: User
Tags sha1 time and date

MySQL Programming
Note: line: # OR--

Defining variables
Set Variable name = variable name
Note: In order to differentiate system variables and fields at the plus of an @ identifier
For example Set @who = ' Korean ';

Use Select to get the value of the current variable


Select 10,15,20 into @a,@b,@c;

Note: = should be an assignment, but within the SELECT statement, the relationship equals, using the specialized assignment operator: =
Same use as set

1, scope, user-defined functions, is global (within the function can), village local scope variables, functions defined within the variable
2. Expiry date (end of connection)


Built-in functions

numeric functions
RAND () gets a random number of 0-1
Floor () Rounding down
Cell () rounding up
Round () rounding
Format () formatting (thousands of bits plus commas)

Time and date functions

Now () Current time
Unix_timestamp () Unix timestamp
From_unixtime ()

String functions

Concat () String connection
Length (), representing the number of bytes char_length () characters
SUBSTRING () Intercept string
Position starting from 1

Lpad () Left complement example Select Lpad (' 1 ', 3, ' 0 '); Output ' 001 '

MD5 () Common encryption, irreversible, all support
Password () encryption, MySQL alone (2 results from SHA1 ())
SHA1 ()

Custom functions



Delimiter $$
Create function SayHello () return varchar (20)
Begin
Return ' Hello World ';
End
$$
Delimiter


Stored procedure insertion (loop insert)

DROP PROCEDURE Test_insert; If this deletion already exists
DELIMITER;; Set the default Terminator to;;
Create PROCEDURE Test_insert () Creating a stored procedure
Begin

DECLARE i INT DEFAULT 0; Define the variable i and set the default value to 0

While I<10 Loop 10 times
Do
INSERT into CMS. Cms_module Inserting data
(
Moduletype,
ModuleName,
Moduledescription,
Designhtml,
QUERY
)
VALUES
(
' 2 ',
CONCAT (' Activity ', I),
' Test join ',
' Aaaaaaaaaa ',
‘‘
);
SET i=i+1; I+1 Guaranteed Cycle 10 times
END while; Exiting while loop

END;; Receive
DELIMITER; Change back to default Terminator
Call Test_insert (); Call

MySQL database programming, built-in functions, stored procedures (loop Insert)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.