Adding Foreign Key Constraint While Using Composite Key Giving Me ERROR - mysql

I am trying to create three tables however I am getting error as
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 'REFERENCES personal_details(personID)
)'
The above error is for third table i.e. table hobbies_person.
I am creating tables as follow.
CREATE TABLE personal_details (
personID INT PRIMARY KEY,
firstName varchar(30),
middleName varchar(30),
lastName varchar(30),
age INT,
aboutMe varchar(500)
);
CREATE TABLE hobbies (
hobbID INT PRIMARY KEY,
hobbName varchar(30)
);
CREATE TABLE hobbies_person (
personID INT,
hobbID INT,
PRIMARY KEY (personID, hobbID),
FOREIGN KEY personID REFERENCES personal_details(personID)
);
I also tried with
CREATE TABLE hobbies_person (
personID INT,
hobbID INT,
PRIMARY KEY (personID, hobbID),
FOREIGN KEY personID REFERENCES personal_details(personID),
FOREIGN KEY hobbID REFERENCES hobbies(hobbID)
);
but still same error.
Link to check query
Any idea how to tackle this?
NOTE
In table hobbies_person I am using composite primary key as PRIMARY KEY (personID, hobbID),

You need to enclose the column personID in brackets for the FK definition:
CREATE TABLE hobbies_person (
personID INT NOT NULL,
hobbID INT NOT NULL,
PRIMARY KEY (personID, hobbID),
FOREIGN KEY (personID) REFERENCES personal_details(personID)
);

You forgot the () around your foreign key
FOREIGN KEY (personID) REFERENCES personal_details(personID)

Related

mysql One-To-Many Relationship

use one_to_many;
create table models(
model_id int primary key auto_increment unique,
name varchar(50),
manufacturer_id int
);
create table manufacturers(
manufacturer_id int primary key,
name varchar(50),
established_on timestamp,
constraint fk_manufacturers_models foreign key(manufacturer_id)
references models(manufacturer_id)
);
When i try to create one to many model like this example https://ibb.co/bPRGQ5 , it throws an error 1215 cannot add foreign key constraint. What am i doing wrong?
models.manufacturer_id must be indexed.
But normally you would put the foreign key constraint on the many table (models) not the one table.
If I understood correctly what you want to get try this
create table manufacturers(
manufacturer_id int primary key auto_increment unique,
name varchar(50),
established_on timestamp
);
create table models(
model_id int primary key auto_increment unique,
name varchar(50),
manufacturer_id int,
foreign key fk_manufacturer(manufacturer_id)
references manufacturers(manufacturer_id)
);

Unable to alter table with foreign key

My two empty tables:
CREATE TABLE person (
person_id SMALLINT UNSIGNED,
fname VARCHAR(20),
lname VARCHAR(20),
gender ENUM('m', 'f'),
birth_date DATE,
street VARCHAR(30),
city VARCHAR(20),
state VARCHAR(20),
country VARCHAR(20),
postal_code VARCHAR(20),
CONSTRAINT pk_person PRIMARY KEY (person_id)
);
create TABLE favorite_food (
person_id SMALLINT UNSIGNED,
food VARCHAR(20),
CONSTRAINT pk_favorite_food PRIMARY KEY (person_id, food),
CONSTRAINT fk_fav_food_person_id FOREIGN KEY (person_id) REFERENCES person (person_id)
);
Needed modification:
ALTER TABLE person MODIFY person_id SMALLINT UNSIGNED AUTO_INCREMENT;
Result:
ERROR 1833 (HY000): Cannot change column 'person_id': used in a
foreign key constraint 'fk_fav_food_person_id' of table
'tom.favorite_food'
Why is this and is there a way around this without dropping the tables and redefining them?
This is probably because there already is data in person.person_id in any rows (NOT NULL). You can circumvent this by disabling foreign key checks
I believe the best way to handle this is to drop the foreign key reference from favorite_food, alter the column in person and then recreate the foreign key reference. That will properly recreate the index on which the key depends.

how to create a double foreign key

