首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将常量整数赋值给现有的新插入值

将常量整数赋值给现有的新插入值
EN

Stack Overflow用户
提问于 2015-12-08 14:11:09
回答 2查看 59关注 0票数 1

我正在寻找一个智能序列生成器,如果表中已经存在字符串列值,它将能够将常量int值赋值给列。方案如下:

代码语言:javascript
复制
_____________________________
|   Col1  |  Col2  |  Col 3 |
|---------------------------|
|    a    |    a   |    1   |
|    b    |    a   |    1   |
|    c    |    a   |    1   |
|    u    |    b   |    2   |
|    v    |    b   |    2   |
|    w    |    b   |    2   |
-----------------------------

假设我向Col1 & Col2插入另一个值('d','a'),我希望Col3自动变成'1‘,因为与'a’对应的Col3值已经以'1‘的形式存在,并变成如下所示

代码语言:javascript
复制
_____________________________
|   Col1  |  Col2  |  Col 3 |
|---------------------------|
|    a    |    a   |    1   |
|    b    |    a   |    1   |
|    c    |    a   |    1   |
|    u    |    b   |    2   |
|    v    |    b   |    2   |
|    w    |    b   |    2   |
|    d    |    a   |    1   |
------------------------------

我是否可以在“Create”中定义它,以便在将Col3值插入到Col1,Col2中时进行值更新?

编辑:

情况是这样的

代码语言:javascript
复制
______________________________________________________________
|   Col1                           |    Col2        |  Col 3 |
|------------------------------------------------------------|
|    Adobe                         |    Adobe       |    1   |
|    Adobe Systems                 |    Adobe       |    1   |
|    Adobe Systems Inc             |    Adobe       |    1   |
|    Honeywell                     |    Honeywell   |    2   |
|    Honeywell Inc                 |    Honeywell   |    2   |
|    Honeywell Inc.                |    Honeywell   |    2   |
--------------------------------------------------------------

当我添加新数据时,我希望它是

代码语言:javascript
复制
______________________________________________________________
|   Col1                           |    Col2        |  Col 3 |
|------------------------------------------------------------|
|    Adobe                         |    Adobe       |    1   |
|    Adobe Systems                 |    Adobe       |    1   |
|    Adobe Systems Inc             |    Adobe       |    1   |
|    Honeywell                     |    Honeywell   |    2   |
|    Honeywell Inc                 |    Honeywell   |    2   |
|    Honeywell Inc.                |    Honeywell   |    2   |
|    Adobe Systems Incorporated    |    Adobe       |    1   |
--------------------------------------------------------------

Col3值必须是一个整数,以便更快地与其他表连接。我将为Col1 & Col2插入值,在插入时,相应的值应该在Col3中可用。

EN

回答 2

Stack Overflow用户

发布于 2015-12-08 16:42:34

只是让它正常化:

代码语言:javascript
复制
create table corporation (
    corporation_id serial,
    short_name text
);

insert into corporation (short_name) values
('Adobe'),('Honeywell');

select * from corporation;
 corporation_id | short_name 
----------------+------------
              1 | Adobe
              2 | Honeywell

现在你的桌子是:

代码语言:javascript
复制
|    Adobe                         |    1
|    Adobe Systems                 |    1
|    Adobe Systems Inc             |    1
|    Honeywell                     |    2
|    Honeywell Inc                 |    2
|    Honeywell Inc.                |    2
|    Adobe Systems Incorporated    |    1
票数 3
EN

Stack Overflow用户

发布于 2015-12-08 15:35:59

Col3值必须是一个整数,以便更快地与其他表连接。

因此,您需要唯一的,但不一定是顺序的值。您可以使用哈希函数将字符串映射为唯一整数,例如:

代码语言:javascript
复制
postgres=# select hashtext('Adobe');
 hashtext  
-----------
 173079840
(1 row)

postgres=# select hashtext('Honeywell');
  hashtext  
------------
 -453308048
(1 row)

使用这样的函数,可以避免查找表中的现有值。

但是,您真的确定使用字符串作为外键会出现性能问题吗?我想你应该在你的数据上测试一下。(还将测试即将推出的9.5,以及它的缩略键功能。)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34158057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档