Tabulky MyISAM jdou taky propojit:
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
CREATE TABLE IF NOT EXISTS `tridy` (
`tid` int(11) NOT NULL AUTO_INCREMENT,
`tnazev` varchar(4) COLLATE utf8_czech_ci NOT NULL,
`tridni` varchar(25) COLLATE utf8_czech_ci NOT NULL,
`obor` varchar(25) COLLATE utf8_czech_ci NOT NULL,
PRIMARY KEY (`tid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1 ;
INSERT INTO `tridy` (`tid`, `tnazev`, `tridni`, `obor`) VALUES
(1, '1.A', 'Malaníková Věra', 'kooperace'),
(2, '1.B', 'Josefík Karel', 'zoo'),
(3, '2.A', 'Malošík Jan', 'agro');
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
CREATE TABLE IF NOT EXISTS `studenti` (
`sid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`tid` int(10) unsigned NOT NULL,
`prijmeni` varchar(15) COLLATE utf8_czech_ci NOT NULL,
`jmeno` varchar(15) COLLATE utf8_czech_ci NOT NULL,
`prum_prospech` float NOT NULL,
PRIMARY KEY (`sid`),
KEY `tid` (`tid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=7 ;
INSERT INTO `studenti` (`sid`, `tid`, `prijmeni`, `jmeno`, `prum_prospech`) VALUES
(1, 1, 'Kapoun', 'Josef', 2.41),
(2, 1, 'Doležálková', 'Jana', 1.56),
(3, 2, 'Bumbálek', 'Zdeněk', 2.84),
(4, 2, 'Surovec', 'Vlastik', 2.4),
(5, 3, 'Nachátelová', 'Ludmila', 1.86),
(6, 3, 'Protivná', 'Jana', 1.45);
Chceme vypsat studenty z 2.A, tak propojíme takto:
select tnazev,prijmeni,jmeno from tridy join studenti on tridy.tid=studenti.tid where tridy.tid=3;
nebo
select tnazev,prijmeni,jmeno from tridy natural join studenti where tridy.tid=3;
Dostaneme:
+--------+---------------+---------+
| tnazev | prijmeni | jmeno |
+--------+---------------+---------+
| 2.A | Nachátelová | Ludmila |
| 2.A | Protivná | Jana |
+--------+---------------+---------+