i am using sequel pro on MACOSX and i've defined the table containing the foreign key and it still gives me this error Can't create table (errno: 150)
CREATE TABLE members (
mem_id INTEGER,
f_name VARCHAR(10),
l_name VARCHAR(10),
address VARCHAR(40),
name VARCHAR(20),
domain VARCHAR(15),
PRIMARY KEY(mem_id)
)
CREATE TABLE member_phone (
mem_id INTEGER,
phone_no INTEGER,
PRIMARY KEY (mem_id,phone_no),
FOREIGN KEY (mem_id) REFERENCES members
)
You need to specify which reference column is the target:
CREATE TABLE members (
mem_id INTEGER,
f_name VARCHAR(10),
l_name VARCHAR(10),
address VARCHAR(40),
name VARCHAR(20),
domain VARCHAR(15),
PRIMARY KEY(mem_id)
)
CREATE TABLE member_phone (
mem_id INTEGER,
phone_no INTEGER,
PRIMARY KEY (mem_id,phone_no),
FOREIGN KEY (mem_id) REFERENCES members (mem_id)
)
Your foreign key in member_phone needs to reference the column within the members table:
FOREIGN KEY (mem_id) REFERENCES members(mem_id)
Related
This is what I have written:
create table employee (
emp_id int,
emp_first_name varchar(20),
emp_last_name varchar(20),
dept_id int,
email varchar(255),
primary key(emp_id, dept_id)
);
create table department(
dept_id int primary key,
dept_name varchar(255),
gm_no int,
foreign key(dept_id) references employee(dept_id)
);
but when I try to create the department table, it gives me the error(ERROR 1215 (HY000): Cannot add foreign key constraint)
all help is appreciated. thank you.
You have the logic backwards. The dept_id is defined in the department table as the primary key. The foreign key constraint belongs in the tables that reference the department:
create table department(
dept_id int primary key,
dept_name varchar(255),
gm_no int
);
create table employee (
emp_id int,
emp_first_name varchar(20),
emp_last_name varchar(20),
dept_id int,
email varchar(255),
primary key(emp_id, dept_id)
foreign key (dept_id) references department (dept_id)
);
Note that this data structure seems suspicious. I would expect a company called employee to have a unique id for empid. Period.
Employees are obvious in departments, but that could change over time. You would then want another table, say employeeDepartment that captured the changing relationship over time.
Your data model, for instance, would get confusing if an employee starts in department A, switches to B, and then returns to A.
The dept_id in the employee table is not key of any type. Make the dept_in column a KEY, UNIQUE KEY, or PRIMARY KEY on the employee. In your situation, just making it UNIQUE KEY is enough.
As #Sharofiddin said:
create table employee (
emp_id int,
emp_first_name varchar(20),
emp_last_name varchar(20),
dept_id int,
email varchar(255),
primary key(emp_id),
UNIQUE KEY (dept_id) /* add this line and run the query again */
);
create table department(
dept_id int primary key,
dept_name varchar(255),
gm_no int,
foreign key(dept_id) references employee(dept_id)
);
Thank you.
For example, I'm creating two tables as shown below:
create table A (
department_id int,
college_id int,
constraint Pk_name primary key(department_id,college_id)
);
create table B (
student_name varchar(75),
department_id int,
college_id int,
foreign key(department_id,college_id) references A(Pk_name)
);
Can I write like this?
I don't think so because there's no way that the RDBMS could know whether PK_name is a column or a constraint name so I suggest if you stick with the usual :
create table A (
department_id int,
college_id int,
constraint Pk_name primary key(department_id,college_id)
);
create table B (
student_name varchar(75),
department_id int,
college_id int,
foreign key(department_id,college_id) references A(department_id,college_id)
);
I will update the answer once I find an other answer .
Hope somone can help me.
Session Table is here:
CREATE TABLE User (
UID int,
Name varchar(40),
Vorname varchar(15),
Titel varchar(40),
Geschlecht int,
EMailAdr varchar(40),
SHID int,
Guthaben int ,
PRIMARY KEY (UID),
FOREIGN KEY (SHID) REFERENCES Schule(SHID),
UNIQUE (EMailAdr)
);
CREATE TABLE Schule (
SHID int,
Name varchar(40),
Adr varchar(80),
PRIMARY KEY (SHID),
UNIQUE (Adr),
UNIQUE (Name)
);
CREATE TABLE Lokal (
LID int,
Name varchar(40),
Adr varchar(40),
KontoNr varchar(40),
PRIMAYR KEY (LID),
UNIQUE (Adr),
UNIQUE (Name)
);
CREATE TABLE Zahlungsarten (
ZID int,
Bezeichnung varchar(40),
PRIMARY KEY (ZID)
);
CREATE TABLE BestellteArtikel (
ArID int,
BID int,
Anzahl int,
PRIMARY KEY (ArID,BID)
);
CREATE TABLE Status (
SID int,
Bezeichnung varchar(40),
PRIMARY KEY (SID)
);
CREATE TABLE Artikel (
ArID int,
Bezeichnuhg varchar(40),
Beschreibung varchar(120),
Preis int,
LID int,
Liefervolumen int,
PRIMARY KEY (ArID),
UNIQUE (Bezeichnung)
);
CREATE TABLE Bestellungen (
BID int,
UID int,
Datum Date,
ZID int,
SID int,
SHID int,
LID int,
PRIMARY KEY (BID),
FOREIGN KEY (UID) REFERENCES User(UID),
FOREIGN KEY (ZID) REFERENCES Zahkungsarten(ZID),
FOREIGN KEY (SID) REFERENCES Status(SID),
FOREIGN KEY (SHID) REFERENCES Schule(SHID),
FOERIGN KEY (LID) REFERENCES Lokal(LID)
);
There are several problems.
In table User:
You're referencing the table Schule before you have created the latter table. The table must exist before you can declare a foreign key that references it. So this works if you create Schule before you create User (I tested it, and it works).
In table Lokal:
PRIMAYR KEY (LID),
You mistyped PRIMARY.
In table Artikel:
Bezeichnuhg varchar(40),
I think you mistyped Bezeichnung.
In table Bestellungen:
FOERIGN KEY (LID) REFERENCES Lokal(LID)
You mistyped FOREIGN.
With all due respect, you should have been able to correct these mistakes yourself without posting a question to Stack Overflow. Most of them are obvious spelling errors.
create table department
(
dept_name varchar(20),
building varchar(20),
budget numeric(12, 2),
primary key(dept_name)
);
This is table that i created before...
now i am writing this:
create table course
(
course_id varchar(7),
title varchar(50),
dept_name varchar(20),
credits numeric(2, 0),
primary key(course_id),
foreign key (dept_name) references department
);
an it shows :
ERROR 1005 (HY000): Can't create table 'satyarth.course' (errno: 150)
You're missing a portion of the FOREIGN KEY requirement, as you cannot simply refer to another table:
foreign key (dept_name) references department(dept_name)
Add the reference table field name PFB
create table course(course_id varchar(7),
title varchar(50),
dept_name varchar(20),
credits numeric(2, 0),
primary key(course_id),
FOREIGN KEY (`dept_name`) REFERENCES `department` (`dept_name`)
);
I am translating a recursive relationship in a schema to sql and i am trying to reference two columns in a table to one column in another table
the Schema is as following :
Table: Request
(
mem_id1,
mem id2,
approved
)
desired foreign keys:
request.mem_id1 will reference member.mem_id
request.mem_id2 will reference member.mem_id
I have tried the following:
CREATE TABLE members (
mem_id INTEGER,
f_name VARCHAR(10),
l_name VARCHAR(10),
address VARCHAR(40),
name VARCHAR(20),
domain VARCHAR(15),
PRIMARY KEY(mem_id)
)
CREATE TABLE member_phone (
mem_id INTEGER,
phone_no INTEGER,
PRIMARY KEY (mem_id,phone_no),
FOREIGN KEY (mem_id) REFERENCES members (mem_id)
)
CREATE TABLE request (
mem_id1 INTEGER,
mem_id2 INTEGER,
approved BIT(1),
PRIMARY KEY (mem_id1,mem_id2),
FOREIGN KEY (mem_id1,mem_id2) REFERENCES members (mem_id)
)
I believe you want two separate foreign keys:
CREATE TABLE request (
mem_id1 INTEGER,
mem_id2 INTEGER,
approved BIT(1),
PRIMARY KEY (mem_id1, mem_id2),
FOREIGN KEY (mem_id1) REFERENCES members (mem_id),
FOREIGN KEY (mem_id2) REFERENCES members (mem_id)
)