开发者

Unable to create tables in SQL script

I am having a problem running this script... every time I try to run it I get (2)cannot create table errors (1) for members table and (1) for session table

What am I overlooking?

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

DROP SCHEMA IF EXISTS `test_db` ;
CREATE SCHEMA IF NOT EXISTS `test_db` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `test_db` ;

-- -----------------------------------------------------
-- Table `test_db`.`role`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`role` ;

CREATE  TABLE IF NOT EXISTS `test_db`.`role` (
  `role_id` INT NOT NULL ,
  `roletype` ENUM('User','Instrct','Admin') NOT NULL ,
  PRIMARY KEY (`role_id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `test_db`.`Student`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`Student` ;

CREATE  TABLE IF NOT EXISTS `test_db`.`Student` (
  `student_id` INT NOT NULL AUTO_INCREMENT ,
  `Parent_id` INT NOT NULL ,
  `firstname` VARCHAR(60) NOT NULL ,
  `lastname` VARCHAR(60) NOT NULL ,
  `nickname` VARCHAR(45) NULL ,
  `birthday` DATE NOT NULL ,
  `gender` ENUM('male','female') NOT NULL ,
  PRIMARY KEY (`student_id`) ,
  UNIQUE INDEX `idStudent_UNIQUE` (`student_id` ASC) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `test_db`.`transaction_details`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`transaction_details` ;

CREATE  TABLE IF NOT EXISTS `test_db`.`transaction_details` (
  `transaction_id` INT NOT NULL ,
  `depositamount` INT NOT NULL ,
  `depositdate` DATE NOT NULL ,
  `balance` INT NULL ,
  `paymenttype` ENUM('cash','check','paypal') NULL ,
  PRIMARY KEY (`transaction_id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `test_db`.`transactions`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`transactions` ;

CREATE  TABLE IF NOT EXISTS `test_db`.`transactions` (
  `transaction_id` INT NOT NULL ,
  `user_id` INT NOT NULL ,
  PRIMARY KEY (`transaction_id`) ,
  CONSTRAINT `fk_transactions_transaction_details1`
    FOREIGN KEY (`transaction_id` )
    REFERENCES `test_db`.`transaction_details` (`transaction_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `test_db`.`member`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`member` ;

CREATE  TABLE IF NOT EXISTS `test_db`.`member` (
  `user_id` INT NOT NULL AUTO_INCREMENT ,
  `firstname` VARCHAR(60) NOT NULL ,
  `lastname` VARCHAR(60) NOT NULL ,
  `address_id` INT NULL ,
  `phone1` VARCHAR(45) NOT NULL ,
  `phone2` VARCHAR(45) NULL ,
  PRIMARY KEY (`user_id`) ,
  INDEX `address_id_UNIQUE` (`address_id` ASC) ,
  CONSTRAINT `fk_member_Student1`
    FOREIGN KEY (`user_id` )
    REFERENCES `test_db`.`Student` (`Parent_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_member_transactions1`
    FOREIGN KEY (`user_id` )
    REFERENCES `test_db`.`transactions` (`user_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `test_db`.`address`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`address` ;

CREATE  TABLE IF NOT EXISTS `test_db`.`address` (
  `address_id` INT NOT NULL AUTO_INCREMENT ,
  `addressline1` VARCHAR(45) NOT NULL ,
  `addressline2` VARCHAR(45) NULL ,
  `city` VARCHAR(45) NOT NULL ,
  `state` VARCHAR(45) NOT NULL ,
  `zipcode` VARCHAR(45) NOT NULL ,
  PRIMARY K开发者_StackOverflow社区EY (`address_id`) ,
  UNIQUE INDEX `address_id_UNIQUE` (`address_id` ASC) ,
  CONSTRAINT `fk_address_member1`
    FOREIGN KEY (`address_id` )
    REFERENCES `test_db`.`member` (`address_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `test_db`.`trainer`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`trainer` ;

CREATE  TABLE IF NOT EXISTS `test_db`.`trainer` (
  `trainer_id` INT NOT NULL ,
  `trainer_firstname` VARCHAR(45) NOT NULL ,
  `trainer_lastname` VARCHAR(45) NOT NULL ,
  `trainer_email` VARCHAR(45) NOT NULL ,
  `trainer_phone` VARCHAR(45) NOT NULL ,
  `address_address_id` INT NOT NULL ,
  PRIMARY KEY (`trainer_id`) ,
  INDEX `fk_trainer_address1` (`address_address_id` ASC) ,
  CONSTRAINT `fk_trainer_address1`
    FOREIGN KEY (`address_address_id` )
    REFERENCES `test_db`.`address` (`address_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `test_db`.`user_master`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`user_master` ;

CREATE  TABLE IF NOT EXISTS `test_db`.`user_master` (
  `user_id` INT NOT NULL AUTO_INCREMENT ,
  `username` VARCHAR(45) NOT NULL ,
  `password` VARCHAR(45) NOT NULL ,
  `role_id` INT NOT NULL ,
  PRIMARY KEY (`user_id`) ,
  UNIQUE INDEX `username_UNIQUE` (`username` ASC) ,
  INDEX `role_id` (`role_id` ASC) ,
  CONSTRAINT `role_id`
    FOREIGN KEY (`role_id` )
    REFERENCES `test_db`.`role` (`role_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_user_master_trainer1`
    FOREIGN KEY (`user_id` )
    REFERENCES `test_db`.`trainer` (`trainer_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_user_master_member1`
    FOREIGN KEY (`user_id` )
    REFERENCES `test_db`.`member` (`user_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `test_db`.`table1`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`table1` ;

CREATE  TABLE IF NOT EXISTS `test_db`.`table1` (
)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `test_db`.`location`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`location` ;

CREATE  TABLE IF NOT EXISTS `test_db`.`location` (
  `location_id` INT NOT NULL ,
  `locationname` VARCHAR(45) NOT NULL ,
  `locationaddress1` VARCHAR(80) NOT NULL ,
  `locationaddress2` VARCHAR(80) NULL ,
  `locationcity` VARCHAR(45) NOT NULL ,
  `locationZip` VARCHAR(5) NOT NULL ,
  `locationphone` VARCHAR(12) NOT NULL ,
  PRIMARY KEY (`location_id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `test_db`.`session_type`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`session_type` ;

CREATE  TABLE IF NOT EXISTS `test_db`.`session_type` (
  `style_id` INT NOT NULL ,
  `sessiontype` ENUM('private','semi-private','mini-group') NOT NULL ,
  `sessionlength` ENUM('20','30') NOT NULL ,
  `cost` INT NOT NULL ,
  `maxstudent` TINYINT NOT NULL ,
  `sessionlocation_id` INT NOT NULL ,
  PRIMARY KEY (`style_id`) ,
  INDEX `fk_sessionType_location1` (`sessionlocation_id` ASC) ,
  CONSTRAINT `fk_sessionType_location1`
    FOREIGN KEY (`sessionlocation_id` )
    REFERENCES `test_db`.`location` (`location_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `test_db`.`session`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`session` ;

CREATE  TABLE IF NOT EXISTS `test_db`.`session` (
  `session_id` INT NOT NULL AUTO_INCREMENT ,
  `session_Student_id` INT NOT NULL ,
  `sessionDay` ENUM('monday','tuesday','wednesday','thursday','friday','saturday','sunday') NOT NULL ,
  `sessiontime_id` INT NOT NULL ,
  `sessionTrainer_id` INT NOT NULL ,
  `sessionnotes` VARCHAR(250) NULL ,
  `session_type_id` INT NOT NULL ,
  `session_cost` INT NULL ,
  `transactions_transaction_id` INT NOT NULL ,
  PRIMARY KEY (`session_id`) ,
  INDEX `fk_session_Student1` (`session_Student_id` ASC) ,
  INDEX `fk_session_trainer1` (`sessionTrainer_id` ASC) ,
  INDEX `fk_session_cost` (`session_cost` ASC) ,
  INDEX `fk_session_session_type1` (`session_type_id` ASC) ,
  INDEX `fk_session_transactions1` (`transactions_transaction_id` ASC) ,
  CONSTRAINT `fk_session_Student1`
    FOREIGN KEY (`session_Student_id` )
    REFERENCES `test_db`.`Student` (`student_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_session_trainer1`
    FOREIGN KEY (`sessionTrainer_id` )
    REFERENCES `test_db`.`trainer` (`trainer_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_session_sessioncost`
    FOREIGN KEY (`session_cost` )
    REFERENCES `test_db`.`session_type` (`cost` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_session_session_type1`
    FOREIGN KEY (`session_type_id` )
    REFERENCES `test_db`.`session_type` (`style_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_session_transactions1`
    FOREIGN KEY (`transactions_transaction_id` )
    REFERENCES `test_db`.`transactions` (`transaction_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;



SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;


The problem is that your foreign key constraints are referencing columns in the other table which are not indexed. Adding indexes to Student.Parent_id and transactions.user_id allowed me to create the members table. Double-check all your foreign key constraints that they are pointing to indexed columns.


Referenced columns should be unique.

  1. Table test_db.member -> REFERENCES test_db.Student (Parent_id )
  2. Table test_db.session-> REFERENCES test_db.session_type (cost )

  3. CREATE TABLE IF NOT EXISTS test_db.table1 ( ) ENGINE = InnoDB;

Check your schema (primary keys, unique keys, foreign keys) if they are correct.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