-2
That one model is pretty flawed.
Obra
andExemplar
are related 1-to-1, and with that, the result is that they are effectively a single logical entity or thatExemplar
is specialization ofLivro
, and neither of those two things is what you wanted. It should be a 1-to-N relationship.The field
idobra
ofExemplar
should be FK, but not PK. A new fieldidexemplar
would be added there. Foreign key inEmprestimo
would then beidexemplar
.In
Obra
, the fieldquantidade
is redundant. For example, to obtain the amount from a givenidobra
, you would do that:SELECT COUNT(e.*) FROM Exemplar e WHERE e.idobra = :idobra
I see no need to specialize
Obra
inLivro
and inPeriodico
. It may be necessary further ahead, but at the moment, this is not bringing much gain no. Having the fieldsISBN
andISSN
inObra
with a check Constraint which prohibits both of them from beingNOT NULL
simultaneously can be better. This check Constraint that would be it:CONSTRAINT livro_coerente CHECK ((isbn IS NULL) <> (issn IS NULL))
However, if you prefer to keep
Livro
andPeriodico
separate (dispensed with this check Constraint), you doSELECT
in them so:-- Seleciona periódicos. SELECT p.issn, o.* FROM Periodico p INNER JOIN Obra o ON p.idobra = o.idobra -- Pode acrescentar um WHERE se quiser.
-- Seleciona livros. SELECT l.issn, o.* FROM Livro l INNER JOIN Obra o ON l.idobra = o.idobra -- Pode acrescentar um WHERE se quiser.
If you’d rather keep it together:
-- Seleciona periódicos. SELECT o.* FROM Obra o WHERE o.issn IS NOT NULL -- Pode acrescentar um AND se quiser.
-- Seleciona livros. SELECT o.* FROM Obra o WHERE o.isbn IS NOT NULL -- Pode acrescentar um AND se quiser.
I don’t see how
Devolucao
is something that makes sense to be separated fromEmprestimo
. A loan can only have a refund and a refund can only belong to a loan. Until I understand that this way, you guarantee that thedataDevolucao
and theidfuncionario
return cannot stay in a state where one is null and the other is not. However, this type of restriction would be easier to deal with check Constraint. My suggestion is to set the fielddataDevolucao DATE NULL
inEmprestimo
with the fieldsidFuncionarioEmprestimo INTEGER NOT NULL [FK]
andidFuncionarioDevolucao INTEGER NOT NULL [FK]
. To check Constraint would that be:CONSTRAINT devolucao_coerente CHECK ((dataDevolucao IS NULL) = (idFuncionarioDevolucao IS NULL))
The table
Multa
I also don’t see how it makes sense to be separated fromEmprestimo
. The fieldidusuario
is redundant. The check Constraint that would apply to this (after putting the fields back inEmprestimo
) it would be something like that:CONSTRAINT multa CHECK ( ( dataMulta IS NOT NULL AND motivoMulta IS NOT NULL AND valorMulta IS NOT NULL AND dataMulta > dataPrevistaRetorno AND dataDevolucao > dataPrevistaRetorno ) OR ( dataMulta IS NULL AND motivoMulta IS NULL AND valorMulta IS NULL ) )
Rename the table
Cadastro
forEndereco
, forCadastro
is a very generic name. Similarly, the fieldsidcadastro
turn intoidendereco
. Better yet, make the relationship N-to-N by adding the intermediate tablesFornecedor_Endereco
,Usuario_Endereco
andFuncionario_Endereco
, because they may have more than one address.Just like the table
Endereco
would be N-to-N withUsuario
, withFornecedor
and withFuncionario
, you could also have a tableEmail
(withFornecedor_Email
,Usuario_Email
andFuncionario_Email
) because users and vendors may have multiple emails as well and these are independent of the physical address. With this, the fieldemail
is no longer on the tableCadastro
Endereco
.You can also have a table
Telefone
which is N-to-N withFornecedor
,Usuario
andFuncionario
(Fornecedor_Telefone
,Usuario_Telefone
andFuncionario_Telefone
). The fieldtelefone
comes off the tableFornecedor
. On the tableTelefone
, you would have the area code (DDD), the number, the extension and maybe the DDI (in case it is in another country).A final recommendation would be to do what our friend Lacobus suggested in His answer to another question from you. You would have the tables
Sexo
,Motivo_Multa
,Situacao_Exemplar
andEstado_Requisicao
with fixed values and existing fields would be foreign keys to these tables. This approach causes far less headache than using check constraints to do this.Again in the table
Exemplar
, the fieldnumeroExemplar
is there for nothing. The fieldidexemplar
is already sufficient to identify only one copy.
Related question (not duplicate): https://answall.com/q/304502/132
– Victor Stafusa
Related question (not duplicate): https://answall.com/q/304828/132
– Victor Stafusa