Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ON DELETE CASCADE

Posted on 2003-11-06
7
Medium Priority
?
835 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 140 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

927 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