2011-03-15 51 views
0

嗨好,我有兩個表一個名爲登錄名和一個叫做用戶有來自用戶表一一對應的關係到登錄表:的mysql數據庫中的關係問題

Login: 
=-=-=-=- 
LoginID (Primary, Not Null, Auto Inc) 
UserID (Primary) 
Username (not null) 
Password (not null) 

User: 
=-=-=-=- 
UserID (Primary) 
Email 
Name etc 

我再也無法迴避它我的頭但我不知道什麼時候將它編碼到我的頁面上如何在與實際用戶相關的登錄表中設置UserID?

如果我將客戶端轉到創建用戶頁面並填寫用戶詳細信息,那麼我會將他轉到用戶名和密碼字段,該字段會轉到另一個表中登錄表中的UserID設置如何? Atm不是?

櫃面任何人想反向工程MySQL的EER圖我的表中的數據看下面的代碼:

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'; 

CREATE SCHEMA IF NOT EXISTS `gymwebsite` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ; 

USE `gymwebsite` ; 
-- ----------------------------------------------------- 

-- Table `gymwebsite`.`User` 

-- ----------------------------------------------------- 

CREATE TABLE IF NOT EXISTS `gymwebsite`.`User` (
    `UserID` INT NOT NULL AUTO_INCREMENT , 
    `Email` VARCHAR(245) NULL , 
    `FirstName` VARCHAR(45) NULL ,  
    `SecondName` VARCHAR(45) NULL ,  
    `DOB` VARCHAR(15) NULL ,  
    `Location` VARCHAR(45) NULL ,  
    `Aboutme` VARCHAR(245) NULL ,  
    PRIMARY KEY (`UserID`)) 
ENGINE = InnoDB; 

-- ----------------------------------------------------- 

-- Table `gymwebsite`.`Pictures` 

-- ----------------------------------------------------- 

CREATE TABLE IF NOT EXISTS `gymwebsite`.`Pictures` (
    `PictureID` INT NOT NULL AUTO_INCREMENT , 
    `UserID` INT NOT NULL , 
    PRIMARY KEY (`PictureID`, `UserID`) , 
    INDEX `fk_Pictures_Userinfo1` (`UserID` ASC) , 
    CONSTRAINT `fk_Pictures_Userinfo1` 
    FOREIGN KEY (`UserID`) 
    REFERENCES `gymwebsite`.`User` (`UserID`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 

-- Table `gymwebsite`.`WallPostings` 

-- ----------------------------------------------------- 

CREATE TABLE IF NOT EXISTS `gymwebsite`.`WallPostings` (
    `UserwallID` INT NOT NULL AUTO_INCREMENT , 
    `UserID` INT NOT NULL , 
    PRIMARY KEY (`UserwallID`, `UserID`) , 
    INDEX `fk_WallPostings_Userinfo1` (`UserID` ASC) , 
    CONSTRAINT `fk_WallPostings_Userinfo1` 
    FOREIGN KEY (`UserID`) 
    REFERENCES `gymwebsite`.`User` (`UserID`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 

-- Table `gymwebsite`.`Login` 

-- ----------------------------------------------------- 

CREATE TABLE IF NOT EXISTS `gymwebsite`.`Login` (
    `LoginID` INT NOT NULL AUTO_INCREMENT , 
    `UserID` INT NOT NULL , 
    `username` VARCHAR(245) NOT NULL , 
    `password` VARCHAR(45) NOT NULL , 
    PRIMARY KEY (`LoginID`, `UserID`) , 
    INDEX `fk_Login_Userinfo` (`UserID` ASC) , 
    CONSTRAINT `fk_Login_Userinfo` 
    FOREIGN KEY (`UserID`) 
    REFERENCES `gymwebsite`.`User` (`UserID`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 

-- Table `gymwebsite`.`DietPlan` 

-- ----------------------------------------------------- 

CREATE TABLE IF NOT EXISTS `gymwebsite`.`DietPlan` (
    `DietPlanID` INT NOT NULL AUTO_INCREMENT , 
    `UserID` INT NOT NULL , 
    PRIMARY KEY (`DietPlanID`, `UserID`) , 
    INDEX `fk_DietPlan_Userinfo1` (`UserID` ASC) , 
    CONSTRAINT `fk_DietPlan_Userinfo1` 
    FOREIGN KEY (`UserID`) 
    REFERENCES `gymwebsite`.`User` (`UserID`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB; 

-- ----------------------------------------------------- 

-- Table `gymwebsite`.`WorkoutPlan` 

-- ----------------------------------------------------- 

CREATE TABLE IF NOT EXISTS `gymwebsite`.`WorkoutPlan` (

    `WorkOutID` INT NOT NULL AUTO_INCREMENT , 

    `UserID` INT NOT NULL , 

    PRIMARY KEY (`WorkOutID`, `UserID`) , 

    INDEX `fk_WorkoutPlan_Userinfo1` (`UserID` ASC) , 

    CONSTRAINT `fk_WorkoutPlan_Userinfo1` 

    FOREIGN KEY (`UserID`) 

    REFERENCES `gymwebsite`.`User` (`UserID`) 

    ON DELETE NO ACTION 

    ON UPDATE NO ACTION) 

ENGINE = InnoDB; 





-- ----------------------------------------------------- 

-- Table `gymwebsite`.`Friends` 

-- ----------------------------------------------------- 

CREATE TABLE IF NOT EXISTS `gymwebsite`.`Friends` (

    `idFriends` INT NOT NULL AUTO_INCREMENT , 

    `UserID` INT NOT NULL , 

    PRIMARY KEY (`idFriends`, `UserID`) , 

    INDEX `fk_Friends_Userinfo1` (`UserID` ASC) , 

    CONSTRAINT `fk_Friends_Userinfo1` 

    FOREIGN KEY (`UserID`) 

    REFERENCES `gymwebsite`.`User` (`UserID`) 

    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; 

回答

1

如果UserLogin有一個1:1的關係,你應該從登錄表中刪除登錄ID和改爲使UserID成爲主鍵。
或者,如果所有用戶都有登錄信息,則可以考慮將列移動到User表中。 (通過這樣做,您還可以刪除與執行1-1關係有關的所有問題)。

您應該而不是插入數據庫中的任何行,直到用戶完成整個註冊表單。保持會話中的表單數據,然後在最後執行所有表中的所有插入,然後提交。

有可能是C#中的功能,找出最後插入的ID(用戶名)把在登錄表,否則看看MySQL的文檔insert ID

+0

是的,我想我將不得不有這樣做,謝謝你的確認 –