SQL 5.6 Foreign key statement error - mysql

i am new to SQL and i am trying out some simple create table statement but i am facing some problem with the foreign key at EMPLOYEEGROUP table. Below is a part of my create table statement
CREATE TABLE `User` (
`Userid` int(11) NOT NULL AUTO_INCREMENT,
`Username` VARCHAR(15) NOT NULL,
`Password` VARCHAR(15) NOT NULL,
PRIMARY KEY (Userid)
);
CREATE TABLE `Group` (
`GroupID` int(11) NOT NULL AUTO_INCREMENT,
`GroupName` VARCHAR(15) NOT NULL,
PRIMARY KEY (GroupID)
);
CREATE TABLE `EMPLOYEEGROUP` (
`EmployeeID` int(11) NOT NULL,
`AssignedGrp` int(11) NOT NULL,
CONSTRAINT EMPLOYEEGROUP_FK1 FOREIGN KEY (EmployeeID) REFERENCES User (Userid),
CONSTRAINT EMPLOYEEGROUP_FK2 FOREIGN KEY (AssignedGrp) REFERENCES Group (GroupID)
);
There is some issue with my EMPLOYEEGROUP_FK2 statement which i can't seem to solve it. any help would be appreciated. Thank you

In mysql GROUP is a reserved keyword. and you used group as table name
. but in table name you write group with `` . and when you take reference of group table you write group without ``. simple add `` in group name
Try blow query. it execute in my system hopfully execute also your system
CREATE TABLE `User` (
`Userid` int(11) NOT NULL AUTO_INCREMENT,
`Username` VARCHAR(15) NOT NULL,
`Password` VARCHAR(15) NOT NULL,
PRIMARY KEY (Userid)
);
CREATE TABLE `Group` (
`GroupID` int(11) NOT NULL AUTO_INCREMENT,
`GroupName` VARCHAR(15) NOT NULL,
PRIMARY KEY (GroupID)
);
CREATE TABLE `EMPLOYEEGROUP` (
`EmployeeID` int(11) NOT NULL,
`AssignedGrp` int(11) NOT NULL,
CONSTRAINT EMPLOYEEGROUP_FK1 FOREIGN KEY (EmployeeID) REFERENCES User (Userid),
CONSTRAINT EMPLOYEEGROUP_FK2 FOREIGN KEY (AssignedGrp) REFERENCES `Group` (GroupID)
);

Related

Error number: 3780 Referencing column '%s' and referenced column '%s' in foreign key constraint '%s' are incompatible

