I just started learning MYSQL in college and I have an important assignment to do for my class. I have to create a small database and I can't seem to add a table with foreign keys because of the errno(150)
Here's what I have.
create table Country
(CountryName varchar (50) not null,
Primary Key (CountryName));
create table InterestGroup
(IntrestgrpName varchar (30) not null,
Primary Key (IntrestgrpName));
create table Organisation
(OrgName varchar (50) not null,
OrgAddress varchar (30),
OrgTelNo.varchar (30),
Primary Key (OrgName));
create table Qualification
(QualName varchar (50) not null,
Primary Key (QualName));
create table Member
(MemberID varchar (15) not null,
MemberName varchar (30),
MemberAdd varchar (50) not null,
CountryName varchar (50) not null,
IntrestgrpName varchar (30) not null,
QualName varchar (50) not null,
OrgName varchar (50) not null,
Primary Key (MemberID),
Foreign Key (CountryName) References Country (CountryName),
Foreign Key (IntrestgrpName) References InterestGroup (InterestgrpName),
Foreign Key (QualName) References Qualification (Qualname),
Foreign Key (OrgName) References Organisation (OrgName));
I cant seem to get the Member table to be created, It gives this error,
ERROR 1005 (HY000): Can't create table 'iicp.member' (errno: 150)
Thanks in advance for the help, I really need to solve this
here the working query
create table Country
(CountryName varchar (50) not null,
Primary Key (CountryName));
create table InterestGroup
(IntrestgrpName varchar (30) not null,
Primary Key (IntrestgrpName));
create table Organisation
(OrgName varchar (50) not null,
OrgAddress varchar (30),
OrgTelNo varchar (30),
Primary Key (OrgName));
create table Qualification
(QualName varchar (50) not null,
Primary Key (QualName));
create table Member
(MemberID varchar (15) not null ,
MemberName varchar (30),
MemberAdd varchar (50) not null,
CountryName varchar (50) not null,
IntrestgrpName varchar (30) not null,
QualName varchar (50) not null,
OrgName varchar (50) not null,
Primary Key (MemberID),
Foreign Key (CountryName) References Country (CountryName),
Foreign Key (IntrestgrpName) References InterestGroup (IntrestgrpName),
Foreign Key (QualName) References Qualification (Qualname),
Foreign Key (OrgName) References Organisation (OrgName));
DEMO HERE SQLFIDDLE
You SQL is correct.
It worked for me with change the following change:
OrgTelNo.varchar (30) to OrgTelNo varchar (30)
SHOW ENGINE INNODB STATUS;
...
------------------------
LATEST FOREIGN KEY ERROR
------------------------
130211 15:09:26 Error in foreign key constraint of table test/member:
Foreign Key (IntrestgrpName) References InterestGroup (InterestgrpName),
Foreign Key (QualName) References Qualification (Qualname),
Foreign Key (OrgName) References Organisation (OrgName)):
Cannot resolve column name close to:
),
Foreign Key (QualName) References Qualification (Qualname),
Foreign Key (OrgName) References Organisation (OrgName))
...
You referred column named InterestgrpName in table InterestGroup but actual name of column is IntrestgrpName
Foreign Key (IntrestgrpName) References InterestGroup (InterestgrpName),
You have type error here -----------------------------------^
OrgTelNo.varchar (30),
Instead of this you should write
OrgTelNo varchar (30),
And also it is a spell mistake in creation of last table member.
FOREIGN KEY ( IntrestgrpName ) REFERENCES InterestGroup ( IntrestgrpName)
Try this out instead.
Related
No matter how long I look at it I can't find the error.
I put it in a syntax checker online and it said the error was around the ending line.
CREATE TABLE employee (
emp_ID INT (30) NOT NULL,
position VARCHAR (30) NOT NULL,
emp_FName VARCHAR (30) NOT NULL,
emp_LName VARCHAR (30) NOT NULL,
ohip VARCHAR (15) NOT NULL,
home_Phone INT (15),
start_Date DATE,
team_ID INT (30) NOT NULL,
Constraint employee_emp_ID_PK Primary Key (emp_ID),
Constraint employee_team_ID_FK Foreign Key (team_ID)
)
A foreign key needs to references something. So, presumably:
Constraint employee_team_ID_FK Foreign Key (team_ID) references teams(team_id)
or something like that.
In addition, I'm not sure what you mean by int(30). This is merely the display width for the value, and because integers can have only 10 digits (well, 11 if you include a negative sign), 30 doesn't make sense.
For foreign key please specify reference table and its primary key.
CREATE TABLE employee (
emp_ID INT NOT NULL Primary Key,
position VARCHAR (30) NOT NULL,
emp_FName VARCHAR (30) NOT NULL,
emp_LName VARCHAR (30) NOT NULL,
ohip VARCHAR (15) NOT NULL,
home_Phone INT ,
start_Date DATE,
team_ID INT NOT NULL FOREIGN KEY REFERENCES reftable(ID),
)
I am currently taking a SQL class in my college and am currently stuck on review question my professor assigned. I keep getting an error for my schedule table. I feel it has something to do with my foreign key call but I'm not 100% sure? For the Schedule table, she wants the doctorID to be the foreign key and date/clinic to be the primaries
CREATE TABLE Doctor (
DoctorID numeric(3) not null,
Drname varchar (25) not null,
phonenum varchar (12) not null,
CONSTRAINT DoctorID_pk PRIMARY KEY (DoctorID)
);
CREATE TABLE Patient (
PatientNum numeric (5) not null,
SSNum numeric (9) not null,
PTName varchar (25) not null,
DOB numeric (8) not null,
PAddress varchar (40) not null,
DateAdmit numeric (8) not null,
Clinic varchar (30) not null,
CONSTRAINT PatientNum_pk PRIMARY KEY(PatientNum)
);
CREATE TABLE Treatment (
Treatmentcode numeric (6) not null,
Description varchar (50) not null,
CONSTRAINT Treatmentcode_pk PRIMARY KEY(Treatmentcode)
);
CREATE TABLE PatDocTreatment (
PatientNum numeric (5) not null,
DoctorID numeric (3) not null,
Treatmentcode numeric (6) not null,
DateofTreat numeric (8) not null,
Comments varchar (80) not null,
CONSTRAINT PK_PatDocTreatment PRIMARY
KEY(Patientnum,DoctorID,Treatmentcode,DateofTreat),
FOREIGN KEY(PatientNum) REFERENCES Patient(PatientNum),
FOREIGN KEY(DoctorID) REFERENCES Doctor(DoctorID),
FOREIGN KEY(Treatmentcode) REFERENCES Treatment(Treatmentcode)
);
CREATE TABLE Schedule (
DoctorID numeric (3) not null,
SchDate numeric (8) not null,
Clinic varchar (30) not null,
Hoursworked numeric (3) not null,
CONSTRAINT PK_Schedule PRIMARY KEY(DoctorID,SchDate,Clinic),
FOREIGN KEY (DoctorID) REFERENCES Doctor (DoctorID)
FOREIGN KEY (CLINIC) REFERENCES Patient (Clinic)
);
You have possibly missed a comma between the last two foreign keys declarations in your schedule table.
I keep on getting error 1072 when trying to add a foreign key attribute and linking my attribute to another table. I've tried different ways of writing this but I keep on getting the message that "department_id doesn't exist in the table". If you could give me a hand that would be awesome! thank you so so much!
Here is the my code:
Alter table employee
add constraint department_fk
foreign key ( department_id)
references department(department_id);
And here is the rest of my tables:
Create table Department (
department_id integer auto_increment primary key not null,
department_name varchar (50) not null,
Office_number varchar(50) not null,
phone char (20) not null
);
Create table employee (
employee_id integer auto_increment primary key not null,
first_name varchar (25) not null,
last_name varchar (25) not null,
phone char(20) not null,
email varchar (100) not null,
salary decimal (10,2)
);
Create table project (
project_id integer auto_increment primary key not null,
max_hours time not null,
start_date datetime not null,
end_date datetime not null,
Project_lead integer,
Constraint project_fk1
foreign key (employee_id)
references employee(employee_id),
Department_id integer,
Constraint project_fk2
foreign key (department_id)
references department (department_id)
);
Create table assignment (
employee_id integer not null,
project_id integer not null,
hours_worked time not null,
primary key (employee_id, project_id),
constraint assignment_fk1
foreign key(employee_id)
references employee(employee_id),
constraint assignment_fk2
foreign key (project_id)
references project(project_id)
);
Alter table project
drop foreign key department_id;
Thank you in advance!
You need a Foreign key column in your employee table:
Create table employee (
employee_id integer auto_increment primary key not null,
first_name varchar (25) not null,
last_name varchar (25) not null,
phone char(20) not null,
email varchar (100) not null,
dept_id integer,
salary decimal (10,2)
);
Alter table employee
add constraint department_fk
foreign key ( dept_id)
references department(department_id);
Trust the message ;) - the column "department_id" doesn't exist in your employee table!
I've a table
CREATE TABLE [dbo].[CoursesOfferedToBatches] (
[CourseId] VARCHAR (50) NOT NULL,
[Batch] INT NOT NULL,
PRIMARY KEY CLUSTERED ([Batch] ASC, [CourseId] ASC),
CONSTRAINT [FK_OfferedCourses_ToCourses] FOREIGN KEY ([CourseId]) REFERENCES [dbo].[Courses] ([Id]),
CONSTRAINT [FK_CoursesOfferedToBatches_ToBatches] FOREIGN KEY ([Batch]) REFERENCES [dbo].[Batches] ([Id])
);
When I export CourseId as foreign key from above table into below table as shown below, it gives error shown below after table
CREATE TABLE [dbo].[StudentsRegisterToCoursesOffered]
(
[StudentId] UNIQUEIDENTIFIER NOT NULL ,
[CourseId] VARCHAR(50) NOT NULL,
PRIMARY KEY ([CourseId], [StudentId]),
CONSTRAINT [FK_StudentsRegisterToCoursesOffered_ToStudents] FOREIGN KEY ([StudentId]) REFERENCES [Students]([StudentId]),
CONSTRAINT [FK_StudentsRegisterToCoursesOffered_ToCoursesOffered] FOREIGN KEY ([CourseId]) REFERENCES [CoursesOfferedToBatches]([CourseId])
)
Error:
Update cannot proceed due to validation errors. Please correct the
following errors and try again.
SQL71516 :: The referenced table '[dbo].[CoursesOfferedToBatches]'
contains no primary or candidate keys that match the referencing
column list in the foreign key. If the referenced column is a computed
column, it should be persisted.
My question finishes here but for reference I'm adding all relevant tables:
CREATE TABLE [dbo].[Courses] (
[Id] VARCHAR (50) NOT NULL,
[Name] VARCHAR (100) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
CREATE TABLE [dbo].[Students] (
[StudentId] UNIQUEIDENTIFIER NOT NULL,
[DepartmentName] VARCHAR (50) NOT NULL,
[Address] VARCHAR (250) NULL,
[TwitterLink] VARCHAR (100) NULL,
[FacebookLink] VARCHAR (100) NULL,
[MemberSince] DATETIME NULL,
[ProfileViews] BIGINT DEFAULT ((0)) NULL,
[CNIC] CHAR (15) NULL,
[AboutMe] VARCHAR (3000) NULL,
PRIMARY KEY CLUSTERED ([StudentId] ASC),
CONSTRAINT [FK_Students_ToStudents] FOREIGN KEY ([StudentId]) REFERENCES [dbo].[Users] ([UserId]),
CONSTRAINT [FK_Students_ToDepartments] FOREIGN KEY ([DepartmentName]) REFERENCES [dbo].[Departments] ([Name])
);
CREATE TABLE [dbo].[Batches] (
[Id] INT NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
The table CoursesOfferedToBatches needs to have its own primary key, which you have created as a compound key PRIMARY KEY CLUSTERED ([Batch] ASC, [CourseId] ASC). It is this compound key that would be a foreign key in StudentsRegisterToCoursesOffered and not just the CourseId.
create table Department (
Dep_ID int not null,
Dep_Name varchar(30),
primary key (Dep_ID),
)
create table Course (
C_ID int not null,
C_Name varchar (30) not null,
C_Duration varchar (10) not null,
DegreeType varchar (20),
Dep_ID int,
primary key (C_ID),
constraint DEP_ID1 foreign key (Dep_ID) references Department (Dep_ID) on update cascade,
)
create table Student (
St_ID int not null,
St_Name varchar (100),
St_age smallint,
St_gender Varchar(6),
St_tel int,
St_ADD varchar (100) not null,
St_city varchar (50)not null,
St_type varchar (20) not null,
St_nationality varchar (5) not null,
Dep_ID int,
C_ID int,
primary key (St_ID),
constraint DEP_ID foreign key (Dep_ID) references Department(Dep_ID) on update cascade,
constraint CO_ID foreign key (C_ID) references Course(C_ID) on update cascade,
)
create table Staff (
Sta_ID int not null,
Sta_Name varchar (100) not null,
Sta_type varchar (20) not null,
Sta_Add varchar (100) not null,
Sta_tel int ,
Dep_ID int,
primary key (Sta_ID),
constraint DEeP_ID foreign key (Dep_ID) references Department (Dep_ID) on update cascade,
)
this is the error im getting why cant i use cascade update on
composite keys
Msg 1785, Level 16, State 0, Line 19
Introducing FOREIGN KEY constraint 'CO_ID' on table 'Student' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Msg 1750, Level 16, State 0, Line 19
Could not create constraint. See previous errors.
I had done a research and get to know that this problem can be resolve by changing the line :
constraint CO_ID foreign key (C_ID) references Course(C_ID) on update cascade, to constraint CO_ID foreign key (C_ID) references Course(C_ID) on update No Action,
I have get a explanation on the following link :
Foreign key constraint may cause cycles or multiple cascade paths?
This may resolve your problem.
Your problem is in table 'Exam'. Since you specified no source for this table I can only guess ;-)
Exam contains a foreign key constraint Course_ID2
It seems that this foreign key also refrences to Department or Course.
So have a look into this constraint first. If you need further assistance, please post source of table definition Exam, Department and Course
When you are adding foreign key
They need to be exactly the same data type in both tables.
Try with your query it works fine with me i just remove "," from end of your column
CREATE TABLE Department (
Dep_ID INT NOT NULL,
Dep_Name VARCHAR(30),
PRIMARY KEY (Dep_ID)
)
CREATE TABLE Course (
C_ID INT NOT NULL,
C_Name VARCHAR (30) NOT NULL,
C_Duration VARCHAR (10) NOT NULL,
DegreeType VARCHAR (20),
Dep_ID INT,
PRIMARY KEY (C_ID),
CONSTRAINT DEP_ID1 FOREIGN KEY (Dep_ID) REFERENCES Department (Dep_ID) ON UPDATE CASCADE
)
CREATE TABLE Student (
St_ID INT NOT NULL,
St_Name VARCHAR (100),
St_age SMALLINT,
St_gender VARCHAR(6),
St_tel INT,
St_ADD VARCHAR (100) NOT NULL,
St_city VARCHAR (50)NOT NULL,
St_type VARCHAR (20) NOT NULL,
St_nationality VARCHAR (5) NOT NULL,
Dep_ID INT,
C_ID INT,
PRIMARY KEY (St_ID),
CONSTRAINT DEP_ID FOREIGN KEY (Dep_ID) REFERENCES Department(Dep_ID) ON UPDATE CASCADE,
CONSTRAINT CO_ID FOREIGN KEY (C_ID) REFERENCES Course(C_ID) ON UPDATE CASCADE
)
CREATE TABLE Staff (
Sta_ID INT NOT NULL,
Sta_Name VARCHAR (100) NOT NULL,
Sta_type VARCHAR (20) NOT NULL,
Sta_Add VARCHAR (100) NOT NULL,
Sta_tel INT ,
Dep_ID INT,
PRIMARY KEY (Sta_ID),
CONSTRAINT DEeP_ID FOREIGN KEY (Dep_ID) REFERENCES Department (Dep_ID) ON UPDATE CASCADE
)