Mysql中如何实现某字段mysql 修改数据库字段自动加1

trackbacks-0
1、assigned
主键由外部程序负责生成,在&save()&之前必须指定一个。Hibernate不负责维护主键生成。与Hibernate和底层数据库都无关,可以跨数据库。在存储对象前,必须要使用主键的setter方法给主键赋值,至于这个值怎么生成,完全由自己决定,这种方法应该尽量避免。
&id name="id" column="id"&
&generator&class="assigned" /&
&ud&是自定义的策略名,人为起的名字,后面均用&ud&表示。
特点:可以跨数据库,人为控制主键生成,应尽量避免。
2、increment
由Hibernate从数据库中取出主键的最大值(每个session只取1次),以该值为基础,每次增量为1,在内存中生成主键,不依赖于底层的数据库,因此可以跨数据库。
&id name="id" column="id"&
&generator class="increment" /&
Hibernate调用org.hibernate.id.IncrementGenerator类里面的generate()方法,使用select max(idColumnName) from tableName语句获取主键最大值。该方法被声明成了synchronized,所以在一个独立的Java虚拟机内部是没有问题的,然而,在多个JVM同时并发访问数据库select max时就可能取出相同的值,再insert就会发生Dumplicate entry的错误。所以只能有一个Hibernate应用进程访问数据库,否则就可能产生主键冲突,所以不适合多进程并发更新数据库,适合单一进程访问数据库,不能用于群集环境。
官方文档:只有在没有其他进程往同一张表中插入数据时才能使用,在集群下不要使用。
特点:跨数据库,不适合多进程并发更新数据库,适合单一进程访问数据库,不能用于群集环境。
hilo(高低位方式high low)是hibernate中最常用的一种生成方式,需要一张额外的表保存hi的值。保存hi值的表至少有一条记录(只与第一条记录有关),否则会出现错误。可以跨数据库。
&id name="id" column="id"&
&generator class="hilo"&
&param name="table"&hibernate_hilo&/param&
&param name="column"&next_hi&/param&
&param name="max_lo"&100&/param&
&/generator&
&param name="table"&hibernate_hilo&/param&&指定保存hi值的表名
&param name="column"&next_hi&/param&&指定保存hi值的列名
&param name="max_lo"&100&/param&&指定低位的最大值
也可以省略table和column配置,其默认的表为hibernate_unique_key,列为next_hi
&id name="id" column="id"&
&generator class="hilo"&
&param name="max_lo"&100&/param&
&/generator&
hilo生成器生成主键的过程(以hibernate_unique_key表,next_hi列为例):
1.&获得hi值:读取并记录数据库的hibernate_unique_key表中next_hi字段的值,数据库中此字段值加1保存。
2.&获得lo值:从0到max_lo循环取值,差值为1,当值为max_lo值时,重新获取hi值,然后lo值继续从0到max_lo循环。
3.&根据公式&hi * (max_lo + 1) + lo计算生成主键值。
注意:当hi值是0的时候,那么第一个值不是0*(max_lo+1)+0=0,而是lo跳过0从1开始,直接是1、2、3&&
那max_lo配置多大合适呢?
这要根据具体情况而定,如果系统一般不重启,而且需要用此表建立大量的主键,可以吧max_lo配置大一点,这样可以减少读取数据表的次数,提高效率;反之,如果服务器经常重启,可以吧max_lo配置小一点,可以避免每次重启主键之间的间隔太大,造成主键值主键不连贯。
特点:跨数据库,hilo算法生成的标志只能在一个数据库中保证唯一。
4、seqhilo
与hilo类似,通过hi/lo算法实现的主键生成机制,只是将hilo中的数据表换成了序列sequence,需要数据库中先创建sequence,适用于支持sequence的数据库,如Oracle。
&id name="id" column="id"&
&generator class="seqhilo"&
&param name="sequence"&hibernate_seq&/param&
&param name="max_lo"&100&/param&
&/generator&
特点:与hilo类似,只能在支持序列的数据库中使用。
5、sequence
采用数据库提供的sequence机制生成主键,需要数据库支持sequence。如oralce、DB、SAP DB、PostgerSQL、McKoi中的sequence。MySQL这种不支持sequence的数据库则不行(可以使用identity)。
&generator class="sequence"&
&param name="sequence"&hibernate_id&/param&
&/generator&
&param name="sequence"&hibernate_id&/param&&指定sequence的名称
Hibernate生成主键时,查找sequence并赋给主键值,主键值由数据库生成,Hibernate不负责维护,使用时必须先创建一个sequence,如果不指定sequence名称,则使用Hibernate默认的sequence,名称为hibernate_sequence,前提要在数据库中创建该sequence。
特点:只能在支持序列的数据库中使用,如Oracle。
6、identity
identity由底层数据库生成标识符。identity是由数据库自己生成的,但这个主键必须设置为自增长,使用identity的前提条件是底层数据库支持自动增长字段类型,如DB2、SQL Server、MySQL、Sybase和HypersonicSQL等,Oracle这类没有自增字段的则不支持。
&id name="id" column="id"&
&generator class="identity" /&
例:如果使用MySQL数据库,则主键字段必须设置成auto_increment。
id int(11) primary key auto_increment
特点:只能用在支持自动增长的字段数据库中使用,如MySQL。
native由hibernate根据使用的数据库自行判断采用identity、hilo、sequence其中一种作为主键生成方式,灵活性很强。如果能支持identity则使用identity,如果支持sequence则使用sequence。
&id name="id" column="id"&
&generator class="native" /&
例如MySQL使用identity,Oracle使用sequence
注意:如果Hibernate自动选择sequence或者hilo,则所有的表的主键都会从Hibernate默认的sequence或hilo表中取。并且,有的数据库对于默认情况主键生成测试的支持,效率并不是很高。
使用sequence或hilo时,可以加入参数,指定sequence名称或hi值表名称等,如
&param name="sequence"&hibernate_id&/param&
特点:根据数据库自动选择,项目中如果用到多个数据库时,可以使用这种方式,使用时需要设置表的自增字段或建立序列,建立表等。
UUID:Universally Unique Identifier,是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的。按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址、纳秒级时间、芯片ID码和许多可能的数字,标准的UUID格式为:
xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx (8-4-4-4-12)
其中每个&x&是&0-9&或&a-f&范围内的一个十六进制的数字。
&id name="id" column="id"&
&generator class="uuid" /&
Hibernate在保存对象时,生成一个UUID字符串作为主键,保证了唯一性,但其并无任何业务逻辑意义,只能作为主键,唯一缺点长度较大,32位(Hibernate将UUID中间的&-&删除了)的字符串,占用存储空间大,但是有两个很重要的优点,Hibernate在维护主键时,不用去数据库查询,从而提高效率,而且它是跨数据库的,以后切换数据库极其方便。
特点:uuid长度大,占用空间大,跨数据库,不用访问数据库就生成主键值,所以效率高且能保证唯一性,移植非常方便,推荐使用。
GUID:Globally Unique Identifier全球唯一标识符,也称作&UUID,是一个128位长的数字,用16进制表示。算法的核心思想是结合机器的网卡、当地时间、一个随即数来生成GUID。从理论上讲,如果一台机器每秒产生个GUID,则可以保证(概率意义上)3240年不重复。
&id name="id" column="id"&
&generator class="guid" /&
Hibernate在维护主键时,先查询数据库,获得一个uuid字符串,该字符串就是主键值,该值唯一,缺点长度较大,支持数据库有限,优点同uuid,跨数据库,但是仍然需要访问数据库。
注意:长度因数据库不同而不同
MySQL中使用select uuid()语句获得的为36位(包含标准格式的&-&)
Oracle中,使用select rawtohex(sys_guid()) from dual语句获得的为32位(不包含&-&)&
特点:需要数据库支持查询uuid,生成时需要查询数据库,效率没有uuid高,推荐使用uuid。
10、foreign
使用另外一个相关联的对象的主键作为该对象主键。主要用于一对一关系中。
&id name="id" column="id"&
&generator class="foreign"&
&param name="property"&user&/param&
&/generator&
&one-to-one name="user" class="domain.User" constrained="true" /&
该例使用domain.User的主键作为本类映射的主键。
特点:很少使用,大多用在一对一关系中。
11、select
使用触发器生成主键,主要用于早期的数据库主键生成机制,能用到的地方非常少。
12、其他注释方式配置
注释方式与配置文件底层实现方式相同,只是配置的方式换成了注释方式
自动增长,适用于支持自增字段的数据库
@GeneratedValue(strategy = GenerationType.IDENTITY)
根据底层数据库自动选择方式,需要底层数据库的设置
如MySQL,会使用自增字段,需要将主键设置成auto_increment。
@GeneratedValue(strategy = GenerationType.AUTO)
使用表存储生成的主键,可以跨数据库。
每次需要主键值时,查询名为"hibernate_table"的表,查找主键列"gen_pk"值为"2"记录,得到这条记录的"gen_val"值,根据这个值,和allocationSize的值生成主键值。
@GeneratedValue(strategy = GenerationType.TABLE, generator = "ud")
@TableGenerator(name = "ud",
table = "hibernate_table",
pkColumnName = "gen_pk",
pkColumnValue = "2",
valueColumnName = "gen_val",
initialValue = 2,
allocationSize = 5)
使用序列存储主键值
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ud")
@SequenceGenerator(name = "ud",
sequenceName = "hibernate_seq",
allocationSize = 1,
initialValue = 2)
1、为了保证对象标识符的唯一性与不可变性,应该让Hibernate来为主键赋值,而不是程序。
2、正常使用Hibernate维护主键,最好将主键的setter方法设置成private,从而避免人为或程序修改主键,而使用assigned方式,就不能用private,否则无法给主键赋值。
2、Hibernate中唯一一种最简单通用的主键生成器就是uuid。虽然是个32位难读的长字符串,但是它没有跨数据库的问题,将来切换数据库极其简单方便,推荐使用!
3、自动增长字段类型与序列
自动增长字段
MS SQL Server
HypersonicSQL
PostgreSQL
4、关于hilo机制注意:
hilo算法生成的标志只能在一个数据库中保证唯一。
当用户为Hibernate自行提供连接,或者Hibernate通过JTA,从应用服务器的数据源获取数据库连接时,无法使用hilo,因为这不能保证hilo单独在新的数据库连接的事务中访问hi值表,这种情况,如果数据库支持序列,可以使用seqhilo。
5、使用identity、native、GenerationType.AUTO等方式生成主键时,只要用到自增字段,数据库表的字段必须设置成自动增加的,否则出错。
6、还有一些方法未列出来,例如uuid.hex,sequence-identity等,这些方法不是很常用,且已被其他方法代替,如uuid.hex,官方文档里建议不使用,而直接使用uuid方法。
7、Hibernate的各版本主键生成策略配置有略微差别,但实现基本相同。如,有的版本默认sequence不指定序列名,则使用名为hibernate_sequence的序列,有的版本则必须指定序列名。
8、还可以自定义主键生成策略,这里暂时不讨论,只讨论官方自带生成策略。&
阅读(...) 评论()&&&&oracle数据库字段值自动加1
oracle数据库字段值自动加1
往数据库中添加数据时,字段值自动加1:
2、创建序列
3、创建触发器
若举报审核通过,可奖励20下载分
被举报人:
举报的资源分:
请选择类型
资源无法下载
资源无法使用
标题与实际内容不符
含有危害国家安全内容
含有反动色情等内容
含广告内容
版权问题,侵犯个人或公司的版权
*详细原因:
VIP下载&&免积分60元/年(1200次)
您可能还需要
数据库下载排行mysql&如何取自动加一字段的值
argument) returns the first
automatically generated value that was set for an AUTO_INCREMENT column by the most recently executed INSERT statement to affect such a column. For
example, after inserting a row that generates an AUTO_INCREMENT value, you can get the value like
mysql& SELECT LAST_INSERT_ID();
if a table contains an AUTO_INCREMENT column and INSERT ... ON DUPLICATE KEY UPDATE updates (rather
than inserts) a row, the value of
meaningful. For a workaround, see
Section&12.2.5.3, “INSERT
... ON DUPLICATE KEY UPDATE Syntax”.
The currently executing statement does not affect the value of
. Suppose that you
generate an AUTO_INCREMENT value with
one statement, and then refer to
in a multiple-row
INSERT statement that inserts rows into a
table with its own AUTO_INCREMENT
column. The value of
will remain
its value for the second and later
rows is not affected by the earlier row insertions. (However, if
you mix references to
and , the effect is
undefined.)
If the previous statement returned an error, the value of
is undefined. For
transactional tables, if the statement is rolled back due to an
error, the value of
undefined. For manual
ROLLBACK, the value of
is not restored
to that be it remains as it was at the point
Within the body of a stored routine (procedure or function) or a
trigger, the value of
changes the same
way as for statements executed outside the body of these kinds of
objects. The effect of a stored routine or trigger upon the value
that is seen by
following statements depends on the kind of routine:
If a stored procedure executes statements that change the value
of , the changed
value will be seen by statements that follow the procedure
For stored functions and triggers that change the value, the
value is restored when the function or trigger ends, so following
statements will not see a changed value.
The ID that was generated is maintained in the server on a
per-connection basis. This
means that the value returned by the function to a given client is
the first AUTO_INCREMENT value
generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be
affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior
ensures that each client can retrieve its own ID without concern
for the activity of other clients, and without the need for locks
or transactions.
The value of
is not changed if
you set the AUTO_INCREMENT column of a
row to a non-“magic” value (that is, a
value that is not NULL and not
If you insert multiple rows using a single INSERT statement,
returns the value
generated for the first
inserted row only. The
reason for this is to make it possible to reproduce easily the same
INSERT statement against some other
For example:
mysql& USE
Database changed
mysql& CREATE TABLE t (
id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(10) NOT NULL
Query OK, 0 rows affected (0.09 sec)
mysql& INSERT INTO t VALUES (NULL, 'Bob');
Query OK, 1 row affected (0.01 sec)
mysql& SELECT * FROM
+----+------+
| id | name |
+----+------+
+----+------+
1 row in set (0.01 sec)
mysql& SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
+------------------+
1 row in set (0.00 sec)
mysql& INSERT INTO t VALUES
-& (NULL, 'Mary'), (NULL, 'Jane'), (NULL, 'Lisa');
Query OK, 3 rows affected (0.00 sec)
Records: 3
Duplicates: 0
Warnings: 0
mysql& SELECT * FROM
+----+------+
| id | name |
+----+------+
2 | Mary |
3 | Jane |
4 | Lisa |
+----+------+
4 rows in set (0.01 sec)
mysql& SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
+------------------+
1 row in set (0.00 sec)
Although the second INSERT statement inserted three new rows into
t, the ID generated for the first of
these rows was 2, and it is this value
that is returned by
for the following
SELECT statement.
If you use INSERT IGNORE and the
row is ignored, the AUTO_INCREMENT
counter is not incremented and
0, which reflects that no row was
expr is given as an
argument to , the value of the
argument is returned by the function and is remembered as the next
value to be returned by . This can be used
to simulate sequences:
Create a table to hold the sequence counter and initialize
mysql& CREATE TABLE sequence (id INT NOT NULL);
mysql& INSERT INTO sequence VALUES (0);
Use the table to generate sequence numbers like this:
mysql& UPDATE sequence SET id=LAST_INSERT_ID(id+1);
mysql& SELECT LAST_INSERT_ID();
The UPDATE statement increments the sequence
counter and causes the next call to
to return the
updated value. The SELECT statement retrieves that value. The
mysql_insert_id() C API function can also be
used to get the value. See Section&20.9.3.37,
“mysql_insert_id()”.
You can generate sequences without calling , but the utility
of using the function this way is that the ID value is maintained
in the server as the last automatically generated value. It is
multi-user safe because multiple clients can issue the UPDATE statement and get their own sequence
value with the SELECT statement (or mysql_insert_id()), without affecting or being
affected by other clients that generate their own sequence
Note that mysql_insert_id() is only updated after
INSERT and UPDATE statements, so you cannot use the C API
function to retrieve the value for
after executing
other SQL statements like SELECT or SET.
/blog/54293
/doc/refman/5.0/en/information-functions.html
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。您的举报已经提交成功,我们将尽快处理,谢谢!
ID 如果是自动增加的话,是不用管的,你只要插入name 与 passwd,id就会相应的加一条。
语句是:insert into user(name,pass...
你可以看看你数据库里插入的数据是乱码?如果是就是插入的问题,有可能是jsp数据编码错误,或者就是MySQL编码错误,你一步一步的找嘛,总会找到的!如果数据库的值...
(window.slotbydup=window.slotbydup || []).push({
id: '2081942',
container: s,
size: '1000,60',
display: 'inlay-fix'mysql 的一个表中只有递增主键一个字段,如何插入数据? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
已注册用户请 &
Great Sites on MySQL
推荐管理工具
MySQL 相关项目
Sponsored by
国内领先的实时后端云野狗 API 可用于开发即时聊天、网络游戏、实时定位等实时场景传输快!响应快!入门快!
Promoted by
mysql 的一个表中只有递增主键一个字段,如何插入数据?
09:27:33 +08:00 · 3312 次点击
还有个问题,我发现discuz数据表中的主键的记录居然是重复的。有点奇怪可以看出pid字段中的值是唯一的,但却没有设置递增和主键。我改如何科学的新增数据?可能这个表和其它表有关联,但我现在要直接操作这个表。我插入时先"SELECT MAX(pid) FROM pre_forum_post",得到pid,但总觉得有点不科学。
13 回复 &| &直到
12:17:53 +08:00
& & 09:44:23 +08:00
第一张图片里,pid这个字段是不同的
& & 09:48:34 +08:00
@ 你说的是第二张图吧,pid是不同呀。但tid在第二张图的表里面才是主键。
& & 09:51:02 +08:00
pid -& post id
tid -& thread id
fid -& forum id
能看出关系了么? 一个thread可以有很多post, 至于那个pid主键单独一个表不知道什么情况
& & 09:59:46 +08:00
@ 嗯,这个我知道。每个主题(thread)里面的楼层(包括0楼)都是post。但从第三张图中可以看见tid是主键,但记录中的值tid有重复。(第二张图)
pid主键单独一个表那个是用来帖子分表用的,一般用不上。我只是不知道这个表只有一个递增主键我改如何新增数据。
& & 10:00:24 +08:00
insert into table——name values (null)
& & 10:02:17 +08:00
INSERT INTO `table`(pid) SELECT max(pid) +1 FROM `table`
& & 10:03:50 +08:00
@ 使用SELECT max(pid) +1 FROM `table`会不会有并发冲突问题。语言是php
& & 10:12:02 +08:00
CREATE TABLE t (
id INT AUTO_INCREMENT NOT NULL PRIMARY KEY
)
INSERT INTO `t` VALUE()
& & 10:13:56 +08:00
@ 原来如此,要用'NULL',一开始老想着留空。尝试了几次发现这样也可以:
& & 10:19:57 +08:00
应该来说不自增的主键不是唯一值, 但是整条数据必须是唯一值
& & 11:04:54 +08:00
看下discuz 的源码怎么写的吧 亲
别乱搞 小心discuz数据结构被你搞坏哈
& & 11:58:30 +08:00
@ 额,原来是我看错了,表里面有两个主键。PRIMARY KEY (`tid`,`position`)
position字段表示的是楼层,这样主题+楼层就可以唯一表示一个帖子了。
那个字段居然在最后,屏幕小,哭瞎了。
& & 12:17:53 +08:00
tid是comment对应的索引
& · & 416 人在线 & 最高记录 1847 & · &
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.7.3 · 54ms · UTC 17:56 · PVG 01:56 · LAX 10:56 · JFK 13:56? Do have faith in what you're doing.

我要回帖

更多关于 mysql数据库字段加密 的文章

 

随机推荐