2017-02-23 158 views
3

我試圖和成功的主鍵在Redshif沒有重複值

create table my_table(id int , 
    primary key(id)); 


insert into my_table values 
(id), 
(1), 
(1), 
(20); 

select count(*) from my_table 

3 

就創建主鍵,但它可以讓我上傳重複值,

,因爲我知道主鍵應該包含獨特值,

我做錯了什麼?

+0

你可以顯示一個完整的片段作品(儘管它應該失敗)?主鍵確實應該禁止重複的值。 – Mureinik

回答

0

您的問題創建一個identity key將作爲一個auto_increment代理鍵。這將起到兩個目的 - 唯一標識表中的記錄並防止插入重複值。

讓我們創建一個虛擬表:

create table scratchpad.test_1 
(id bigint identity(1,1), 
name varchar(10)); 

insert into scratchpad.test_1(name) 
values 
('a'),('b'),('c'),('d'); 

select * from scratchpad.test_1; 

enter image description here

id列充當primary key。從表中刪除任何記錄不會影響其他值的排序,並且id列可以用於唯一標識後續行。

enter image description here