Solved

ON DELETE CASCADE

Posted on 2003-11-06
7
794 Views
Last Modified: 2008-03-06
I have two tables created using following:
CREATE TABLE USERS(
      ID             int(11)       NOT NULL,
      FNAME             varchar(25)      NOT NULL,
      LNAME            varchar(25)      NOT NULL,
      Division       varchar(20),
      Address       varchar(100),
      Phone             varchar(20),
      Email             varchar(30)      NOT NULL,
      Login             varchar(10)      NOT NULL,
      Password       varchar(10)      NOT NULL,
      IPRequested       varchar(20),

      FOREIGN KEY(Division)
      REFERENCES Divisions(DivisionName)
      ON DELETE RESTRICT ON UPDATE CASCADE,

      PRIMARY KEY(ID)
)
CREATE TABLE ACCESS(
      User             int(11)       NOT NULL,
      Realm            varchar(35)      NOT NULL,

      FOREIGN KEY(User)
      REFERENCES Users(ID) ON DELETE CASCADE ON UPDATE CASCADE,
      FOREIGN KEY(Realm)
      REFERENCES Realms(RealmName) ON DELETE RESTRICT ON UPDATE CASCADE,

      PRIMARY KEY(User, Realm)
)

When I delete a user from USERS, the corresponding records in ACCESS don't get deleted.
Am I messing up something in SQL?
I never used MySQL before.
Thank you
0
Comment
Question by:Gula
  • 4
  • 3
7 Comments
 
LVL 17

Accepted Solution

by:
Squeebee earned 35 total points
Comment Utility
Well, if this is a direct quote of your create table statements, then I do not see you assigning the tables as InnoDB. Innodb tables are required for foreign keys.

CREATE TABLE USERS(
     ID           int(11)      NOT NULL,
     FNAME           varchar(25)     NOT NULL,
     LNAME          varchar(25)     NOT NULL,
     Division      varchar(20),
     Address      varchar(100),
     Phone           varchar(20),
     Email           varchar(30)     NOT NULL,
     Login           varchar(10)     NOT NULL,
     Password      varchar(10)     NOT NULL,
     IPRequested      varchar(20),

     FOREIGN KEY(Division)
     REFERENCES Divisions(DivisionName)
     ON DELETE RESTRICT ON UPDATE CASCADE,

     PRIMARY KEY(ID)
)TYPE = InnoDB;

CREATE TABLE ACCESS(
     User           int(11)      NOT NULL,
     Realm          varchar(35)     NOT NULL,

     FOREIGN KEY(User)
     REFERENCES Users(ID) ON DELETE CASCADE ON UPDATE CASCADE,
     FOREIGN KEY(Realm)
     REFERENCES Realms(RealmName) ON DELETE RESTRICT ON UPDATE CASCADE,

     PRIMARY KEY(User, Realm)
)TYPE = InnoDB;
0
 
LVL 1

Author Comment

by:Gula
Comment Utility
Thank you,
I have never used InnoDB in other databases, is it only for MySQL?
and what other types are there?

Thank again.
0
 
LVL 1

Author Comment

by:Gula
Comment Utility
I tried to add TYPE = InnoDB and it gave me
Can't create table './userlogin/users.frm' (errno: 150)

Can I have varchar foreign keys?
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 17

Expert Comment

by:Squeebee
Comment Utility
You can foreign key any column type, but you should make sure they match on both tables. Did you drop the table before re-issueing the create statement? That could cause the error you are seeing.

0
 
LVL 17

Expert Comment

by:Squeebee
Comment Utility
The two main types for tables are MyISAM and InnoDB.

You can read about all teh table types at http://www.mysql.com/doc/en/Table_types.html
0
 
LVL 1

Author Comment

by:Gula
Comment Utility
I got my query to work after 1 hour trying everything in this world.
Finally it worked when I made the Foreign key index before declaring the foreign key.

Do I always have to do that?

0
 
LVL 17

Expert Comment

by:Squeebee
Comment Utility
Yes you do, sorry I should have looked closer at your create table. There is an error message that gives the problem away, but I would have had to see it to know the key was missing.

In any case, yes. Foreign keys can only be created on indexed columns.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now