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)
);
Related
Hello I have an issue when trying inserting multiple foreign keys to a table. I have searched a lot of hours and still I don't figure it out.. It pops this error. I don't know what else I can do about that. Also I tried to add constraint .. foreign key ... references command and it didn't work.
DROP DATABASE IF EXISTS db;
CREATE DATABASE db;
USE db;
CREATE TABLE BOOKS(
Bno int not null primary key auto_increment,
Title text,
PDate date,
Cno int,
Cname text
);
CREATE TABLE AUTHORS(
Ano int not null primary key auto_increment,
Asurname text,
Aname text
);
CREATE TABLE CATEGORIES(
Cno int not null primary key auto_increment,
Cname text,
No_Of_Books int
);
CREATE TABLE SUMMARY_LANG(
Bno int not null primary key auto_increment,
Language text,
FOREIGN KEY (Bno) REFERENCES BOOKS(Bno)
);
CREATE TABLE WRITER(
Bno int,
Ano int,
Asurname text,
Aname text,
FOREIGN KEY (Bno) REFERENCES BOOKS(Bno),
FOREIGN KEY (Ano) REFERENCES AUTHORS(Ano),
FOREIGN KEY (Asurname) REFERENCES AUTHORS(Asurname),
FOREIGN KEY (Aname) REFERENCES AUTHORS(Aname)
);
INSERT INTO BOOKS(Title,PDate,Cname)
VALUES
('A first course in database systems','2014-01-01','DATABASE'),
('FUNDAMENTAL CONCEPTS OF PROGRAMMING SYSTEMS','1976-01-01','PROGRAMMING');
ALTER TABLE AUTHORS auto_increment = 100;
INSERT INTO AUTHORS(Asurname,Aname)
VALUES
('ULLMAN','JEFF'),
('WIDOM','JENNIFER');
ALTER TABLE CATEGORIES auto_increment = 10;
INSERT INTO CATEGORIES(Cname, No_Of_Books)
VALUES
('DATABASE',1),
('PROGRAMMING',1);
INSERT INTO SUMMARY_LANG(Language)
VALUES
('ENG'),
('GRE'),
('ENG'),
('FRA');
Your definition of SUMMARY_LANG is wrong
CREATE TABLE SUMMARY_LANG(
Bno int not null primary key auto_increment,
Language text,
FOREIGN KEY (Bno) REFERENCES BOOKS(Bno) <-- remove this reference
);
Remove the foreign key, because this is a Table that is used only as reference number to another table also called a helper table, because the text would be redundant in the referenced table.
But i can't see any column that references language.
So add a column to BOOKS, where you add the reference to SUMMARY_LANG and when you add new rows SUMMARY_LANG you won't get any errors anymore.
So the new tables can be like this
CREATE TABLE BOOKS (
Bno INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Title TEXT,
PDate DATE,
Cno INT,
Cname TEXT,
SNno int,
FOREIGN KEY (SNno)
REFERENCES SUMMARY_LANG (SNno)
);
CREATE TABLE SUMMARY_LANG(
SNno int not null primary key auto_increment,
Language text
);
I want to know how can I create weak entities in mySql by Creating tables, I have the code like this:
CREATE TABLE users(
user_Id int AUTO_INCREMENT,
full_Name varchar(60),
email varchar(30),
password varchar(30),
reg_Date timestamp
);
CREATE TABLE personal_Infos(
...
);
These are the tables, all I want to Know is how can I Connect to each-other with foreign key and should I create another primary key at the second table? (Second table has some more informations for the first table)
should I create another primary key at the second table?
Yes you do need to create a primary key in your second table personal_Infos table and that primary key will become foreign key in your users table.
how can I Connect to each-other with foreign key ?
Suppose your primary key in personal_Infos table is P_id, then you can add it as foreign key in your users table like shown below
CREATE TABLE users(
user_Id int AUTO_INCREMENT,
full_Name varchar(60),
email varchar(30),
password varchar(30),
reg_Date timestamp,
p_id int not null,
FOREIGN KEY (P_Id) REFERENCES personal_Infos(P_Id)
);
im trying to create a products table with a primary and foreign key with this.
...
here is the code that i have tried so far
CREATE TABLE products
(
prod_id int NOT NULL,
prod_name int NOT NULL,
price varchar(15)
5on_hand varchar(15),
supp_id varchar(20),
PRIMARY KEY (prod_id),
FOREIGN KEY (supp_id)
);
any help would be greatly appreciated
A foreign key is a reference to a field in another table. To specify a foreign key, you have to specify what field(s) it refers to. The database cannot guess this.
So, guessing that supp_id refers to the id in the Suppliers table, your foreign key clause should look like this:
FOREIGN KEY (supp_id) REFERENCES Supplier(id)
I would strongly encourage the following:
All tables have an primary key column that is an integer and auto-incremented.
Names should be character strings.
Prices should be decimals.
So, I'm thinking something like this is appropriate:
CREATE TABLE Products (
ProductId int NOT NULL auto_increment primary key,
Name varchar(255) NOT NULL,
Price decimal(19, 4),
OnHand integer, -- assuming this is quantity on-hand
SupplierId int,
FOREIGN KEY (SupplierId) REFERENCES Suppliers(SupplierId)
);
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.
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)