Strange Error When Creating Table!

Discussion in 'Databases' started by Masterful, Sep 2, 2008.

  1. #1
    I'm trying to create my first database. The problem is, I get a strange error when I try to create my last table (Coupon_Table). It's the only one with foreign keys, which I think are causing the error. What am I doing wrong? Please advise.

    Database

    Advertiser_Table

    advertiser_id
    advertiser_name
    advertiser_logo
    advertiser_url

    -------------

    Offer_Instructions_Table

    offer_instructions_id
    offer_instructions
    block_anchor_text

    -------------

    Category_Table

    category_id
    category_name
    category_url

    -------------

    Coupon_Table

    coupon_id
    offer_anchor_text
    offer_url
    coupon_code
    offer_expiration_date
    advertiser_id
    offer_instructions_id
    category_id

    Code

    create table Advertiser_Table (
    advertiser_id smallint unsigned not null auto_increment primary key,
    advertiser_name char(25),
    advertiser_logo char(25),
    advertiser_url char(30)
    ) engine=innodb;


    create table Offer_Instructions_Table (
    offer_instructions_id smallint unsigned not null auto_increment primary key,
    offer_instructions char(45),
    block_anchor_text char(10)
    ) engine=innodb;


    create table Category_Table (
    category_id smallint unsigned not null auto_increment primary key,
    category_name char(20),
    category_url char(36)
    ) engine=innodb;


    create table Coupon_Table (
    coupon_id smallint unsigned not null auto_increment primary key,
    offer_anchor_text char(40),
    offer_url varchar(200),
    coupon_code char(25),
    offer_expiration_date date,
    foreign key (advertiser_id) references Advertiser_Table (advertiser_id),
    foreign key (offer_instructions_id) references Offer_Instructions_Table (offer_instructions_id),
    foreign key (category_id) references Category_Table (category_id)
    ) engine=innodb;

    ERROR 1072 (42000): Key column 'advertiser_id' doesn't exist in table
     
    Masterful, Sep 2, 2008 IP
  2. wootty

    wootty Peon

    Messages:
    447
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I would say you need to actually define advertiser_id etc as columns in Coupon_Table, and then define the foreign keys on them.
     
    wootty, Sep 3, 2008 IP
  3. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Hi, Wootty,

    Do you mean create Coupon_Table first, then the other (parent) tables? If so, I've already tried that. I still get the same error.

    Any other ideas?
     
    Masterful, Sep 3, 2008 IP
  4. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #4
    Oh . . . Now I get it! ;)

    I should define the columns' attributes (smallint, etc.), before assigning them as foreign keys.

    Thanks, Wootty!

    I tried to give you reputation points again but the system wouldn't let me. Apparently, I have to spread some points around before giving more to you. Sorry, dude.
     
    Masterful, Sep 3, 2008 IP
  5. vasago

    vasago Peon

    Messages:
    95
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    create table Coupon_Table (
    coupon_id smallint unsigned not null auto_increment primary key,
    offer_anchor_text char(40),
    offer_url varchar(200),
    coupon_code char(25),
    offer_expiration_date date,
    foreign key (advertiser_id) references Advertiser_Table (advertiser_id),
    foreign key (offer_instructions_id) references Offer_Instructions_Table (offer_instructions_id),
    foreign key (category_id) references Category_Table (category_id)
    ) engine=innodb;


    You need a field called advertiser_id in this table too.
    For example:

    create table Coupon_Table (
    coupon_id smallint unsigned not null auto_increment primary key,
    offer_anchor_text char(40),
    offer_url varchar(200),
    coupon_code char(25),
    offer_expiration_date date,
    advertiser_id smallint unsigned not null
    foreign key (advertiser_id) references Advertiser_Table (advertiser_id),
    foreign key (offer_instructions_id) references Offer_Instructions_Table (offer_instructions_id),
    foreign key (category_id) references Category_Table (category_id)
    ) engine=innodb;

    It should work now.
     
    vasago, Sep 5, 2008 IP