DROP DATABASE IF EXISTS ProviderPatients;
CREATE DATABASE ProviderPatients;
USE ProviderPatients;
CREATE TABLE IF NOT EXISTS Date_Dim
(
Date_ID integer not null,
Date_ date,
Full_Date_Des varchar(25) not null,
Day_Of_Week int(11) not null,
Calender_Year int(11) not null,
Weekday_Indicator int(11) not null,
PRIMARY KEY (Date_ID)
);
CREATE TABLE IF NOT EXISTS Insurer_DIM
(
Insurer_ID int(11) not null,
Insurer_Name varchar(25) not null,
Line_Of_Buissness varchar(25) not null,
PRIMARY KEY (Insurer_ID)
);
CREATE TABLE IF NOT EXISTS Member_DIM
(
Member_ID int(11) not null,
Member_Name varchar(25) not null,
Age int(11) not null,
Ethnicity varchar(25) not null,
Health_Condition varchar(25) not null,
PRIMARY KEY (Member_ID)
);
CREATE TABLE IF NOT EXISTS Geography_Dim
(
Geography_ID varchar(25) not null,
Country varchar(25) not null,
State varchar(10) not null,
State_Code int(11) not null,
County_Code int(11) not null,
PRIMARY KEY (Geography_ID)
);
CREATE TABLE Provider_Dim
(
Provider_ID int(11) not null,
Provider_Name VARCHAR(45) NOT NULL,
Gender Varchar(25) Not Null,
NPI Varchar(25) Not Null,
Credential Varchar(25) Not Null,
PRIMARY KEY(Provider_ID)
);
CREATE TABLE Eval_Fact_Table
(
Date_ID int(11) not null,
Member_ID int(11) not null,
Provider_ID int(11) not null,
Insurer_ID int(11) not null,
Geography_ID int(11) not null,
Num_Visits int(11) not null,
Eval_Costint int(11) not null,
Eval_Start date not null,
Eval_End date not null,
FOREIGN KEY (Date_ID)
REFERENCES Date_Dim (Date_Id) ON DELETE RESTRICT,
FOREIGN KEY (Member_ID)
REFERENCES Member_Dim (Member_ID) ON DELETE RESTRICT,
FOREIGN KEY (Geography_ID)
REFERENCES Geography_Dim (Geography_ID) ON DELETE RESTRICT,
FOREIGN KEY (Provider_ID)
REFERENCES Proveider_Dim (Provider_ID) ON DELETE RESTRICT,
FOREIGN KEY (Insurer_ID)
REFERENCES Insurer_Dim (Insurer_ID) ON DELETE RESTRICT
);
Error number: 3780; Symbol: ER_FK_INCOMPATIBLE_COLUMNS; SQLSTATE: HY000
Message:Error Code: 3780. Referencing column 'Geography_ID' and referenced column 'Geography_ID' in foreign key constraint 'eval_fact_table_ibfk_3' are incompatible.
Error Referencing column 'Geography_ID' and referenced column 'Geography_ID' in foreign key constraint 'eval_fact_table_ibfk_3' are incompatible.
is quite clear, columns are incompatible:
CREATE TABLE IF NOT EXISTS Geography_Dim (
Geography_ID varchar(25) not null,
CREATE TABLE Eval_Fact_Table(
... truncated
Geography_ID int(11) not null,
Make them of same type or remove foreign key constraint.
You can read more about foreign key constraints in documentation, most interesting part is
Corresponding columns in the foreign key and the referenced key must
have similar data types.
That is not true in your case : varchar(25) vs. int(11)
I tried all other ways but I found this useful as it worked for me. It is applicable also to the new MySQL version(v8.0)
CREATE TABLE Customers(
Customers_ID INT(10) PRIMARY KEY AUTO_INCREMENT,
Customers_name varchar(40) not null,
Customers_phone INT(10) not null,
Customers_email varchar(40) not null,
date_became_customer DATE not null,
login varchar(40) not null,
password varchar(40) not null,
other_details varchar(40) not null,
Customer_Types_code int(10) not null
REFERENCES Customer_types(Customer_Types_code));
Just change Geograpy_ID on Geography_Dim table to Geography_ID int(11) or change Geograpy_ID on Eval_Fact_Table to Geography_ID varchar(25) to solve the problem.
Error number: 3780 is due to incompatible foreign keys, the datatype of foreign key in each table must be same .
You could also not edit the primary key column in one table if it is a foreign key in another table.
For editing first drop the foreign key constraint in child table and then edit the primary key in the parent table and then again add constraints to child table.
HERE: Geography_ID varchar(25) vs Geography_ID int(11) are incompatible
First drop or alter this
FOREIGN KEY (Geography_ID) REFERENCES Geography_Dim (Geography_ID)
Geography_ID int(11) NOT NULL, and then add new column:
FOREIGN KEY (Geography_ID) REFERENCES Geography_Dim (Geography_ID)
with Geography_ID varchar(25)

MySQL MariaDB wont create Table with a foreign key

I'm having a problem with my SQL Tables. So I have my Usertable:
CREATE TABLE IF NOT EXISTS `user` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`NICKNAME` varchar(50) NOT NULL,
`PASSWORD` varchar(255) NOT NULL,
`EMAIL` varchar(50) DEFAULT NULL,
PRIMARY KEY (`NICKNAME`),
UNIQUE KEY `ID` (`ID`)
) ;
This table already exists and was created without any problems. I Want to create a score Table like this:
CREATE TABLE IF NOT EXISTS `scores` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`NICKNAME` varchar(50) NOT NULL,
`HIGHSCORE` int(11) NOT NULL,
PRIMARY KEY (`ID`),
FOREIGN KEY (NICKNAME) REFERENCES USER(Nickname)
)
On execution of the query I get this errormassage:
**#1005 - Kann Tabelle `xxxxx`.`scores` nicht erzeugen (Fehler: 150 "Foreign key constraint is incorrectly formed") (Details…)**
Its in German and says cant create Table, Error 150, I cant see where the key constraint is wrong, I used the same syntax in postgreSQL and it worked fine, but somehow MySQL is not accepting it. Any Ideas?
That is strange. If nickname is indeed the primary key, then your code should work.
It is much more nature, though, to use the auto-incremented column as the primary key. If you want the nickname, then use a join when you query.
So:
CREATE TABLE IF NOT EXISTS `scores` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
UserId int(11) NOT NULL,
`NICKNAME` varchar(50) NOT NULL,
`HIGHSCORE` int(11) NOT NULL,
PRIMARY KEY (`ID`),
FOREIGN KEY (UserId) REFERENCES USER(Id)
);
user - is a keyword.
It may be a good idea to use another table name e.g. "tblUser",
or use quotes again ``
FOREIGN KEY (NICKNAME) REFERENCES `USER`(ID)
Thanks to Mr. Gordon Linoff I was able to find my Mistake:
I had to declare NICKNAME to be declared UNIQUE, only then the FOREIGN KEY constraint could be added to other tables
CREATE TABLE IF NOT EXISTS `user` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`NICKNAME` varchar(50) NOT NULL,
`PASSWORD` varchar(255) NOT NULL,
`EMAIL` varchar(100) DEFAULT NULL,
PRIMARY KEY (`NICKNAME`),
UNIQUE KEY `ID` (`ID`),
UNIQUE KEY `NICKNAME` (`NICKNAME`)
);

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

