How do I create this relationship in phpMyAdmin? - php

I'm following along with a book that wants me to create the following relationships in my database:
ALTER TABLE 'tbl_issue' ADD CONSTRAINT 'FK_issue_project' FOREIGN KEY ('project_id') REFERENCES 'tbl_project' ('id') ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE 'tbl_issue' ADD CONSTRAINT 'FK_issue_owner' FOREIGN KEY ('owner_id') REFERENCES 'tbl_user' ('id') ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE 'tbl_issue' ADD CONSTRAINT 'FK_issue_requester' FOREIGN KEY ('requester_id') REFERENCES 'tbl_user' ('id') ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE 'tbl_project_user' ADD CONSTRAINT 'FK_project_ user' FOREIGN KEY ('project_id') REFERENCES 'tbl_project' ('id') ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE 'tbl_project_user' ADD CONSTRAINT 'FK_user_ project' FOREIGN KEY ('user_id') REFERENCES 'tbl_user' ('id') ON DELETE CASCADE ON UPDATE RESTRICT;
When I try to run the SQL, I get an error like:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''tbl_issue' ADD CONSTRAINT 'FK_issue_project' FOREIGN KEY ('project_id') REFEREN' at line 1
So, I want to try doing it (visually) in phpMyAdmin. How do you do that?
DB SQL:
-- phpMyAdmin SQL Dump
-- version 3.3.9.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 24, 2011 at 01:27 PM
-- Server version: 5.5.9
-- PHP Version: 5.3.5
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `trackstar`
--
-- --------------------------------------------------------
--
-- Table structure for table `tbl_issue`
--
CREATE TABLE `tbl_issue` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(256) NOT NULL,
`description` varchar(2000) DEFAULT NULL,
`project_id` int(11) DEFAULT NULL,
`type_id` int(11) DEFAULT NULL,
`status_id` int(11) DEFAULT NULL,
`owner_id` int(11) DEFAULT NULL,
`requester_id` int(11) DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
`create_user_id` int(11) DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`update_user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Dumping data for table `tbl_issue`
--
-- --------------------------------------------------------
--
-- Table structure for table `tbl_project`
--
CREATE TABLE `tbl_project` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) DEFAULT NULL,
`description` text,
`create_time` datetime DEFAULT NULL,
`create_user_id` int(11) DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`update_user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Dumping data for table `tbl_project`
--
-- --------------------------------------------------------
--
-- Table structure for table `tbl_project_user`
--
CREATE TABLE `tbl_project_user` (
`project_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`create_time` datetime DEFAULT NULL,
`create_user_id` int(11) DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`update_user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`project_id`,`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `tbl_project_user`
--
-- --------------------------------------------------------
--
-- Table structure for table `tbl_user`
--
CREATE TABLE `tbl_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(256) NOT NULL,
`username` varchar(256) DEFAULT NULL,
`password` varchar(256) DEFAULT NULL,
`last_login_time` datetime DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
`create_user_id` int(11) DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`update_user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Dumping data for table `tbl_user`
--

You should use backticks, like so:
ALTER TABLE `tbl_issue`
ADD CONSTRAINT `FK_issue_project` FOREIGN KEY (`project_id`)
REFERENCES `tbl_project` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;

Remove the quotes from around the table names - the quotes make MySQL think you're altering a string, not a table:
ALTER TABLE tbl_issue ADD CONSTRAINT 'FK_issue_project' FOREIGN KEY ('project_id') REFERENCES tbl_project ('id') ON DELETE CASCADE ON UPDATE RESTRICT;

Related

Unable to create foreign key in PHPmyadmin

I am trying to create foreign key in one of my table but i am getting this error can you help with this
CREATE TABLE IF NOT EXISTS `albums` (
`id` smallint(5) unsigned NOT NULL,
`url` mediumtext NOT NULL,
`year` mediumint(9) NOT NULL,
`album_name` varchar(150) NOT NULL,
`album_cover` text NOT NULL,
`tracks_id` text NOT NULL,
`category` tinyint(4) NOT NULL,
`reciter` tinyint(4) NOT NULL,
`keywords` varchar(300) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
Foreign Key Table Structure
CREATE TABLE IF NOT EXISTS `album_likes` (
`album_id` int(5) NOT NULL,
`likes` int(11) NOT NULL,
`dislikes` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Now what i did to make this foreign key is that i went to the structure of this table in phpmyadmin and then add constraint name select the columns
Here is the query
ALTER TABLE `album_likes` ADD CONSTRAINT `album_like_fk` FOREIGN KEY (`album_id`) REFERENCES `mp3script`.`albums`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
The output i got
1215 - Cannot add foreign key constraint
A foreign key requires an index on the column you are referencing, in this case albums.id.
Add the index:
CREATE INDEX ids_nn_1 on albums(id);
And you create foreign key should work

How can i create entities in Doctrine for my already build two tables

Table 1:-
--
-- Table structure for table page
--
DROP TABLE IF EXISTS `page`;
CREATE TABLE IF NOT EXISTS `page` (
`id` int(11) NOT NULL,
`site_id` int(11) NOT NULL,
`keyword_id` int(11) NOT NULL,
`state` char(2) DEFAULT NULL,
`location_group_id` int(11) DEFAULT NULL,
`location_id` int(11) DEFAULT NULL,
`uri` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7194 ;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `page`
--
ALTER TABLE `page`
ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `site_id` (`site_id`,`uri`),
ADD KEY `location_group_id` (`location_group_id`),
ADD KEY `keyword_id` (`keyword_id`), ADD KEY `uri` (`uri`),
ADD KEY `state` (`state`), ADD KEY `location_id` (`location_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `page`
--
ALTER TABLE `page`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=7194;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `page`
--
ALTER TABLE `page`
ADD CONSTRAINT `page_ibfk_1` FOREIGN KEY (`site_id`) REFERENCES `site` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `page_ibfk_2` FOREIGN KEY (`keyword_id`) REFERENCES `keyword` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
**ADD CONSTRAINT `page_ibfk_3` FOREIGN KEY (`state`) REFERENCES `state` (`abbreviation`) ON DELETE CASCADE ON UPDATE CASCADE,**
ADD CONSTRAINT `page_ibfk_4` FOREIGN KEY (`location_group_id`) REFERENCES `location_group` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `page_ibfk_5` FOREIGN KEY (`location_id`) REFERENCES `location` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
Table 2:-
--
-- Table structure for table `state`
--
CREATE TABLE IF NOT EXISTS `state` (
`id` int(11) NOT NULL,
`abbreviation` char(2) NOT NULL,
`name` varchar(255) NOT NULL,
`territory` tinyint(1) DEFAULT '0',
`capital_city` varchar(255) DEFAULT NULL,
`nickname` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=56 ;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `state`
--
ALTER TABLE `state`
ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `abbreviation` (`abbreviation`),
ADD UNIQUE KEY `name` (`name`), ADD KEY `abbreviation_2` (`abbreviation`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `state`
--
ALTER TABLE `state`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=56;
Please check the unique key relationship between the state table and page table and help me how can i identify this relationship during create my new Entity for this type of DB structure.

phpmyadmin Designer Tab meaning of different colors?

Ok, so I clicked on the Designer tab in phpmyadmin to see the relationships between my tables. There are a lot of different colors connecting the foreign keys from one table to the other table. The colors are blue, green, yellow, purple and red. It is obvious that green means that the relationship is a valid one and I think the same thing is true of blue. I guess yellow means caution (or warning) and I suppose that red means that this relationship is a bad one and should be deleted.
My questions are: 1) Why the different colors? 2) What does each color mean? 3) If red means that the relationship is wrong, why is it wrong and how do I fix it?
I am including my SQL tables structure in case you want to look at it and tell me what is wrong with the way I have structured my tables or why the foreign key relationship is wrong and how it needs to be fixed.
This is a PHP/MySQL quiz application that keeps track of member results. I might be organizing these tables all wrong. Any advice would be much appreciated.
Thanks a lot guys, you guys are awesome and have helped me a lot.
-- phpMyAdmin SQL Dump
-- version 3.4.10.1deb1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Feb 15, 2014 at 12:56 PM
-- Server version: 5.5.35
-- PHP Version: 5.3.10-1ubuntu3.9
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `Asheville_Potpourri`
--
-- --------------------------------------------------------
--
-- Table structure for table `answers`
--
CREATE TABLE IF NOT EXISTS `answers` (
`id_answer` int(11) NOT NULL AUTO_INCREMENT,
`answer` varchar(255) NOT NULL,
`addedBy` int(11) NOT NULL,
`dateAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id_answer`),
KEY `id_user` (`id_user`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `bannedUsers`
--
CREATE TABLE IF NOT EXISTS `bannedUsers` (
`id_bannedUser` int(11) NOT NULL AUTO_INCREMENT,
`id_user` int(11) NOT NULL,
`bannedBy` int(11) NOT NULL,
`reasonBanned` text NOT NULL,
`dateBanned` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id_bannedUser`),
KEY `id_user` (`id_user`),
KEY `bannedBy` (`bannedBy`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `blogPosts`
--
CREATE TABLE IF NOT EXISTS `blogPosts` (
`id_blogPost` int(11) NOT NULL AUTO_INCREMENT,
`id_blogTopic` int(11) NOT NULL,
`blogPostTitle` varchar(255) NOT NULL,
`blogPostContent` text NOT NULL,
`postedBy` int(11) NOT NULL,
`datePosted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id_blogPost`),
UNIQUE KEY `blogPostTitle` (`blogPostTitle`),
KEY `id_blogTopic` (`id_blogTopic`),
KEY `postedBy` (`postedBy`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `blogTopics`
--
CREATE TABLE IF NOT EXISTS `blogTopics` (
`id_blogTopic` int(11) NOT NULL AUTO_INCREMENT,
`blogTopicName` varchar(255) NOT NULL,
`blogTopicDescription` text NOT NULL,
`addedBy` int(11) NOT NULL,
`dateAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id_blogTopic`),
UNIQUE KEY `blogTopicName` (`blogTopicName`),
KEY `addedBy` (`addedBy`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `feedback`
--
CREATE TABLE IF NOT EXISTS `feedback` (
`id_feedback` int(11) NOT NULL AUTO_INCREMENT,
`firstName` varchar(40) NOT NULL,
`lastName` varchar(40) NOT NULL,
`email` varchar(60) NOT NULL,
`comments` text NOT NULL,
`dateSent` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id_feedback`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `forumPosts`
--
CREATE TABLE IF NOT EXISTS `forumPosts` (
`id_forumPost` int(11) NOT NULL AUTO_INCREMENT,
`id_forumTopic` int(11) NOT NULL,
`forumPostTitle` varchar(255) NOT NULL,
`forumPostContent` text NOT NULL,
`postedBy` int(11) NOT NULL,
`datePosted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id_forumPost`),
UNIQUE KEY `forumPostTitle` (`forumPostTitle`),
KEY `id_forumTopic` (`id_forumTopic`),
KEY `postedBy` (`postedBy`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `forumTopics`
--
CREATE TABLE IF NOT EXISTS `forumTopics` (
`id_forumTopic` int(11) NOT NULL AUTO_INCREMENT,
`forumTopicName` varchar(255) NOT NULL,
`forumTopicDescription` text NOT NULL,
`addedBy` int(11) NOT NULL,
`dateAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id_forumTopic`),
UNIQUE KEY `forumTopicName` (`forumTopicName`),
KEY `addedBy` (`addedBy`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `memberAllQuizzesTotalPoints`
--
CREATE TABLE IF NOT EXISTS `memberAllQuizzesTotalPoints` (
`id_memberAllQuizzesTotalPoints` int(11) NOT NULL AUTO_INCREMENT,
`id_user` int(11) NOT NULL,
`memberAllQuizzesTotalPoints` int(11) NOT NULL DEFAULT '0',
`dateUpdated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id_memberAllQuizzesTotalPoints`),
KEY `id_user` (`id_user`),
KEY `memberAllQuizzesTotalPoints` (`memberAllQuizzesTotalPoints`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `memberProfilePages`
--
CREATE TABLE IF NOT EXISTS `memberProfilePages` (
`id_memberProfilePage` int(11) NOT NULL AUTO_INCREMENT,
`id_user` int(11) NOT NULL,
`memberAllQuizzesTotalPoints` int(11) NOT NULL DEFAULT '0',
`profileImageFilename` varchar(60) NOT NULL DEFAULT '',
`facebookAddress` varchar(60) NOT NULL DEFAULT '',
`twitterAddress` varchar(60) NOT NULL DEFAULT '',
`dateUpdated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id_memberProfilePage`),
KEY `id_user` (`id_user`),
KEY `memberAllQuizzesTotalPoints` (`memberAllQuizzesTotalPoints`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `memberQuizNameAnswers`
--
CREATE TABLE IF NOT EXISTS `memberQuizNameAnswers` (
`id_memberQuizNameAnswer` int(11) NOT NULL AUTO_INCREMENT,
`id_user` int(11) NOT NULL,
`id_quiz` int(11) NOT NULL,
`quizNameQuestionNumber` int(11) NOT NULL,
`id_answer` int(11) NOT NULL,
`isCorrect` enum('0','1') NOT NULL DEFAULT '0',
`dateQuizTaken` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id_memberQuizNameAnswer`),
KEY `id_user` (`id_user`),
KEY `id_quiz` (`id_quiz`),
KEY `quizNameQuestionNumber` (`quizNameQuestionNumber`),
KEY `id_answer` (`id_answer`),
KEY `isCorrect` (`isCorrect`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `memberQuizNameResults`
--
CREATE TABLE IF NOT EXISTS `memberQuizNameResults` (
`id_memberQuizNameResult` int(11) NOT NULL AUTO_INCREMENT,
`id_user` int(11) NOT NULL,
`id_quiz` int(11) NOT NULL,
`quizNamePointsEarned` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id_memberQuizNameResult`),
KEY `id_user` (`id_user`),
KEY `id_quiz` (`id_quiz`),
KEY `quizNamePointsEarned` (`quizNamePointsEarned`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `questions`
--
CREATE TABLE IF NOT EXISTS `questions` (
`id_question` int(11) NOT NULL AUTO_INCREMENT,
`question` varchar(255) NOT NULL,
`addedBy` int(11) NOT NULL,
`dateAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id_question`),
KEY `addedBy` (`addedBy`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `quizCategories`
--
CREATE TABLE IF NOT EXISTS `quizCategories` (
`id_quizCategory` int(11) NOT NULL AUTO_INCREMENT,
`categoryName` varchar(40) NOT NULL,
`categoryDescription` text NOT NULL,
`addedBy` int(11) NOT NULL,
`dateAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id_quizCategory`),
KEY `addedBy` (`addedBy`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `quizNameAnswers`
--
CREATE TABLE IF NOT EXISTS `quizNameAnswers` (
`id_quizNameAnswer` int(11) NOT NULL AUTO_INCREMENT,
`id_quiz` int(11) NOT NULL,
`quizNameQuestionNumber` int(11) NOT NULL,
`id_answer` int(11) NOT NULL,
`isCorrect` enum('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (`id_quizNameAnswer`),
KEY `id_quiz` (`id_quiz`),
KEY `quizNameQuestionNumber` (`quizNameQuestionNumber`),
KEY `id_answer` (`id_answer`),
KEY `isCorrect` (`isCorrect`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `quizNameQuestions`
--
CREATE TABLE IF NOT EXISTS `quizNameQuestions` (
`id_quizNameQuestion` int(11) NOT NULL AUTO_INCREMENT,
`id_quiz` int(11) NOT NULL,
`id_question` int(11) NOT NULL,
`quizNameQuestionNumber` int(11) NOT NULL,
PRIMARY KEY (`id_quizNameQuestion`),
KEY `id_question` (`id_question`),
KEY `quizNameQuestionNumber` (`quizNameQuestionNumber`),
KEY `id_quiz` (`id_quiz`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `quizzes`
--
CREATE TABLE IF NOT EXISTS `quizzes` (
`id_quiz` int(11) NOT NULL AUTO_INCREMENT,
`id_quizCategory` int(11) NOT NULL,
`quizName` varchar(40) NOT NULL,
`quizDescription` text NOT NULL,
`addedBy` int(11) NOT NULL,
`dateAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id_quiz`),
KEY `id_quizCategory` (`id_quizCategory`),
KEY `addedBy` (`addedBy`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`id_user` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(20) NOT NULL,
`firstName` varchar(40) NOT NULL,
`lastName` varchar(40) NOT NULL,
`email` varchar(60) NOT NULL,
`password` varchar(20) NOT NULL,
`emailCode` varchar(32) NOT NULL,
`active` enum('0','1') NOT NULL DEFAULT '0',
`passwordRecover` enum('0','1') NOT NULL DEFAULT '0',
`userType` enum('1','2') NOT NULL DEFAULT '1',
`dateRegistered` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id_user`),
UNIQUE KEY `userName` (`userName`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `answers`
--
ALTER TABLE `answers`
ADD CONSTRAINT `answers_ibfk_1` FOREIGN KEY (`addedBy`) REFERENCES `users` (`id_user`);
--
-- Constraints for table `bannedUsers`
--
ALTER TABLE `bannedUsers`
ADD CONSTRAINT `bannedUsers_ibfk_3` FOREIGN KEY (`id_user`) REFERENCES `users` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `bannedUsers_ibfk_4` FOREIGN KEY (`bannedBy`) REFERENCES `users` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `blogPosts`
--
ALTER TABLE `blogPosts`
ADD CONSTRAINT `blogPosts_ibfk_3` FOREIGN KEY (`id_blogTopic`) REFERENCES `blogTopics` (`id_blogTopic`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `blogPosts_ibfk_4` FOREIGN KEY (`postedBy`) REFERENCES `users` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `blogTopics`
--
ALTER TABLE `blogTopics`
ADD CONSTRAINT `blogTopics_ibfk_2` FOREIGN KEY (`addedBy`) REFERENCES `users` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `forumPosts`
--
ALTER TABLE `forumPosts`
ADD CONSTRAINT `forumPosts_ibfk_3` FOREIGN KEY (`id_forumTopic`) REFERENCES `forumTopics` (`id_forumTopic`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `forumPosts_ibfk_4` FOREIGN KEY (`postedBy`) REFERENCES `users` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `forumTopics`
--
ALTER TABLE `forumTopics`
ADD CONSTRAINT `forumTopics_ibfk_2` FOREIGN KEY (`addedBy`) REFERENCES `users` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `memberAllQuizzesTotalPoints`
--
ALTER TABLE `memberAllQuizzesTotalPoints`
ADD CONSTRAINT `memberAllQuizzesTotalPoints_ibfk_2` FOREIGN KEY (`id_user`) REFERENCES `users` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `memberProfilePages`
--
ALTER TABLE `memberProfilePages`
ADD CONSTRAINT `memberProfilePages_ibfk_3` FOREIGN KEY (`id_user`) REFERENCES `users` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `memberProfilePages_ibfk_4` FOREIGN KEY (`memberAllQuizzesTotalPoints`) REFERENCES `memberAllQuizzesTotalPoints` (`id_memberAllQuizzesTotalPoints`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `memberQuizNameAnswers`
--
ALTER TABLE `memberQuizNameAnswers`
ADD CONSTRAINT `memberQuizNameAnswers_ibfk_5` FOREIGN KEY (`id_user`) REFERENCES `users` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `memberQuizNameAnswers_ibfk_6` FOREIGN KEY (`id_quiz`) REFERENCES `quizzes` (`id_quiz`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `memberQuizNameAnswers_ibfk_7` FOREIGN KEY (`quizNameQuestionNumber`) REFERENCES `quizNameQuestions` (`quizNameQuestionNumber`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `memberQuizNameAnswers_ibfk_8` FOREIGN KEY (`id_answer`) REFERENCES `answers` (`id_answer`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `memberQuizNameResults`
--
ALTER TABLE `memberQuizNameResults`
ADD CONSTRAINT `memberQuizNameResults_ibfk_3` FOREIGN KEY (`id_user`) REFERENCES `users` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `memberQuizNameResults_ibfk_4` FOREIGN KEY (`id_quiz`) REFERENCES `quizzes` (`id_quiz`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `questions`
--
ALTER TABLE `questions`
ADD CONSTRAINT `questions_ibfk_2` FOREIGN KEY (`addedBy`) REFERENCES `users` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `quizCategories`
--
ALTER TABLE `quizCategories`
ADD CONSTRAINT `quizCategories_ibfk_2` FOREIGN KEY (`addedBy`) REFERENCES `users` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `quizNameAnswers`
--
ALTER TABLE `quizNameAnswers`
ADD CONSTRAINT `quizNameAnswers_ibfk_7` FOREIGN KEY (`quizNameQuestionNumber`) REFERENCES `quizNameQuestions` (`quizNameQuestionNumber`),
ADD CONSTRAINT `quizNameAnswers_ibfk_4` FOREIGN KEY (`id_quiz`) REFERENCES `quizzes` (`id_quiz`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `quizNameAnswers_ibfk_6` FOREIGN KEY (`id_answer`) REFERENCES `answers` (`id_answer`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `quizNameQuestions`
--
ALTER TABLE `quizNameQuestions`
ADD CONSTRAINT `quizNameQuestions_ibfk_2` FOREIGN KEY (`id_question`) REFERENCES `questions` (`id_question`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `quizNameQuestions_ibfk_3` FOREIGN KEY (`id_quiz`) REFERENCES `quizzes` (`id_quiz`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `quizzes`
--
ALTER TABLE `quizzes`
ADD CONSTRAINT `quizzes_ibfk_3` FOREIGN KEY (`id_quizCategory`) REFERENCES `quizCategories` (`id_quizCategory`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `quizzes_ibfk_4` FOREIGN KEY (`addedBy`) REFERENCES `users` (`id_user`) ON DELETE CASCADE ON UPDATE CASCADE;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
the only reason for different colors is to make it easier to distinguish them.

MYSQL ADD CONSTRAINT ERROR

I am getting errors for my add constraints.
I am using MYSQL workbench to create the tables
Message Error from MYSQL workbench:
Error Code: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ADD CONSTRAINT user_contact_ibfk1 FOREIGN KEY(user_id) REFERENCES `user_ac' at line 1
Could this just be a version problem I'm having. I'm using 5.2.4.7
Error:
22:47:35 ADD CONSTRAINT user_contact_ibfk_1 FOREIGN KEY (user_id) REFERENCES user_account (user_id) ON DELETE CASCADE ON UPDATE CASCADE Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ADD CONSTRAINT user_contact_ibfk_1 FOREIGN KEY (user_id) REFERENCES `user_ac' at line 1 0.000 sec
--
-- Table structure for table `user_contact`
--
CREATE TABLE IF NOT EXISTS `user_contact` (
`contact_id` bigint(11) NOT NULL AUTO_INCREMENT,
`user_id` bigint(11) NOT NULL,
`first_name` varchar(25) NOT NULL,
`last_name` varchar(25) NOT NULL,
`email` varchar(50) NOT NULL,
`email_code` varchar(32) NOT NULL,
`cellnumber` decimal(32,0) NOT NULL,
`city` varchar(25) NOT NULL,
`state` varchar(25) NOT NULL,
`country` varchar(25) NOT NULL,
`username` varchar(50) NOT NULL,
PRIMARY KEY (`contact_id`),
UNIQUE KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;
-- --------------------------------------------------------
--
-- Table structure for table `user_profile`
--
CREATE TABLE IF NOT EXISTS `user_profile` (
`profile_id` bigint(11) NOT NULL AUTO_INCREMENT,
`user_id` bigint(11) NOT NULL,
`username` varchar(50) NOT NULL,
`about_me` text NOT NULL,
`work_info` varchar(255) NOT NULL,
`education_info` varchar(255) NOT NULL,
`rating` float NOT NULL,
`user_image` varchar(1024) NOT NULL,
`friend_array` text NOT NULL,
`online_status` tinyint(1) NOT NULL,
PRIMARY KEY (`profile_id`),
UNIQUE KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
--
-- Table structure for table `user_account`
--
CREATE TABLE IF NOT EXISTS `user_account` (
`user_id` bigint(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`type` int(11) NOT NULL,
`online_status` tinyint(1) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;
--
-- Constraints for table `user_contact`
--
ALTER TABLE `user_contact`
ADD CONSTRAINT `user_contact_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user_account` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `user_profile`
--
ALTER TABLE `user_profile`
ADD CONSTRAINT `user_profile_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user_account` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE;
The only thing I can think of is that FOREIGN KEY name might be taken,try changing it.
ALTER TABLE `user_contact`
ADD CONSTRAINT `user_contact_ibfk_stuff` FOREIGN KEY (`user_id`) REFERENCES `user_account` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE;
Run this post the result
SELECT ##SESSION.sql_mode;
Or directly run this:
SET SESSION SQL_MODE='NO_ENGINE_SUBSTITUTION';
Turns out to be some internal bug with MYSQL workbench. I used phpmyadmin and it worked fine.

Problem creating a database with PHP PDO

I'm having a problem with a SQL query in my PHP Application. When the user access it for the first time, the app executes this query to create all the database:
CREATE TABLE `databases` (
`id` bigint(20) NOT NULL auto_increment,
`driver` varchar(45) NOT NULL,
`server` text NOT NULL,
`user` text NOT NULL,
`password` text NOT NULL,
`database` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
-- --------------------------------------------------------
--
-- Table structure for table `modules`
--
CREATE TABLE `modules` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`title` varchar(100) NOT NULL,
`type` varchar(150) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=29 ;
-- --------------------------------------------------------
--
-- Table structure for table `modules_data`
--
CREATE TABLE `modules_data` (
`id` bigint(20) NOT NULL auto_increment,
`module_id` bigint(20) unsigned NOT NULL,
`key` varchar(150) NOT NULL,
`value` tinytext,
PRIMARY KEY (`id`),
KEY `fk_modules_data_modules` (`module_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=184 ;
-- --------------------------------------------------------
--
-- Table structure for table `modules_position`
--
CREATE TABLE `modules_position` (
`user_id` bigint(20) unsigned NOT NULL,
`tab_id` bigint(20) unsigned NOT NULL,
`module_id` bigint(20) unsigned NOT NULL,
`column` smallint(1) default NULL,
`line` smallint(1) default NULL,
PRIMARY KEY (`user_id`,`tab_id`,`module_id`),
KEY `fk_modules_order_users` (`user_id`),
KEY `fk_modules_order_tabs` (`tab_id`),
KEY `fk_modules_order_modules` (`module_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `tabs`
--
CREATE TABLE `tabs` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`title` varchar(60) NOT NULL,
`columns` smallint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;
-- --------------------------------------------------------
--
-- Table structure for table `tabs_has_modules`
--
CREATE TABLE `tabs_has_modules` (
`tab_id` bigint(20) unsigned NOT NULL,
`module_id` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`tab_id`,`module_id`),
KEY `fk_tabs_has_modules_tabs` (`tab_id`),
KEY `fk_tabs_has_modules_modules` (`module_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`login` varchar(60) NOT NULL,
`password` varchar(64) NOT NULL,
`email` varchar(100) NOT NULL,
`name` varchar(250) default NULL,
`user_level` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_users_user_levels` (`user_level`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
-- --------------------------------------------------------
--
-- Table structure for table `users_has_tabs`
--
CREATE TABLE `users_has_tabs` (
`user_id` bigint(20) unsigned NOT NULL,
`tab_id` bigint(20) unsigned NOT NULL,
`order` smallint(2) NOT NULL,
`columns_width` varchar(255) default NULL,
PRIMARY KEY (`user_id`,`tab_id`),
KEY `fk_users_has_tabs_users` (`user_id`),
KEY `fk_users_has_tabs_tabs` (`tab_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `user_levels`
--
CREATE TABLE `user_levels` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`level` smallint(2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
-- --------------------------------------------------------
--
-- Table structure for table `user_meta`
--
CREATE TABLE `user_meta` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`user_id` bigint(20) unsigned default NULL,
`key` varchar(150) NOT NULL,
`value` longtext NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_user_meta_users` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `modules_data`
--
ALTER TABLE `modules_data`
ADD CONSTRAINT `fk_modules_data_modules` FOREIGN KEY (`module_id`) REFERENCES `modules` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
--
-- Constraints for table `modules_position`
--
ALTER TABLE `modules_position`
ADD CONSTRAINT `fk_modules_order_modules` FOREIGN KEY (`module_id`) REFERENCES `modules` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_modules_order_tabs` FOREIGN KEY (`tab_id`) REFERENCES `tabs` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_modules_order_users` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
--
-- Constraints for table `users`
--
ALTER TABLE `users`
ADD CONSTRAINT `fk_users_user_levels` FOREIGN KEY (`user_level`) REFERENCES `user_levels` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `user_meta`
--
ALTER TABLE `user_meta`
ADD CONSTRAINT `fk_user_meta_users` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
INSERT INTO `user_levels` VALUES(1, 10);
INSERT INTO `user_levels` VALUES(2, 1);
INSERT INTO `users` VALUES(1, 'admin', 'password', 'changethis#testing.com', NULL, 1);
INSERT INTO `user_meta` VALUES (NULL, 1, 'last_tab', 1);
In some environments i get this error:
SQLSTATE[HY000]: General error: 1005
Can't create table 'dms.databases'
(errno: 150)
I tried everything that I could find on Google but nothing works.
The strange part is that if I run this query in PhpMyAdmin he creates my database, without any error.
The problem is likely to be in the constraints part of your queries.
The error 150 is mentioned in the InnoDB Documentation:
If you re-create a table that was
dropped, it must have a definition
that conforms to the foreign key
constraints referencing it. It must
have the right column names and types,
and it must have indexes on the
referenced keys, as stated earlier. If
these are not satisfied, MySQL returns
error number 1005 and refers to error
150 in the error message.
If MySQL reports an error number 1005 from a CREATE TABLE statement,
and the error message refers to error
150, table creation failed because a
foreign key constraint was not
correctly formed. Similarly, if an
ALTER TABLE fails and it refers to
error 150, that means a foreign key
definition would be incorrectly formed
for the altered table. You can use
SHOW ENGINE INNODB STATUS to display a
detailed explanation of the most
recent InnoDB foreign key error in the
server.
Source: http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
It will also occur if the definitions of the references are not identical (i.e. one is signed, one is unsigned bigint) or mixing unique indexes over several columns with them.

Categories