Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Help with RDBMS and PHP.

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with RDBMS and PHP.

    Hello all!

    I am building an application and I've got some problems with MYSQL(I am newbie in it). Basically I am trying to build an application whose basic database structure looks like this:

    Table jokes:
    field id
    field name
    field email
    field author_id

    Table authors
    field author_id
    field author_name

    Now what I want is to be the author_id field relational to both tables. How can I do it without the php script to write the id on both tables? How do I set up my database?

    I hope I was able to clear myself.

    Thanks!

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    author_id = primary key in authors category
    author_id = foreign key in jokes category

  3. #3
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    boogyman: Thanks for the reply but how do I do that?

  4. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Code:
    DROP TABLE IF EXISTS Author {
     author_id INT NOT NULL AUTO_INCREMENT;
     author_name VARCHAR(50);
     PRIMARY KEY(author_id);
    }
    DROP TABLE IF EXISTS Joke {
     joke_id INT NOT NULL AUTO_INCREMENT;
     joke_title VARCHAR(20);
     joke_message VARCHAR(1024);
     PRIMARY KEY(joke_id);
     FOREIGN KEY(author_id);
     INDEX(joke_title);
    }
    that will create both of your tables and make the primary keys and it will index the joke_title, which if you choose to set up the site with a "search" bar will allow the user to search for a title.
    ceiling of 20 characters - title 1024 characters - message
    Code:
    INSERT INTO table name VALUES(values);
    NOTE: seperate values by commas( , )

    that is all good and dandy if you want to insert a whole row into the database, but if you want to only insert a specific field in a record use
    Code:
    INSERT INTO table name(col1,col2) VALUES(value1, value2);
    also, I suggest you take a look at the Official MySQL Manual

  5. #5
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    But ... wait a minute, don't I need a author_id column in table Joke too?

    I've seen the manual but couldn't get anything out of it.

  6. #6
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    oops sorry I made a couple errors, should be

    Code:
    DROP TABLE IF EXISTS Author {
     author_id INTEGER NOT NULL AUTO_INCREMENT;
     author_name VARCHAR(50);
     PRIMARY KEY(author_id);
    }
    DROP TABLE IF EXISTS Joke {
     joke_id INTEGER NOT NULL AUTO_INCREMENT;
     joke_title VARCHAR(20);
     joke_message VARCHAR(1024);
     author_id INTEGER NOT NULL;
     PRIMARY KEY(joke_id);
     FOREIGN KEY(author_id) REFERENCES Author(author_id);
     INDEX(joke_title);
    }
    sry

  7. #7
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    nevermind, it's not your fault.

    So, now if I insert the id into the Author table, will it automagically add that tho the Joke table?

  8. #8
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    By the way, I am getting lots of errors and the sql doesn't seem to work.

  9. #9
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by shachi View Post
    So, now if I insert the id into the Author table, will it automagically add that tho the Joke table?
    the author_id field will automatically increment itself, so you should be able to just leave that blank, and I am not entirely certain how it manages itself, but yes your author_id will be updated as well.



    Quote Originally Posted by shachi View Post
    By the way, I am getting lots of errors and the sql doesn't seem to work.
    Yah I made more errors :/ change the curly braces { } on the tables to parenthesis ( )
    [code]

    Code:
    DROP TABLE IF EXISTS Author (
     author_id INTEGER NOT NULL UNIQUE AUTO_INCREMENT;
     author_name VARCHAR(50);
     PRIMARY KEY(author_id);
    ) ENGINE=InnoDB;
    [B][COLOR="Green"])
    DROP TABLE IF EXISTS Joke (
     joke_id INTEGER NOT NULL UNIQUE AUTO_INCREMENT;
     joke_title VARCHAR(20);
     joke_message VARCHAR(1024);
     author_id INTEGER NOT NULL;
     PRIMARY KEY(joke_id);
     FOREIGN KEY(author_id) REFERENCES Author(author_id);
     INDEX(joke_title);
    ) ENGINE=InnoDB;
    and also notice that I added the engine type. I didnt know this either, but I guess that is the only engine that supports foreign keys

    Let me know if you got any other problems

  10. #10
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yep, I fixed it(I made the dbs manually).

    There's one thing I am confused about now, which database do I put the data in? The Author or the Joke?

    Thanks again.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •