Jump to content

Search the Community

Showing results for tags 'foreign key'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • W3Schools
    • General
    • Suggestions
    • Critiques
  • HTML Forums
    • HTML/XHTML
    • CSS
  • Browser Scripting
    • JavaScript
    • VBScript
  • Server Scripting
    • Web Servers
    • Version Control
    • SQL
    • ASP
    • PHP
    • .NET
    • ColdFusion
    • Java/JSP/J2EE
    • CGI
  • XML Forums
    • XML
    • XSLT/XSL-FO
    • Schema
    • Web Services
  • Multimedia
    • Multimedia
    • FLASH

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


AIM


MSN


Website URL


ICQ


Yahoo


Jabber


Skype


Location


Interests


Languages

Found 4 results

  1. PROBLEM: Turn a two-step SELECT procedure into a one-step procedure. The two-step procedure is 1) Discover with a query to a parent table what other rows in the parent table are related to the queried row. 2) Obtain selected data for the queried row and other related rows from the parent table and a child table that are connected by a FOREIGN KEY. BACKGROUND: I have two tables -- a parent (parent_table) and a child (child_table) table - connected by a valid FOREIGN KEY and an additional table (ref_table) that contains information about the relationship among the rows of both the parent and child tables. Please find below the results of three SHOW CREATE TABLE statements to help you in your understanding of the table structure. In addition, I have included the INSERT statements for the child_table and ref-table. I accidentally destroyed the INSERT statement for the parent_table. This said, the parent and child tables are very similar in structure. DISCLAIMER: Please understand that the problem that I have created is heuristic in nature and is being used to create a prototype for subsequent, more practical use. The PARENT Table parent_table CREATE TABLE `parent_table` ( `id` int(3) NOT NULL DEFAULT '0', `usertype` enum('1','2','3') CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '2' COMMENT '1=good, 2=neutral, 3=bad', `username` char(150) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, PRIMARY KEY (`id`,`usertype`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 The CHILD Table child_table CREATE TABLE `child_table` ( `id` int(3) NOT NULL DEFAULT '0', `usertype` enum('1','2','3') CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '2' COMMENT '1=good, 2=neutral, 3=bad', `userbio` varchar(500) DEFAULT NULL, KEY `parent` (`id`,`usertype`), CONSTRAINT `child_table_ibfk_1` FOREIGN KEY (`id`, `usertype`) REFERENCES `parent_table` (`id`, `usertype`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=latin1 INSERT INTO `child_table` (`id`, `usertype`, `userbio`) VALUES ('1', '1', 'I am Roddy.'), ('2', '2', 'I am Beth.'), ('3', '3', 'I am Matt.'), ('4', '3', 'I am Tim.'), ('5', '2', 'I am Tylor.'), ('6', '1', 'I am Liz.'), ('7', '1', 'I am Aldo.'), ('8', '1', 'I am Adzit.'), ('9', '3', 'I am Jason.'), ('10', '3', 'I am David.') The REFERENCE Table ref_table CREATE TABLE `ref_table` ( `ref_id` int(3) NOT NULL DEFAULT '0', `id` int(3) DEFAULT NULL, `ref` int(3) DEFAULT NULL, `count_ref` int(1) DEFAULT NULL, KEY `par_ref` (`ref_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 INSERT INTO ref_table (ref_id,id,ref,count_ref) VALUES ('1','1','2','1'), ('2','1','6','2'), ('3','1','7','3'), ('4','2','6','1'), ('5','2','9','2'), ('6','4','1','1'), ('7','4','2','2'), ('8','4','6','3'), ('9','4','10','4'), ('10','5','6','1'), ('11','6','1','1'), ('12','6','2','2'), ('13','6','9','3'), ('14','7','5','1'), ('15','7','6','2'), ('16','8','1','1'), ('17','9','10','1'), ('18','10','4','1'), ('19','10','6','2'), ('20',10, NULL,'1'); EXPLANATION BY EXAMPLE: Say a user is interested in row 4 of the parent_table. When the database is queried a SELECT JOIN statement looks in the id field of the ref_table and finds four corresponding rows identified by ref_id 6, 7, 8, and 9. With each of these latter rows is associated a different row -- namely, 1, 2, 6, 10. Without further selection is returned all of the information contained in the parent_table and child_table associated with rows 1, 2, 4, 6, and 10. This is what is supposed to happen, but does not. The REJECTED SQL STATEMENT SELECT * FROM parent_table OUTER JOIN child_table ON parent_table.id = child_table.id OUTER JOIN ref_table ON parent_table.id = ref_table.id QUESTION ONE: Does the table structure make sense? Are the constraints properly set? QUESTION TWO: Why is my SQL statement rejected as poorly formatted? This is my first attempt to use the JOIN clause. So, please be as thorough with your answer as possible. I must believe that I have a long road ahead with MySQL and should prepare for it as i move forward. Roddy
  2. jaylow

    foreign key error

    Hello, i have 2 tables. one is for users to sign up and the other is to make character. i added a foreign key to the character table to link it to the members table not when i sign up it just adds me to the table but when i want to make a character it give me this error i want to link the character_id to the id in the members table, so i know what character belongs to what member.. i get: ERROR:could not able to execute INSERT INTO `character` (charname, ######, gold, xp, accdate) VALUES ('jay', 'male', 100000, 0, now()).Cannot add or update a child row: a foreign key constraint fails (`secure_login`.`character`, CONSTRAINT `character_ibfk_1` FOREIGN KEY (`character_id`) REFERENCES `members` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) Members table: CREATE TABLE IF NOT EXISTS `members` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(30) NOT NULL, `email` varchar(50) NOT NULL, `password` char(128) NOT NULL, `salt` char(128) NOT NULL, `accdate` timestamp NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ; `character` table: CREATE TABLE IF NOT EXISTS `character` ( `id` int(11) NOT NULL AUTO_INCREMENT, `character_id` int(11) NOT NULL, `charname` varchar(30) NOT NULL, `######` tinyint(4) NOT NULL, `gold` int(11) NOT NULL, `xp` int(11) NOT NULL, `accdate` timestamp NOT NULL, PRIMARY KEY (`id`), KEY `character_id` (`character_id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;---- Constraints for table `character`--ALTER TABLE `character` ADD CONSTRAINT `character_ibfk_1` FOREIGN KEY (`character_id`) REFERENCES `members` (`id`) ON DELETE CASCADE ON UPDATE CASCADE; without a foreign key i can update the characters table without any issue i messed around with the tables for the last two hours and just dont know anymore. if i switch the foreign key to my members table the sign up does not go into the members table
  3. I have Table2 with a foreign key to Table1. I want to create a trigger that automatically update Table2 when Table1 is updated. Here is what's on my mind: create trigger MyTrigger on Table1 instead of update asif UPDATE(Table1_ID)begin declare @OldID as int, @NewID as int; select @OldID=P_ID from deleted; select @NewID=P_ID from inserted; update Table2 set Table1_ID=@NewID where Table2.Table1_ID=@OldID; update Table1 set Table1_ID=@NewID where Table1_ID=@OldID;end I received an error from SQL Server indicating that there was a conflict with the foreign key constraint when I tried to update a row. So I modified it a bit: create trigger MyTrigger on Table1 instead of update asif UPDATE(Table1_ID)begin declare @OldID as int, @NewID as int; select @OldID=P_ID from deleted; select @NewID=P_ID from inserted; update Table2 set Table1_ID=null where Table2.Table1_ID=@OldID; update Table1 set Table1_ID=@NewID where Table1_ID=@OldID; update Table2 set Table1_ID=@NewID where Table1_ID=null;end It does work. But pre-existing null values from Table2 are obviously lost if the trigger runs. What is the correct way to do this? Note that I already know of the cascading option when creating table. I just want to do it using trigger. Thanks. Note: I don't know how to loop through the inserted and deleted tables when multiple rows are updated. I'm assuming 1 row is updated at a time.
  4. I am using phpMyAdmin and currently having trouble trying to make a foreign key. table album - soloID -> table solo artist - id Already tried: Indexing soloID Different sites: youtube, stackoverflow...etc After searching, I have found nobody actually made a detailed tutorial on how to make a foreign key for somebody who already created tables. Hopefully, somebody can help me here.
×
×
  • Create New...