I have the following sql table called datatable
,
date yesteday today tomorrow reading source
2021-01-01 x x x x 1
2021-01-01 x x x x 2
2021-01-01 x x x x 3
...
2021-01-02 x x x x 1
2021-01-02 x x x x 2
2021-01-02 x x x x 3
...
2021-05-31 x x x x 1
2021-05-31 x x x x 2
2021-05-31 x x x x 3
When I created the table, I set the following,
create table datatable
(
date date,
yesterday real,
today real,
tomorrow real,
reading real,
source varchar
)
Then I created an index:
create index datatable_idx on datatable (date, source)
Every day, the source numbers will repeat themselves. So date together with source would be unique.
I want to improve my data collection and storing process. I was told that I should create a primary key.
I'm guessing the command to use here would be
ALTER TABLE datatable ADD PRIMARY KEY (date, source)
My question is why should I do this and what is the difference between this and the index I created. Will it affect my process?
Also next time I create a table, do I have to create an index and primary like this or is there a way to do both upon creation?