Ver Mensaje Individual
  #1 (permalink)  
Antiguo 24/04/2017, 22:01
italo_pm
 
Fecha de Ingreso: enero-2011
Mensajes: 24
Antigüedad: 13 años, 3 meses
Puntos: 0
uso correcto de llaves primarias autoincrement y luego foreanas?

Hola a todos, necesito ayuda para aclararme alguna duda en el funcionamiento de las llaves primarias y foreanas, estoy creado una web en forma de practica... es sobre uan simulacion de una cuenta corriente de un banco...

he creado un database, pero nose si he hecho buen uso de las llave foreanas, ya que uso pk autoincrement y cuando son foreanas nose si es correcto usarlas o puedo remplazarlas por otro atributo...

este el codigo mysql

Cita:
--
-- Table structure for table `banco`
--
CREATE TABLE `banco` (
`id_banco` int(5) NOT NULL AUTO_INCREMENT,
`nombre` varchar(50) NOT NULL,
`direccion` varchar(50) NOT NULL,
`telefono` varchar(20) NOT NULL,
`email` varchar(50) NOT NULL,
`ruc` varchar(50) NOT NULL,
PRIMARY KEY (`id_banco`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `cliente` cliente
--
CREATE TABLE `cliente` (
`id_cliente` int(5) NOT NULL AUTO_INCREMENT,
`id_banco` int(5) NOT NULL,
`apellido` varchar(50) NOT NULL,
`nombre` varchar(50) NOT NULL,
`direccion` varchar(50) NOT NULL,
`telefono` varchar(20) NOT NULL,
`email` varchar(50) NOT NULL,
`fecha_nacimiento` date NOT NULL,
`dni` varchar(16) NOT NULL,
`ruc` varchar(50) DEFAULT NULL,
`userid` varchar(7) NOT NULL, -- userid cliente generado con php.
`password` varchar(32) NOT NULL, -- password con md5 php
PRIMARY KEY (`id_cliente`),
FOREIGN KEY (`id_banco`) REFERENCES `banco` (`id_banco`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `cuenta_corriente`
--
CREATE TABLE `cuenta_corriente` (
`id_cuenta_corriente` int(5) NOT NULL AUTO_INCREMENT,
`numero_cuenta` varchar(50) NOT NULL,
`IBAN` varchar(27) NOT NULL,
`fecha_inicio` date NOT NULL,
`fecha_cierre` date DEFAULT NULL,
`saldo_actual` decimal(19,2) NOT NULL,
PRIMARY KEY (`id_cuenta_corriente`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `titular_cuenta`
--
CREATE TABLE `titular_cuenta` (
`id_cliente` int(5) NOT NULL,
`id_cuenta_corriente` int(5) NOT NULL,
PRIMARY KEY (`id_cliente`,`id_cuenta_corriente`),
FOREIGN KEY (`id_cliente`) REFERENCES `cliente` (`id_cliente`),
FOREIGN KEY (`id_cuenta_corriente`) REFERENCES `cuenta_corriente` (`id_cuenta_corriente`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `tipo_movimiento_cuenta`
--
CREATE TABLE `tipo_movimiento_cuenta` (
`id_tipo_movimiento_cuenta` int(5) NOT NULL AUTO_INCREMENT,
`codigo` varchar(50) NOT NULL,
`tipo` varchar(50) NOT NULL,
`descripcion` varchar(50) NOT NULL,
PRIMARY KEY (`id_tipo_movimiento_cuenta`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `movimiento_cuenta`
--
CREATE TABLE `movimiento_cuenta` (
`id_movimiento_cuenta` int(5) NOT NULL AUTO_INCREMENT,
`id_cliente` int(5) NOT NULL,
`id_cuenta_corriente` int(5) NOT NULL,
`id_tipo_movimiento_cuenta` int(5) NOT NULL,
`cantidad_movimiento` decimal(19,2) NOT NULL,
`fecha` date NOT NULL,
`hora` time NOT NULL,
PRIMARY KEY (`id_movimiento_cuenta`,`id_cliente`,`id_cuenta_co rriente`,`id_tipo_movimiento_cuenta`,`fecha`),
FOREIGN KEY (`id_tipo_movimiento_cuenta`) REFERENCES `tipo_movimiento_cuenta` (`id_tipo_movimiento_cuenta`),
FOREIGN KEY (`id_cliente`) REFERENCES `cliente` (`id_cliente`),
FOREIGN KEY (`id_cuenta_corriente`) REFERENCES `cuenta_corriente` (`id_cuenta_corriente`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `transferencia`
--
CREATE TABLE `transferencia` (
`id_transferencia` int(5) NOT NULL AUTO_INCREMENT,
`numero_transferencia` int(5) NOT NULL,
`fecha_transferencia` date NOT NULL,
`hora_transferencia` time NOT NULL,
`motivo_transferencia` varchar(50) NOT NULL,
`cantidad_transferencia` decimal(19,2) NOT NULL,
`id_cuenta_retiro` int(5) NOT NULL,
`id_cuenta_deposito` int(5) NOT NULL,
`id_cliente` int(5) NOT NULL,
PRIMARY KEY (`id_transferencia`,`id_cliente`,`fecha_transferen cia`),
FOREIGN KEY (`id_cuenta_retiro`) REFERENCES `cuenta_corriente` (`id_cuenta_corriente`),
FOREIGN KEY (`id_cuenta_deposito`) REFERENCES `cuenta_corriente` (`id_cuenta_corriente`),
FOREIGN KEY (`id_cliente`) REFERENCES `cliente` (`id_cliente`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `tarjeta`
--
CREATE TABLE `tarjeta` (
`id_cuenta_corriente` int(5) NOT NULL,
`numero_tarjeta` int(10) NOT NULL,
`fecha_caducidad` date NOT NULL,
`pin` int(5) NOT NULL,
PRIMARY KEY (`numero_tarjeta`),
FOREIGN KEY (`id_cuenta_corriente`) REFERENCES `cuenta_corriente` (`id_cuenta_corriente`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `operacion_tarjeta`
--
CREATE TABLE `operacion_tarjeta` (
`id_operacion_tarjeta` int(5) NOT NULL AUTO_INCREMENT,
`numero_tarjeta` int(10) NOT NULL,
`fecha_operacion` date NOT NULL,
`hora_operacion` time NOT NULL,
`cantidad_operacion_tarjeta` decimal(19,4) NOT NULL,
PRIMARY KEY (`id_operacion_tarjeta`),
FOREIGN KEY (`numero_tarjeta`) REFERENCES `tarjeta` (`numero_tarjeta`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
y este es el resultado grafico:



mi duda es si en la tabla "titular_cuenta" es correcto usar como llaves id_cliente y id_cuenta_corriente y no userid de la tabla cliente y numero_cuenta de la tabla cuenta_corriente y en las otras donde uso id_cliente y id_cuenta_corriente u otros por ejemplo las tablas trasferencia, movimiento_cuenta, tarjeta, cuenta_corriente.

cuando se que no debo usar los campos con id_xxyy por mas que sean unicos conciderando que hay otros campos de la tabla que tambien son unicos??

se que el id_xxyy con auto_increment es para que sean unicos los records, pero en este caso userid, numero_cuenta tambien son unicos, lo mismo que hice con la tabla tarjeta y operacion_tarjeta mediante numero_tarjeta podria estaria bien aplicarlo dejando de lado los campos id_xxyy??

gracias anticipadas por la ayuda.

Italo.