set it so admin can approve a user after registration - php

How do I set it up that an admin must approve a user registration...
many admins can approve many users. So if user signs up, they have to be approved before have access to a page. How would this best be done?
-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Nov 21, 2014 at 04:39 AM
-- Server version: 5.6.17
-- PHP Version: 5.5.12
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: `membersappdb`
--
-- --------------------------------------------------------
--
-- Table structure for table `admin`
--
CREATE TABLE IF NOT EXISTS `admin` (
`adminID` int(11) NOT NULL AUTO_INCREMENT,
`userID` int(11) NOT NULL,
PRIMARY KEY (`adminID`),
KEY `userID` (`userID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
--
-- Dumping data for table `admin`
--
INSERT INTO `admin` (`adminID`, `userID`) VALUES
(1, 1);
-- --------------------------------------------------------
--
-- Table structure for table `groups`
--
CREATE TABLE IF NOT EXISTS `groups` (
`grpID` int(11) NOT NULL AUTO_INCREMENT,
`grptype` varchar(500) DEFAULT NULL,
`createdAdminID` int(11) DEFAULT NULL,
`createdMemID` int(11) DEFAULT NULL,
`approvedAdminID` int(11) DEFAULT NULL,
PRIMARY KEY (`grpID`),
KEY `createdAdminID` (`createdAdminID`),
KEY `createdMemID` (`createdMemID`),
KEY `approvedAdminID` (`approvedAdminID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
--
-- Dumping data for table `groups`
--
INSERT INTO `groups` (`grpID`, `grptype`, `createdAdminID`, `createdMemID`, `approvedAdminID`) VALUES
(1, 'TechnoMusic', 1, NULL, NULL);
-- --------------------------------------------------------
--
-- Table structure for table `member`
--
CREATE TABLE IF NOT EXISTS `member` (
`memID` int(11) NOT NULL AUTO_INCREMENT,
`userID` int(11) NOT NULL,
`signupDate` datetime DEFAULT NULL,
`lastlogin` datetime DEFAULT NULL,
`location` varchar(1000) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`bio` varchar(1000) DEFAULT NULL,
PRIMARY KEY (`memID`),
KEY `userID` (`userID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `memberjoinedgroup`
--
CREATE TABLE IF NOT EXISTS `memberjoinedgroup` (
`memID` int(11) NOT NULL,
`grpID` int(11) NOT NULL,
`joinedDate` datetime DEFAULT NULL,
PRIMARY KEY (`memID`,`grpID`),
KEY `grpID` (`grpID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `profile`
--
CREATE TABLE IF NOT EXISTS `profile` (
`profileID` int(11) NOT NULL AUTO_INCREMENT,
`imagepath` varchar(1000) DEFAULT NULL,
`memID` int(11) NOT NULL,
PRIMARY KEY (`profileID`),
KEY `memID` (`memID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `user`
--
CREATE TABLE IF NOT EXISTS `user` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`fname` varchar(200) DEFAULT NULL,
`lname` varchar(200) DEFAULT NULL,
`username` varchar(200) DEFAULT NULL,
`password` varchar(200) DEFAULT NULL,
`usertype` varchar(20) DEFAULT 'm',
`email` varchar(200) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
--
-- Dumping data for table `user`
--
INSERT INTO `user` (`ID`, `fname`, `lname`, `username`, `password`, `usertype`, `email`) VALUES
(1, 'james', 'halpin', 'jamesh', '1234', 'a', 'jameshalpin#halpin.com'),
(2, 'Sarah', 'Faulkner', 'Sarah', 'PASS1234', 'm', 'sfaulkner#faulkner.com'),
(3, 'Barney', 'Mitchell', 'BM', '987654', 'm', 'bmitchell#mitchell.com'),
(7, '', '', '', '', 'm', '');
--
-- Constraints for dumped tables
--
--
-- Constraints for table `admin`
--
ALTER TABLE `admin`
ADD CONSTRAINT `admin_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `user` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `groups`
--
ALTER TABLE `groups`
ADD CONSTRAINT `groups_ibfk_1` FOREIGN KEY (`createdAdminID`) REFERENCES `admin` (`adminID`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `groups_ibfk_2` FOREIGN KEY (`createdMemID`) REFERENCES `member` (`memID`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `groups_ibfk_3` FOREIGN KEY (`approvedAdminID`) REFERENCES `admin` (`adminID`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `member`
--
ALTER TABLE `member`
ADD CONSTRAINT `member_ibfk_1` FOREIGN KEY (`userID`) REFERENCES `user` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `memberjoinedgroup`
--
ALTER TABLE `memberjoinedgroup`
ADD CONSTRAINT `memberjoinedgroup_ibfk_1` FOREIGN KEY (`memID`) REFERENCES `member` (`memID`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `memberjoinedgroup_ibfk_2` FOREIGN KEY (`grpID`) REFERENCES `groups` (`grpID`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `profile`
--
ALTER TABLE `profile`
ADD CONSTRAINT `profile_ibfk_1` FOREIGN KEY (`memID`) REFERENCES `member` (`memID`) 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 */;

You should add one more column to user table, let's say status
Your table will look like this:
CREATE TABLE IF NOT EXISTS `user` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`fname` varchar(200) DEFAULT NULL,
`lname` varchar(200) DEFAULT NULL,
`username` varchar(200) DEFAULT NULL,
`password` varchar(200) DEFAULT NULL,
`usertype` varchar(20) DEFAULT 'm',
`status` tinyint unsigned NOT NULL DEFAULT '0',
`email` varchar(200) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
When a user registers to your website, his default status will be 0, then when you approve it, status will change to 1

Related

SQL insert and update doesn't update or insert all the records, only a few record

My php code:
for($i=0;$i<num_rows($sql);$i++)
if(isset($_POST['knight'.$i]))
{
$data=
[
$pm_id=$_POST['pm_id'.$i],
$knight_id=$_POST['knight'.$i],
];
mysql_query("update `project_waiting` set `chosen`=(b'1') where `pm_id`='$pm_id' and `knight_id`='$knight_id';");
mysql_query("insert into `project_working`(`pm_id`,`knight_id`) values('$pm_id','$knight_id');");
}
when I use echo instead of mysql_query it returns:
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_1' and `knight_id`='K1';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_1','K1');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_1' and `knight_id`='K2';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_1','K2');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_1' and `knight_id`='K3';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_1','K3');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_1' and `knight_id`='K4';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_1','K4');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_2' and `knight_id`='K1';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_2','K1');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_2' and `knight_id`='K4';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_2','K4');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_3' and `knight_id`='K2';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_3','K2');
It runs perfectly on my phpmyadmin, but when I change echo to mysql_query it only affect 5 rows (both tables)
P1_1-K1
P1_1-K4
P1_1-K3
P1_1-K2
P1_2-K1
My full database
-- phpMyAdmin SQL Dump
-- version 4.3.11
-- http://www.phpmyadmin.net
-- Host: 127.0.0.1
-- Generation Time: Jul 23, 2015 at 12:16 PM
-- Server version: 5.6.24
-- PHP Version: 5.6.8
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: knightit
CREATE DATABASE IF NOT EXISTS knightit DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
USE knightit;
--
-- Table structure for table admin
DROP TABLE IF EXISTS admin;
CREATE TABLE IF NOT EXISTS admin (
id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
password binary(60) NOT NULL,
level int(2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table employers
DROP TABLE IF EXISTS employers;
CREATE TABLE IF NOT EXISTS employers (
emp_id varchar(16) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
password longtext COLLATE utf8_unicode_ci NOT NULL,
email varchar(32) COLLATE utf8_unicode_ci NOT NULL,
emp_name text COLLATE utf8_unicode_ci,
coins bigint(20) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table knights
DROP TABLE IF EXISTS knights;
CREATE TABLE IF NOT EXISTS knights (
knight_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
password longtext COLLATE utf8_unicode_ci NOT NULL,
knight_name text COLLATE utf8_unicode_ci,
email varchar(32) COLLATE utf8_unicode_ci NOT NULL,
s_id varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
points int(11) DEFAULT '0',
coins int(32) NOT NULL DEFAULT '0',
status bit(2) DEFAULT b'1',
bio longtext COLLATE utf8_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table message
DROP TABLE IF EXISTS message;
CREATE TABLE IF NOT EXISTS message (
m_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
content text COLLATE utf8_unicode_ci NOT NULL,
send_date date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table message_send
DROP TABLE IF EXISTS message_send;
CREATE TABLE IF NOT EXISTS message_send (
m_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
receiver_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
status bit(1) NOT NULL DEFAULT b'0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table projects
DROP TABLE IF EXISTS projects;
CREATE TABLE IF NOT EXISTS projects (
pro_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
pro_name text COLLATE utf8_unicode_ci NOT NULL,
emp_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
p_describe text COLLATE utf8_unicode_ci NOT NULL,
price int(32) NOT NULL,
cut int(32) NOT NULL,
s_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
status bit(2) NOT NULL DEFAULT b'1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table project_milestone
DROP TABLE IF EXISTS project_milestone;
CREATE TABLE IF NOT EXISTS project_milestone (
pm_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
pro_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
milestone int(2) NOT NULL,
job text COLLATE utf8_unicode_ci,
deadline int(3) DEFAULT NULL,
knight_amt int(2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table project_waiting
DROP TABLE IF EXISTS project_waiting;
CREATE TABLE IF NOT EXISTS project_waiting (
pm_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
knight_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
chosen bit(1) NOT NULL DEFAULT b'0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table project_working
DROP TABLE IF EXISTS project_working;
CREATE TABLE IF NOT EXISTS project_working (
pm_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
knight_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
submit date DEFAULT NULL,
approve tinyint(1) DEFAULT NULL,
paid tinyint(1) DEFAULT NULL,
coins_paid int(32) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table security
DROP TABLE IF EXISTS security;
CREATE TABLE IF NOT EXISTS security (
name varchar(100) COLLATE utf8_unicode_ci NOT NULL,
value longtext COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table skills
DROP TABLE IF EXISTS skills;
CREATE TABLE IF NOT EXISTS skills (
s_id varchar(16) COLLATE utf8_unicode_ci NOT NULL,
skill_name text COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Indexes for dumped tables
--
-- Indexes for table admin
ALTER TABLE admin
ADD PRIMARY KEY (id);
--
-- Indexes for table employers
ALTER TABLE employers
ADD PRIMARY KEY (emp_id);
--
-- Indexes for table knights
ALTER TABLE knights
ADD PRIMARY KEY (knight_id), ADD KEY s_id (s_id);
--
-- Indexes for table message
ALTER TABLE message
ADD PRIMARY KEY (m_id);
--
-- Indexes for table message_send
ALTER TABLE message_send
ADD KEY m_id (m_id);
--
-- Indexes for table projects
ALTER TABLE projects
ADD PRIMARY KEY (pro_id), ADD KEY emp_id (emp_id), ADD KEY s_id (s_id);
--
-- Indexes for table project_milestone
ALTER TABLE project_milestone
ADD PRIMARY KEY (pm_id), ADD KEY pm_id (pm_id,pro_id,milestone), ADD KEY project_milestone_ibfk_1 (pro_id);
--
-- Indexes for table project_waiting
ALTER TABLE project_waiting
ADD PRIMARY KEY (knight_id,pm_id), ADD KEY knight_id (knight_id), ADD KEY pm_id (pm_id);
--
-- Indexes for table project_working
ALTER TABLE project_working
ADD PRIMARY KEY (pm_id,knight_id), ADD KEY pm_id (pm_id,knight_id), ADD KEY knight_id (knight_id);
--
-- Indexes for table security
ALTER TABLE security
ADD PRIMARY KEY (name);
--
-- Indexes for table skills
ALTER TABLE skills
ADD PRIMARY KEY (s_id);
--
-- Constraints for dumped tables
--
-- Constraints for table knights
ALTER TABLE knights
ADD CONSTRAINT knights_ibfk_1 FOREIGN KEY (s_id) REFERENCES skills (s_id) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table message_send
ALTER TABLE message_send
ADD CONSTRAINT message_send_ibfk_1 FOREIGN KEY (m_id) REFERENCES message (m_id) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table projects
ALTER TABLE projects
ADD CONSTRAINT projects_ibfk_1 FOREIGN KEY (emp_id) REFERENCES employers (emp_id) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT projects_ibfk_2 FOREIGN KEY (s_id) REFERENCES skills (s_id) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table project_milestone
ALTER TABLE project_milestone
ADD CONSTRAINT project_milestone_ibfk_1 FOREIGN KEY (pro_id) REFERENCES projects (pro_id);
--
-- Constraints for table project_waiting
ALTER TABLE project_waiting
ADD CONSTRAINT project_waiting_ibfk_1 FOREIGN KEY (pm_id) REFERENCES project_milestone (pm_id) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT project_waiting_ibfk_2 FOREIGN KEY (knight_id) REFERENCES knights (knight_id) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table project_working
ALTER TABLE project_working
ADD CONSTRAINT project_working_ibfk_1 FOREIGN KEY (pm_id) REFERENCES project_milestone (pm_id) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT project_working_ibfk_2 FOREIGN KEY (knight_id) REFERENCES knights (knight_id) ON DELETE CASCADE ON UPDATE CASCADE;
I have solved the problem with combine all of them into one query:
insert into `project_working`(`pm_id`,`knight_id`) values('P1_1','K1'),('valueX','valueY');
update set `chosen`=true where (`pm_id`='P1_3' and `knight_id`='K2') or (`pm_id`='P1_1' and `knight_id`='K1') or(...);

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.

How do I create this relationship in phpMyAdmin?

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;

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