I have to create a table how's structure is
create table reparto
(
numrep integer,
nomespec varchar(20),
nomeosp varchar(20),
cittaosp varchar(25),
primary key (numrep,nomespec,nomeosp,cittaosp),
foreign key(nomeosp,cittaosp) references ospedale(nomeosp,cittaosp),
foreign key nomespec references specializzazione(nomeospe)
);
of course I've already create the tables
create table ospedale
(
nomeosp varchar(20),
cittaosp varchar(25),
numasl integer,
idasp varchar(4),
primary key(nomeosp,cittaosp)
);
and
create table specializzazione
(
nomespe varchar (20) primary key
);
of course it doesn't work and I don't know what to do, can someone tell me how to create several differentsforeign key?
There's a few obvious things we can point out here. The column name given in the foreign key definition:
references specializzazione(nomeospe)
^
Doesn't match the column name in the table definition
... specializzazione ( nomespe ...
^^^^^^^
And the column list for the foreign key should be enclosed in parens
... foreign key (nomespec)
^ ^
You have some literal error and incorect order creating tables please check this:
create table specializzazione
(
nomespe varchar (20) primary key
);
create table ospedale
(
nomeosp varchar(20),
cittaosp varchar(25),
numasl integer,
idasp varchar(4),
primary key(nomeosp,cittaosp)
);
create table reparto
(
numrep integer,
nomespec varchar(20),
nomeosp varchar(20),
cittaosp varchar(25),
primary key (numrep,nomespec,nomeosp,cittaosp),
foreign key(nomeosp,cittaosp) references ospedale(nomeosp,cittaosp),
foreign key (nomespec) references specializzazione(nomespe)
);

MySQL - complete newbie - what's wrong with my foreign key coding?

When trying to create the foreign keys on the last table I get the error "cannot add foreign key constraint" -
create database library_PW;
use library_PW;
create table title(
title_id varchar(20)primary key,
name varchar(50)not null,
reservation_no numeric(10),
lending_time varchar(15));
create table item(
title_id varchar(20)not null,
item_id varchar(20)not null,
constraint pk_item primary key(title_id,item_id));
create table magazine(
mag_id varchar(20)not null,
mag_date varchar(15)not null,
constraint pk_magazine primary key(mag_id,mag_date));
create table book(
ISBN varchar(20)primary key,
date_added date not null);
create table author(
author_id varchar(20)primary key,
author_name varchar(30)not null);
create table book_author(
ISBN varchar(20),
author_id varchar(20),
index (ISBN),
index (author_id),
constraint pk_book_author primary key(ISBN,author_id),
constraint fk_ISBNCode foreign key (ISBN) references book(ISBN),
constraint fk_authorcode foreign key (author_id) references author(author_id));
create table borrower(
membership_id varchar(20)primary key,
name varchar(20)not null,
address varchar(60)not null,
dob date not null,
date_joined date not null,
telephone numeric(12),
email varchar(30));
create table reservation(
title_id varchar(20),
membership_id varchar(20),
reserve_date varchar(20),
index (title_id),
index (membership_id),
constraint pk_reservation primary key(title_id, membership_id,reserve_date),
constraint fk_title foreign key(title_id) references title(title_id),
constraint fk_mem_id foreign key(membership_id) references borrower(membership_id));
create table loan(
title_id varchar(20),
item_id varchar(20),
borrower_date varchar(20),
index (title_id),
index (item_id),
constraint pk_reservation primary key(title_id,item_id,borrower_date),
constraint fk_loantitle foreign key(title_id) references title(title_id),
constraint fk_loanitem foreign key(item_id) references item(item_id));
Thanks in advance!
When I run into this issue, its always because I choose the wrong type data type.. The data type for the child column must match the parent column exactly.
Example: table.key = int & table.child=vchar
For me, its always that! Hope that helps.
Thanks all! It wasn't the data type (for once), it was the problem with the table item as suggested, and SHOW ENGINE INNODB STATUS\G pointed me to the answer.
All that was wrong was in the table item I should have had item_id before title_id.
Thanks again.

Syntax error User_id not found in table help !

Sql wont let me create these table because of a syntax error can someone help me.
drop table users;
drop table intrest;
drop table friendships;
create table users(
id INT,
Fname char(15),
Lname char(15),
email char(20),
street char(15),
state char(2),
zip INT,
age INT,
gender char (2),
phone INT,
User_password char(15),
primary key (id),
foreign key (intrest_id) references intrest(id)
);
create table Intrests(
id INT,
description char(30),
Primary key (id),
foreign key (users_id) references users(id)
);
create table User_intrest(
foreign key (users_id) references users(id),
foreign key (intrest_id) references intrest(id)
);
create table friendships(
User_1_id INT,
User_2_id INT,
description Char(20),
foreign key (users_id) references users(id)
);
create table Intrests( id INT, description char(30),
Primary key (id),
foreign key (users_id) references users(id) );
create table User_intrest( foreign key (users_id) references users(id),
foreign key (intrest_id) references intrest(id) );
For interests table, where is the column users_id defined?
Column definitions for user_interest table?
You have a cyclical FK relationship:
Users has Interests(id) as a FK, and Interests has user(id) as a FK.
You don't need EITHER of these since you have a user_intrest table to link them!
You also have several typos with table names, like intrests and intrest.
Also, you should make both your User fields in Friendships be FK to Users, I'm not sure why you want a third, unrelated Users_Id field.
The first (of many) syntax errors you have is in
create table users(
...
primary key (id),
foreign key (intrest_id) references intrest(id) <--- there is no table intrest
);
I would suggest:
But STOP! Don't just copy-paste. Try yourself to see why MySQL gives you that error message: Key column 'intrest_id' doesn't exist in table, isn't the error you get?
Try to first fix that error. What do you have to do? Add an intrest_id field in table users, not just declare it as FOREIGN KEY. (It's not a useful field to have in the end, but do it anyway).
Then rerun your query and try to fix next error. One by one. If you realy get stuck somewhere, well, you know a site to ask questions :)
So, try to fix errors one by one, until you have a script that is running without any errors at all. You'll have learned much more than simply copying and pasting any answer.
CREATE TABLE Users(
id INT,
Fname char(15),
Lname char(15),
email char(50), <-- 20 is too small for emails
street char(15),
state char(2),
zip INT,
age INT,
gender char (2),
phone char(20), <-- phone should be char, not INT
user_password char(15),
PRIMARY KEY (id)
); <-- You don't really need a foreign key to Interests,
<-- that's why you have the User_interest "middle" table
CREATE TABLE Interests(
id INT,
description char(30),
PRIMARY KEY (id)
); <-- You don't really need a foreign key to Users,
<-- for the same reasons.
CREATE TABLE User_interest(
user_id INT, <-- These foreign keys are
interest_id INT, <-- good, but you need to
<-- declare them as fields, too
PRIMARY KEY (user_id, interest_id), <-- It's good if all tables have PK
FOREIGN KEY (user_id) REFERENCES Users(id),
FOREIGN KEY (interest_id) REFERENCES Interests(id)
);
CREATE TABLE Friendships(
user_1_id INT,
user_2_id INT,
description char(20),
PRIMARY KEY (user_1_id, user_2_id),
FOREIGN KEY (user_1_id) REFERENCES Users(id),
FOREIGN KEY (user_2_id) REFERENCES Users(id)
);