Reaves.dev

v0.1.0

built using

Phoenix v1.7.12

Db Design

Stephen M. Reaves

::

2023-06-18

Notes about Lesson 7 of CS-6400

Summary

Mapping EER

EER

GruRegular Userruudru--ruuaAttendsru--aNrueEmploymentru--rueNuUsersSchoolssts--sstNsnSchool Names--snstSchool typetnType Namest--tneEmployerruu--ua--sMygYear Graduateda--ygsst--st1rue--eMjtJob Titlerue--jt

Mappings

GruRegular UserEmailBirthdateSexCurrentCityHomeTownsSchoolSchoolNameTypestSchoolTypeTypeNames:t->st:tempEmployerEmployeeNameaAttendEmailSchoolNameYearGraduateda:e->ru:ea:n->s:nemptEmploymentEmailEmployeeNameJobTitleempt:e->ru:eempt:n->emp:e

Super/Sub type

EER

GuUserruRegular Userruudru--ruuauAdminUserau--ruuruu--ueEmaile--upPasswordp--unNamen--ufnFirstNamefn--nlnLastNameln--niInterestsi--rusSexs--rubdBirthDatebd--ruccCurrentCitycc--ruhtHomeTownht--rullLastLoginll--au

Mapping

GuUserEmailFirstNameLastNamePasswordauAdmin UserEmailLastLoginau:e->u:eruRegular UserEmailBirthDateSexCurrentCityHometownru:e->u:eiInterestsEmailInteresti:e->ru:e

Weak Entity Type

EER

GruRegular Userruudru--ruuaAcceptru--a1reqRequestru--req1uUserfFriendshipruu--ua--fNreq--fNrRelationshipr--fdcDate Connecteddc--f

Mapping

GruRegular UserEmailBirthdateSexCurrentCityHomeTownfFriendshipEmailFriendEmailDateConnectedRelationshipf:e->ru:ef:f->ru:e

SQL

EER

GuUserruRegular Userruudru--ruuauAdminUserau--ruuruu--ueEmaile--upPasswordp--unNamen--ufnFirstNamefn--nlnLastNameln--niInterestsi--rusSexs--rubdBirthDatebd--ruccCurrentCitycc--ruhtHomeTownht--rullLastLoginll--au

Mapping

GuUserEmailFirstNameLastNamePasswordauAdmin UserEmailLastLoginau:e->u:eruRegular UserEmailBirthDateSexCurrentCityHometownru:e->u:eiInterestsEmailInteresti:e->ru:e

Code

CREATE TABLE User(
  Email varchar(50) NOT NULL,
  FirstName varchar(50) NOT NULL,
  LastName varchar(50) NOT NULL,
  Password varchar(50) NOT NULL,
  PRIMARY KEY (EMAIL));

CREATE TABLE RegularUser(
  Email varchar(50) NOT NULL,
  Sex char(1) NULL,
  Birthdate datetime NULL,
  CurrentCity varchar(50) NULL,
  HomeTown varchar(50) NULL,
  PRIMARY KEY (Email),
  FOREIGN KEY (Email) REFERENCES `User` (Email));

CREATE TABLE AdminUser(
  Email varchar(50) NOT NULL,
  LastLogin datetime NULL,
  PRIMARY KEY (Email),
  FOREIGN KEY (Email) REFERENCES `User` (Email));

CREATE TABLE Interests(
  Email varchar(50) NOT NULL,
  Interest varchar(50) NOT NULL,
  PRIMARY KEY (Email, Interests),
  FOREIGN KEY (Email) REFERENCES RegularUser (Email));

More examples

GaAttendEmailSchoolNameYearGraduatedemptEmploymentEmailEmployeeNameJobTitle
CREATE TABLE Attend(
  Email varchar(50) NOT NULL,
  SchoolName varchar(50) NOT NULL,
  YearGraduated int NULL,
  UNIQUE (Email, SchoolName, YearGraduated), -- YearGraduated can be NULL so
                                             -- this can't be a key.
  FOREIGN KEY (Email) REFERENCES RegularUser (Email),
  FOREIGN KEY (SchoolName) REFERENCES School (SchoolName));

CREATE TABLE Employment(
  Email varchar(50) NOT NULL,
  EmployerName varchar(50) NOT NULL,
  JobTitle varchar(50) NOT NULL,
  UNIQUE (Email, EmployerName, JobTitle),
  FOREIGN KEY (Email) REFERENCES RegularUser (Email),
  FOREIGN KEY (EmployerName) REFERENCES Employer (EmployerName));

Frienship

GfFriendshipEmailFriendEmailDateConnectedRelationship
CREATE TABLE Friendship(
  Email varchar(50) NOT NULL,
  FriendEmail varchar(50) NOT NULL,
  Relationship varchar(50) NOT NULL,
  DateConnected datetime NULL,
  PRIMARY KEY (Email, FriendEmail),
  FOREIGN KEY (Email) REFERENCES RegularUser (Email),
  FOREIGN KEY (FriendEmail) REFERENCES RegularUser (Email));

Abstract Code with SQL

for each $interest
  INSERT INTO Interest (Email, Interest) VALUES ($Email, $Interest);
end for