#1215 - Cannot add foreign key constraint in my sql tables

I have 3 tables in which user_id is defined as primary key user_id in table named users and in another table named updates I'm declaring a foreign key references to user_id,this is my users table:
CREATE TABLE `users` (
`user_id` INT(11) NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(45) ,
`password` VARCHAR(100) ,
`email` VARCHAR(45) ,
`friend_count` INT(11) ,
`profile_pic` VARCHAR(150),
PRIMARY KEY (`user_id`));
My update table is:
CREATE TABLE `updates` (
`update_id` INT(11) AUTO_INCREMENT ,
`update` VARCHAR(45),
`user_id_fk` VARCHAR(45),
`created` INT(11) ,
`ip` VARCHAR(45),
PRIMARY KEY (`update_id`),
FOREIGN KEY (user_id_fk) REFERENCES users(user_id));
on adding foreign key it is giving error saying:
#1215 - Cannot add foreign key constraint
so help me out in this?
You should have users.user_id with the same type and length of updates.user_id_fk :
CREATE TABLE `updates` (
`update_id` INT(11) AUTO_INCREMENT ,
`update` VARCHAR(45),
`user_id_fk` INT(11),
`created` INT(11) ,
`ip` VARCHAR(45),
PRIMARY KEY (`update_id`),
FOREIGN KEY (user_id_fk) REFERENCES users(user_id));

mysql composite foreign key referencing more than 2 attributes

Hi I have a table with 3 attributes as primary key
Products:
PRIMARY KEY (product_name,category,product_type).
Now i am referencing this composite primary key in another table
order_details:
foreign key(product_name,product_type,category) references products(product_name,product_type,category)
however I am getting an error in the console saying missing parenthesis and I am not able to add the foreign key. But if i add only 2 column names in the reference(example : "foreign key(product_name,product_type,category) references products(product_name,product_type)
") the query is not giving an error.
Please help me to resolve this issue. Please find my code below
CREATE TABLE `products` (
`product_name` varchar(45) NOT NULL,
`product_type` varchar(45) NOT NULL,
`category` varchar(45) NOT NULL,
`product_desc` varchar(150) DEFAULT NULL,
`unit_price` int(11) NOT NULL,
`supplier_id` int(11) NOT NULL,
`units_in_stock` int(11) NOT NULL,
PRIMARY KEY (`product_name`,`category`,`product_type`),
INDEX (product_name,category,product_type),
CONSTRAINT `supplier_prod_table_fkey` FOREIGN KEY (`supplier_id`) REFERENCES
`supplier` (`supplier_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=INNODB;
CREATE TABLE `order_details` (
`order_id` int(11) NOT NULL AUTO_INCREMENT,
`product_name` varchar(45) NOT NULL,
`product_type` varchar(45) NOT NULL,
`category` varchar(45) NOT NULL,
`quantity` int(11) DEFAULT NULL,
CONSTRAINT `orderid_fkey` FOREIGN KEY (`order_id`) REFERENCES `orders`
(`order_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
PRIMARY KEY (`order_id`,`product_name`,`product_type`,`category`),
INDEX (product_name,product_type,category),
foreign key(product_name,product_type,category) references products(product_name,product_type,category)
);
Fields in REFERENCES and in correspondent indices must go in the same order in both tables, i.e. (product_name, category, product_type).
CREATE TABLE `order_details` (
`order_id` int(11) NOT NULL AUTO_INCREMENT,
`product_name` varchar(45) NOT NULL,
`product_type` varchar(45) NOT NULL,
`category` varchar(45) NOT NULL,
`quantity` int(11) DEFAULT NULL,
CONSTRAINT `orderid_fkey` FOREIGN KEY (`order_id`) REFERENCES `orders`
(`order_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
PRIMARY KEY (`order_id`,`product_name`,`product_type`,`category`),
INDEX (product_name,category,product_type),
FOREIGN KEY(product_name,category,product_type) REFERENCES products(product_name,category,product_type)
);