Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 25th, 2003, 10:05 PM
Authorized User
 
Join Date: Oct 2003
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),
);

 
Old November 26th, 2003, 01:27 PM
Friend of Wrox
 
Join Date: Jun 2003
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/





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





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