SQL保证开始/结束索引?
我(将)有数十万条记录,我插入一次,永远不会更新许多行持有相同的 previousId。我可以保证开始/结束索引吗?我在哪里用事务在 table_c 中插入 X 行并将开始和结束(或开始和长度或结束和长度)写入 table_b 而不是让每行保存 table_b ID?
如果是的话我该如何编写SQL?我在想
begin transaction
insert XYZ rows into tbl_c
c_rowId = last_insert_rowid
insert table_b with data + start=c_rowId-lengthOfInsert, end=c_rowId;
commit; end transaction
这会像我预期的那样工作吗?
I (will) have hundreds of thousand of records where i insert once, never update with many rows holding the same previousId. Can i guaranteed a start/end index? where i insert X number of rows in table_c with a transaction and write the start and end (or start and length or end and length) into table_b instead of having each row hold table_b ID?
if so how do i write the SQL? i was thinking
begin transaction
insert XYZ rows into tbl_c
c_rowId = last_insert_rowid
insert table_b with data + start=c_rowId-lengthOfInsert, end=c_rowId;
commit; end transaction
would this work as i expect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您想要的是一个自动编号列。在大多数 DBMS 中,您可以将此列定义为表定义的一部分,并且它将自动对行进行编号。您可以使用函数来获取最后插入的行的ID;在 SQL Server (T-SQL) 中,您可以使用 SCOPE_IDENTITY() 函数。如果 ID 必须是特定值或值范围,您可能需要手动执行此操作并使用锁定提示来防止另一个并发事务修改您的标识符信息。
It seems like what you want is an autonumber column. In most DBMS, you can define this column as part of the table definition, and it will number the rows automatically. You can use a function to get the ID of the last row inserted; in SQL Server (T-SQL), you can use the SCOPE_IDENTITY() function. If the IDs -have- to be a certain value or range of values, you may need to do it manually and use a locking hint to prevent another concurrent transaction from modifying your identifier information.
您可以使用 tabblockx 锁定 sql server 中的全表。
所以:
这样做的问题是它完全锁定了表。自动递增列(auto_increment (mysql) 或identity mssql)可能是您真正想要的,并且不会限制对整个表的访问。 (这个在另一个答案里有提到)
You can lock the full table in sql server by using tablockx.
so:
The problem with this though is that it totally locks the table. An auto incrementing column (auto_increment (mysql) or identity mssql) might be what you really want and would not limit access to the whole table. (This was noted in another answer)