Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
unfamiliar with this MySQL syntax
Old 08-01-2010, 08:19 AM unfamiliar with this MySQL syntax
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
Code:
//create table
$sql = "CREATE TABLE `s_tags` (
`id` INT(11) NOT NULL AUTO_INCREMENT, 
`user` INT(11) NOT NULL DEFAULT '0', 
`photo` INT(11) NOT NULL DEFAULT '0', 
PRIMARY KEY (`id`), 
KEY `tag_photo_ind` (`photo`), 
KEY `tag_user_ind` (`user`)
) 
ENGINE=InnoDB DEFAULT CHARSET=utf8";
mysql_query($sql) or die(mysql_error());
 
 
// alter table
$sql = "ALTER TABLE `s_tags` 
ADD CONSTRAINT `s_ibfk_1` 
FOREIGN KEY (`user`) 
REFERENCES `users` (`id`) 
ON DELETE CASCADE, 
ADD CONSTRAINT `s_ibfk_2` 
FOREIGN KEY (`photo`) 
REFERENCES `photos` (`id`) 
ON DELETE CASCADE";
mysql_query($sql) or die(mysql_error());
Why is the purpose of the syntax (that I have underlined)?

What is the purpose of ADD CONSTRAINT, FOREIGN KEY, REFERENCES and ON DELETE CASCADE; in the //alter table part of the SQL code, in relation to the //create table part?


Thanks,
Lashtal
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE
Lashtal is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-01-2010, 09:16 AM Re: unfamiliar with this MySQL syntax
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
This may be helpful
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 08-01-2010, 11:35 PM Re: unfamiliar with this MySQL syntax
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
W3schools was able to answer the first three constraints, but not the fourth: ON DELETE CASCADE

does anyone know this one?
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 08-02-2010, 09:34 AM Re: unfamiliar with this MySQL syntax
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Hmm, I don't know, but I'll guess. Maybe it means that when you delete something, the data from the previous record fills that spot?
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 08-02-2010, 10:13 AM Re: unfamiliar with this MySQL syntax
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Quick google:
Use the ON DELETE CASCADE option to specify whether you want rows deleted in a child table when corresponding rows are deleted in the parent table. If you do not specify cascading deletes, the default behavior of the database server prevents you from deleting data in a table if other tables reference it.
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 08-02-2010, 12:45 PM Re: unfamiliar with this MySQL syntax
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
For MySQL take note that CONSTRAINT, FOREIGN KEY, and ON DELETE CASCADE will ONLY be valid IF you are using InnoDb as the storage engine.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-07-2010, 07:39 AM Re: unfamiliar with this MySQL syntax
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
Thank you, lynxus
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 08-08-2010, 11:43 PM Re: unfamiliar with this MySQL syntax
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 680
Name: Lashtal
Trades: 0
Lemme' me see if I have broken this down right.


So...

the ALTER TABLE portion of this script sets a constraint for `users` (`id`) and `photos` (`id`)

then: if either 'id', 'users', or 'photos' is deleted from the parent table; the child table and it's constraints, will also be removed from the database?
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE
Lashtal is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to unfamiliar with this MySQL syntax
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.60006 seconds with 12 queries