I've found information on how to export data from MySQL to Excel, but not upside down, from Excel to MySQL.
Also, I'm asking using PHPExcel because a friend suggested to do it with it, but if there is an easiest way to do it, it's welcome.
I know I can export from excel to MySql (with static field) using following syntax:
LOAD DATA LOCAL INFILE '\\path' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n'
But not all Excel fields will be imported into MySQL or in the same order.
This is the table structure that I have:
CREATE TABLE cliente (
clienteId int(11) NOT NULL AUTO_INCREMENT,
clienteIdentidadTipo varchar(45) DEFAULT NULL,
clienteIdEntidadNumero varchar(45) DEFAULT NULL,
clienteNombres varchar(100) DEFAULT NULL,
clienteApellidosPrimero varchar(45) DEFAULT NULL,
clienteApellidosSegundo varchar(45) DEFAULT NULL,
clienteDireccion varchar(45) DEFAULT NULL,
clienteCelular varchar(45) DEFAULT NULL,
clienteTelefono varchar(45) DEFAULT NULL,
clienteGenero varchar(45) DEFAULT NULL,
clienteFechaNacimiento date DEFAULT NULL,
clienteEstadoCivil varchar(45) DEFAULT NULL,
clienteCargo varchar(45) DEFAULT NULL,
clienteOcupacion varchar(45) DEFAULT NULL,
clienteRecibeMensajesEmail tinyint(1) DEFAULT NULL,
clienteEmail varchar(45) DEFAULT NULL,
clienteClave varchar(100) DEFAULT NULL,
clienteRecibeMensajesSms tinyint(1) DEFAULT NULL,
clienteObjetivoVermeMejor tinyint(1) DEFAULT NULL,
clienteObjetivoSalud tinyint(1) DEFAULT NULL,
clienteObjetivoDesempeno tinyint(1) DEFAULT NULL,
clientePracticaDeporte tinyint(1) DEFAULT NULL,
clienteServicioCardio tinyint(1) DEFAULT NULL,
clienteServicioPersonalizado tinyint(1) DEFAULT NULL,
clienteServicioGrupales tinyint(1) DEFAULT NULL,
clienteServicioPesas tinyint(1) DEFAULT NULL,
clienteServicioFisioterapia tinyint(1) DEFAULT NULL,
clienteServicioFuncional tinyint(1) DEFAULT NULL,
clienteServicioOtro varchar(45) DEFAULT NULL,
clienteMedioConocioGym varchar(45) DEFAULT NULL,
clienteEmergenciaNombre varchar(45) DEFAULT NULL,
clienteEmergenciaTelefono varchar(45) DEFAULT NULL,
clienteEps varchar(45) DEFAULT NULL,
clienteIps varchar(45) DEFAULT NULL,
clienteCiudadId int(11) NOT NULL,
clienteEmpresaId int(11) NOT NULL,
clienteGrupoId int(11) NOT NULL,
clienteCertificaConcentimientoInformado tinyint(1) DEFAULT NULL,
clienteCertificaCuestionamientoAntecedente tinyint(1) DEFAULT NULL,
clienteCongelacion int(11) DEFAULT NULL,
clienteDiasTolerancia int(11) DEFAULT NULL,
clienteFechaPago int(11) DEFAULT NULL,
clienteTiquetera tinyint(1) DEFAULT NULL,
clienteUsuario varchar(45) DEFAULT NULL,
historiaClinicaId int(11) NOT NULL,
planId int(11) NOT NULL,
clientePracticaDeporteCual varchar(45) DEFAULT NULL,
clienteCupoCredito int(7) NOT NULL,
clienteImagen varchar(500) NOT NULL,
clienteValidacion int(11) NOT NULL,
clienteRh varchar(45) DEFAULT NULL,
PRIMARY KEY (clienteId,clienteCiudadId,clienteEmpresaId,clienteGrupoId,historiaClinicaId,planId),
UNIQUE KEY clienteId_UNIQUE (clienteId),
KEY fk_cliente_ciudad_idx (clienteCiudadId),
KEY fk_cliente_empresa1_idx (clienteEmpresaId),
KEY fk_cliente_grupo1_idx (clienteGrupoId),
KEY fk_cliente_historiaClinica1_idx (historiaClinicaId),
KEY fk_cliente_plan1_idx (planId),
CONSTRAINT fk_cliente_ciudad FOREIGN KEY (clienteCiudadId) REFERENCES ciudad (ciudadId) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_cliente_empresa1 FOREIGN KEY (clienteEmpresaId) REFERENCES empresa (empresaId) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_cliente_grupo1 FOREIGN KEY (clienteGrupoId) REFERENCES grupo (grupoId) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_cliente_historiaClinica1 FOREIGN KEY (historiaClinicaId) REFERENCES historiaclinica (historiaClinicaId) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_cliente_plan1 FOREIGN KEY (planId) REFERENCES plan (planId) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And this is the Excel file that I want to import into the database:
This will probably help:
Import an excel file into a MySQL table with PHPExcel
Also, in the future, most of the questions you will have with programming have already been answered. So you can usually find the answer in 1-2 google searches.
Related
I am creating a database that will store 'graduate details'. I have been able to set the ID to auto increment, Unique and also my primary key that start from 1...
I also want to make the certificate number field unique and auto-increment value in an increasing order that will start from 00001.
Please any help on how to go about this will be very appreciated. Thank you.
This is the MYSQL code for the database below;
CREATE TABLE IF NOT EXISTS `graduates` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`f_name` varchar(25) NOT NULL,
`l_name` varchar(25) NOT NULL,
`gender` varchar(6) DEFAULT NULL,
`address` varchar(100) DEFAULT NULL,
`city` varchar(15) DEFAULT NULL,
`region` varchar(30) DEFAULT NULL,
`phone` varchar(15) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`certificate_number` int(50) NOT NULL AUTO_INCREMENT,
`programme` varchar(50) CHARACTER SET utf8mb4 NOT NULL,
`marks` int(10) NOT NULL,
`college_name` varchar(50) CHARACTER SET utf8mb4 NOT NULL,
`date_of_birth` date DEFAULT NULL,
`created_by` int(10) UNSIGNED NOT NULL DEFAULT 0,
`created_at` date DEFAULT NULL,
`updated_by` int(10) UNSIGNED NOT NULL DEFAULT 0,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `certificate_number` (`certificate_number`)
) ENGINE=InnoDB AUTO_INCREMENT=90 DEFAULT CHARSET=utf8;
first of all thanks for your time reading this.
I'm working on a PHP web application that deals with the following data, as of right now there is noticeable time when trying to retrieve data, especially when drilling down the options. The primary options right now are VehicleType, Make, Model, Year, InternetPrice and Mileage. There will eventually be even more columns being used. Other columns from this list are being used for displaying data throughout the page, as 10 records are displayed at one instance.
Table structure:
CREATE TABLE `vehicles` (
`id` int(12) NOT NULL AUTO_INCREMENT,
`DealerID` int(6) DEFAULT NULL,
`VIN` varchar(17) DEFAULT NULL,
`StockNumber` varchar(10) DEFAULT NULL,
`Status` varchar(1) DEFAULT NULL,
`VehicleType` int(1) DEFAULT NULL,
`Year` int(4) DEFAULT NULL,
`Make` varchar(13) DEFAULT NULL,
`Model` varchar(24) DEFAULT NULL,
`Trim` varchar(35) DEFAULT NULL,
`Body` varchar(25) DEFAULT NULL,
`VehicleClass` varchar(50) DEFAULT NULL,
`VehicleCategory` varchar(6) DEFAULT NULL,
`Mileage` int(6) DEFAULT NULL,
`Transmission` varchar(24) DEFAULT NULL,
`EngineDisplacement` varchar(7) DEFAULT NULL,
`EngineSize` varchar(15) DEFAULT NULL,
`Induction` varchar(25) DEFAULT NULL,
`DriveTrain` varchar(3) DEFAULT NULL,
`FuelType` varchar(9) DEFAULT NULL,
`FuelEconomyCity` int(2) DEFAULT NULL,
`FuelEconomyHighway` int(2) DEFAULT NULL,
`FuelEconomyCombined` int(1) DEFAULT NULL,
`Doors` int(1) DEFAULT NULL,
`OEMColorCodeExterior` varchar(10) DEFAULT NULL,
`OEMColorCodeInterior` varchar(10) DEFAULT NULL,
`OEMColorNameExterior` varchar(49) DEFAULT NULL,
`OEMColorNameInterior` varchar(10) DEFAULT NULL,
`GenericColorExterior` varchar(35) DEFAULT NULL,
`GenericColorInterior` varchar(38) DEFAULT NULL,
`InternetPrice` int(6) DEFAULT NULL,
`ComparisonPrice` int(6) DEFAULT NULL,
`WholeSalePrice` varchar(10) DEFAULT NULL,
`MSRP` varchar(10) DEFAULT NULL,
`InternetSpecial` varchar(1) DEFAULT NULL,
`OemModelCode` varchar(12) DEFAULT NULL,
`HasWarranty` varchar(1) DEFAULT NULL,
`CertificationWarranty` int(3) DEFAULT NULL,
`WarrantyMonth` int(1) DEFAULT NULL,
`WarrantyMiles` int(1) DEFAULT NULL,
`CertificationNumber` varchar(7) DEFAULT NULL,
`ServiceContract` varchar(1) DEFAULT NULL,
`InServiceDate` varchar(19) DEFAULT NULL,
`CertificationDate` varchar(19) DEFAULT NULL,
`DateManufactured` varchar(19) DEFAULT NULL,
`DateCreated` varchar(19) DEFAULT NULL,
`DateUpdated` varchar(19) DEFAULT NULL,
`DateRemoved` varchar(19) DEFAULT NULL,
`DatePhotosUpdated` varchar(19) DEFAULT NULL,
`Photos` int(2) DEFAULT NULL,
`SuperSizePhotos` int(2) DEFAULT NULL,
`AddendumDetails` varchar(10) DEFAULT NULL,
`DepartmentComments` varchar(239) DEFAULT NULL,
`VehicleComments` varchar(1987) DEFAULT NULL,
`Options` varchar(2264) DEFAULT NULL,
`PurchasePayment` decimal(5,2) DEFAULT NULL,
`PurchaseDownPayment` decimal(6,2) DEFAULT NULL,
`PurchaseTerm` int(2) DEFAULT NULL,
`PurchaseDisclosure` varchar(10) DEFAULT NULL,
`PurchaseRate` decimal(3,2) DEFAULT NULL,
`LeasePayment` decimal(2,2) DEFAULT NULL,
`LeaseDownPayment` decimal(2,2) DEFAULT NULL,
`LeaseTerm` int(1) DEFAULT NULL,
`LeaseDisclosure` varchar(10) DEFAULT NULL,
`LeaseRate` decimal(2,2) DEFAULT NULL,
`LeaseResidual` decimal(2,2) DEFAULT NULL,
`Reserved1` varchar(10) DEFAULT NULL,
`Reserved2` varchar(10) DEFAULT NULL,
`Reserved3` varchar(10) DEFAULT NULL,
`Reserved4` varchar(10) DEFAULT NULL,
`Reserved5` varchar(10) DEFAULT NULL,
`Reserved6` varchar(10) DEFAULT NULL,
`sitecert` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `VIN` (`VIN`),
KEY `Make` (`Make`),
KEY `StockNumber` (`StockNumber`),
KEY `Model` (`Model`),
KEY `Trim` (`Trim`),
KEY `Body` (`Body`),
KEY `VehicleClass` (`VehicleClass`),
KEY `Transmission` (`Transmission`),
KEY `DealerID` (`DealerID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=46527428 ;
And an example query:
SELECT *
FROM vehicles
WHERE Make = 'Audi'
AND VehicleType = '0'
AND Model = 'A4'
AND InternetPrice BETWEEN 0 AND 999999
AND Year BETWEEN 1983 AND 2015
AND Mileage > 0
AND DealerID IN ('AA'
,'156553'
,'229602'
,'160483'
,'2953'
,'161712'
,'228576'
,'228921'
,'7590'
,'20813'
,'158105'
,'160286'
,'164479'
,'164487'
,'182543'
,'158860'
,'186479'
,'227170'
,'226327'
)
ORDER BY sitecert DESC
,InternetPrice DESC
LIMIT 0, 10
Would anyone have any advice to optimize this query or table? Of course there is other code throughout the page but I've timed it down to this query that is causing the delay.
Thanks for your help!
UPDATE:
MySQL Explain:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE vehicles ref Make,Model,DealerID Model 75 const 1675 Using where; Using filesort
Year was updated to INT
Depending on your database, you should add indexes on the following fields:
Make
VehicleType
Model
InternetPrice
Year
Mileage
DealerId
Year should be DATE type, not text (INT also makes sense).
This should solve your problem.
My advices :
use a group by make
partition your table, columns might be of use (make,Year)
store your table in inndb engine instead of myisam and build your
self some clustered indexes to attend your predicates !
For such a case you have to read EXPLAIN results and tweak indexes accordingly.
However, for only 1600 rows to sort I wouldn't bother at all. If you still want to make it faster, it seems you need a combined key that would include a Model and a sitecert followed by price, both in a reverse order.
Means you have to have two additional columns where negative values of sitecert and price are stored. then buind an index on (Model,nsitecert, nInternetPrice) and order without DESC to eliminate a filesort
If you have search queries that involve no Model but any combination of parameters - then it's time to think of an external search engine like Sphinx Search.
I keep on getting the below error message when I try to enter some important records I accentually deleted.
Duplicate entry 'EMIR2023 ' for key 'flightNo'
Is there a way in the phpMyAdmin environment I can disable the UNIQUE KEY then activate it when I am done with inserting the records?
Find below the structure of my table, I hope it helps
--
-- Table structure for table `flightSched`
--
CREATE TABLE IF NOT EXISTS `flightSched` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`timePeriod` time DEFAULT '00:00:00',
`depOrArriv` varchar(9) DEFAULT NULL,
`flightNo` varchar(9) NOT NULL,
`airline` varchar(20) DEFAULT NULL,
`dest` varchar(30) DEFAULT NULL,
`origin` varchar(30) DEFAULT NULL,
`depature` time DEFAULT '00:00:00',
`don` set('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday') DEFAULT NULL,
`arrivalTime` datetime DEFAULT '0000-00-00 00:00:00',
`arrivalTimeSec` varchar(28) DEFAULT NULL,
`status` varchar(15) NOT NULL,
`image_type` varchar(25) DEFAULT NULL,
`image` blob NOT NULL,
`image_size` varchar(25) DEFAULT NULL,
`image_name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `flightNo` (`flightNo`),
UNIQUE KEY `arrivalTime_2` (`arrivalTime`),
KEY `arrivalTime` (`arrivalTime`),
KEY `arrivalTime_3` (`arrivalTime`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=537 ;
Looking forward to your reply :-)
Try unique_checks
SET unique_checks=0;
... import operation ...
SET unique_checks=1;
check reference
I have a database setup like
`id` int(11) unsigned NOT NULL auto_increment,
`ad-id` int(11) default NULL,
`advertiser-id` int(11) default NULL,
`advertiser-name` varchar(250) default NULL,
`advertiser-category` varchar(250) default NULL,
`buy-url` varchar(255) default NULL,
`catalog-id` varchar(255) default NULL,
`currency` varchar(3) default NULL,
`description` varchar(255) default NULL,
`image-url` varchar(255) default NULL,
`in-stock` varchar(255) default NULL,
`isbn` varchar(13) default NULL,
`manufacturer-name` varchar(255) default NULL,
`manufacturer-sku` varchar(50) default NULL,
`name` varchar(255) default NULL,
`price` decimal(10,2) default NULL,
`retail-price` decimal(10,2) default NULL,
`sale-price` decimal(10,2) default NULL,
`sku` varchar(50) default NULL,
`upc` varchar(50) default NULL,
I am trying to load the data like this:
LOAD DATA LOW_PRIORITY LOCAL INFILE "/home/datafeed/tempfile.csv" REPLACE INTO TABLE products_import FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\n" (ad-id, advertiser-id, advertiser-name, advertiser-category, buy-url,catalog-id,currency, description,image-url,in-stock,isbn,manufacturer-name,manufacturer-sku,name,price,retail-price,sale-price,sku, upc);
but it doesn't seem to like database names with dashes in them. The field names are pretty much set in stone. If I try to insert without the field names it sets everything in the wrong field (starts with autoinc)
I'm not very good with MySQL any help would be appreciated.
Put back-ticks around the field names: `ad-id`, etc., like in your table structure listing.
I have got a problem with two tables:
CREATE TABLE IF NOT EXISTS `addresses` (
`adr_id` int(11) NOT NULL auto_increment,
`per_id` int(11) NOT NULL,
`adr_street` varchar(50) NOT NULL,
`adr_houseno` int(11) default NULL,
`adr_housenoadd` varchar(10) default NULL,
`adr_postalcode` varchar(25) NOT NULL,
`adr_city` varchar(20) NOT NULL,
`adr_type` varchar(45) default NULL,
`cnt_id` int(11) NOT NULL,
`adr_date` date default NULL,
`sys-mut-dt` timestamp NULL default NULL,
`sys-mut-user` varchar(20) default NULL,
`sys-mut-id` int(11) NOT NULL default '0',
PRIMARY KEY (`adr_id`),
KEY `per_id` (`per_id`),
KEY `cnt_id` (`cnt_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
--
-- RELATIES VOOR TABEL `addresses`:
-- `cnt_id`
-- `countries` -> `cnt_id`
-- `per_id`
-- `persons` -> `per_id`
--
CREATE TABLE `events` (
`evt_id` int(11) NOT NULL auto_increment,
`evt_name` varchar(50) NOT NULL,
`evt_description` varchar(100) default NULL,
`evt_startdate` date NOT NULL,
`evt_enddate` date default NULL,
`evt_starttime` time default NULL,
`evt_endtime` time default NULL,
`evt_amtpersons` int(11) default NULL,
`sts_id` int(11) NOT NULL,
`adr_id` int(11) default NULL,
`evt_amtPersonsSubs` tinyint(4) NOT NULL default '0',
`evt_photo` varchar(50) default NULL,
`sys-mut-dt` timestamp NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`sys-mut-user` varchar(20) default NULL,
`sys-mut-id` int(11) NOT NULL default '0',
PRIMARY KEY (`evt_id`),
KEY `sts_id` (`sts_id`),
KEY `adr_id` (`adr_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The problem is, when i try to delete a record from address table I get the following error
DELETE
FROM addresses
WHERE per_id = 45
1451 - Cannot delete or update a parent row: a foreign key constraint fails (`site/events`, CONSTRAINT `adr_id` FOREIGN KEY (`adr_id`) REFERENCES `addresses` (`adr_id`) ON DELETE NO ACTION ON UPDATE NO ACTION) ;
The weird thing is that there is no record with the same per_id or adr_id in the events table.
So what's going on over here?
How Can I solve this? ;-)
/* I think it means "How can I solve this?" */
It may sound stupid, but are you absolutely sure there are no child rows?
Could you please run this:
SELECT e.*
FROM addresses a
JOIN events e
ON e.adr_id = a.adr_id
WHERE a.per_id = 45