Foreign key with more than 1 value

0

I'm working on building my CBT and I've had a problem. I have a schedule table, and this table needs to store some data, among them, the activities that will be passed during the year. Due to this, I created the activities table, but there arises a problem: a schedule can have multiple activities, how can I store more than one activity in that schedule ?? The value is undefined, a timeline can have up to 47 activities, and I'd like to add them dynamically to the timeline. That is, the activities table should borrow more than one key at a time in the schedule table, how to do this ??

    
asked by anonymous 19.11.2017 / 23:58

1 answer

0

What you will do is a 1: n relationship, which means that a schedule will have n activities.

In practice, you need to add a foreign key in the atividades table by referencing the primary key of your cronograma .

Assuming your bank is already modeled, you can change the atividades table to have this foreign key as follows (rename as necessary):

-- Adicionando o campo para armazenar a chave estrangeira
ALTER TABLE 'atividades' ADD COLUMN 'cronograma_id' INTEGER NOT NULL;

-- Configurando sua chave estrangeira
ALTER TABLE 'atividades'
ADD CONSTRAINT
FOREIGN KEY(cronograma_id) -- o nome do campo que vai armazenar a chave
REFERENCES cronogramas(id); -- a referência da tabela e do campo da tabela que tem essa chave
    
20.11.2017 / 01:49