3

i'm trying and success to create primary key in 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

but it allows me to upload duplicated value ,

as far as i know primary key should contain unique values ,

did i do something wrong?

Eran Levi
  • 33
  • 4
  • Can you show a full snippet of that works (although it's supposed to fail)? A primary key should indeed prohibit duplicate values. – Mureinik Feb 23 '17 at 10:47

2 Answers2

1

you can find your answer here

How to create an Index in Amazon Redshift

one of the answers mention your problem with the primary key

Community
  • 1
  • 1
user3600910
  • 2,839
  • 4
  • 22
  • 36
0

Create an identity key which will act as an auto_increment surrogate key. This'll serve both the purpose - to uniquely identify the records in the table and prevent insertion of duplicate values.

Let's create a dummy table:

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

The id column acts as a primary key. Deleting any record from the table does not impact the sequencing of other values and the id column can be used to uniquely identify the subsequent row.

enter image description here

Yusuf Hassan
  • 1,933
  • 1
  • 12
  • 21