1
2
3
4
5
6
7
|
DROP
TABLE
IF EXISTS
sequence
;
CREATE
TABLE
sequence
(
name
VARCHAR
(50)
NOT
NULL
,
current_value
INT
NOT
NULL
,
increment
INT
NOT
NULL
DEFAULT
1,
PRIMARY
KEY
(
name
)
) ENGINE=InnoDB;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
DROP
FUNCTION
IF EXISTS currval;
DELIMITER $
CREATE
FUNCTION
currval (seq_name
VARCHAR
(50))
RETURNS
INTEGER
LANGUAGE SQL
DETERMINISTIC
CONTAINS
SQL
SQL SECURITY DEFINER
COMMENT
''
BEGIN
DECLARE
value
INTEGER
;
SET
value = 0;
SELECT
current_value
INTO
value
FROM
sequence
WHERE
name
= seq_name;
RETURN
value;
END
$
DELIMITER ;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
DROP
FUNCTION
IF EXISTS nextval;
DELIMITER $
CREATE
FUNCTION
nextval (seq_name
VARCHAR
(50))
RETURNS
INTEGER
LANGUAGE SQL
DETERMINISTIC
CONTAINS
SQL
SQL SECURITY DEFINER
COMMENT
''
BEGIN
UPDATE
sequence
SET
current_value = current_value + increment
WHERE
name
= seq_name;
RETURN
currval(seq_name);
END
$
DELIMITER ;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
DROP
FUNCTION
IF EXISTS setval;
DELIMITER $
CREATE
FUNCTION
setval (seq_name
VARCHAR
(50), value
INTEGER
)
RETURNS
INTEGER
LANGUAGE SQL
DETERMINISTIC
CONTAINS
SQL
SQL SECURITY DEFINER
COMMENT
''
BEGIN
UPDATE
sequence
SET
current_value = value
WHERE
name
= seq_name;
RETURN
currval(seq_name);
END
$
DELIMITER ;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public
void
testGetSequence() {
Connection conn = JDBCUtils.getConnection(url, userName, password);
String sql =
"SELECT CURRVAL('TestSeq');"
;
PreparedStatement ptmt =
null
;
ResultSet rs =
null
;
try
{
ptmt = conn.prepareStatement(sql);
rs = ptmt.executeQuery();
int
count =
0
;
while
(rs.next()) {
count = rs.getInt(
1
);
}
System.out.println(count);
}
catch
(SQLException e) {
e.printStackTrace();
}
finally
{
JDBCUtils.close(rs, ptmt, conn);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
DROP
TABLE
IF EXISTS
sequence
;
-- 建sequence表,指定seq列为无符号大整型,可支持无符号值:0(default)到18446744073709551615(0到2^64–1)。
CREATE
TABLE
sequence
(
name
VARCHAR
(50)
NOT
NULL
,
current_value
BIGINT
UNSIGNED
NOT
NULL
DEFAULT
0,
increment
INT
NOT
NULL
DEFAULT
1,
PRIMARY
KEY
(
name
)
-- 不允许重复seq的存在。
) ENGINE=InnoDB;
DELIMITER /
DROP
FUNCTION
IF EXISTS currval /
CREATE
FUNCTION
currval(seq_name
VARCHAR
(50))
RETURNS
BIGINT
BEGIN
DECLARE
value
BIGINT
;
SELECT
current_value
INTO
value
FROM
sequence
WHERE
upper
(
name
) =
upper
(seq_name);
-- 大小写不区分.
RETURN
value;
END
;
/
DELIMITER ;
DELIMITER /
DROP
FUNCTION
IF EXISTS nextval /
CREATE
FUNCTION
nextval (seq_name
VARCHAR
(50))
RETURNS
BIGINT
BEGIN
DECLARE
value
BIGINT
;
UPDATE
sequence
SET
current_value = current_value + increment
WHERE
upper
(
name
) =
upper
(seq_name);
RETURN
currval(seq_name);
END
;
/
DELIMITER ;
DELIMITER /
DROP
FUNCTION
IF EXISTS setval /
CREATE
FUNCTION
setval (seq_name
VARCHAR
(50), value
BIGINT
)
RETURNS
BIGINT
BEGIN
UPDATE
sequence
SET
current_value = value
WHERE
upper
(
name
) =
upper
(seq_name);
RETURN
currval(seq_name);
END
;
/
DELIMITER ;
|
1
|
mysql>
insert
into
sequence
set
name
=
'myseq'
;
|
1
|
mysql>
select
*
from
sequence
;
|
1
2
3
4
5
6
|
+-------+---------------+-----------+
| name | current_value | increment |
+-------+---------------+-----------+
| myseq | 0 | 1 |
+-------+---------------+-----------+
1 row in set (0.00 sec)
|
1
|
mysql>
select
nextval(
'myseq'
);
|
1
2
3
4
5
6
|
+------------------+
| nextval('myseq') |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
|