MySQL function authoring and stored procedures

Source: Internet
Author: User

#建表

#创建表CLASS

CREATE Table Classes (/* Class table */

Classno mediumint UNSIGNED not NULL DEFAULT 0,

ClassName VARCHAR () not NULL DEFAULT "",

Loc VARCHAR (+) not NULL DEFAULT ""

) Engine=innodb DEFAULT Charset=utf8;


#创建表STUDENTS学生表

CREATE TABLE Students

(Studentno mediumint UNSIGNED not NULL DEFAULT 0,

Studentname VARCHAR () not NULL DEFAULT "",

Job VARCHAR (9) Not NULL DEFAULT "",

Mgr Mediumint UNSIGNED not NULL DEFAULT 0,

HireDate DATE not NULL,

Sal DECIMAL (7,2) is not NULL,

Comm DECIMAL (7,2) is not NULL,

Classno mediumint UNSIGNED not NULL DEFAULT 0

) Engine=innodb DEFAULT Charset=utf8;


#学生薪水级别表 (1-5000,2-5000-8000 3-8000+)

CREATE TABLE Salgrade

(

Grade Mediumint UNSIGNED not NULL DEFAULT 0,

Losal DECIMAL (17,2) is not NULL,

Hisal DECIMAL (17,2) not NULL

) Engine=innodb DEFAULT Charset=utf8;


INSERT into Salgrade VALUES (1,1500,5000);

INSERT into Salgrade VALUES (2,5001,8000);

INSERT into Salgrade VALUES (3,8001,12000);

INSERT into Salgrade VALUES (4,12001,15000);

INSERT into Salgrade VALUES (5,15001,59999);



#创建一个随机产生字符串的函数:

Create function rand_string (n INT)

Returns varchar (255)

Deterministic

Begin

DECLARE chars_str varchar (+) Default

' ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ ';

DECLARE return_str varchar (255) default ';

declare i int default 0;

While I<n do

Set return_str = Concat (return_str,substring (Chars_str,floor (1+rand () *52), 1));

Set i = i + 1;

End while;

return return_str;

End | |


#创建一个随机产生的数字

Create function Rand_num (n INT)

returns INT (5)

Deterministic

Begin

declare i int default 0;

Set i = Floor (1+rand () *n);

return i;

End | |



#存储过程:

#插入学生

CREATE PROCEDURE insert_students (in Start int (ten), in Max_num Int (10))

Begin

declare i int default 0;

Set autocommit = 0;

Repeat

Set i = i + 1;

INSERT into students values ((start+i), rand_string (6), ' salesman ', 0001,curdate (), 2000,400,rand_num (500));

Until I = Max_num

End repeat;

Commit

End | |


#插入班级

CREATE PROCEDURE Insert_class (in Start int (ten), in Max_num Int (10))

Begin

declare i int default 0;

Set autocommit = 0;

Repeat

Set i = i + 1;

INSERT into class values ((Start+i), rand_string (Ten), rand_string (8));

Until I = Max_num

End repeat;

Commit

End | |


MySQL function authoring and stored procedures

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.