Solved

ON DELETE CASCADE

Posted on 2003-11-06
7
823 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

626 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