Jump to content

how should I make this database?


CyberShot

Recommended Posts

I have been searching for days for a good tutorial that covers this and have not been able to find one. I am using wamp and have phpmyadmin set up and everything is working fine. I am trying to figure out how to make my database work properly. Lets say I have three tables

 

names

phone

bills

 

I want the phone and bills table to be linked to the names table so that when I select a name, the phone and bills information will come up with the name. but if no phone number is entered and no bills are entered, then nothing should come up with that. The only way I can find to do it is to manually update the foreign key field when you insert records. I don't want to do that, I want the database to auto increment. I can't figure out how to do it. Do you know of a tutorial?

Link to comment
Share on other sites

that had some good information in it but still misses the mark. there was nothing good in there about how to set up the database in phpmyadmin or in sql. It leaves out the foreign key inforamation. Doesn't talk about how the tables are linked together when you have null values. The search continues

Link to comment
Share on other sites

yep. I am sure. Yes, it talked about 1nf and second and so on but didn't show how to relate the tables. It talked about how you would get an error if you tried to add to the tables in a certain way. I have gotten those errors. What I wanted the article to do was give a step by step on how to set up the database it was talking about. Primary and foreign keys. and then show how to insert data into those tables while keeping the relationship. It did not do that.

Link to comment
Share on other sites

i used the nnodb or whatever it is. Here is exactly what I am trying to figure out

 

I want to make a database for a bill application. So I will have

 

tables = names, phone, amounts

 

so phone and amounts will be linked to names by foreign keys. so the idea is that I can enter 4 names, 2 numbers and one amount and be able to pull the information out of the database that goes with the right name. That means that I think the phone table and the amounts table needs to know how to keep track of what information goes with which name. I have tried setting the relationships but I get errors when it comes time to input the data. So how do you set up the database so that it will keep track of the id's automatically like the auto increment? Or do I have to tell each table to automatically insert blanks if there is no data so that each insert will match up?

 

Link to comment
Share on other sites

DROP TABLE IF EXISTS bill;
DROP TABLE IF EXISTS contact;
DROP TABLE IF EXISTS user;

CREATE TABLE user (
  user_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  ..other
) ENGINE=InnoDB;

CREATE TABLE contact (
  contact_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id INT UNSIGNED NOT NULL,
  ..other
  INDEX (user_id),
  FOREIGN KEY (user_id) REFERENCES user (user_id),
  PRIMARY KEY (contact_id)
) ENGINE=InnoDB;

CREATE TABLE bill (
  bill_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id INT UNSIGNED NOT NULL,
  ..other
  INDEX (user_id),
  FOREIGN KEY (user_id) REFERENCES user (user_id),
  PRIMARY KEY (bill_id)
) ENGINE=InnoDB;

Link to comment
Share on other sites

The foreign keys do not auto-increment.  You have to enter the data into the first table, and then get the ID (that was created via autoincrement) using mysql_insert_id().  Then use that to insert into the table that contains the foreign key.

 

So, in your case, you would insert into the user table and use mysql_insert_id() to get the user_id you just created.  Then you'll use that to set the value of user_id in the insert statement to the contact and/or bill tables.  If the user already exists, you look up that user and get the user_id, and again use that to insert into the other tables.  The foreign key relations don't do that for you (the engine would have no way of knowing what record you want to link); the relations merely prevent you from having an invalid link and other automated management once the link exists.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.