无法在SQL脚本中创建表

时间:2011-07-29 05:26:19

标签: mysql sql create-table

我在运行此脚本时出现问题...每次尝试运行它时,我得到(2)无法为成员表创建表错误(1)和(1)对于会话表

我在俯瞰什么?

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 KEY (`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;

2 个答案:

答案 0 :(得分:0)

问题是您的外键约束引用了另一个表中未编制索引的列。将索引添加到StudentParent_idtransactionsuser_id允许我创建成员表。仔细检查它们指向索引列的所有外键约束。

答案 1 :(得分:0)

引用列应该是唯一的。

  1. 表格test_dbmember - >参考test_dbStudentParent_id
  2. test_dbsession - >参考test_dbsession_typecost

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

  4. 检查您的架构(主键,唯一键,外键)是否正确。