Observe the following scenario:
create table pktest1(id int); create table pktest2 like pktest1; insert into pktest1 values (1),(1),(2),(2),(3),(3),(1); insert into pktest2 select * from pktest1;
alter table pktest1 add primary key(id); alter ignore table pktest2 add primary key(id);
Which of the statements below are true?
(Find all correct answers)
a) The ALTER on pktest1 will fail. b) The ALTER on pktest2 will fail. c) The ALTER on pktest2 will remove duplicates. d) The ALTER on pktest1 will remove duplicates.
[ Tables and Indexes (15%) - Altering Tables ]
Answers:
a c
In general, you can only add a constraint to a table, if the existing data does not violate the constraint. However, with the IGNORE option, any duplicates will be removed while adding primary or unique keys (very useful!).