Correct organization of the database structure and selection

androiddatabase-designselectsqlite

Sorry for newbie question.

For example, there is some kind of abstract device, we will call the Device class and the tasks for it: Task1, Task2, Task3.
For concreteness, I will describe everything in java codes.

class Device {
long id;
String name;
int ip;
int filed1;
String filed2;
// and so on
}

class Task1 {
long id;
long device_id; // id from Device class
int type = 1;
int property1;
char property2;
byte property3;
// Properties and their number are completely different in all Task, that is, the tasks are completely different.
}

class Task2 {
long id;
long device_id; // id from Device class
int type = 2;
String anotherProperty1;
float anotherProperty2;
long anotherProperty3;
short anotherProperty4;
double anotherProperty5;
int anotherProperty6;
// Properties and their number are completely different in all Task, that is, the tasks are completely different.
}

class Task3 {
long id;
long device_id; // id from Device class
int type = 3;
Charsequence yetAnotherProperty1;
AnotherType yetAnotherProperty2;
// Properties and their number are completely different in all Task, that is, the tasks are completely different.
}

Further, for each device a certain number of tasks are assigned, both with different classes and with the same ones. For example.

// pseudocode
Device device1 = new Device (.....);
// The list of tasks for the device 1 (Any combination, any number)
1. new Task2
2. new Task2
3. new Task1
4. new Task3

Device device2 = new Device (.....);
// The list of tasks for the device 2 (Any combination, any number)
1. new Task1
2. new Task3

Device device3 = new Device (.....);
// The list of tasks for the device 3 (Any combination, any number)
1. new Task1
2. new Task3
3. new Task1
4. new Task3
5. new Task1
6. new Task3
7. new Task1
8. new Task3
9. new Task1
10. new Task3
// and so on

I created the Device table with all the required fields, but I do not know how to save all this correctly in the database.
I will assume that:

  • is need to create more tables: Task1, Task2, Task3?

  • (when saving) to the specific tables put the all tasks, Device.id which corresponds to Task.device_id and the order of their execution?

  • (when load) do a query from all the tables Device, Task1, Task2, Task3 – and check from each task whether Device.id = Task1.device_id AND Device.id = Task2.device_id AND Device.id = Task3.device_id?

First, is the correct assumption, is the organization of the database structure correct or ….?

If yes, how correctly to do the optimized selection from several tables, write sql request if it is possible ..

I'm working with sqlite

Best Answer

I think what you want is either an

  • Entity-Value Table
  • Schemaless column

You can do what you want in PostgreSQL simply with JSONB (a binary and indexable store for json).

CREATE TABLE device (
  device_id  int   GENERATED BY DEFAULT AS IDENTITY,
  name       text
);
CREATE TABLE task (
  task_id    int   GENERATED BY DEFAULT AS IDENTITY,
  device_id  int   REFERENCES device,
  type       int,
  properties jsonb
);
CREATE INDEX ON task(device_id, type);

INSERT INTO device (name) VALUES ('foobar')
INSERT INTO task (device_id, type, properties) VALUES
  (1, 1, '{"property2": "foo", "property3": "foobar"}'),
  (1, 2, '{"another property": "qux", "another property2": "quux"}')
;