need help with database please

Discussion in 'MySQL' started by Alidad, Sep 22, 2008.

  1. #1
    Perhaps someone can help me with an example I've come up with to help me to better understand how to build a relationship in Mysql.

    I have database name “members” and two table name “registered” and “Screen”

    Registered table:

    First name
    Last name
    User name
    Password

    Screen table:

    Title
    Category
    Date
    Comments

    What they do is that when member log in from registered table, then input information then send data to the Screen table.

    Now I have two questions:

    1) How would or SHOULD I go about creating the database relationship to show that the screen table is come from right user name!

    2) How do I create insert statement into the Screen table that come from right user name!

    Please help thanks.

    AM
     
    Alidad, Sep 22, 2008 IP
  2. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #2
    Regarding #1, it depends on the relationship of the two entities. Will there be only 1 screen entry for each user? If so, a 1-to-1 relationship is appropriate. Will there be more than 1 screen entry per user, then you have a 1-to-many relationship, etc. Model to meet your requirements.

    Regarding #2, you should have the users rec id in the session or persisted in some temporary store after login. Use the value as needed to insert the record into the database. I just noticed that the Registered table does not have a numeric record identifier. I would suggest you add this unless you want to join on the VARCHAR username column (I recommend not doing so).

    Anyway, back to an example:

    Registered
    UserRecID
    FirstName
    LastName
    UserName
    Password

    Screen
    ScreenRecID
    UserRecID
    Title
    Category
    Date
    Comments

    After a user logs in, you should have the UserRecID value. When the user saves a Screen record your insert will look something like this ... INSERT into dbo.Screen (UserRecID, Title, Category, Date, Comments) VALUES (1234, "Some Title", "Some Category", 10-10-2008, "Some Comments").

    The 1234 value is the UserRecID from the logged in user. I hope this makes sense and gets you moving in the right direction. I assumed a 1-to-many relationship, but the approach is the same. Just model and update the SQL as needed. Good luck.
     
    Social.Network, Sep 22, 2008 IP