{"id":1209,"date":"2023-04-13T15:07:55","date_gmt":"2023-04-13T14:07:55","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=1209"},"modified":"2023-04-18T13:18:49","modified_gmt":"2023-04-18T12:18:49","slug":"databases","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/data-modelling\/databases\/","title":{"rendered":"Databases"},"content":{"rendered":"\n<p>The following notes are to assist you if your work placement does not provide their own instruction. Continue to do your own research and ask colleagues in the work place for advice. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Flat file databases<\/h2>\n\n\n\n<p>A flat file database is simply a table of data that stores all the information about a given object in a row.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"185\" src=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13150923\/DB1-1024x185-1.png\" alt=\"Example flat file database table\" class=\"wp-image-1213\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13150923\/DB1-1024x185-1.png 1024w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13150923\/DB1-1024x185-1-300x54.png 300w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13150923\/DB1-1024x185-1-768x139.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Example table<\/figcaption><\/figure><\/div>\n\n\n\n<p>You can see that some information has been added in multiple places. This will increase the size of the database and can lead to the following issues:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Insert anomalies<\/li><li>Delete anomalies<\/li><li>Update anomalies<\/li><\/ul>\n\n\n\n<p>An insert anomaly is where you can\u2019t add new information to the table because you are lacking part of the row. In the above example you can\u2019t add a new department until you have the teacher\u2019s details.<\/p>\n\n\n\n<p>A delete anomaly is where deleting the information about a given object also removes all the information about a related object. In the above example Marie Macleod could retire. But if we remove her entry then we lose all the data about the maths department.<\/p>\n\n\n\n<p>An update anomaly happens when you update a row with new information but fail to update a row that shares the information. In the above example we could update the telephone number for the English department on Donal Morrison\u2019s row but forget to make the update to Julie Stevens\u2019. Now there are two telephone numbers listed for the English department.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Relational databases<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>AKA Linked Databases<\/li><li><strong>R<\/strong>elational&nbsp;<strong>D<\/strong>ata<strong>B<\/strong>ase&nbsp;<strong>M<\/strong>anagement&nbsp;<strong>S<\/strong>ystem(<strong>RDBMS<\/strong>)<\/li><li>MS Access, MySQL, MariaSQL, Microsoft SQL Server, Oracle<\/li><\/ul>\n\n\n\n<p>A relational database splits the data into multiple tables. The aim being to only have data about an object stored in a single place. These data objects are then linked with&nbsp;<strong>relations<\/strong>.<\/p>\n\n\n\n<p>The data in the previous example can be split into two tables. One stores the data about the department, and one stores the data about the teacher including an identifier stating in which department they work.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"869\" height=\"256\" src=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13151248\/DB5.png\" alt=\"Teacher table\" class=\"wp-image-1215\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13151248\/DB5.png 869w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13151248\/DB5-300x88.png 300w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13151248\/DB5-768x226.png 768w\" sizes=\"auto, (max-width: 869px) 100vw, 869px\" \/><figcaption>Teacher table<\/figcaption><\/figure><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"715\" height=\"189\" src=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13151306\/DB6.png\" alt=\"Department table\" class=\"wp-image-1216\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13151306\/DB6.png 715w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13151306\/DB6-300x79.png 300w\" sizes=\"auto, (max-width: 715px) 100vw, 715px\" \/><figcaption>Department table<\/figcaption><\/figure><\/div>\n<\/div>\n<\/div>\n\n\n\n<p>We\u2019ve now split the data into two tables.<\/p>\n\n\n\n<p>Each table contains data about a single object type: Teachers and Departments.<\/p>\n\n\n\n<p>By assigning a Department ID to each teacher we can look up all the information about that department.<\/p>\n\n\n\n<p>This has fixed all the anomalies. Any change should now only have to alter a single row.<\/p>\n\n\n\n<p>In an RDBMS an entity set is represented by a table.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Entities (rows)<\/h2>\n\n\n\n<p>In an RDBMS an entity is represented as a row in a table. A row stores all the data about a given object. This row of data can also be called an&nbsp;<strong>Entity<\/strong>. An entity stores information about an object or something we want to record.<\/p>\n\n\n\n<p>An entity is made up of multiple attributes or fields.<\/p>\n\n\n\n<p>An entity should only store data relevant to that object or a key to relate it to another table.<\/p>\n\n\n\n<p>For example a garage might make a database for their customer details. But it would not make sense to store the vehicle details in the same table as the customer details. A customer might have multiple vehicles.<\/p>\n\n\n\n<p><strong>Strong entities<\/strong>&nbsp;\u2013 An entity that has an independent existence, i.e. doesn\u2019t rely on another entity.<\/p>\n\n\n\n<p><strong>Weak entities<\/strong>&nbsp;\u2013 An entity that relies on another entity to exist.<\/p>\n\n\n\n<p>Example. A table containing staff details could hold strong entities, but a table containing next of kin details would be weak as the entities within are only needed based on a staff entity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Attributes<\/h2>\n\n\n\n<p><strong>Entities<\/strong>&nbsp;are made up of&nbsp;<strong>attributes<\/strong>. These store the individual properties of data about an entity.<\/p>\n\n\n\n<p>There are various types of attribute:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Simple attribute \u2013 These attributes are atomic. We cannot break them down further. e.g \u2013 id, first_name, etc.<\/li><li>Composite attribute \u2013 These attributes are a collection of simple attributes. e.g. \u2013 Name can consist of first_name, middle_name and last_name.<\/li><li>Derived attribute \u2013 These attributes are not available in the database, but we can find them using other attributes. e.g. \u2013 We can derive age using the date_of_birth.<\/li><li>Single-valued attribute \u2013 These attributes only have a single value. e.g.- student_id<\/li><li>Multi-valued attribute \u2013 These attributes can have multiple values. e.g. \u2013 email, phone_number<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Domains<\/h3>\n\n\n\n<p>Domain refers to a set of permitted values we can assign to an attribute.<\/p>\n\n\n\n<p>For example, age must be a positive integer. Or we could have a colours attribute that can only be one of the values: \u201cRed\u201d, \u201cBlue\u201d, \u201cYellow\u201d or \u201cGreen\u201d.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Data types<\/h2>\n\n\n\n<p>Databases can store different types of data in a field.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Text field<\/strong>&nbsp;\u2013 Stores words, numbers, special characters etc<\/li><li><strong>Number field&nbsp;<\/strong>\u2013 Stores integers, floats etc<\/li><li><strong>Date field<\/strong>&nbsp;\u2013 Stores dates<\/li><li><strong>Time field<\/strong>&nbsp;\u2013 Stores times<\/li><li><strong>Date\/Time fields<\/strong>&nbsp;\u2013 Some databases will use a single type for both Date and Time<\/li><li><strong>Boolean<\/strong>&nbsp;\u2013 Stores a True or False value.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Relationships<\/h2>\n\n\n\n<p>Remember how we\u2019re dealing with&nbsp;<strong>R<\/strong>elational&nbsp;<strong>D<\/strong>ata<strong>B<\/strong>ase&nbsp;<strong>M<\/strong>anagement&nbsp;<strong>S<\/strong>ystem(<strong>RDBMS<\/strong>)? So what are these relationships?<\/p>\n\n\n\n<p>Relationships are how tables are linked to each other. How an entity in one table might rely on an entity (or entities) in another table.<\/p>\n\n\n\n<p>Relationships can be described with 3 constraints: Degree, Cardinality and Participation<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Degree<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Unary<\/h4>\n\n\n\n<p>A unary relationship occurs when the table has a relationship with itself.<\/p>\n\n\n\n<p>Example \u2013 A staff table could have a supervisor column that refers to another member of staff.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Binary<\/h4>\n\n\n\n<p>The most common relationship is where one entity has a relation with another.<\/p>\n\n\n\n<p>Example \u2013 A staff table has a binary relation with a next of kin table.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Ternary<\/h4>\n\n\n\n<p>In a ternary relationship there are 3 entities all relating to one another.<\/p>\n\n\n\n<p>Example \u2013 A staff table is in a ternary relationship with a department and manager table.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">N-ary<\/h4>\n\n\n\n<p>In a n-ary relationship there are N entities all relating to one another. N-ary relationships are thankfully uncommon.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Cardinality<\/h3>\n\n\n\n<p>Relationships between two tables can be categorised into one of the following:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">One-to-one<\/h4>\n\n\n\n<p>Example \u2013 A member of staff must have a single next of kin record<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">One-to-many<\/h4>\n\n\n\n<p>Example \u2013 A member of staff might have multiple pieces of company equipment assigned to them<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Many-to-many<\/h4>\n\n\n\n<p>Example \u2013 Members of staff might be on multiple training courses.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Participation<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Mandatory (full)<\/h4>\n\n\n\n<p>In a mandatory relation, all the entities must exist and be related.<\/p>\n\n\n\n<p>Example \u2013 all employee entities must have a contact details entity linked to them.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Optional (partial)<\/h4>\n\n\n\n<p>In an optional relation, the entities and relations exist as needed.<\/p>\n\n\n\n<p>Example \u2013 employee entities might have one or more company equipment entities linked to them.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Keys<\/h2>\n\n\n\n<p>Keys are used to identify entities.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Primary keys<\/h3>\n\n\n\n<p>A primary key is a field that can uniquely identify an entity in a table.<\/p>\n\n\n\n<p>Usually it will be an id such as a number that automatically increments each time an entity is added to the table.<\/p>\n\n\n\n<p>But if unique data exists within the table you can use that as the primary key. For example a table that has a National Insurance number or a student ID in a school could use that as a primary key.<\/p>\n\n\n\n<p>The Entity Integrity rule states that every table must have a primary key column containing values that are unique and not null.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Surrogate key<\/h3>\n\n\n\n<p>A surrogate key is just when a field is created specifically to be a key. In other words there was no naturally occurring unique data in that table.<\/p>\n\n\n\n<p>So an auto incrementing number added just to uniquely identify an entity is both a surrogate and primary key.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Composite key<\/h3>\n\n\n\n<p>A composite key is when two or more fields are used in conjunction to uniquely identify an entity.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"297\" src=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13151853\/DB7-1024x297-1.png\" alt=\"Table without a primary or surrogate key\" class=\"wp-image-1218\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13151853\/DB7-1024x297-1.png 1024w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13151853\/DB7-1024x297-1-300x87.png 300w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13151853\/DB7-1024x297-1-768x223.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Table without a primary or surrogate key<\/figcaption><\/figure><\/div>\n\n\n\n<p>We can\u2019t use either the subject or teacher to find the room as each subject and teach occurs multiple times. But we can use a composite made from the subject and teacher as a key.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Foreign keys<\/h3>\n\n\n\n<p>Foreign keys are attributes that contain the primary key of another table. They are used to identify a relationship between the tables.<\/p>\n\n\n\n<p>For example a next of kin table might have a column called employee_id which stores the id from the employee table where it is a primary key. In the next of kin table this employee_id column is a foreign key.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Compound keys<\/h3>\n\n\n\n<p>Compound keys are similar to composite keys in that they use two or more fields together to make a unique identifier.<\/p>\n\n\n\n<p>Compound keys use foreign key attributes to form the key for this table.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"461\" height=\"149\" src=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13152121\/DB8-e1652974936766.png\" alt=\"Compound key example from BBC Bitesize\" class=\"wp-image-1219\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13152121\/DB8-e1652974936766.png 461w, https:\/\/blogs.glowscotland.org.uk\/es\/public\/software\/uploads\/sites\/4063\/2023\/04\/13152121\/DB8-e1652974936766-300x97.png 300w\" sizes=\"auto, (max-width: 461px) 100vw, 461px\" \/><figcaption>Compound key example from BBC Bitesize<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/data-modelling\/database-analysis\/\" data-type=\"page\" data-id=\"1221\">Next: Database analysis<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following notes are to assist you if your work placement does not provide their own instruction. Continue to do your own research and ask colleagues in the work place for advice. Flat file databases A flat file database is simply a table of data that stores all the information about a given object in&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/data-modelling\/databases\/\">Continue reading <span class=\"screen-reader-text\">Databases<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":1179,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1209","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/1209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/users\/5710"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/comments?post=1209"}],"version-history":[{"count":5,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/1209\/revisions"}],"predecessor-version":[{"id":1324,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/1209\/revisions\/1324"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/1179"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/media?parent=1209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}