Solved

ON DELETE CASCADE

Posted on 2003-11-06
7
816 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 17

Accepted Solution

by:
Squeebee earned 35 total points
ID: 9695922
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
ID: 9695964
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
ID: 9695986
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
Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

 
LVL 17

Expert Comment

by:Squeebee
ID: 9696012
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
ID: 9696064
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
ID: 9696565
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
ID: 9696882
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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