将主键字段添加到现有 Derby 表

发布于 2024-10-17 14:16:14 字数 336 浏览 5 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泅渡 2024-10-24 14:16:14

我遇到了同样的问题,据我所知,无法为现有 Apache Derby 数据库创建新的主键。 Derby 不允许您添加具有 GENERATED 作为属性的列。

这是我发现效果最好的方法。


基于原始表:

CREATE TABLE contacts (firstname varchar(50), lastname varchar(50), 
address varchar(50), phone1 varchar(14), phone2 varchar(14) );

看起来像:

firstname     lastname           address        phone1          phone2
rich          bush               27 oak hill dr 11932035551234  11932035551234

除了具有主键之外,创建一个具有相同结构的新表。

     CREATE TABLE contactsTemp 
 (primary_key INT not null GENERATED ALWAYS as identity, 
 firstname varchar(50), lastname varchar(50), 
 address varchar(50), phone1 varchar(14), phone2 varchar(14)) 

分解primary_key字段。前两个是标准 SQL 关键字。键将是一个整数,并且不能为空。 Apache DB 使用生成的关键字作为其 AUTO_GENERATED。 generated ALWAYS 和 AS DEFAULT 后面可以跟随两个关键字。始终意味着不能手动输入,AS DEFAULT 意味着它将自动输入,除非您指定数字。根据 Apache 规范

通过 INSERT

insert into contactsTemp (firstname, lastname, address , phone1 , phone2 ) 
SELECT firstname, lastname, address , phone1 , phone2 from contacts

现在删除原来的contacts表

DROP TABLE contacts

将contactsTemp重命名为contacts

RENAME TABLE contactstemp TO 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:

CREATE TABLE contacts (firstname varchar(50), lastname varchar(50), 
address varchar(50), phone1 varchar(14), phone2 varchar(14) );

Which looks like:

firstname     lastname           address        phone1          phone2
rich          bush               27 oak hill dr 11932035551234  11932035551234

Create a new table with the same structure, in addition to having the primary key.

     CREATE TABLE contactsTemp 
 (primary_key INT not null GENERATED ALWAYS as identity, 
 firstname varchar(50), lastname varchar(50), 
 address varchar(50), phone1 varchar(14), phone2 varchar(14)) 

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

insert into contactsTemp (firstname, lastname, address , phone1 , phone2 ) 
SELECT firstname, lastname, address , phone1 , phone2 from contacts

Now remove the original contacts table

DROP TABLE contacts

Rename contactsTemp to contacts

RENAME TABLE contactstemp TO contacts
相守太难 2024-10-24 14:16:14

这已在 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文