为现有的新插入值分配一个常量整数

时间:2015-12-08 14:11:10

标签: postgresql create-table sequence-generators

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

_____________________________
|   Col1  |  Col2  |  Col 3 |
|---------------------------|
|    a    |    a   |    1   |
|    b    |    a   |    1   |
|    c    |    a   |    1   |
|    u    |    b   |    2   |
|    v    |    b   |    2   |
|    w    |    b   |    2   |
-----------------------------

我要说我在Col1&amp ;;中插入另一个值(' d'' a') Col2分别,我希望Col3成为' 1'自动作为对应于' a'的Col3值。已经存在为' 1'并且如下所示

_____________________________
|   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   |
------------------------------

我有没有办法在“创建表格”中定义它?以便在将值插入Col1,Col2?

时发生Col3值更新

编辑:

场景是这样的

______________________________________________________________
|   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   |
--------------------------------------------------------------

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

______________________________________________________________
|   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中提供相应的值。

2 个答案:

答案 0 :(得分:3)

正常化它:

BOOST_GEOMETRY_REGISTER_POINT_2D(cv::Point2d, double, boost::geometry::cs::cartesian, x, y)

现在你的表是:

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

答案 1 :(得分:0)

  

Col3值必须是一个整数才能更快地与其他联接   表。

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

postgres=# select hashtext('Adobe');
 hashtext  
-----------
 173079840
(1 row)

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

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

但是你真的确定使用外键字符串会遇到性能问题吗?我想你应该对你的数据进行测试。 (还测试即将发布的9.5及其abbreviated keys功能。)

相关问题