-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvehicle-registration-schema.sql
62 lines (56 loc) · 2.03 KB
/
vehicle-registration-schema.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
CREATE TABLE VehicleRegistration (
Id varchar(22) primary key,
State varchar(2) not null,
City varchar(255) not null,
PendingPenaltyTicketAmount decimal(5, 2),
ValidFromDate datetime not null,
ValidToDate datetime not null
);
CREATE TABLE Person (
Id varchar(22) primary key,
FirstName varchar(255) not null,
LastName varchar(255) not null,
DOB datetime not null,
GovId varchar(255) not null,
GovIdType varchar(255) not null,
Address varchar(2000) not null
);
CREATE INDEX GovIdIdx ON Person (GovId);
CREATE TABLE VehicleOwnership (
VehicleRegistrationIdFk varchar(22) not null,
PersonIdFk varchar(22) not null,
IsPrimaryOwner boolean,
FOREIGN KEY (VehicleRegistrationIdFk) REFERENCES VehicleRegistration(Id) ON DELETE CASCADE,
FOREIGN KEY (PersonIdFk) REFERENCES Person(Id) ON DELETE CASCADE,
PRIMARY KEY (VehicleRegistrationIdFk, PersonIdFk)
);
CREATE TABLE DriversLicense (
Id varchar(22) primary key,
LicenseType varchar(255) not null,
ValidFromDate datetime not null,
ValidToDate datetime not null,
PersonIdFk varchar(22),
FOREIGN KEY (PersonIdFk) REFERENCES Person(Id) ON DELETE CASCADE
);
CREATE TABLE Vehicle (
Id varchar(22) primary key,
VehicleType varchar(255) not null,
Year int not null,
Make varchar(255) not null,
Model varchar(255) not null,
Color varchar(255) not null
);
CREATE TABLE VinMapping (
VIN varchar(255) primary key,
VehicleIdFk varchar(22),
VehicleRegistrationIdFk varchar(22),
FOREIGN KEY (VehicleIdFk) REFERENCES Vehicle(Id) ON DELETE RESTRICT,
FOREIGN KEY (VehicleRegistrationIdFk) REFERENCES VehicleRegistration(Id) ON DELETE RESTRICT
);
CREATE TABLE LicensePlateNumberMapping (
LicensePlateNumber varchar(255) primary key,
VehicleRegistrationIdFk varchar(22),
DriversLicenseIdFk varchar(22),
FOREIGN KEY (VehicleRegistrationIdFk) REFERENCES VehicleRegistration(Id) ON DELETE RESTRICT,
FOREIGN KEY (DriversLicenseIdFk) REFERENCES DriversLicense(Id) ON DELETE RESTRICT
);