Solved

ON DELETE CASCADE

Posted on 2003-11-06
7
801 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

19 Experts available now in Live!

Get 1:1 Help Now