jeudi 2 juillet 2015

Insert records in table that is shared by two other tables (sqlite3 c++)

Introduction

I have the following three tables as illustrated in figure 1. The database that I am using is Sqlite3 on c++. I am really not satisfied with the architecture. I have no problem with the code. t I will be giving just a simple pseudo-code.


Architecture

  • An entity can have one to many literal values. But Literal can have to one and only one Entity
  • A CVT can have one to many iteral values. But Literal can have one and only one CVT.

An Entity is a thing or person like for example steve jobs. He was born in 1955 and was the co-founder of Apple. Was born and co-founder are the Properties. 1955 and Apple are the literals.

tables


Step 1

 int idliteral = 0;
 for(int identity = 0; identity < n; identity++)
 {
   for(auto &p:properties)
   {
       db <<"INSERT INTO LITERALS(ID, ID_ENTITY, ID_CVT, PROPERTY, Literal) VALUES(?, ?, ?, ?, ?);"
                 << idLiteral
                 << identity
                 << 0
                 << p.first
                 << p.second;
                 idLiteral++;
   }
 }


Step 2

int id_cvt = 0;
idLiteral = MAX(ID) from table Literal (Don't want to have duplicates)
for(auto &c:cvt)
{
   db << "INSERT INTO CVT(IDentifiant, ID_FREEBASE, type) VALUES(?, ?, ?);"
          << idcvt
          << c.first   
          << c.second;  
   db <<"INSERT INTO LITERALS(ID, ID_ENTITY, ID_CVT, PROPERTY, Literal) VALUES(?, ?, ?, ?, ?);"
                 << idLiteral
                 << 0
                 << idcvt
                 << "test"
                 << "test";
                 idLiteral++;
                 idcvt++;
   }
}


Problem

As you can see in both steps when the foreign key identity is not null idCVT is. When the foreign keyidCVT is not null identity is. I am really not satisfied with the architecture. Is there a better way to represent these tables. Can I use link tables? to solve the problem?

  • When I am inserting records in Literal, id_cvt attribute will be equal to 0
  • When I am inserting records in CVT. I have to also insert some records in Literal, therefore id_entity will be equal to 0 and id_cvt will de equal to identifiant of table CVT

Aucun commentaire:

Enregistrer un commentaire