-- Schema: spider__chinook_1
CREATE SCHEMA IF NOT EXISTS "spider__chinook_1";

-- Table: Album
CREATE TABLE "spider__chinook_1"."Album" (
    "AlbumId" BIGINT NOT NULL,
    "Title" TEXT NOT NULL,
    "ArtistId" BIGINT NOT NULL,
    CONSTRAINT "Album_pkey" PRIMARY KEY ("AlbumId")
);

-- Table: Artist
CREATE TABLE "spider__chinook_1"."Artist" (
    "ArtistId" BIGINT NOT NULL,
    "Name" TEXT,
    CONSTRAINT "Artist_pkey" PRIMARY KEY ("ArtistId")
);

-- Table: Customer
CREATE TABLE "spider__chinook_1"."Customer" (
    "CustomerId" BIGINT NOT NULL,
    "FirstName" TEXT NOT NULL,
    "LastName" TEXT NOT NULL,
    "Company" TEXT,
    "Address" TEXT,
    "City" TEXT,
    "State" TEXT,
    "Country" TEXT,
    "PostalCode" TEXT,
    "Phone" TEXT,
    "Fax" TEXT,
    "Email" TEXT NOT NULL,
    "SupportRepId" BIGINT,
    CONSTRAINT "Customer_pkey" PRIMARY KEY ("CustomerId")
);

-- Table: Employee
CREATE TABLE "spider__chinook_1"."Employee" (
    "EmployeeId" BIGINT NOT NULL,
    "LastName" TEXT NOT NULL,
    "FirstName" TEXT NOT NULL,
    "Title" TEXT,
    "ReportsTo" BIGINT,
    "BirthDate" TIMESTAMP,
    "HireDate" TIMESTAMP,
    "Address" TEXT,
    "City" TEXT,
    "State" TEXT,
    "Country" TEXT,
    "PostalCode" TEXT,
    "Phone" TEXT,
    "Fax" TEXT,
    "Email" TEXT,
    CONSTRAINT "Employee_pkey" PRIMARY KEY ("EmployeeId")
);

-- Table: Genre
CREATE TABLE "spider__chinook_1"."Genre" (
    "GenreId" BIGINT NOT NULL,
    "Name" TEXT,
    CONSTRAINT "Genre_pkey" PRIMARY KEY ("GenreId")
);

-- Table: Invoice
CREATE TABLE "spider__chinook_1"."Invoice" (
    "InvoiceId" BIGINT NOT NULL,
    "CustomerId" BIGINT NOT NULL,
    "InvoiceDate" TIMESTAMP NOT NULL,
    "BillingAddress" TEXT,
    "BillingCity" TEXT,
    "BillingState" TEXT,
    "BillingCountry" TEXT,
    "BillingPostalCode" TEXT,
    "Total" NUMERIC(10,2) NOT NULL,
    CONSTRAINT "Invoice_pkey" PRIMARY KEY ("InvoiceId")
);

-- Table: InvoiceLine
CREATE TABLE "spider__chinook_1"."InvoiceLine" (
    "InvoiceLineId" BIGINT NOT NULL,
    "InvoiceId" BIGINT NOT NULL,
    "TrackId" BIGINT NOT NULL,
    "UnitPrice" NUMERIC(10,2) NOT NULL,
    "Quantity" BIGINT NOT NULL,
    CONSTRAINT "InvoiceLine_pkey" PRIMARY KEY ("InvoiceLineId")
);

-- Table: MediaType
CREATE TABLE "spider__chinook_1"."MediaType" (
    "MediaTypeId" BIGINT NOT NULL,
    "Name" TEXT,
    CONSTRAINT "MediaType_pkey" PRIMARY KEY ("MediaTypeId")
);

-- Table: Playlist
CREATE TABLE "spider__chinook_1"."Playlist" (
    "PlaylistId" BIGINT NOT NULL,
    "Name" TEXT,
    CONSTRAINT "Playlist_pkey" PRIMARY KEY ("PlaylistId")
);

-- Table: PlaylistTrack
CREATE TABLE "spider__chinook_1"."PlaylistTrack" (
    "PlaylistId" BIGINT NOT NULL,
    "TrackId" BIGINT NOT NULL,
    CONSTRAINT "PlaylistTrack_pkey" PRIMARY KEY ("PlaylistId", "TrackId")
);

