Import SQL query from MYSQL to MsAccess - mysql

I need to create sql statments to import data from an existing MySQL database into MsAccess.
Has anyone got any ideas on the best way i can do this?
This is the code i used to create the database in MySQL just to give an idea of the tables etc.
CREATE DATABASE IF NOT EXISTS horsedb;
USE horsedb; CREATE TABLE `horse`.`horse` (
`HORSE_id` INT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(45) NULL,
`Colour` VARCHAR(45) NULL,
`Sire` INT NULL,
`Dam` INT NULL,
`Born` YEAR NULL,
`Trainer_id` INT NOT NULL,
PRIMARY KEY (`HORSE_id`))
ENGINE = InnoDB;
USE horsedb; CREATE TABLE `horse`.`showsite` (
`show_id` INT NOT NULL AUTO INCREMENT,
`Name` VARCHAR(45) NULL,
`Address` VARCHAR(45) NULL,
PRIMARY KEY (`show_id`));
USE horsedb; CREATE TABLE `horse`.`judge` (
`Judge_id` INT NOT NULL AUTO INCREMENT,
`Name` VARCHAR(45) NULL,
`Address` VARCHAR(45) NULL,
PRIMARY KEY (`Judge_id`));
USE horsedb; CREATE TABLE `horse`.`event` (
`Event_id` INT NOT NULL AUTO INCREMENT,
`Show_id` INT NOT NULL,
`Event_name` VARCHAR(45) NOT NULL,
`Judge_id` INT NOT NULL,
PRIMARY KEY (`Event_id`),
INDEX `show_id_idx` (`Show_id` ASC),
INDEX `judge_id_idx` (`Judge_id` ASC),
CONSTRAINT `show_id`
FOREIGN KEY (`Show_id`)
REFERENCES `horsedb`.`showsite` (`Show_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `judge_id`
FOREIGN KEY (`Judge_id`)
REFERENCES `horsedb`.`judge` (`Judge_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
USE horsedb; CREATE TABLE `horse`.`entry` (
`Event_id` INT NOT NULL AUTO INCREMENT,
`horse-id` INT NOT NULL,
`Place` INT NULL,
INDEX `horse_id_idx` (`Horse_id` ASC),
CONSTRAINT `horse_id`
FOREIGN KEY (`Horse_id`)
REFERENCES `horsedb`.`horse` (`Horse_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
USE horsedb; CREATE TABLE `horse`.`prize` (
`Event_id` INT NOT NULL AUTO INCREMENT,
`place` INT NULL,
`money` INT NULL,
));
USE horsedb; CREATE TABLE `horse`.`trainer` (
`Trainer_id` INT NOT NULL AUTO INCREMENT,
`Name` VARCHAR(45) NULL,
PRIMARY KEY (`Trainer_id`));

The most direct method for transferring MySQL tables into Access would be to install the MySQL ODBC driver (MySQL Connector/ODBC) and then use an ODBC connection to Import (not Link) the tables from MySQL. For more detailed instructions see:
Using Connector/ODBC with Microsoft Access

Related

MySQL #1075 error when importing sql file?

I have code code to create tables in MySQL:
It returns the following error:
#1075 - There can be only one auto column and it must be defined as a key
What is wrong with it? It is generated by DrawSQL application.
I tried this code:
CREATE TABLE `sp_players`(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`image` VARCHAR(255) NOT NULL,
`club` INT NOT NULL,
`trophies` INT NOT NULL,
`birthdate` DATE NOT NULL,
`prints` INT NOT NULL
);
ALTER TABLE
`sp_players` ADD PRIMARY KEY `sp_players_id_primary`(`id`);
CREATE TABLE `sp_prints`(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`player` INT NOT NULL,
`momemt` INT NOT NULL,
`season` INT NOT NULL,
`compeitition` INT NOT NULL
);
ALTER TABLE
`sp_prints` ADD PRIMARY KEY `sp_prints_id_primary`(`id`);
ALTER TABLE
`sp_players` ADD CONSTRAINT `sp_players_prints_foreign` FOREIGN KEY(`prints`) REFERENCES `sp_prints`(`id`);
ALTER TABLE
`sp_prints` ADD CONSTRAINT `sp_prints_player_foreign` FOREIGN KEY(`player`) REFERENCES `sp_players`(`id`);
Expected to generate all the tables correctly

Error Code 1215. Cannot add foreign key constraint

Creating a new database here for school and having difficulty in understanding what's wrong here.
For example, I want to create this table (automated SQL output):
-- -----------------------------------------------------
-- Table `jobsearch`.`Employer`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `jobsearch`.`Employer`
(
`EmployerID` SMALLINT(5) NOT NULL AUTO_INCREMENT,
`IndustryID` SMALLINT(5) NOT NULL,
`Address` VARCHAR(45) NOT NULL,
`City` VARCHAR(45) NOT NULL,
`State` CHAR(2) NOT NULL,
`Zip` VARCHAR(5) NOT NULL,
`Phone` VARCHAR(10) NOT NULL,
PRIMARY KEY (`EmployerID`, `IndustryID`),
INDEX `fk_Employer_Industry1_idx` (`IndustryID` ASC),
CONSTRAINT `fk_Employer_Industry1`
FOREIGN KEY (`IndustryID`) REFERENCES `job search`.`Industry` (`IndustryID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
I have this table that the foreign should be referencing (This table was created without any issue):
CREATE TABLE IF NOT EXISTS `jobsearch`.`Industry`
(
`IndustryID` INT NOT NULL AUTO_INCREMENT,
`IndustryName` VARCHAR(45) NOT NULL,
`Region` VARCHAR(45) NOT NULL,
PRIMARY KEY (`IndustryID`))
ENGINE = InnoDB;
The datatype for the referencing column must match exactly that of the referenced column. You've defined `Industry.IndustryID as
`IndustryID` INT ...
and Employer.IndustryID as
`IndustryID` SMALLINT(5) ...
Change Employer.IndustryID to INT and you should be shiny.

Error 1215: Cannot add foreign key constraint when forward engineering

when I try to forward engineer my new schema, I am getting this error. Anyone can offer your assistance to ?
-- -----------------------------------------------------
-- Table `SLIOP`.`schedule`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SLIOP`.`schedule` (
`scheduleID` INT NOT NULL AUTO_INCREMENT,
`lecturerID` INT NOT NULL,
`courseID` INT NOT NULL,
`type` VARCHAR(30) NOT NULL,
PRIMARY KEY (`scheduleID`),
INDEX `fk_schedule_academic_staff1_idx` (`lecturerID` ASC),
INDEX `fk_schedule_course1_idx` (`courseID` ASC),
CONSTRAINT `lecturerID`
FOREIGN KEY (`lecturerID`)
REFERENCES `SLIOP`.`academic_staff` (`lecturerID`)
CONSTRAINT `courseID`
FOREIGN KEY (`courseID`)
REFERENCES `SLIOP`.`course` (`courseID`)
ENGINE = InnoDB
Parent tables
-- -----------------------------------------------------
-- Table `SLIOP`.`course`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SLIOP`.`course` (
`courseID` INT NOT NULL AUTO_INCREMENT,
`course_code` VARCHAR(10) NOT NULL,
`course_name` VARCHAR(40) NOT NULL,
`lecturer_name` VARCHAR(40) NOT NULL,
`time` TIMESTAMP NOT NULL,
`fee` DECIMAL(10,2) NOT NULL,
`requirement` MEDIUMTEXT NOT NULL,
`lecturerID` INT NOT NULL,
PRIMARY KEY (`courseID`),
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `SLIOP`.`academic_staff`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SLIOP`.`academic_staff` (
`lecturerID` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(60) NOT NULL,
`last_name` VARCHAR(60) NOT NULL,
`profile_image` BLOB NULL,
PRIMARY KEY (`lecturerID`))
ENGINE = InnoDB;
I have searched related posts here, but i couldn't find where is my error.
Main issue I believe is a missing comma before CONSTRAINT courseID
But you missed several closing braces as well.
So here are working statements:
http://sqlfiddle.com/#!9/2bb11
CREATE TABLE IF NOT EXISTS `course` (
`courseID` INT NOT NULL AUTO_INCREMENT,
`course_code` VARCHAR(10) NOT NULL,
`course_name` VARCHAR(40) NOT NULL,
`lecturer_name` VARCHAR(40) NOT NULL,
`time` TIMESTAMP NOT NULL,
`fee` DECIMAL(10,2) NOT NULL,
`requirement` MEDIUMTEXT NOT NULL,
`lecturerID` INT NOT NULL,
PRIMARY KEY (`courseID`)
)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `academic_staff` (
`lecturerID` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(60) NOT NULL,
`last_name` VARCHAR(60) NOT NULL,
`profile_image` BLOB NULL,
PRIMARY KEY (`lecturerID`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `schedule` (
`scheduleID` INT NOT NULL AUTO_INCREMENT,
`lecturerID` INT NOT NULL,
`courseID` INT NOT NULL,
`type` VARCHAR(30) NOT NULL,
PRIMARY KEY (`scheduleID`),
INDEX `fk_schedule_academic_staff1_idx` (`lecturerID` ASC),
INDEX `fk_schedule_course1_idx` (`courseID` ASC),
CONSTRAINT `lecturerID`
FOREIGN KEY (`lecturerID`)
REFERENCES `academic_staff` (`lecturerID`),
CONSTRAINT `courseID`
FOREIGN KEY (`courseID`)
REFERENCES `course` (`courseID`))
ENGINE = InnoDB
I have been able to create the tables without any issue, after correcting the syntax errors. Just make sure you are running the scripts in correct order. i.e.
Table SLIOP.schedule should be created last since it is referencing
SLIOP.academic_staff and SLIOP.course

MySQL Workbench Database error

I created a Database with Mysql Qorkbench and i tried to Forward Engineer to Database but it completed with errors and this is the Message Log:
> Executing SQL script in server
ERROR: Error 1005: Can't create table `mydb`.`cds` (errno: 121 "Duplicate key on write or update")
SQL Code:
-- -----------------------------------------------------
-- Table `mydb`.`CDs`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`CDs` (
`CDid` INT NOT NULL AUTO_INCREMENT,
`Titel` VARCHAR(45) NOT NULL,
`Autor` VARCHAR(45) NOT NULL,
`Erscheinungsjahr` VARCHAR(45) NOT NULL,
`Genre` VARCHAR(45) NOT NULL,
`Stockwerk` VARCHAR(45) NOT NULL,
`Regal` INT NOT NULL,
`Ausgeborgt` INT NULL,
`Rezensionen` VARCHAR(600) NULL,
`Kurzbeschreibung` VARCHAR(600) NOT NULL,
PRIMARY KEY (`CDid`),
UNIQUE INDEX `CDid_UNIQUE` (`CDid` ASC),
INDEX `Buchungsid_idx` (`Ausgeborgt` ASC),
CONSTRAINT `Buchungsid`
FOREIGN KEY (`Ausgeborgt`)
REFERENCES `mydb`.`Buchung` (`Buchungsid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 9 succeeded, 1 failed
Fetching back view definitions in final form.
Could not get definition for mydb.view1 from server
1 views were read back.
This is the EER Diagram of my Database
A primary key is essentially an indexed unique not null constraint. You don't need to add an additional unique constraint, and indeed can't, as the error specifies. Remove the extra unique index clause and you should be OK:
CREATE TABLE IF NOT EXISTS `mydb`.`CDs` (
`CDid` INT NOT NULL AUTO_INCREMENT,
`Titel` VARCHAR(45) NOT NULL,
`Autor` VARCHAR(45) NOT NULL,
`Erscheinungsjahr` VARCHAR(45) NOT NULL,
`Genre` VARCHAR(45) NOT NULL,
`Stockwerk` VARCHAR(45) NOT NULL,
`Regal` INT NOT NULL,
`Ausgeborgt` INT NULL,
`Rezensionen` VARCHAR(600) NULL,
`Kurzbeschreibung` VARCHAR(600) NOT NULL,
PRIMARY KEY (`CDid`), -- no need for an extra index
INDEX `Buchungsid_idx` (`Ausgeborgt` ASC),
CONSTRAINT `Buchungsid`
FOREIGN KEY (`Ausgeborgt`)
REFERENCES `mydb`.`Buchung` (`Buchungsid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB

Mysql: using two foreign keys to the same table

I'm using MySQL workbench to design a database. Server is mysql 5.5.6
I've defined a few foreign keys linking the "candidates" table to the "countries" table. I get this error:
Executing SQL script in server
ERROR: Error 1005: Can't create table 'customer.candidats' (errno: 150)
The thing is: i'm referencing twice the countries table: once for the "nationality" column, once for the user address's country of origin. Is that allowed? Is this the right way to do it?
Here is the generated code that seems to trigger the issue.
CREATE TABLE IF NOT EXISTS `customer`.`candidats` (
`id` INT NOT NULL AUTO_INCREMENT,
`nom` VARCHAR(40) NULL,
`prenom` VARCHAR(40) NULL,
`qualite` ENUM('0001','0002') NULL COMMENT '0001 = Madame\n0002 = Monsieur',
`sexe` SET('1','2') NULL COMMENT '1 = Femme\n2 = Homme',
`date_de_naissance` DATE NULL,
`Nationalite` INT NOT NULL,
`selor_bilinguisme` TINYINT(1) NULL,
`rue` VARCHAR(60) NULL,
`numero` VARCHAR(10) NULL,
`pays` INT NOT NULL,
`region` INT NOT NULL,
`localité` VARCHAR(40) NULL,
`code_postal` VARCHAR(10) NULL,
`email` VARCHAR(241) NULL,
`tel_domicile` VARCHAR(30) NULL,
`tel_bureau` VARCHAR(30) NULL,
`tel_mobile` VARCHAR(30) NULL,
`tel_prefere` ENUM('01','02','03') NULL DEFAULT '03',
PRIMARY KEY (`id`),
INDEX `fk_candidats_pays_idx` (`Nationalite` ASC, `pays` ASC),
INDEX `fk_candidats_régions1_idx` (`region` ASC),
CONSTRAINT `fk_candidats_pays`
FOREIGN KEY (`Nationalite` , `pays`)
REFERENCES `customer`.`pays` (`id` , `id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_candidats_régions1`
FOREIGN KEY (`region`)
REFERENCES `customer`.`régions` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
The "pays" table ("countries" in French)
CREATE TABLE IF NOT EXISTS `customer`.`pays` (
`id` INT NOT NULL AUTO_INCREMENT,
`nom_fr` VARCHAR(45) NULL,
`nom_nl` VARCHAR(45) NULL,
`nationalite_fr` VARCHAR(45) NULL,
`nationalite_nl` VARCHAR(45) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `id_UNIQUE` (`id` ASC))
ENGINE = InnoDB
Your schema generator doesn't work correctly.
It should generate:
CONSTRAINT `fk_candidats_pays`
FOREIGN KEY (`pays`)
REFERENCES `customer`.`pays` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_candidats_Nationalite`
FOREIGN KEY (`Nationalite`)
REFERENCES `customer`.`pays` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
To your other question: This type of referencing seems strange when you see it the first time, but it's quite normal and I think there is no other way of constructing this type of relationship.