When I am trying to add foreign key constraint to the course table mysql said:
1215 - Cannot add foreign key constraint
create table course
(course_id varchar(8),
title varchar(50),
dept_name varchar(20),
credits numeric(2,0) check (credits > 0),
primary key (course_id),
foreign key (dept_name) references department
on delete set null
);
The ddl statement I used to create the table department is:
create table department
(dept_name varchar(20),
building varchar(15),
budget numeric(12,2) check (budget > 0),
primary key (dept_name)
);
I have used set foreign_key_checks=0 before creating any table.
The LATEST FOREIGN KEY ERROR says:
Syntax error close to:
on delete set null
Try including the column name in the foreign key definition:
foreign key (dept_name) references department(dept_name)
Here is a SQL Fiddle.
Note that the check constraint is parsed, but not enforced. MySQL accepts the syntax but does nothing.
Related
CREATE DATABASE employeeDB;
USE employeeDB;
CREATE TABLE employees(
employeeid NUMERIC(9),
firstname VARCHAR(10),
lastname VARCHAR(20),
deptCode CHAR(5),
salary NUMERIC(9, 2),
PRIMARY KEY (employeeid)
);
CREATE TABLE projects(
projectid CHAR(8),
deptcode CHAR(5),
description VARCHAR(200),
startdate DATE,
stopdate DATE,
revenue NUMERIC(12, 2),
PRIMARY KEY (projectid),
FOREIGN KEY (deptcode) REFERENCES employees(deptCode)
);
CREATE TABLE departments(
code CHAR(5),
name VARCHAR(5),
managerid NUMERIC(9),
subdeptof CHAR(5),
PRIMARY KEY (code),
FOREIGN KEY (managerid) REFERENCES employees(employeeid),
FOREIGN KEY (subdeptof) REFERENCES projects(deptcode)
);
ALTER TABLE employees ADD FOREIGN KEY (deptCode) REFERENCES projects(deptcode);
Something wrong at the line CREATE TABLE projects(...). When I run the code in MySQL it give the Error Code 1822. What is the problem ? Any expert can help ?
You cant create foreign key with a non-primary key, and if you really want to create foreign key to non-primary key (column), the column must be indexed with a unique constraint on it.
So either create unique constraint on deptCode column, or reference by already existing primary key, or change the primary key.
i have been trying to figure out what is wrong but could not find the problem.
MySQL said: Documentation
1064
CREATE TABLE Show
(
Show_Id int NOT NULL AUTO_INCREMENT,
FOREIGN KEY (cinema_id) REFERENCES cinema(cinema_id),
FOREIGN KEY (movie_id) REFERENCES movie(movie_id),
FOREIGN KEY (show_time_id) REFERENCES show_time(show_time_id),
FOREIGN KEY (show_day_id) REFERENCES show_day(show_day_id),
PRIMARY KEY (show_id)
);
The foreign key constraint is applied to an already declared column. It doesn't do the declaration. So, you need to declare the columns:
CREATE TABLE Shows (
Show_Id int NOT NULL AUTO_INCREMENT,
Cinema_id int,
Movie_id int,
Show_Time_id int,
Show_Day_id int,
FOREIGN KEY (cinema_id) REFERENCES cinema(cinema_id),
FOREIGN KEY (movie_id) REFERENCES movie(movie_id),
FOREIGN KEY (show_time_id) REFERENCES show_time(show_time_id),
FOREIGN KEY (show_day_id) REFERENCES show_day(show_day_id),
PRIMARY KEY (show_id)
);
Note that show is a reserved word in MySQL, so I changed the table name to shows. I typically use plurals for table names anyway, because they are collections of things.
create table classroom
(building varchar(15),
room_number varchar(7),
capacity numeric(4,0),
primary key (building, room_number)
);
create table department
(dept_name varchar(20),
building varchar(15),
budget numeric(12,2) check (budget > 0),
primary key (dept_name)
);
The first two create sentence is right. But in
create table course
(course_id varchar(8),
title varchar(50),
dept_name varchar(20),
credits numeric(2,0) check (credits > 0),
primary key (course_id),
foreign key (dept_name) references department
on delete set null
);
It occurs ERROR 1005 (HY000): Can't create table ...
I don't know how to fix it.
Obviously, it's data type of foreign key is consistent.
To get details on the error run SHOW ENGINE INNODB STATUS\G and look for the LATEST FOREIGN KEY ERROR. It should be something like this:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2013-12-04 09:50:19 139428000 Error in foreign key constraint of table test/course:
foreign key (dept_name) references department
on delete set null
):
Syntax error close to:
on delete set null
)
The solution is to specify the column name of the parent table, like this:
create table course
(course_id varchar(8),
title varchar(50),
dept_name varchar(20),
credits numeric(2,0) check (credits > 0),
primary key (course_id),
foreign key (dept_name) references department (dept_name)
on delete set null
);
P.S. You should be aware that the CHECK constraints in your CREATE TABLE statements do not actually do anything. As stated in the manual:
The CHECK clause is parsed but ignored by all storage engines.
I've been reading around StackOverflow and various forums about this problem but I cannot seem to figure it out. When trying to run the "CREATE TABLE Class" and "CREATE TABLE Enroll" commands below I get "ERROR 1005: Can't create table university.class (errno: 150)". I am using InnoDB as my storage engine. The first two "CREATE" statements work fine.
What changes do I need to make so that the "CREATE TABLE Class and CREATE TABLE Enroll" sections work?
CREATE TABLE Student (
stuId VARCHAR(6),
lastName VARCHAR(20) NOT NULL,
firstName VARCHAR(20) NOT NULL,
major VARCHAR(10),
credits FLOAT(3) DEFAULT 0,
CONSTRAINT Student_stuId_pk PRIMARY KEY (stuId),
CONSTRAINT Student_credits_cc CHECK ((credits>=0) AND (credits < 150)));
CREATE TABLE Faculty (
facId VARCHAR(6),
name VARCHAR(20) NOT NULL,
department VARCHAR(20),
rank VARCHAR(10),
CONSTRAINT Faculty_facId_pk PRIMARY KEY (facId));
CREATE TABLE Class (
classNumber VARCHAR(8),
facId VARCHAR(6) NOT NULL,
schedule VARCHAR(8),
room VARCHAR(6),
CONSTRAINT Class_classNumber_pk PRIMARY KEY (classNumber),
CONSTRAINT Class_facId_fk FOREIGN KEY (facId) REFERENCES Faculty
(facId) ON DELETE SET NULL,
CONSTRAINT Class_schedule_room_uk UNIQUE (schedule, room));
CREATE TABLE Enroll (
stuId VARCHAR(6),
classNumber VARCHAR(8),
grade VARCHAR(2),
CONSTRAINT Enroll_classNumber_stuId_pk PRIMARY KEY
(classNumber, stuId),
CONSTRAINT Enroll_classNumber_fk FOREIGN KEY (classNumber)
REFERENCES Class (classNumber) ON DELETE CASCADE,
CONSTRAINT Enroll_stuId_fk FOREIGN KEY (stuId) REFERENCES Student
(stuId)ON DELETE CASCADE);
Here is the full command and error:
mysql> CREATE TABLE Class (classNumber VARCHAR(8), facId VARCHAR(6) NOT NULL, schedule VARCHAR(8), room VARCHAR(6), CONSTRAINT Class_classNumber_pk PRIMARY KEY (classNumber), CONSTRAINT Class_facId_fk FOREIGN KEY (facId) REFERENCES Faculty (facId) ON DELETE SET NULL, CONSTRAINT Class_schedule_room_uk UNIQUE (schedule, room));
ERROR 1005 (HY000): Can't create table 'university.Class' (errno: 150)
Remove NOT NULL in defination of facId
Did you created the Faculty Table first.
check this link
inno db error
which says if you get 1005 with error 150 this means
a foreign key constraint was not correctly formed
This is occur due to a foreign keye error. To get more details on foreign key error, run SHOW ENGINE INNODB STATUS\G and look at the "LATEST FOREIGN KEY ERROR" section.
I think it will tell that the foreign key is invalid because there is no unique index or primary key index on Faculty.facid.
I am new to databases and I keep getting an error when I try to create these tables. Where is a mistake?
I have found loads of questions similar to mine, but they didn't help me to resolve this problem.
CREATE TABLE IF NOT EXISTS course
(
cou_id VARCHAR(3),
course_name VARCHAR(25),
CONSTRAINT pk_course PRIMARY KEY (cou_id, course_name)
);
CREATE TABLE IF NOT EXISTS students_marks
(
stu_id INT,
student_name VARCHAR(25),
course_name VARCHAR(25),
first_mark NUMERIC(2,0),
second_mark NUMERIC(2,0),
third_mark NUMERIC(2,0),
CONSTRAINT pk_studentsmarks PRIMARY KEY (stu_id),
CONSTRAINT fk_studentsmarks_course FOREIGN KEY (course_name) REFERENCES course (course_name)
);
This is from INNODB STATUS.
LATEST FOREIGN KEY ERROR
------------------------
130603 20:17:22 Error in foreign key constraint of table testdb/students_marks:
FOREIGN KEY (course_name) REFERENCES course (course_name)
):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
Your primary key is set to cou_id and course_name in your course table. To add a foreign key constraint in your students_marks table, you'd have to reference both fields (and include both fields in your table definition):
CREATE TABLE IF NOT EXISTS course
(
cou_id VARCHAR(3),
course_name VARCHAR(25),
CONSTRAINT pk_course PRIMARY KEY (cou_id, course_name)
);
CREATE TABLE IF NOT EXISTS students_marks
(
stu_id INT,
student_name VARCHAR(25),
cou_id VARCHAR(3),
course_name VARCHAR(25),
first_mark NUMERIC(2,0),
second_mark NUMERIC(2,0),
third_mark NUMERIC(2,0),
CONSTRAINT pk_studentsmarks PRIMARY KEY (stu_id),
CONSTRAINT fk_studentsmarks_course FOREIGN KEY (cou_id,course_name) REFERENCES course(cou_id,course_name)
);
SQL Fiddle Demo
You are referencing only one field of a composed primary key!