--
-- Set up a many-to-many relationship between tables (mini)series and eps
-- -----------------------------------------------------------------------------
--

--
-- Table structure for the mapping table 'series_eps'
--
DROP TABLE IF EXISTS series_eps;
CREATE TABLE IF NOT EXISTS series_eps (
    series_id int(5) NOT NULL
	REFERENCES miniseries(id),
    eps_id int(5) NOT NULL
	REFERENCES eps(id),
    PRIMARY KEY series_eps_pk (series_id,eps_id)
) ENGINE=InnoDB;

SHOW warnings;

--
-- Make a view to simplify access to eps and (mini)series. This simply
-- joins the two tables through the 'series_eps' table. Some fields need
-- renaming to avoid clashes.
--
DROP VIEW IF EXISTS eps_with_series;
CREATE VIEW eps_with_series AS
    SELECT
	e.id AS eps_id,
	e.date,
	e.title,
	e.duration,
	e.summary,
	e.notes,
	e.hostid,
	e.explicit,
	e.license,
	e.tags,
	e.version,
	e.downloads,
	e.valid AS eps_valid,
	ms.id AS series_id,
	ms.name,
	ms.description,
	ms.private,
	ms.image,
	ms.valid AS series_valid
    FROM eps e
    JOIN series_eps se ON (e.id = se.eps_id)
    JOIN miniseries ms ON (ms.id = se.series_id)
    ORDER BY e.id, ms.id;

SHOW warnings;

/*
vim: syntax=sql ai tw=75:
*/