CREATE TABLE PDBENTRY (
	pdbcode CHAR(4) NOT NULL,
	created DATE NOT NULL,
	title VARCHAR(1200) NOT NULL,
	PRIMARY KEY (pdbcode)
);

CREATE TABLE PDBMOLECULE (
	pdbcode CHAR(4) NOT NULL,
	molid VARCHAR(16) NOT NULL,
	PRIMARY KEY (pdbcode,molid),
	FOREIGN KEY (pdbcode) REFERENCES PDBENTRY(pdbcode)
		ON DELETE CASCADE ON UPDATE CASCADE
);

CREATE TABLE PDBCHAIN (
	chain VARCHAR(16) NOT NULL,
	molid VARCHAR(16) NOT NULL,
	pdbcode CHAR(4) NOT NULL,
	seq TEXT NOT NULL,
	seqlength NUMERIC(8,0) NOT NULL,
	PRIMARY KEY (pdbcode,molid,chain),
	UNIQUE (pdbcode, chain),
	FOREIGN KEY (pdbcode, molid) REFERENCES PDBMOLECULE (pdbcode,molid)
		ON DELETE CASCADE ON UPDATE CASCADE
);

CREATE TABLE PDB_IN_SW (
	accnumber CHAR(6) NOT NULL,
	chain VARCHAR(16) NOT NULL,
	molid VARCHAR(16) NOT NULL,
	pdbcode CHAR(4) NOT NULL,
	FOREIGN KEY (accnumber) REFERENCES SWISSENTRY (accnumber)
		ON DELETE RESTRICT ON UPDATE RESTRICT,
	FOREIGN KEY (pdbcode,molid,chain) REFERENCES PDBCHAIN (pdbcode,molid,chain) 
		ON DELETE CASCADE ON UPDATE CASCADE
);