-- Table: Track
CREATE TABLE "spider__chinook_1"."Track" (
    "TrackId" BIGINT NOT NULL,
    "Name" TEXT NOT NULL,
    "AlbumId" BIGINT,
    "MediaTypeId" BIGINT NOT NULL,
    "GenreId" BIGINT,
    "Composer" TEXT,
    "Milliseconds" BIGINT NOT NULL,
    "Bytes" BIGINT,
    "UnitPrice" NUMERIC(10,2) NOT NULL,
    CONSTRAINT "Track_pkey" PRIMARY KEY ("TrackId")
);

-- Foreign Keys
ALTER TABLE "spider__chinook_1"."Album" ADD CONSTRAINT "FK_AlbumArtistId" FOREIGN KEY ("ArtistId") REFERENCES "spider__chinook_1"."Artist" ("ArtistId") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "spider__chinook_1"."Customer" ADD CONSTRAINT "FK_CustomerSupportRepId" FOREIGN KEY ("SupportRepId") REFERENCES "spider__chinook_1"."Employee" ("EmployeeId") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "spider__chinook_1"."Employee" ADD CONSTRAINT "FK_EmployeeReportsTo" FOREIGN KEY ("ReportsTo") REFERENCES "spider__chinook_1"."Employee" ("EmployeeId") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "spider__chinook_1"."Invoice" ADD CONSTRAINT "FK_InvoiceCustomerId" FOREIGN KEY ("CustomerId") REFERENCES "spider__chinook_1"."Customer" ("CustomerId") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "spider__chinook_1"."InvoiceLine" ADD CONSTRAINT "FK_InvoiceLineInvoiceId" FOREIGN KEY ("InvoiceId") REFERENCES "spider__chinook_1"."Invoice" ("InvoiceId") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "spider__chinook_1"."InvoiceLine" ADD CONSTRAINT "FK_InvoiceLineTrackId" FOREIGN KEY ("TrackId") REFERENCES "spider__chinook_1"."Track" ("TrackId") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "spider__chinook_1"."PlaylistTrack" ADD CONSTRAINT "FK_PlaylistTrackPlaylistId" FOREIGN KEY ("PlaylistId") REFERENCES "spider__chinook_1"."Playlist" ("PlaylistId") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "spider__chinook_1"."PlaylistTrack" ADD CONSTRAINT "FK_PlaylistTrackTrackId" FOREIGN KEY ("TrackId") REFERENCES "spider__chinook_1"."Track" ("TrackId") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "spider__chinook_1"."Track" ADD CONSTRAINT "FK_TrackAlbumId" FOREIGN KEY ("AlbumId") REFERENCES "spider__chinook_1"."Album" ("AlbumId") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "spider__chinook_1"."Track" ADD CONSTRAINT "FK_TrackGenreId" FOREIGN KEY ("GenreId") REFERENCES "spider__chinook_1"."Genre" ("GenreId") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "spider__chinook_1"."Track" ADD CONSTRAINT "FK_TrackMediaTypeId" FOREIGN KEY ("MediaTypeId") REFERENCES "spider__chinook_1"."MediaType" ("MediaTypeId") ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Indexes
CREATE INDEX "idx_Album_IFK_AlbumArtistId" ON "spider__chinook_1"."Album" ("ArtistId");
CREATE INDEX "idx_Customer_IFK_CustomerSupportRepId" ON "spider__chinook_1"."Customer" ("SupportRepId");
CREATE INDEX "idx_Employee_IFK_EmployeeReportsTo" ON "spider__chinook_1"."Employee" ("ReportsTo");
CREATE INDEX "idx_Invoice_IFK_InvoiceCustomerId" ON "spider__chinook_1"."Invoice" ("CustomerId");
CREATE INDEX "idx_InvoiceLine_IFK_InvoiceLineInvoiceId" ON "spider__chinook_1"."InvoiceLine" ("InvoiceId");
CREATE INDEX "idx_InvoiceLine_IFK_InvoiceLineTrackId" ON "spider__chinook_1"."InvoiceLine" ("TrackId");
CREATE INDEX "idx_PlaylistTrack_IFK_PlaylistTrackTrackId" ON "spider__chinook_1"."PlaylistTrack" ("TrackId");
CREATE INDEX "idx_Track_IFK_TrackAlbumId" ON "spider__chinook_1"."Track" ("AlbumId");
CREATE INDEX "idx_Track_IFK_TrackGenreId" ON "spider__chinook_1"."Track" ("GenreId");
CREATE INDEX "idx_Track_IFK_TrackMediaTypeId" ON "spider__chinook_1"."Track" ("MediaTypeId");
