我有一个案例:将数据从Access数据库传输到SQL。我遇到过如何在SQL中创建大量新主键的问题。
当我转移previos表时,我使用了Generator ……
而不是创建随机密钥,使用序列将数字序列转换为高基数(例如使用javascript id_as_char = id_as_int.toString(36) )。
id_as_char = id_as_int.toString(36)
当然,你有其他方法!
谢谢你的帮助。我在字符串之前添加了一行来添加“0”。
id = id.toString(36)while (id.length < (5 || 2)) {id = “0” + id;}</code>
id = id.toString(36)while (id.length < (5 || 2)) {id = “0” + id;}
</code>
00zzm00zzn00zzo00zzp00zzq00zzr00zzs00zzt00zzu00zzv00zzw00zzx00zzy00zzz010000100101002010030100401005010060100701008010090100a0100B 就是这样 https://gist.github.com/endel/321925f6cafa25bbfbde Number.prototype.pad = function(size) { var s = String(this); while (s.length < (size || 2)) {s = “0” + s;} return s;}(1).pad(3) // => “001”(10).pad(3) // => “010”(100).pad(3) // => “100”</code>
00zzm00zzn00zzo00zzp00zzq00zzr00zzs00zzt00zzu00zzv00zzw00zzx00zzy00zzz010000100101002010030100401005010060100701008010090100a0100B
就是这样 https://gist.github.com/endel/321925f6cafa25bbfbde
Number.prototype.pad = function(size) { var s = String(this); while (s.length < (size || 2)) {s = “0” + s;} return s;}(1).pad(3) // => “001”(10).pad(3) // => “010”(100).pad(3) // => “100”</code>
Number.prototype.pad = function(size) { var s = String(this); while (s.length < (size || 2)) {s = “0” + s;} return s;}
(1).pad(3) // => “001”(10).pad(3) // => “010”(100).pad(3) // => “100”
为什么不把序列处理留给sql server?你在使用哪个数据库?如果您使用mysql或postgres,您可以创建一个具有串行类型的新列,并且在db级别处创建此序列唯一连续编号将更好。
https://www.postgresql.org/docs/9.5/static/datatype-numeric.html#DATATYPE-SERIAL