# 4.5: Solutions

## 4.1 Solution

```sql
CREATE TABLE Course (
    CourseId int PRIMARY KEY NOT NULL,
    Title varchar(50) NOT NULL,
    Credits int NOT NULL
);

CREATE TABLE Student (
    StudentId int PRIMARY KEY NOT NULL,
    LastName varchar(50) NOT NULL,
    FirstName varchar(25) NOT NULL,
    EnrollmentDate timestamp NULL
);

CREATE TABLE Enrollment (
    EnrollmentId int PRIMARY KEY NOT NULL,
    CourseID int REFERENCES Course (CourseId),
    StudentID int REFERENCES Student (StudentId),
    Grade varchar(25) NULL        
);
```

## 4.2 Solution

```sql
CREATE TABLE Library (
    LibraryId int PRIMARY KEY NOT NULL,
    LocationName varchar(50) NOT NULL
);

CREATE TABLE Member (
    MemberId int PRIMARY KEY NOT NULL,
    FirstName varchar(25) NOT NULL,
    LastName varchar(25) NOT NULL,
    LibraryId int REFERENCES Library (LibraryId)
);

CREATE TABLE Book (
    BookId int PRIMARY KEY NOT NULL,
    Title varchar(100) NOT NULL,
    Author varchar(50) NOT NULL,
    CheckOutDate timestamp NULL,
    DueDate timestamp NULL,
    MemberId int REFERENCES Member (MemberId) NULL,
    LibraryId int REFERENCES Library (LibraryId)
);
```

Here is the UML table diagram:\
&#x20;

![Solution 2](https://1712903417-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAU8YgF0bbt1vxBgc5B%2F-LAU8dTUP51jQtyZvnhQ%2F-LAU8o4pExXW-YIv0Idw%2F3.2_4.2solution.png?generation=1524162330074514\&alt=media)

## 4.3 Solution

```sql
CREATE TABLE Contact (
    ContactId int PRIMARY KEY NOT NULL,
    Phone varchar(10) NOT NULL,
    HomeAddress varchar(100) NOT NULL,
    Email varchar(50) NULL
);

CREATE TABLE Staff (
    StaffId int PRIMARY KEY NOT NULL,
    StaffName varchar(50) NOT NULL,
    Position varchar(25) NOT NULL,
    Salary money NOT NULL,
    ContactId int REFERENCES Contact (ContactId)
);

CREATE TABLE Owner (
    OwnerId int PRIMARY KEY NOT NULL,
    OwnerName varchar(50) NOT NULL,
    ContactId int REFERENCES Contact (ContactId),
    PetId int NOT NULL
);

CREATE TABLE Pet (
    PetId int PRIMARY KEY NOT NULL,
    PetName varchar(25) NOT NULL,
    AnimalType varchar(15) NOT NULL,
    Breed varchar(25) NOT NULL,
    Color varchar(25) NOT NULL,
    PetWeight int NULL,
    LastAppointment timestamp NULL,
    ServiceId int NULL,
    OwnerId int REFERENCES Owner (OwnerId)
);

CREATE TABLE Service (
    ServiceId int PRIMARY KEY NOT NULL,
    ServiceName varchar(25) NOT NULL,
    Cost money NOT NULL,
    PetID int REFERENCES Pet (PetId) NULL        
);
```
