2011-07-29 84 views
0

我有運行此腳本的一個問題......每次我嘗試運行它,我得到(2)不能創建表錯誤(1)會員表(1)會話表無法創建SQL腳本表

我忽略了什麼?

SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0; 
SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; 
SET @[email protected]@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 [email protected]_SQL_MODE; 
SET [email protected]_FOREIGN_KEY_CHECKS; 
SET [email protected]_UNIQUE_CHECKS; 

回答

0

的問題是,你的外鍵約束中未編入索引的其他表引用列。將索引添加到StudentParent_idtransactionsuser_id允許我創建成員表。仔細檢查它們指向索引列的所有外鍵約束。

+0

完全工作!非常感謝! – Matthew

0

引用的列應該是唯一的。

  1. test_dbmember - >參考test_dbStudentParent_id
  2. test_db。參考test_dbsession_typecost

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

檢查您的模式(主鍵,唯一鍵,外鍵)是否正確。