Ask a Question related to MySQL, Design and Development.
-
lyonnyte #1
Self Referencing foreign key
I am trying to create an employee/supervisor relationship. Since both
are essentially employees, I created a foreign key relationship which
referenced itself. I was wondering if there is a better way to do
this? Maybe without having to create the first record?
CREATE TABLE employee
(
employeeid longint(20) unsigned NOT NULL auto_increment,
employeefirstname varchar(20) NOT NULL,
employeelastname varchar(20) NOT NULL,
employeetitle varchar(10) ,
supervisorid longint(20) unsigned,
primary key(employeeid),
);
INSERT INTO employee VALUES
('1', 'Chief', 'Officer', 'CEO', ' ');
ALTER TABLE 'employee' ADD FOREIGN KEY(supervisorid) REFERENCES
'employee'('employeeid') ON DELETE CASCADE ;
lyonnyte Guest
-
Referencing the Table Name
Say I have a Table called Predator: country(varchar 20), animal(varchar 20) And I want to search for the animal "Python" in my Predator table,... -
Foreign key
Hi First, i assume i RTFM but am new to MySQL world so i got some problems. I got 2 tables TABLE1 T1Col1 Varchar(30) PRIMARY KEY TABLE2... -
Cross referencing in XML
Hello all, My company does medical publishing and we are in the process of switching over from FrameMaker+SGML to InDesign. One of the issues we are... -
referencing scenes
Hi Can anyone tell me how to reference a scene from another scene If you just want the next scene its: nextScene(); but what if you want to target... -
referencing XML nodes by name
I would like to access the info in an XML file by name but Macromedias XML-Xtra instructions only explain how to access data via child-number eg... -
Giuseppe Maxia #2
Re: Self Referencing foreign key
lyonnyte wrote:
You can do that in one pass:> I am trying to create an employee/supervisor relationship. Since both
> are essentially employees, I created a foreign key relationship which
> referenced itself. I was wondering if there is a better way to do
> this? Maybe without having to create the first record?
>
> CREATE TABLE employee
> (
> employeeid longint(20) unsigned NOT NULL auto_increment,
> employeefirstname varchar(20) NOT NULL,
> employeelastname varchar(20) NOT NULL,
> employeetitle varchar(10) ,
> supervisorid longint(20) unsigned,
> primary key(employeeid),
> );
> INSERT INTO employee VALUES
> ('1', 'Chief', 'Officer', 'CEO', ' ');
>
> ALTER TABLE 'employee' ADD FOREIGN KEY(supervisorid) REFERENCES
> 'employee'('employeeid') ON DELETE CASCADE ;
>
CREATE TABLE employee (
employeeid int(10) unsigned NOT NULL auto_increment,
employeefirstname varchar(20) NOT NULL,
employeelastname varchar(20) NOT NULL,
employeetitle varchar(10) default NULL,
supervisorid int(10) unsigned default NULL,
PRIMARY KEY (employeeid),
FOREIGN KEY (supervisorid) REFERENCES employee (employeeid) ON DELETE CASCADE
) ENGINE=InnoDB
The engine will do the rest:
show create table employee\G
*************************** 1. row ***************************
Table: employee
Create Table: CREATE TABLE `employee` (
`employeeid` int(10) unsigned NOT NULL auto_increment,
`employeefirstname` varchar(20) NOT NULL,
`employeelastname` varchar(20) NOT NULL,
`employeetitle` varchar(10) default NULL,
`supervisorid` int(10) unsigned default NULL,
PRIMARY KEY (`employeeid`),
KEY `supervisorid` (`supervisorid`),
CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`supervisorid`) REFERENCES `employee` (`employeeid`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
ciao
gmax
--
_ _ _ _
(_|| | |(_|>< The Data Charmer
_|
[url]http://datacharmer.blogspot.com/[/url]
Giuseppe Maxia Guest
-
lyonnyte #3
Re: Self Referencing foreign key
I'm a newbie in mysql and was wondering whether the engine came with
the intial install or do I have to d/l it? Also, is the same engine
available in Oracle's SQL ?
Thanks
lyonnyte Guest
-
Bill Karwin #4
Re: Self Referencing foreign key
Giuseppe Maxia wrote:
So when a supervisor leaves the company, all his direct reports are laid> CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`supervisorid`) REFERENCES `employee` (`employeeid`) ON DELETE CASCADE
off? :-)
I'd rather use ON DELETE SET NULL (actually, I'd prefer ON DELETE SET
DEFAULT, but MySQL doesn't support that).
Regards,
Bill K.
Bill Karwin Guest
-
Bill Karwin #5
Re: Self Referencing foreign key
lyonnyte wrote:
Typically the MySQL engine (the server) is part of the product.> I'm a newbie in mysql and was wondering whether the engine came with
> the intial install or do I have to d/l it?
The exception seems to be Linux, where you can download different
subsets of the product separately.
No, Oracle and MySQL are different products, even though they both> Also, is the same engine available in Oracle's SQL ?
recognize a large part of the SQL language in common.
Regards,
Bill K.
Bill Karwin Guest
-
lyonnyte #6
Re: Self Referencing foreign key
Bill Karwin wrote:Thanks for catching that, would've really messed up the database if> Giuseppe Maxia wrote:>> > CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`supervisorid`) REFERENCES `employee` (`employeeid`) ON DELETE CASCADE
> So when a supervisor leaves the company, all his direct reports are laid
> off? :-)
>
> I'd rather use ON DELETE SET NULL (actually, I'd prefer ON DELETE SET
> DEFAULT, but MySQL doesn't support that).
>
> Regards,
> Bill K
someone hadn't seen it.
Wouldn't ON DELETE SET DEFAULT be what normally happens if you don't
put on ON DELETE CASCADE? .
lyonnyte Guest
-
Bill Karwin #7
Re: Self Referencing foreign key
lyonnyte wrote:
Nope; in MySQL, ON DELETE RESTRICT and ON DELETE NO ACTION are> Wouldn't ON DELETE SET DEFAULT be what normally happens if you don't
> put on ON DELETE CASCADE? .
equivalent to omitting the ON DELETE clause.
See
[url]http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html[/url]
for more information on this.
Regards,
Bill K.
Bill Karwin Guest



Reply With Quote

