Search code examples
mysqlsqlforeign-keyscreate-table

How can I fix this syntax to make it work?


I've been trying to create foreign keys on a table to refer to a price table, this is the syntax I have so far, which creates errors,

CREATE TABLE shirts(
        shirt_id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
        shirt_name VARCHAR(100) NOT NULL,
        shirt_type VARCHAR(10) NOT NULL,
        shirt_size VARCHAR(20) NOT NULL,
        qp_price NUMERIC(6,2) FOREIGN KEY REFERENCES price_list.price ON price_list.price_id=shirts.qp_price NOT NULL,
        o_price NUMERIC(6,2) FOREIGN KEY REFERENCES price_list.price ON price_list.price_id=shirts.o_price NOT NULL,
        clr_options VARCHAR(30) NULL,
        qty NUMERIC(5,0) NULL
    )ENGINE=INNODB

The "price table" has 3 columns, which is price_id, price_cat and price. What I want the qp_price and o_price columns in the shirts table do is show the price column of the price_list table according to the number I put into those columns on the shirts table. For example,

if I do

INSERT INTO shirts(shirt_name,shirt_type,shirt_size,qp_price,o_price)VALUES
        ('Crewneck Tee','Men','S','1','2'),
        ('Crewneck Tee','Men','M','1','2'),
        ('Crewneck Tee','Men','L','1','2'),
        ('Crewneck Tee','Men','1X','1','2'),
        ('Crewneck Tee','Men','2X','3','4'),
        ('Crewneck Tee','Men','3X','5','6'),
        ('Crewneck Tee','Men','4X','7','8'),
        ('Crewneck Tee','Men','5X','9','10')

the S-L crewneck tees would be showing the price in the 1st and 2nd rows of the price column because it's linking to the price_id column on the price_list table... how can I do this?


Solution

  • The ON in a CREATE TABLE statmenet FOREIGN KEY REFERENCES clause is not a join, it's meant to specify behavior, like ON UPDATE CASCADE ON DELETE RESTRICT.

    Also, I found this comment regarding MySQL 5.1 at 14.6.4.4. FOREIGN KEY Constraints:

    Ensure that you indexed both Table 1 and Table 2's referenced columns on Table 1 and Table 2 respectively. If you don't, the error "#1005 - Can't create table 'prospect' (errno:105)" will be flagged. (Indexing is good practice as it avoids full table-scans!) Once this is taken care of and the referenced columns are of same data-type, you would have successfully created your desired table with as many FKs as you want on it.

    CREATE TABLE shirts(
        shirt_id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
        shirt_name VARCHAR(100) NOT NULL,
        shirt_type VARCHAR(10) NOT NULL,
        shirt_size VARCHAR(20) NOT NULL,
        qp_price NUMERIC(6,2) NOT NULL, 
        o_price NUMERIC(6,2) NOT NULL, 
        clr_options VARCHAR(30) NULL,
        qty NUMERIC(5,0) NULL
        INDEX (qp_price),
        INDEX (o_price),
        FOREIGN KEY (qp_price) REFERENCES price_list(price_id),
        FOREIGN KEY (o_price) REFERENCES price_list(price_id),
    )ENGINE=INNODB
    

    FYI, http://www.dpriver.com/pp/sqlformat.htm is pretty useful when you don't happen to have a particular database environment stood up.