Buat kunci primer tambahan otomatis di postgresql, menggunakan urutan khusus:
Langkah 1, buat urutan Anda:
create sequence splog_adfarm_seq
start 1
increment 1
NO MAXVALUE
CACHE 1;
ALTER TABLE fact_stock_data_detail_seq
OWNER TO pgadmin;
Langkah 2, buat tabel Anda
CREATE TABLE splog_adfarm
(
splog_key INT unique not null,
splog_value VARCHAR(100) not null
);
Langkah 3, masukkan ke tabel Anda
insert into splog_adfarm values (
nextval('splog_adfarm_seq'),
'Is your family tree a directed acyclic graph?'
);
insert into splog_adfarm values (
nextval('splog_adfarm_seq'),
'Will the smart cookies catch the crumb? Find out now!'
);
Langkah 4, amati baris
el@defiant ~ $ psql -U pgadmin -d kurz_prod -c "select * from splog_adfarm"
splog_key | splog_value
----------+--------------------------------------------------------------------
1 | Is your family tree a directed acyclic graph?
2 | Will the smart cookies catch the crumb? Find out now!
(3 rows)
Dua baris memiliki kunci yang mulai dari 1 dan bertambah 1, seperti yang didefinisikan oleh urutan.
Bonus Elite ProTip:
Pemrogram benci mengetik, dan mengetik nextval('splog_adfarm_seq')itu mengganggu. Anda bisa mengetikkan DEFAULTparameter itu, seperti ini:
insert into splog_adfarm values (
DEFAULT,
'Sufficient intelligence to outwit a thimble.'
);
Agar hal di atas berfungsi, Anda harus menetapkan nilai default untuk kolom kunci pada tabel splog_adfarm. Yang lebih cantik.