-2
That one model is pretty flawed.
ObraandExemplarare related 1-to-1, and with that, the result is that they are effectively a single logical entity or thatExemplaris specialization ofLivro, and neither of those two things is what you wanted. It should be a 1-to-N relationship.The field
idobraofExemplarshould be FK, but not PK. A new fieldidexemplarwould be added there. Foreign key inEmprestimowould then beidexemplar.In
Obra, the fieldquantidadeis redundant. For example, to obtain the amount from a givenidobra, you would do that:SELECT COUNT(e.*) FROM Exemplar e WHERE e.idobra = :idobraI see no need to specialize
ObrainLivroand inPeriodico. It may be necessary further ahead, but at the moment, this is not bringing much gain no. Having the fieldsISBNandISSNinObrawith a check Constraint which prohibits both of them from beingNOT NULLsimultaneously 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
LivroandPeriodicoseparate (dispensed with this check Constraint), you doSELECTin 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
Devolucaois 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 thedataDevolucaoand theidfuncionarioreturn 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 NULLinEmprestimowith 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
MultaI also don’t see how it makes sense to be separated fromEmprestimo. The fieldidusuariois 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
CadastroforEndereco, forCadastrois a very generic name. Similarly, the fieldsidcadastroturn intoidendereco. Better yet, make the relationship N-to-N by adding the intermediate tablesFornecedor_Endereco,Usuario_EnderecoandFuncionario_Endereco, because they may have more than one address.Just like the table
Enderecowould be N-to-N withUsuario, withFornecedorand withFuncionario, you could also have a tableEmail(withFornecedor_Email,Usuario_EmailandFuncionario_Email) because users and vendors may have multiple emails as well and these are independent of the physical address. With this, the fieldemailis no longer on the tableCadastroEndereco.You can also have a table
Telefonewhich is N-to-N withFornecedor,UsuarioandFuncionario(Fornecedor_Telefone,Usuario_TelefoneandFuncionario_Telefone). The fieldtelefonecomes 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_ExemplarandEstado_Requisicaowith 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 fieldnumeroExemplaris there for nothing. The fieldidexemplaris 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