Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 25th, 2003, 10:05 PM
Authorized User
 
Join Date: Oct 2003
Location: kl, wp, Malaysia.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default MySql tables

Below are a few of my mysql tables. I have problem with the sales_detail table. How can i make the prod_id in the sales_detail table reference the prod_id in the product table? i'm confused as the sales_id and prod_id in sales details are primary key. Can primary key be reference? I have this problem because i once accidentally enter a prod_id in sales_details that does not exist in the product table and the database accepts it without error. How can i correct this situation? If yes, how can i alter the sales_details table?

CREATE TABLE PRODUCT
(
    PROD_ID VARCHAR(5) NOT NULL,
    PROD_NAME VARCHAR(20) NOT NULL,
    PROD_DESC VARCHAR(20) NOT NULL,
    PROD_PRICE DECIMAL NOT NULL,
    PROD_CAT INT NOT NULL,
    PRIMARY KEY(PROD_ID),
    FOREIGN KEY(PROD_CAT) REFERENCES CATEGORY,
);
CREATE TABLE SALES
(
    SALES_ID INT NOT NULL,
    SALES_DATE DATE NOT NULL,
    CUST_ID VARCHAR(5) NOT NULL,
    EMP_ID VARCHAR(6) NOT NULL,
    PRIMARY KEY(SALES_ID),
    FOREIGN KEY (CUST_ID) REFERENCES CUSTOMER,
    FOREIGN KEY (EMP_ID) REFERENCES EMPLOYEE,
);

CREATE TABLE SALES_DETAIL
(
    SALES_ID INT NOT NULL,
    PROD_ID VARCHAR(5) NOT NULL,
    QUANTITY INT NOT NULL,
    PRIMARY KEY (SALES_ID, PROD_ID),
);

Reply With Quote
  #2 (permalink)  
Old November 26th, 2003, 01:27 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In MySQL you need to be using the InnoDB table type. The default type is MyISAM, if I remember correctly. As a developer, the best you can do with MySQL and the default table type is to write your code to ONLY insert valid values into your tables. Don't insert something into a table that shouldn't be there.

If you'd still like the database engine to enforce these constraints, read the manual:
  http://www.mysql.com/doc/en/InnoDB_f...nstraints.html



Take care,

Nik
http://www.bigaction.org/
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mysql Sorting results from multiple tables dcb22 SQL Language 8 October 25th, 2004 10:57 AM
Category SubCategories and two MySQL tables cjp_qld PHP Databases 2 August 29th, 2004 04:14 AM
Dynamically Link MySQL Tables at Runtime Rational Access 0 June 27th, 2004 09:15 PM
Display all tables in a mySql database question mb92010 MySQL 1 April 9th, 2004 04:26 PM
Locking tables using mySQL fcamilo Beginning PHP 1 June 13th, 2003 12:30 AM



All times are GMT -4. The time now is 02:07 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.