将主键字段添加到现有 Derby 表
我是 SQL 新手,但设法使用 ij 将 CSV 数据导入 Apache Derby DB。
我的表不包含主键,并且没有合适的现有字段,因此我想创建一个新字段,生成唯一的数字来填充它,并将其设置为主键。我该怎么做?
例如,我尝试过
ALTER TABLE myTable ADD pk AUTO_INCREMENT PRIMARY KEY
,但在 AUTO_INCRMENT 上出现语法错误。
另外,当我在 google 上搜索此类内容时,我发现了很多 SQL 技巧,但其中很少有适用于 Derby 的技巧。我应该寻找特定的 SQL 变体吗?
I'm new to SQL, but managed to import my CSV data into an Apache Derby DB using ij.
My table contains no primary key, and no existing field is suitable, so I'd like to create a new field, generate unique numbers to fill it, and set it as the primary key. How can I do this?
For example, I tried
ALTER TABLE myTable ADD pk AUTO_INCREMENT PRIMARY KEY
but I got a syntax error on AUTO_INCREMENT.
Also, when I google for this sort of thing, I find lots of SQL tips, but few of them seem to apply to Derby. Is there a particular variant of SQL that I should be searching for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,据我所知,无法为现有 Apache Derby 数据库创建新的主键。 Derby 不允许您添加具有 GENERATED 作为属性的列。
这是我发现效果最好的方法。
基于原始表:
看起来像:
除了具有主键之外,创建一个具有相同结构的新表。
分解primary_key字段。前两个是标准 SQL 关键字。键将是一个整数,并且不能为空。 Apache DB 使用生成的关键字作为其 AUTO_GENERATED。 generated ALWAYS 和 AS DEFAULT 后面可以跟随两个关键字。始终意味着不能手动输入,AS DEFAULT 意味着它将自动输入,除非您指定数字。根据 Apache 规范
通过
INSERT
现在删除原来的contacts表
将contactsTemp重命名为contacts
I had the same problem, and as far as I can tell, there is no way to create a new primary key to an existing Apache Derby database. Derby does not allow you to add a column with GENERATED as an attribute.
Here's the method I found has worked the best.
Based on the original table:
Which looks like:
Create a new table with the same structure, in addition to having the primary key.
Breaking down the primary_key field. The first two are standard SQL keywords. The key will be an integer, and can't be null. Apache DB uses the keyword generated as their AUTO_GENERATED. There's two keywords that can follow generated ALWAYS and AS DEFAULT. Always means that it can't be manually entered, AS DEFAULT means it will be auto, unless you specify a number. Per the Apache spec
Copy data into new table via a
INSERT
Now remove the original contacts table
Rename contactsTemp to contacts
这已在 Derby 的问题跟踪器 (Jira) 中记录为 DERBY-3888。对此问题发表评论和/或投票可能会增加在下一个版本中修复该问题的机会......
This has been logged as DERBY-3888 in Derby's issue tracker (Jira). Commenting and/or voting for this issue might increase the chance of it being fixed in the next release...