开发者

mysql database relational problems

Hi ok I have two tables one called Login and one called User there is a one to one relationship from the user table to the login table:

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

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

I cant get my head around it but I dont know when it comes to coding it on my page how the UserID is set in the Login table related to the actual user?

If I take the client to the create user page and he fills in the user details then I forward him to the username and password fields which goes to a different table how is the UserID set in the Login Table? Atm its not?

Incase any one wants to reverse engineer my table data in mysql eer diagram see code below:

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

CREATE SCHEM开发者_JS百科A 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 SQL_MODE=@OLD_SQL_MODE;

SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;

SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;


If User and Login has a 1:1 relation, you should drop LoginID from the Login table, and make UserID the primary key instead.
Or, if all users have login-information, you may consider moving the columns into the User table instead. (By doing so you also remove an entire class of problems related to enforcing 1-1 relations).

You should not insert any rows in the database until the user completes the entire registration form. Keep the form data in the session, and then at the end perform all inserts in all tables at once, then commit.

There is probably a function in c# to find out the last inserted id (UserID) to put in the Login table, otherwise have a look at the MySQL documentation for insert ID

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