schema_mig_to_1.sql 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. --
  2. -- Copyright (c) 2014-2015, Marcus Rohrmoser mobile Software, http://mro.name/me
  3. -- All rights reserved.
  4. --
  5. -- Redistribution and use in source and binary forms, with or without modification, are permitted
  6. -- provided that the following conditions are met:
  7. --
  8. -- 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  9. -- and the following disclaimer.
  10. --
  11. -- 2. The software must not be used for military or intelligence or related purposes nor
  12. -- anything that's in conflict with human rights as declared in http://www.un.org/en/documents/udhr/ .
  13. --
  14. -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  15. -- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16. -- FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  17. -- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. -- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19. -- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  20. -- IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  21. -- THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. --
  23. PRAGMA foreign_keys = ON;
  24. PRAGMA recursive_triggers = ON;
  25. PRAGMA encoding = 'UTF-8';
  26. -- URIs for subjects and objects
  27. CREATE TABLE so_uris (
  28. id INTEGER PRIMARY KEY
  29. ,uri TEXT NOT NULL -- UNIQUE -- redundant constraint (hash should do), could be dropped to save space
  30. );
  31. -- blank node IDs for subjects and objects
  32. CREATE TABLE so_blanks (
  33. id INTEGER PRIMARY KEY
  34. ,blank TEXT NOT NULL -- UNIQUE -- redundant constraint (hash should do), could be dropped to save space
  35. );
  36. -- URIs for predicates
  37. CREATE TABLE p_uris (
  38. id INTEGER PRIMARY KEY
  39. ,uri TEXT NOT NULL -- UNIQUE -- redundant constraint (hash should do), could be dropped to save space
  40. );
  41. -- URIs for literal types
  42. CREATE TABLE t_uris (
  43. id INTEGER PRIMARY KEY
  44. ,uri TEXT NOT NULL -- UNIQUE -- redundant constraint (hash should do), could be dropped to save space
  45. );
  46. -- literal values
  47. CREATE TABLE o_literals (
  48. id INTEGER PRIMARY KEY
  49. ,datatype_id INTEGER NULL REFERENCES t_uris(id)
  50. ,language TEXT NULL
  51. ,text TEXT NOT NULL
  52. );
  53. -- CREATE UNIQUE INDEX o_literals_index ON o_literals (text,language,datatype_id); -- redundant constraint (hash should do), could be dropped to save space
  54. -- URIs for context
  55. CREATE TABLE c_uris (
  56. id INTEGER PRIMARY KEY
  57. ,uri TEXT NOT NULL -- UNIQUE -- redundant constraint (hash should do), could be dropped to save space
  58. );
  59. CREATE TABLE triple_relations (
  60. id INTEGER PRIMARY KEY
  61. ,s_uri_id INTEGER NULL REFERENCES so_uris(id)
  62. ,s_blank_id INTEGER NULL REFERENCES so_blanks(id)
  63. ,p_uri_id INTEGER NOT NULL REFERENCES p_uris(id)
  64. ,o_uri_id INTEGER NULL REFERENCES so_uris(id)
  65. ,o_blank_id INTEGER NULL REFERENCES so_blanks(id)
  66. ,o_lit_id INTEGER NULL REFERENCES o_literals(id)
  67. ,c_uri_id INTEGER NULL REFERENCES c_uris(id)
  68. , CONSTRAINT null_subject CHECK ( -- ensure uri/blank are mutually exclusive
  69. (s_uri_id IS NOT NULL AND s_blank_id IS NULL) OR
  70. (s_uri_id IS NULL AND s_blank_id IS NOT NULL)
  71. )
  72. , CONSTRAINT null_object CHECK ( -- ensure uri/blank/literal are mutually exclusive
  73. (o_uri_id IS NOT NULL AND o_blank_id IS NULL AND o_lit_id IS NULL) OR
  74. (o_uri_id IS NULL AND o_blank_id IS NOT NULL AND o_lit_id IS NULL) OR
  75. (o_uri_id IS NULL AND o_blank_id IS NULL AND o_lit_id IS NOT NULL)
  76. )
  77. );
  78. -- redundant constraint (hash should do), could be dropped to save space:
  79. -- CREATE UNIQUE INDEX triple_relations_index ON triple_relations(s_uri_id,s_blank_id,p_uri_id,o_uri_id,o_blank_id,o_lit_id,c_uri_id);
  80. -- optional indexes for lookup performance, mostly on DELETE.
  81. CREATE INDEX triple_relations_index_s_uri_id ON triple_relations(s_uri_id); -- WHERE s_uri_id IS NOT NULL;
  82. CREATE INDEX triple_relations_index_s_blank_id ON triple_relations(s_blank_id); -- WHERE s_blank_id IS NOT NULL;
  83. CREATE INDEX triple_relations_index_p_uri_id ON triple_relations(p_uri_id); -- WHERE p_uri_id IS NOT NULL;
  84. CREATE INDEX triple_relations_index_o_uri_id ON triple_relations(o_uri_id); -- WHERE o_uri_id IS NOT NULL;
  85. CREATE INDEX triple_relations_index_o_blank_id ON triple_relations(o_blank_id); -- WHERE s_blank_id IS NOT NULL;
  86. CREATE INDEX triple_relations_index_o_lit_id ON triple_relations(o_lit_id); -- WHERE o_lit_id IS NOT NULL;
  87. CREATE INDEX o_literals_index_datatype_id ON o_literals(datatype_id); -- WHERE datatype_id IS NOT NULL;
  88. -- continue DB schema setup in next migration to avoid
  89. -- : warning: string length ‘7405’ is greater than the length ‘4095’ ISO C99 compilers are required to support [-Woverlength-strings]
  90. PRAGMA user_version=1;