MySQL returns only one result - mysql

This is my product table:
CREATE TABLE `products` (
`idproduct` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`description` text NOT NULL,
`price` double unsigned NOT NULL,
`shipping` double unsigned DEFAULT NULL,
`condition` int(10) unsigned NOT NULL,
`returns` int(10) unsigned NOT NULL,
`date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`image` varchar(255) DEFAULT NULL,
PRIMARY KEY (`idproduct`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8
And this is my product_categories table
CREATE TABLE `products_subcategories` (
`idproduct_category` int(10) unsigned NOT NULL AUTO_INCREMENT,
`idproduct` int(10) unsigned NOT NULL,
`idsubcategory` int(10) unsigned NOT NULL,
PRIMARY KEY (`idproduct_category`),
KEY `products_categories_products_idx` (`idproduct`),
KEY `products_categories_categories_idx` (`idsubcategory`),
CONSTRAINT `products_subcategories_products` FOREIGN KEY (`idproduct`) REFERENCES `products` (`idproduct`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `products_subcategories_subcategories` FOREIGN KEY (`idsubcategory`) REFERENCES `subcategories` (`idSubcategory`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
I trying to search in products like that:
Select products.* from products
INNER JOIN products_subcategories
ON products.idproduct = products_subcategories.idproduct
WHERE name LIKE '%p%'
but it returns only one row.. without inner join it returns me more rows

Related

MYSQL - Impossible to create an external key

I am a self taught CS and I am really novice at mySQL. I created a table called "jobs". I would like to create a new table keywords with 3 columns:
keyword_id as a primary key
job_id as a foreign key from the jobs table
keyword, text
This is the query I wrote:
CREATE TABLE `keywords` (
`keyword_id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY(`keyword_id`),
`keyword` text NOT NULL,
FOREIGN KEY (job_id) REFERENCES jobs(job_id)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;
I got this error message:
Key column 'job_id' doesn't exist in table
The current jobs table code is the following:
CREATE TABLE `jobs` (
`title` text NOT NULL,
`type` text NOT NULL,
`location` text NOT NULL,
`salary` int(11) NOT NULL,
`description` text NOT NULL,
`date` date NOT NULL,
`job_id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`job_id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;
You need to have a column in the keywords table to hold the foreign key.
Like this
CREATE TABLE `jobs` (
`title` text NOT NULL,
`type` text NOT NULL,
`location` text NOT NULL,
`salary` int(11) NOT NULL,
`description` text NOT NULL,
`date` date NOT NULL,
`job_id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`job_id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;
CREATE TABLE `keywords` (
`keyword_id` int(11) NOT NULL AUTO_INCREMENT,
`keyword` text NOT NULL,
`job_id` int(11) NOT NULL, #<- new column
PRIMARY KEY(`keyword_id`),
FOREIGN KEY (job_id) REFERENCES jobs(job_id)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;

What Is Possible Use One Reference Field To Multiple Foreign Key Constraint

I want to make 3 Tables like this :
wc_groups Table
CREATE TABLE IF NOT EXISTS `wc_groups` (
`id` int(2) unsigned NOT NULL AUTO_INCREMENT,
`idgroup` int(7) NOT NULL,
`title` varchar(10) NOT NULL,
`content` text,
`status` smallint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
UNIQUE KEY `idgroup` (`idgroup`),
KEY `status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
wc_matches Table
CREATE TABLE IF NOT EXISTS `wc_matches` (
`id` int(4) unsigned NOT NULL AUTO_INCREMENT,
`time` date NOT NULL,
`group_id` int(2) unsigned NOT NULL,
`place` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`team_id_1` int(10) unsigned NOT NULL,
`team_id_2` int(10) unsigned NOT NULL,
`edituser` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `group_id_foreign` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
wc_teams Table
CREATE TABLE IF NOT EXISTS `wc_teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`group_id` int(2) unsigned NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
ALTER TABLE `wc_teams`
ADD CONSTRAINT `group_id_foreign` FOREIGN KEY (`group_id`) REFERENCES `wc_groups` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
Why i got an error when execution Code to create table wc_team ?
What's possible to use What Is Possible Use One Reference Field ( wc_groups (id) ) To Multiple Foreign Key Constraint ?
I don't understand what you mean with multiple foreign keys.
But the problem in your wc_teams create query is that you have a lost , behind
`group_id` int(2) unsigned NOT NULL,
But you also miss your primary key so i guess you need this
CREATE TABLE IF NOT EXISTS `wc_teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`group_id` int(2) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

Fail to create relationship between two tables

I have two tables :
CREATE TABLE `Users` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL DEFAULT '',
`last_name` varchar(50) NOT NULL DEFAULT '',
`login` varchar(50) NOT NULL DEFAULT '',
`password` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
AND
CREATE TABLE `Books` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL DEFAULT '',
`author` varchar(50) NOT NULL DEFAULT '',
`year` int(4) NOT NULL,
`available` int(3) NOT NULL DEFAULT '0',
`availabledate` date NOT NULL,
`user_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
I am trying to create a relationship between those two, so that one user may have multiple books (user_id) but whatever I'm doing I'm getting errors. Either
Cannot add or update a child row: a foreign key constraint fails
(bookstore.,
CONSTRAINT books_fk FOREIGN KEY (user_id) REFERENCES users
(user_id) ON DELETE CASCADE ON UPDATE CASCADE)
or before I didn't use unsigned int in the Books table and I said that default value is 0 (which I would prefere but I don't think I can do that?) In that case I got error 150.
I'd recommend, to change your database schema. Why?
Can a book exist without having a user? If yes, you shouldn't have a foreign key from books referencing users. Can a user exist without having a book? If yes, you shouldn't have a foreign key from users referencing books.
Can a user have multiple books? And a book multiple users? If yes, you have a m:n relationship. This means you need a bridge table.
In your tables you don't need foreign keys:
CREATE TABLE `Users` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL DEFAULT '',
`last_name` varchar(50) NOT NULL DEFAULT '',
`login` varchar(50) NOT NULL DEFAULT '',
`password` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
CREATE TABLE `Books` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL DEFAULT '',
`author` varchar(50) NOT NULL DEFAULT '',
`year` int(4) NOT NULL,
`available` int(3) NOT NULL DEFAULT '0',
`availabledate` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
And a bridge table would look like this:
CREATE TABLE books_users (
book_id int(11) unsigned NOT NULL,
user_id int(11) unsigned NOT NULL,
PRIMARY KEY (book_id, user_id),
KEY idx_user_id (user_id),
FOREIGN KEY fk_books (book_id) REFERENCES Books(id),
FOREIGN KEY fk_users (user_id) REFERENCES Users(user_id)
) ENGINE=InnoDB;
This solves both problems and is common practice.
To query both users and books in one query, you join them like this:
SELECT
whatever
FROM
Books b
INNER JOIN books_users bu ON b.id = bu.book_id
INNER JOIN users u ON bu.user_id = u.user_id
WHERE user_id = 1 /*for example*/
;
If you want to insert something in the tables, just do the insert and get the id which was generated for the row with SELECT LAST_INSERT_ID();, then insert this id in the books_users bridge table.
Updates don't affect anything, you can simply perform them on users or books. If you really really have to update the auto_increment column (which usually isn't needed and not recommended) you can add ON UPDATE CASCADE after the foreign keys in the books_users table.
Change these lines and try again
change user_id int(11) unsigned NOT NULL AUTO_INCREMENT to user_id
int(11) NOT NULL AUTO_INCREMENT
and
id int(11) unsigned NOT NULL AUTO_INCREMENT, to id int(11) NOT
NULL AUTO_INCREMENT,
and
user_id int(11) unsigned NOT NULL, to user_id int(11) NOT NULL,
Finally try
CREATE TABLE `Users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL DEFAULT '',
`last_name` varchar(50) NOT NULL DEFAULT '',
`login` varchar(50) NOT NULL DEFAULT '',
`password` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
and
CREATE TABLE `Books` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL DEFAULT '',
`author` varchar(50) NOT NULL DEFAULT '',
`year` int(4) NOT NULL,
`available` int(3) NOT NULL DEFAULT '0',
`availabledate` date NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
If you are using Mysql then you must use InnoDB database engine to enable relationship between two tables
MyISAM will not allow this relationship
alter table `users` add constraint `FK_users` FOREIGN KEY (`user_id`) REFERENCES `books` (`user_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
or you can use this query for cascade edit delete
alter table `users` add constraint `FK_users` FOREIGN KEY (`user_id`) REFERENCES `books` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
This is your new DDL for two tables try this
CREATE TABLE IF NOT EXISTS `books` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL DEFAULT '',
`author` varchar(50) NOT NULL DEFAULT '',
`year` int(4) NOT NULL,
`available` int(3) NOT NULL DEFAULT '0',
`availabledate` date NOT NULL,
`user_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL DEFAULT '',
`last_name` varchar(50) NOT NULL DEFAULT '',
`login` varchar(50) NOT NULL DEFAULT '',
`password` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `users`
--
ALTER TABLE `users`
ADD CONSTRAINT `FK_users` FOREIGN KEY (`user_id`) REFERENCES `books` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

MYSQL Double Counting Sum

I am trying to sum the views from the impression table but the answer of some are doubling because there are more than 1 flight for some of the campaigns.
I am combining the data from 3 different tables.
Is there something I can do so only one row from the flight table is returned?
SELECT CAMPAIGN_CPM, CAMPAIGN.CAMPAIGN_ID, CAMPAIGN_NAME, MONTHNAME(IMP_DATE), SUM(VIEWS), CONCAT(MONTH(IMP_DATE),'-',YEAR(IMP_DATE)) AS MonthYear
FROM IMPRESSION
INNER JOIN CAMPAIGN
ON IMPRESSION.CAMPAIGN_ID = CAMPAIGN.CAMPAIGN_ID
INNER JOIN FLIGHT
ON FLIGHT.CAMPAIGN_ID = CAMPAIGN.CAMPAIGN_ID
WHERE MONTH(IMP_DATE) = MONTH(now() - interval 1 MONTH) AND CAMPAIGN_CPM > 0
GROUP BY MONTHNAME(IMP_DATE), IMPRESSION.CAMPAIGN_ID, CAMPAIGN_CPM;
If any other information is need please let me know.
Tables
CREATE TABLE `FLIGHT` (
`FLIGHT_ID` int(11) NOT NULL,
`FLIGHT_NAME` varchar(255) DEFAULT NULL,
`FLIGHT_START_DATE` date DEFAULT NULL,
`FLIGHT_END_DATE` date DEFAULT NULL,
`CAMPAIGN_FREQUENCY` int(11) DEFAULT NULL,
`CAMPAIGN_FREQUENCY_PERIOD` varchar(255) DEFAULT NULL,
`CAMPAIGN_CPM` double DEFAULT NULL,
`CAMPAIGN_STATUS` varchar(255) DEFAULT NULL,
`CUSTOM_CAMPAIGN_TYPE` varchar(255) DEFAULT NULL,
`CAMPAIGN_ID` int(11) NOT NULL,
PRIMARY KEY (`FLIGHT_ID`),
KEY `CAMPAIGN` (`CAMPAIGN_ID`),
CONSTRAINT `CAMPAIGN_FK4` FOREIGN KEY (`CAMPAIGN_ID`) REFERENCES `CAMPAIGN` (`CAMPAIGN_ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `IMPRESSION` (
`IMP_DATE` date NOT NULL,
`CAMPAIGN_ID` int(11) NOT NULL,
`BANNER_ID` int(11) DEFAULT NULL,
`CUSTOMER_ID` int(11) DEFAULT NULL,
`ADVERTISER_ID` int(11) DEFAULT NULL,
`PLACEMENT_ID` int(11) DEFAULT NULL,
`IMPRESSIONS_WITH_DEFAULTS` int(11) DEFAULT NULL,
`IMPRESSIONS_WITHOUT_DEFAULTS` int(11) DEFAULT NULL,
`VIEWS` int(11) DEFAULT NULL,
`DEFAULTS` int(11) DEFAULT NULL,
`CLICKS` int(11) DEFAULT NULL,
`IMPRESSION_ID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`IMPRESSION_ID`),
KEY `CAMPAIGN_ID` (`CAMPAIGN_ID`),
KEY `BANNER_ID` (`BANNER_ID`),
KEY `CUSTOMER_ID` (`CUSTOMER_ID`),
KEY `ADVERTISER_ID` (`ADVERTISER_ID`),
KEY `PLACEMENT_ID` (`PLACEMENT_ID`),
KEY `CAMPAIGN_ID_2` (`CAMPAIGN_ID`),
CONSTRAINT `DSF` FOREIGN KEY (`PLACEMENT_ID`) REFERENCES `PLACEMENT` (`PLACEMENT_ID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `ADVERTISER_FK9` FOREIGN KEY (`ADVERTISER_ID`) REFERENCES `ADVERTISER` (`ADVERTISER_ID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `BANNER_FK5` FOREIGN KEY (`BANNER_ID`) REFERENCES `BANNER` (`BANNER_ID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `CAMPAIGN_FK3` FOREIGN KEY (`CAMPAIGN_ID`) REFERENCES `CAMPAIGN` (`CAMPAIGN_ID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `CUSTOMER_FK5` FOREIGN KEY (`CUSTOMER_ID`) REFERENCES `CUSTOMER` (`CUSTOMER_ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=35673 DEFAULT CHARSET=utf8
CREATE TABLE `CAMPAIGN` (
`CAMPAIGN_ID` int(11) NOT NULL,
`CAMPAIGN_NAME` varchar(255) DEFAULT NULL,
`CAMPAIGN_START_DATE` date DEFAULT NULL,
`CAMPAIGN_END_DATE` date DEFAULT NULL,
`ADVERTISER_ID` int(11) NOT NULL,
PRIMARY KEY (`CAMPAIGN_ID`),
KEY `ADVERTISER` (`ADVERTISER_ID`),
CONSTRAINT `ADVERTISER_FK2` FOREIGN KEY (`ADVERTISER_ID`) REFERENCES `ADVERTISER` (`ADVERTISER_ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Thanks in advance.
Try this:
I have added FLIGHT to show that one flight number is shown and I changed the location from views.
SQL code:
SELECT * FROM (
SELECT FLIGHT.FLIGHT_ID, SUM(VIEWS), CAMPAIGN_CPM, CAMPAIGN.CAMPAIGN_ID, CAMPAIGN_NAME, MONTHNAME(IMP_DATE), CONCAT(MONTH(IMP_DATE),'-',YEAR(IMP_DATE)) AS MonthYear
FROM IMPRESSION
INNER JOIN CAMPAIGN
ON IMPRESSION.CAMPAIGN_ID = CAMPAIGN.CAMPAIGN_ID
INNER JOIN FLIGHT
ON FLIGHT.CAMPAIGN_ID = CAMPAIGN.CAMPAIGN_ID
WHERE MONTH(IMP_DATE) = MONTH(now() - interval 1 MONTH) AND CAMPAIGN_CPM > 0
GROUP BY MONTHNAME(IMP_DATE), IMPRESSION.CAMPAIGN_ID, CAMPAIGN_CPM
) a GROUP BY a.CAMPAIGN_ID;
I have made some small alterations because I had to fill the tables with sample data. Furthermore, the there were some issues with the primary keys and cascading. However, Every should work in your version.
TABLE structure:
CREATE TABLE `FLIGHT` (
`FLIGHT_ID` int(11) NOT NULL PRIMARY KEY,
`FLIGHT_NAME` varchar(255) DEFAULT NULL,
`FLIGHT_START_DATE` date DEFAULT NULL,
`FLIGHT_END_DATE` date DEFAULT NULL,
`CAMPAIGN_FREQUENCY` int(11) DEFAULT NULL,
`CAMPAIGN_FREQUENCY_PERIOD` varchar(255) DEFAULT NULL,
`CAMPAIGN_CPM` double DEFAULT NULL,
`CAMPAIGN_STATUS` varchar(255) DEFAULT NULL,
`CUSTOM_CAMPAIGN_TYPE` varchar(255) DEFAULT NULL,
`CAMPAIGN_ID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `IMPRESSION` (
`IMP_DATE` date NOT NULL,
`CAMPAIGN_ID` int(11) NOT NULL,
`BANNER_ID` int(11) DEFAULT NULL,
`CUSTOMER_ID` int(11) DEFAULT NULL,
`ADVERTISER_ID` int(11) DEFAULT NULL,
`PLACEMENT_ID` int(11) DEFAULT NULL,
`IMPRESSIONS_WITH_DEFAULTS` int(11) DEFAULT NULL,
`IMPRESSIONS_WITHOUT_DEFAULTS` int(11) DEFAULT NULL,
`VIEWS` int(11) DEFAULT NULL,
`DEFAULTS` int(11) DEFAULT NULL,
`CLICKS` int(11) DEFAULT NULL,
`IMPRESSION_ID` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY
) ENGINE=InnoDB AUTO_INCREMENT=35673 DEFAULT CHARSET=utf8;
CREATE TABLE `CAMPAIGN` (
`CAMPAIGN_ID` int(11) NOT NULL PRIMARY KEY,
`CAMPAIGN_NAME` varchar(255) DEFAULT NULL,
`CAMPAIGN_START_DATE` date DEFAULT NULL,
`CAMPAIGN_END_DATE` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SAMPLE Data:
INSERT INTO FLIGHT (FLIGHT_ID, CAMPAIGN_ID,CAMPAIGN_CPM)
VALUES (255060,1,250),
(255080,1,250),
(255070,1,250),
(255020,1,40),
(255025,2,300),
(255040,3,250),
(255045,3,20),
(255046,3,100),
(255071,4,210),
(255055,5,280);
INSERT INTO IMPRESSION (IMP_DATE,CAMPAIGN_ID, VIEWS)
VALUES
((NOW() - INTERVAL 10 DAY),1,250),
((NOW() - INTERVAL 1 MONTH),2,4050),
((NOW() - INTERVAL 1 MONTH),3,10),
((NOW() - INTERVAL 3 DAY),3,40),
((NOW() - INTERVAL 1 MONTH),3,10),
((NOW() - INTERVAL 3 DAY),4,40),
((NOW() - INTERVAL 1 MONTH),5,1350);
INSERT INTO CAMPAIGN (CAMPAIGN_ID, CAMPAIGN_NAME)
VALUES
(1,'jkajoiuwejoijdkjjklja'),
(2,'ajiuiouwoihajdhjhhjh'),
(3, 'aauuehxbhchuhnb'),
(4,'aduuiuhzjcnckjnxkj'),
(5,'zzzdfhjfhajkhwkjeajqw');
Here is a SQLFIDDLE demo. Try it out. Good luck with you project.

mysql table creation Error Code: 1005 in this specific case

I have tried everything in solving this issue and yes I know that this type of question is already asked here but I could not solve my issue
It is mysql database
Error Code: 1005
Can't create table '.\project\comments.frm' (errno: 150)
the foreign keys are matching in structure (i.e length and type) then what can be the possible problem in the table creation
Table which is giving error is comments:
CREATE TABLE `comments`(
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`description` VARCHAR(100) NOT NULL,
`user_id` INT(10) UNSIGNED NOT NULL,
`post_id` INT(10) UNSIGNED NOT NULL,
FOREIGN KEY (`post_id`) REFERENCES `posts`.`id`,
FOREIGN KEY (`user_id`) REFERENCES `users`.`id`,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;`
Here is posts table which is already created in the databas
CREATE TABLE `posts` (
`id` int(10) unsigned NOT NULL auto_increment,
`title` varchar(30) default NULL,
`description` longtext,
`image` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Here is the users table which is also already created in the database
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL auto_increment,
`user_name` varchar(33) default NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) default NULL,
`type` varchar(255) NOT NULL,
`registrationDate` date NOT NULL,
PRIMARY KEY (`id`,`email`,`type`)
)
Your syntax is incorrect. The REFERENCES keyword should be followed by table (columns):
CREATE TABLE `comments`(
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`description` VARCHAR(100) NOT NULL,
`user_id` INT(10) UNSIGNED NOT NULL,
`post_id` INT(10) UNSIGNED NOT NULL,
FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`),
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;