1- CREATE TABLE `user ` (
2- ` id` bigint (11 ) NOT NULL AUTO_INCREMENT,
3- ` name` varchar (50 ) NOT NULL COMMENT ' 姓名' ,
4- ` sex` varchar (2 ) NOT NULL COMMENT ' 性别' ,
5- ` age` int (3 ) NOT NULL COMMENT ' 年龄' ,
6- ` phone` varchar (11 ) NOT NULL DEFAULT ' 0' COMMENT ' 手机' ,
7- ` deliveryaddress` varchar (200 ) DEFAULT NULL COMMENT ' 收货地址' ,
8- ` adddate` int (11 ) NOT NULL COMMENT ' 添加时间' ,
9- PRIMARY KEY (` id` )
10- ) ENGINE= InnoDB AUTO_INCREMENT= 1 DEFAULT CHARSET= utf8;
11-
12- DROP TABLE IF EXISTS ` address` ;
13- CREATE TABLE `address ` (
14- ` user_id` bigint (20 ) DEFAULT NULL ,
15- ` province` varchar (255 ) DEFAULT NULL ,
16- ` city` varchar (255 ) DEFAULT NULL
17- ) ENGINE= InnoDB DEFAULT CHARSET= utf8;
18-
19- DROP TABLE IF EXISTS ` menu` ;
20- CREATE TABLE `menu ` (
21- ` id` bigint (20 ) NOT NULL DEFAULT ' 0' ,
22- ` name` varchar (255 ) DEFAULT NULL COMMENT ' 菜单名' ,
23- ` parent_id` bigint (20 ) DEFAULT NULL COMMENT ' 父id' ,
24- PRIMARY KEY (` id` )
25- ) ENGINE= InnoDB DEFAULT CHARSET= utf8;
26-
27- -- ----------------------------
28- -- Records of menu
29- -- ----------------------------
30- INSERT INTO ` menu` VALUES (' 1' , ' menu' , ' 0' );
31- INSERT INTO ` menu` VALUES (' 2' , ' submenu' , ' 1' );
32-
33- DROP TABLE IF EXISTS ` card` ;
34- CREATE TABLE `card ` (
35- ` user_id` bigint (20 ) NOT NULL DEFAULT ' 0' ,
36- ` card_no` varchar (255 ) DEFAULT NULL ,
37- PRIMARY KEY (` user_id` )
38- ) ENGINE= InnoDB DEFAULT CHARSET= utf8;
39-
40- -- ----------------------------
41- -- Records of card
42- -- ----------------------------
43- INSERT INTO ` card` VALUES (' 1' , ' this is cardno' );
44-
45- -- ----------------------------
46- -- Function structure for `fristPinyin`
47- -- ----------------------------
48- DROP FUNCTION IF EXISTS ` fristPinyin` ;
49- DELIMITER ;;
50- CREATE DEFINER= ` root` @` %` FUNCTION ` fristPinyin` (P_NAME VARCHAR (255 )) RETURNS varchar (255 ) CHARSET utf8
51- BEGIN
52- DECLARE V_RETURN VARCHAR (255 );
53- SET V_RETURN = ELT(INTERVAL(CONV(HEX(left(CONVERT (P_NAME USING gbk),1 )),16 ,10 ),
54- 0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7,
55- 0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,
56- 0xC8F6,0xCBFA,0xCDDA,0xCEF4,0xD1B9,0xD4D1),
57- ' A' ,' B' ,' C' ,' D' ,' E' ,' F' ,' G' ,' H' ,' J' ,' K' ,' L' ,' M' ,' N' ,' O' ,' P' ,' Q' ,' R' ,' S' ,' T' ,' W' ,' X' ,' Y' ,' Z' );
58- RETURN V_RETURN;
59- END
60- ;;
61- DELIMITER ;
62-
63- CREATE TABLE if not exists ` statistics` (
64- ` usertotal` bigint (20 )
65- ) ENGINE= InnoDB DEFAULT CHARSET= utf8;
66-
67- -- 触发器测试
68- DROP TRIGGER IF EXISTS tri_countUserTotal;
69- CREATE TRIGGER tri_countUserTotal AFTER
70- INSERT ON USER
71- FOR EACH ROW BEGIN DECLARE c int ;
72- SET c =
73- (SELECT count (* )
74- FROM USER);
75- UPDATE STATISTICS
76- SET usertotal = c; END;
77- update statistics set usertotal= 0 ;
78-
79- CREATE DEFINER= ` root` @` %` PROCEDURE ` insert_touser` (in start int (10 ),in max_num int (10 ))
80- begin
81- declare i int default 0 ;
82- -- set autocommit =0 把autocommit设置成0 不自动提交,循环完统一提交
83- set autocommit = 0 ;
84- repeat
85- set i = i + 1 ;
86- insert into user(name,phone,adddate) values ((start+ i),' test' ,UNIX_TIMESTAMP(NOW()));
87- until i = max_num
88- end repeat;
89- commit ;
90- end
91- -- 添加1w个用户
92- call insert_touser(1 ,10000 );
93- -- 锁表
94- lock tables user read;
95- lock tables user write;
1+ CREATE TABLE `user ` (
2+ ` id` bigint (11 ) NOT NULL AUTO_INCREMENT,
3+ ` name` varchar (50 ) NOT NULL COMMENT ' 姓名' ,
4+ ` sex` varchar (2 ) NOT NULL COMMENT ' 性别' ,
5+ ` age` int (3 ) NOT NULL COMMENT ' 年龄' ,
6+ ` phone` varchar (11 ) NOT NULL DEFAULT ' 0' COMMENT ' 手机' ,
7+ ` deliveryaddress` varchar (200 ) DEFAULT NULL COMMENT ' 收货地址' ,
8+ ` adddate` int (11 ) NOT NULL COMMENT ' 添加时间' ,
9+ PRIMARY KEY (` id` )
10+ ) ENGINE= InnoDB AUTO_INCREMENT= 1 DEFAULT CHARSET= utf8;
11+
12+ DROP TABLE IF EXISTS ` address` ;
13+ CREATE TABLE `address ` (
14+ ` user_id` bigint (20 ) DEFAULT NULL ,
15+ ` province` varchar (255 ) DEFAULT NULL ,
16+ ` city` varchar (255 ) DEFAULT NULL
17+ ) ENGINE= InnoDB DEFAULT CHARSET= utf8;
18+
19+ DROP TABLE IF EXISTS ` menu` ;
20+ CREATE TABLE `menu ` (
21+ ` id` bigint (20 ) NOT NULL DEFAULT ' 0' ,
22+ ` name` varchar (255 ) DEFAULT NULL COMMENT ' 菜单名' ,
23+ ` parent_id` bigint (20 ) DEFAULT NULL COMMENT ' 父id' ,
24+ PRIMARY KEY (` id` )
25+ ) ENGINE= InnoDB DEFAULT CHARSET= utf8;
26+
27+ -- ----------------------------
28+ -- Records of menu
29+ -- ----------------------------
30+ INSERT INTO ` menu` VALUES (' 1' , ' menu' , ' 0' );
31+ INSERT INTO ` menu` VALUES (' 2' , ' submenu' , ' 1' );
32+
33+ DROP TABLE IF EXISTS ` card` ;
34+ CREATE TABLE `card ` (
35+ ` user_id` bigint (20 ) NOT NULL DEFAULT ' 0' ,
36+ ` card_no` varchar (255 ) DEFAULT NULL ,
37+ PRIMARY KEY (` user_id` )
38+ ) ENGINE= InnoDB DEFAULT CHARSET= utf8;
39+
40+ -- ----------------------------
41+ -- Records of card
42+ -- ----------------------------
43+ INSERT INTO ` card` VALUES (' 1' , ' this is cardno' );
44+
45+ -- ----------------------------
46+ -- Function structure for `fristPinyin`
47+ -- ----------------------------
48+ DROP FUNCTION IF EXISTS ` fristPinyin` ;
49+ DELIMITER ;;
50+ CREATE DEFINER= ` root` @` %` FUNCTION ` fristPinyin` (P_NAME VARCHAR (255 )) RETURNS varchar (255 ) CHARSET utf8
51+ BEGIN
52+ DECLARE V_RETURN VARCHAR (255 );
53+ SET V_RETURN = ELT(INTERVAL(CONV(HEX(left(CONVERT (P_NAME USING gbk),1 )),16 ,10 ),
54+ 0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7,
55+ 0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,
56+ 0xC8F6,0xCBFA,0xCDDA,0xCEF4,0xD1B9,0xD4D1),
57+ ' A' ,' B' ,' C' ,' D' ,' E' ,' F' ,' G' ,' H' ,' J' ,' K' ,' L' ,' M' ,' N' ,' O' ,' P' ,' Q' ,' R' ,' S' ,' T' ,' W' ,' X' ,' Y' ,' Z' );
58+ RETURN V_RETURN;
59+ END
60+ ;;
61+ DELIMITER ;
62+
63+ CREATE TABLE if not exists ` statistics` (
64+ ` usertotal` bigint (20 )
65+ ) ENGINE= InnoDB DEFAULT CHARSET= utf8;
66+
67+ -- 触发器测试
68+ DROP TRIGGER IF EXISTS tri_countUserTotal;
69+ CREATE TRIGGER tri_countUserTotal AFTER
70+ INSERT ON USER
71+ FOR EACH ROW BEGIN DECLARE c int ;
72+ SET c =
73+ (SELECT count (* )
74+ FROM USER);
75+ UPDATE STATISTICS
76+ SET usertotal = c; END;
77+ update statistics set usertotal= 0 ;
78+
79+ CREATE DEFINER= ` root` @` %` PROCEDURE ` insert_touser` (in start int (10 ),in max_num int (10 ))
80+ begin
81+ declare i int default 0 ;
82+ -- set autocommit =0 把autocommit设置成0 不自动提交,循环完统一提交
83+ set autocommit = 0 ;
84+ repeat
85+ set i = i + 1 ;
86+ insert into user(name,phone,adddate) values ((start+ i),' test' ,UNIX_TIMESTAMP(NOW()));
87+ until i = max_num
88+ end repeat;
89+ commit ;
90+ end
91+ -- 添加1w个用户
92+ call insert_touser(1 ,10000 );
93+ -- 锁表
94+ lock tables user read;
95+ lock tables user write;
9696unlock tables;
0 commit comments