START TRANSACTION;

USE $DB_NAME;

CREATE TABLE User (
    UserId int(10) unsigned NOT NULL AUTO_INCREMENT,
    Username varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL,
    Password varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL,
    Email varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
    JabberAddress varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
    IsAdmin BOOL NOT NULL DEFAULT 0,
    PRIMARY KEY (UserId),
    UNIQUE KEY Username (Username),
    UNIQUE KEY Email (Email),
    UNIQUE KEY JabberAddress (JabberAddress)
);

CREATE TABLE Fingerprint (
    FingerprintId int(10) unsigned NOT NULL AUTO_INCREMENT,
    Fingerprint varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
    UserId int(10) unsigned NOT NULL,
    PRIMARY KEY (FingerprintId),
    UNIQUE KEY Fingerprint (Fingerprint),
    KEY User (UserId),
    CONSTRAINT User FOREIGN KEY (UserId) REFERENCES User (UserId) ON DELETE CASCADE
);

CREATE TABLE Sharing (
    SharingId int(10) unsigned NOT NULL AUTO_INCREMENT,
    User int(10) unsigned NOT NULL,
    UserShared int(10) unsigned NOT NULL,
    PRIMARY KEY (SharingId),
    UNIQUE KEY User (User, UserShared),
    KEY UserSharedId (UserShared),
    CONSTRAINT UserId FOREIGN KEY (User) REFERENCES User (UserId) ON DELETE CASCADE,
    CONSTRAINT UserSharedId FOREIGN KEY (UserShared) REFERENCES User (UserId) ON DELETE CASCADE
);

COMMIT;