Tengo una base de datos SQL
en MySQL
de comunidades, con sus respectivas provincias.
He añadido mi unión de ambas tablas. Por ejemplo, para saber que Sevilla y Córboba pertenecen a Andalucía.
¿Es correcta mi respuesta?
Código:
use spain;
CREATE TABLE comunidades (
id int(10) unsigned NOT NULL PRIMARY KEY,
comunidad varchar(255) COLLATE utf8_spanish_ci NOT NULL UNIQUE KEY,
capital_id int(11) NOT NULL
);
INSERT INTO comunidades (id, comunidad, capital_id) VALUES
(1, "Andalucia", 6152),
(2, "Aragon", 8113),
(3, "Principado de Asturias", 5009),
(4, "Islas Baleares", 836),
(5, "Canarias", 5252),
(6, "Cantabria", 5823),
(7, "Castilla y Leon", 7415),
(8, "Castilla - La Mancha", 6934),
(9, "Cataluña", 881),
(10, "Comunitad Valenciana", 7219),
(11, "Extremadura", 712),
(12, "Galicia", 2198),
(13, "Comunidad de Madrid ", 4356),
(14, "Region de Murcia", 4588),
(15, "Comunidad Foral de Navarra", 4815),
(16, "Pais Vasco", 46),
(17, "La Rioja", 4124),
(18, "Ceuta", 8115),
(19, "Melilla", 8116);
CREATE TABLE provincias (
id int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
provincia varchar(255) COLLATE utf8_spanish_ci NOT NULL,
comunidad_id int(10) unsigned NOT NULL,
capital_id int(11) NOT NULL DEFAULT "-1"
FOREIGN KEY (comunidad_id) REFERENCES comunidades(id) ON DELETE CASCADE
);
INSERT INTO provincias (id, provincia, comunidad_id, capital_id) VALUES
(1, "Álava", 16, 46),
(2, "Albacete", 8, 54),
(3, "Alicante", 10, 152),
(4, "Almería", 1, 292),
(5, "Ávila", 7, 395),
(6, "Badajoz", 11, 644),
(7, "Illes Balears", 4, 836),
(8, "Barcelona", 9, 881),
(9, "Burgos", 7, 1220),
(10, "Cáceres", 11, 1580),
(11, "Cádiz", 1, 1776),
(12, "Castellón", 10, 1844),
(13, "Ciudad Real", 8, 1978),
(14, "Córdoba", 1, 2065),
(15, "A Coruña", 12, 2150),
(16, "Cuenca", 8, 2285),
(17, "Girona", 9, 2526),
(18, "Granada", 1, 2747),
(19, "Guadalajara", 8, 2947),
(20, "Guipúzcoa", 16, 3159),
(21, "Huelva", 1, 3257),
(22, "Huesca", 2, 3396),
(23, "Jaén", 1, 3545),
(24, "León", 7, 3676),
(25, "Lleida", 9, 3918),
(26, "La Rioja", 17, 4124),
(27, "Lugo", 12, 4238),
(28, "Madrid", 13, 4356),
(29, "Málaga", 1, 4523),
(30, "Murcia", 14, 4588),
(31, "Navarra", 15, 4815),
(32, "Ourense", 12, 4925),
(33, "Asturias", 3, 5009),
(34, "Palencia", 7, 5137),
(35, "Las Palmas", 5, 5252),
(36, "Pontevedra", 12, 5312),
(37, "Salamanca", 7, 5588),
(38, "Santa Cruz de Tenerife", 5, 5732),
(39, "Cantabria", 6, 5823),
(40, "Segovia", 7, 6024),
(41, "Sevilla", 1, 6152),
(42, "Soria", 7, 6307),
(43, "Tarragona", 9, 6499),
(44, "Teruel", 2, 6721),
(45, "Toledo", 8, 6934),
(46, "Valencia", 10, 7219),
(47, "Valladolid", 7, 7415),
(48, "Vizcaya", 16, 7489),
(49, "Zamora", 7, 7821),
(50, "Zaragoza", 2, 8113),
(51, "Ceuta", 18, 8115),
(52, "Melilla", 19, 8116);
foreing key