trying to create a table relationship with mysql v5.5 - mysql

Hi all trying to create a relationship between two tables with mysql v5.5 Curious How I go about this...
CREATE TABLE posts(id INT AUTO_INCREMENT,
title varchar(100) NOT NULL,
body varchar(500) NOT NULL,
PRIMARY KEY (id) );
//posts_id not null creating error?
CREATE TABLE comments(id INT AUTO_INCREMENT,
comment varchar(250)NOT NULL,
posts_id NOT NULL,
PRIMARY KEY (id) );
I would like to have a posts_id in comments table share the relationship with the post table id in comments table.

You haven't set posts_id type:
posts_id int(11) NOT NULL
Then your comments table should look like this:
CREATE TABLE IF NOT EXISTS comments (
id int(11) NOT NULL AUTO_INCREMENT,
comment varchar(250) NOT NULL,
posts_id int(11) NOT NULL,
PRIMARY KEY (id),
KEY posts (posts_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
And finally, a foreign relation:
ALTER TABLE comments
ADD CONSTRAINT comments_ibfk_1 FOREIGN KEY (posts_id) REFERENCES posts (id) ON DELETE CASCADE ON UPDATE CASCADE

Give it a type, like varchar, or INT
posts_id varchar(20) NOT NULL

Related

SQL Key Column doesn't exist when ALTER TABLE

ALTER TABLE book
ADD FOREIGN KEY (category_id)
REFERENCES category (category_id)
ON DELETE CASCADE
ON UPDATE CASCADE;
Whenever I run this ALTER TABLE statement, it keeps coming back with, "Error Code: 1072. Key column 'category_id' does not exist."
I'm sure this is an easy fix, but I'm having problems adding a foreign key to one of my tables. I just started learning SQL last week so any help would be appreciated!
My CREATE TABLE statements are as follows:
CREATE TABLE IF NOT EXISTS book
(book_id INT not null PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(100) not null,
description TEXT not null,
price DEC(18,2) not null
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS category
(category_id INT not null PRIMARY KEY AUTO_INCREMENT,
code1 VARCHAR(20) not null,
name1 VARCHAR(100) not null
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS category
(category_id INT not null PRIMARY KEY AUTO_INCREMENT,
code1 VARCHAR(20) not null,
name1 VARCHAR(100) not null
) ENGINE=InnoDB;
This Table has the Primay Key category_id but below table has no Column
as "category_id" which is being referenced in the Alter Command.Add
Column "category_id in below table and then execute Alter command.
CREATE TABLE IF NOT EXISTS book
(book_id INT not null PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(100) not null,
description TEXT not null,
price DEC(18,2) not null
) ENGINE=InnoDB;
Here is the complete script then
CREATE TABLE IF NOT EXISTS book
(book_id INT not null PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(100) not null,
description TEXT not null,
price DEC(18,2) not null,
category_id INT not null
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS category
(category_id INT not null PRIMARY KEY AUTO_INCREMENT,
code1 VARCHAR(20) not null,
name1 VARCHAR(100) not null
) ENGINE=InnoDB;
ALTER TABLE book
ADD FOREIGN KEY (category_id)
REFERENCES category (category_id)
ON DELETE CASCADE
ON UPDATE CASCADE;

Foreign Key Constraints In MySQL DB Gives Error

am trying to implement a foreign key constraint but the mysql keeps giving me an error There are two tables "groups" table and "members" table.I have a many to many relationship between these tables and therefore used a third table called "members_groups" table for the many to many relationship. Note: "groups" and "members" tables have been created and contain data but I now want to add the "members_groups" table. Below are the sql codes:
Below is the script for the members table.
CREATE TABLE IF NOT EXISTS `members` (
`member_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(30) NOT NULL,
`email` varchar(50) NOT NULL,
`password` char(128) NOT NULL,
`salt` char(128) NOT NULL,
`group_id` bigint(64) unsigned NOT NULL,
`perm_override_add` bigint(64) unsigned NOT NULL,
`perm_override_remove` bigint(64) unsigned NOT NULL,
PRIMARY KEY (`member_id`),
KEY `member_id` (`member_id`)
) ENGINE=InnoDB;
script for the groups table
CREATE TABLE IF NOT EXISTS `groups` (
`group_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`group_name` varchar(50) NOT NULL,
`permission` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`group_id`)
) ENGINE=InnoDB
script for the members_groups table
CREATE TABLE members_groups
(
member_id INT(11) NOT NULL ,
group_id INT(10) NOT NULL ,
PRIMARY KEY (member_id, group_id),
FOREIGN KEY (member_id) REFERENCES members(member_id) ON UPDATE CASCADE,
FOREIGN KEY (group_id) REFERENCES groups(group_id) ON UPDATE CASCADE
) ENGINE = InnoDB
This is the error I get from the mysql admin console.
Thanks.
You need to make the type same in your table. In your groups table you have defined
`group_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
whereas in your members_groups table it is
group_id INT(10) NOT NULL ,
Try to make the change like
CREATE TABLE members_groups
(
member_id INT(11) NOT NULL ,
group_id INT(10) unsigned NOT NULL , --The datatype should be same as group_id in `groups` table
PRIMARY KEY (member_id, group_id),
FOREIGN KEY (member_id) REFERENCES members(member_id) ON UPDATE CASCADE,
FOREIGN KEY (group_id) REFERENCES `groups`(group_id) ON UPDATE CASCADE
) ENGINE = InnoDB;
SQL FIDDLE DEMO

mysql creating table foreign key

I've created a table :
CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY
,uName VARCHAR(50)
,uSecondName VARCHAR(50)
,eMail VARCHAR(50)
)
After this I even insert some data without any problems. But when I've tried to create new table with FOREIGN KEY referenced to users.id I've got an error:
CREATE TABLE posts(
id INT(6) AUTO_INCREMENT NOT NULL
,pTitle VARCHAR(155) NOT NULL DEFAULT 'not_set'
,pText TEXT
,pAuthor INT(6)
,PRIMARY KEY(id)
,CONSTRAINT fk_PerAuthor FOREIGN KEY (pAuthor)
REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE
);
Did I miss something?

Errno:150 in MySQL Database

Have an error in my SQL code, but could not understand, where it is. Please, help me to solve that. Here is my code:
CREATE TABLE IF NOT EXISTS Records (
record_id int(11) NOT NULL AUTO_INCREMENT,
record_year year(4) NOT NULL,
record_quarter int(1) NOT NULL,
profit_tax int(11) NOT NULL,
PRIMARY KEY (record_id, record_year, record_quarter),
UNIQUE(record_year, record_quarter)
);
CREATE TABLE IF NOT EXISTS ProductsList (
product_id int(11) NOT NULL AUTO_INCREMENT,
product_name varchar(24) NOT NULL,
PRIMARY KEY (product_id)
);
An error is in that table:
CREATE TABLE IF NOT EXISTS RecordsProducts (
recordproduct_id int(11) NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (recordproduct_id),
FOREIGN KEY (record_id) REFERENCES Records (record_id),
FOREIGN KEY (product_id) REFERENCES ProductsList (record_id)
);
referencing column should be INDEX
referencing column should have same data type of referenced column
both referencing, referenced table should be InnoDB
CREATE TABLE IF NOT EXISTS RecordsProducts (
...
...
record_id int NOT NULL, <=== check data type
product_id int NOT NULL,
INDEX(record_id), <===== check INDEXed or not
INDEX(product_id), <====
FOREIGN KEY (record_id) REFERENCES Records (record_id),
FOREIGN KEY (product_id) REFERENCES ProductsList (record_id)
) ENGINE = InnoDB; <=== add 'ENGINE=InnoDB' explicitly

MySQL creating a table (errno 150)

I would like to ask something that troubles me many many days...
Here is what I mean:
I create these two tables:
CREATE TABLE IF NOT EXISTS journal (
issn varchar(20) NOT NULL,
j_title varchar(100) NOT NULL,
j_publisher varchar(30) NOT NULL,
PRIMARY KEY (issn)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS volume (
volume_no int(11) NOT NULL,
issn varchar(20) NOT NULL,
year int(11) NOT NULL,
PRIMARY KEY (issn,volume_no),
FOREIGN KEY (issn) REFERENCES journal(issn)
) ENGINE=InnoDB;
When I try to create this:
CREATE TABLE IF NOT EXISTS issue (
issue_no int(11) NOT NULL,
issue_pages varchar(10) NOT NULL,
issue_date varchar(10) NOT NULL,
issn varchar(20) NOT NULL,
volume_no int(11) NOT NULL,
PRIMARY KEY (issue_no,issn,volume_no),
FOREIGN KEY (issn) REFERENCES journal(issn),
FOREIGN KEY (volume_no) REFERENCES volume(volume_no)
) ENGINE=InnoDB;
it throws an error (errno 150)
The error is in the foreign key volume_no.
Without FOREIGN KEY (volume_no) REFERENCES volume(volume_no)
the table is created without a problem.... I can't explain what's going on... I have seen it many times again and again but nothing!! Does anybody know what's going on?
Thanks in advance!!
I could see that the foreign key doesnt include issn but which is actually included in primary key for volumn table.
CREATE TABLE IF NOT EXISTS issue ( issue_no int(11) NOT NULL,
issue_pages varchar(10) NOT NULL,
issue_date varchar(10) NOT NULL,
issn varchar(20) NOT NULL,
volume_no int(11) NOT NULL,
PRIMARY KEY (issue_no,issn,volume_no),
FOREIGN KEY (issn,volume_no) REFERENCES volume(issn,volume_no) ) ENGINE=InnoDB;
Look at the below sql fiddle.
http://sqlfiddle.com/#!2/55a63
maybe volume_no needs to be UNSIGNED
FOREIGN keys must reference a PRIMARY or a UNIQUE key in the parent table.
You need only one Foreign Key at table issue, not two. And it should reference the Primary Key of volume:
CREATE TABLE IF NOT EXISTS issue (
issue_no int(11) NOT NULL,
issue_pages varchar(10) NOT NULL,
issue_date varchar(10) NOT NULL,
issn varchar(20) NOT NULL,
volume_no int(11) NOT NULL,
PRIMARY KEY (issn, volume_no, issue_no),
FOREIGN KEY (issn, volume_no)
REFERENCES volume(issn, volume_no)
) ENGINE=InnoDB;
If the PK of the parent table is more than one field, the order of the fields in the FK must be the same as the order in the PK.
issue: FOREIGN KEY (issn, volume_no) REFERENCES volume(issn, volume_no)
These conditions must be satisfied to not get error 150:
The two tables must be ENGINE=InnoDB.
The two tables must have the same charset.
The PK column(s) in the parent table and the FK column(s) must be the same data type.
The PK column(s) in the parent table and the FK column(s), if they have a define collation type, must have the same collation type;
If there is data already in the foreign key table, the FK column value(s) must match values in the parent table PK columns.
source: MySQL Creating tables with Foreign Keys giving errno: 150
I had about the same issue with my database. It wasn't about the definition of the foreign key, actually it was the definition of the primary key field.
CREATE TABLE tblProcesses (
fldProcessesID SMALLINT(5) UNIQUE NOT NULL AUTO_INCREMENT ,
CREATE TABLE tblProcessesMessage (
fldProcesses TEXT NOT NULL,
fldProcessesID VARCHAR(15) NOT NULL DEFAULT ,
The primary key in tblProcesses (fldProcessesID) did not have the UNSIGNED keyword while the foreign key in tblProcessesMessage (fldProcessesID) had the UNSIGNED keyword. This keyword was causing the problem - inconsistent type of field. So i added the UNSIGNED keyword to fldProcessesID in tblPreocesses:
CREATE TABLE tblProcesses (
fldProcessesID SMALLINT(5) UNSIGNED UNIQUE NOT NULL AUTO_INCREMENT,
I hope that this will help you solve your problems.
Best regards,
Nicholas