ERROR 1064 (42000) MySQL PRIMARY KEY REFERENCES - mysql

I never used MySQL before but I don't have choice this time. I've an error I don't understand. I try to create a table :
CREATE TABLE proprietaire(
num_cpt INT REFERENCES compte(num_cpt) PRIMARY KEY,
num_client INT REFERENCES client(num_client),
num_commercant INT REFERENCES commercant(num_commercant)
);
I can't see my mistake. Could you please help me with this ?
Thank You

In MySQL, to enforce foreign key references, you cannot put the references as a modifier on the column. You need a separate constraint for them:
CREATE TABLE proprietaire (
num_cpt INT PRIMARY KEY,
num_client INT,
num_commercant INT,
CONSTRAINT fk_num_cpt FOREIGN KEY REFERENCES compte(num_cpt),
CONSTRAINT fk_num_client FOREIGN KEY REFERENCES client(num_client),
CONSTRAINT fk_num_commercant FOREIGN KEY REFERENCES commercant(num_commercant)
);
However, your specific problem was the REFERENCES before the PRIMARY KEY constraint.
As a note: in most English-language databases, your use of num would be id. num sounds like a count or "number of" something, whereas id is short for identifier.

Related

error 1215: CANNOT ADD FOREIGN KEY CONSTRAINT ; Composite Foreign key

I am getting ERROR CODE 1215: cannot add foreign key constraint for the child table.
Parent table has composite primary key. I want to use that composite primary key as foreign key in the child table.
Please guide me.
PARENT TABLE
CREATE TABLE health.procedures(
Specialty varchar(40),
Procedure_Name varchar(60),
PRIMARY KEY (Procedure_Name, Specialty)
);
CHILD TABLE
CREATE TABLE health.procedureProvided(
specialization varchar(40),
procedure_name varchar(60),
Insurance_ID int REFERENCES health.insurance (idInsurance),
Hospital_ID int REFERENCES health.hospital (idHospital) ,
Doctor_ID int REFERENCES health.doctor( idDoctor) ,
CONSTRAINT procedures_fk foreign key (specialization,procedure_name)references health.procedures(Specialty,Procedure_Name) ,
PRIMARY KEY (specialization, procedure_name, Insurance_ID, Hospital_ID, Doctor_ID)
);
You are specifying a foreign key that is invalid. Your primary key is procedure_name in health.procedure, but you are trying to create a composite foreign key in health.procedureProvided. You can't create that as a foreign key as the column Specialty in the master table is not part of the primary. A foreign key must contain all the columns in the contributing table's primary key but cannot contain values that are not in that primary key. You have three real options. 1. Specify Specialty as being a component of the primary key in procedure. Unfortunately that means the procedure will not necessarily by unique, it will be unique by specialty. 2. add a surrogate key - a system generated sequence value, uuid or something else (timestamps are not recommended). 3. Create validation tables valid_procedures and valid_specialties and use the table health.procedure as an intersection between those to provide valid procedures and correlated specialties and then you could migrate the entire primary key.

SQL Syntax error phpmyadmin #1064

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.

MySQL Foreign Key constraint fails for multiple keys

Hi I am creating several tables some of which have multiple keys linking to the same table. When I try to create the foreign key links I am given:
errno: 150 "Foreign key constraint is incorrectly formed"
Here are the tables I am creating:
create table EventFeed (
EventFeedID integer auto_increment not null,
EventName varchar(100),
EventTime Timestamp,
EventLocation varchar(100),
primary key (EventFeedID)
);
create table Calendar (
MyEventID integer auto_increment not null,
EventFeedID integer,
EventName varchar(100),
EventTime Timestamp,
EventLocation varchar(100),
AttendeeID varchar(100),
primary key (MyEventID),
foreign key (EventFeedID) references EventFeed (EventFeedID),
foreign key (EventName) references EventFeed (EventName),
foreign key (EventTime) references EventFeed (EventTime),
foreign key (EventLocation) references EventFeed (EventLocation),
foreign key (AttendeeID) references Users (UserID)
);
The error seems to be coming from the foreign keys in the 'Calendar' table however some of my other tables have used similar foreign keys and given no errors
can anyone advise what I am doing wrong?
Any help is much appreciated
You cannot have a foreign key on the column which is a non-primary key except the columns with unique constraint. So you have to make EventName, EventTime, etc unique if you want it to be referred by a foreign key.
You can check here at: Technet Microsoft
A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in another table; it can also be defined to reference the columns of a UNIQUE constraint in another table.
You can see this fiddle for working example: http://sqlfiddle.com/#!9/05663

How do I make a column not null and be a foreign key in MySQL

I am trying to create a table that is a foreign key of another table and make it not null, but I am running into trouble making both happen. I was able to successfully get foreign keys working that did not require NOT NULL but I can't get both working.
Here is the line giving me trouble
CONSTRAINT instructor FOREIGN KEY (id) REFERENCES Instructor(id) NOT NULL
then I get the error:
CONSTRAINT instructor FOREIGN KEY (id) REFERENCES Instructor(id) NOT NULL,
*
ERROR at line 5:
ORA-00907: missing right parenthesis
Also, I am getting a weird error when trying to create a table (note, this table is created after creating the table that contains the above error) where it fails at a very trivial part:
CREATE TABLE Enrollment (
CONSTRAINT class_id FOREIGN KEY (id) REFERENCES Class(id) NOT NULL,
CONSTRAINT member_id FOREIGN KEY (id) REFERENCES RecCenterMember(id) NOT NULL,
cost int NOT NULL
);
Then for that command I get this error:
CREATE TABLE Enrollment (
*
ERROR at line 1:
ORA-00904: : invalid identifier
How can I fix these two errors?
You need to create the column before you try creating constraints on the column.
You have:
CREATE TABLE Enrollment (
CONSTRAINT class_id FOREIGN KEY (id) REFERENCES Class(id) NOT NULL,
CONSTRAINT member_id FOREIGN KEY (id) REFERENCES RecCenterMember(id) NOT NULL,
cost int NOT NULL
);
You need:
CREATE TABLE Enrollment (
id INT NOT NULL CONSTRAINT class_id REFERENCES Class(id),
CONSTRAINT member_id FOREIGN KEY (id) REFERENCES RecCenterMember(id),
cost INT NOT NULL
);
Note that in the first line, the FOREIGN KEY isn't necessary because the column is implied. In the second line, the id is identified. You could also write:
CREATE TABLE Enrollment
(
id INT NOT NULL,
CONSTRAINT class_id FOREIGN KEY (id) REFERENCES Class(id),
CONSTRAINT member_id FOREIGN KEY (id) REFERENCES RecCenterMember(id),
cost INT NOT NULL
);
It is unusual, though not automatically wrong, to make a single column (id) be a foreign key of two tables simultaneously. It isn't clear if you actually want three columns in your table — and if you do, which column names are in your table.
You could also use the appropriate notation for an automatically allocated type in MySQL syntax (SERIAL instead of INT NOT NULL, or add AUTO_INCREMENT, etc).
Maybe you're really after:
CREATE TABLE Enrollment
(
id SERIAL,
class_id INT NOT NULL CONSTRAINT class_id REFERENCES Class(id),
member_id INT NOT NULL CONSTRAINT member_id REFERENCES RecCenterMember(id),
cost INT NOT NULL
);
This makes more sense in general. You're creating a new enrollment record for a pre-existing class, and for a pre-existing recreation centre member, and recording its cost.
Syntax diagrams vs actual behaviour
If, as Michael - sqlbot suggests — and I've no reason whatsoever to disbelieve him — MySQL recognizes but does not respond to the REFERENCES clause in a column definition in a CREATE TABLE statement but only acts on full FOREIGN KEY clauses, then you have to adjust my suggested answers from their syntactically correct but semantically ignored form to something like:
Option 1 (minimally changing the SQL from the question):
CREATE TABLE Enrollment (
id INT NOT NULL,
CONSTRAINT class_id FOREIGN KEY (id) REFERENCES Class(id),
CONSTRAINT member_id FOREIGN KEY (id) REFERENCES RecCenterMember(id),
cost int NOT NULL
);
Option 2 (what I consider the most plausible version):
CREATE TABLE Enrollment
(
id SERIAL,
class_id INT NOT NULL,
CONSTRAINT fk_class_id FOREIGN KEY (class_id) REFERENCES Class(id),
member_id INT NOT NULL,
CONSTRAINT fk_member_id FOREIGN KEY (member_id) REFERENCES RecCenterMember(id),
cost INT NOT NULL
);
Or some other variant of this syntax based on the desired table schema ignoring the FK constraints, then adding the constraints along the lines shown.
Key Point
You must define the columns before you define the foreign keys based on those columns.

Cannot add foreign key constraint on a date field

I have the following SQL statements:
CREATE TABLE patient(
Name varchar(255),
Geburtsdatum date,
CONSTRAINT pk_patient PRIMARY KEY (Name,Geburtsdatum)
);
CREATE TABLE fake(
Name varchar(255),
PName varchar(255),
PGeburtsdatum date,
CONSTRAINT pk_fake PRIMARY KEY (Name,PName,PGeburtsdatum),
CONSTRAINT fk_PName2 FOREIGN KEY (PName) REFERENCES patient(Name) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT fk_PGeburtsdatum FOREIGN KEY (PGeburtsdatum) REFERENCES patient(Geburtsdatum) ON DELETE CASCADE ON UPDATE CASCADE
);
This gives me the error "#1215 - Cannot add foreign key constraint". If I remove the last constraint in the second table creation everything works. All my other foreign key constraints work exactly the same way. What am I missing here?
Not sure why you tagged a mysql question as db2. Anyway, the MySQL documentation states:
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan
So add an index in the Geburtsdatum column:
CREATE TABLE patient(
Name varchar(255),
Geburtsdatum date,
INDEX (Geburtsdatum),
CONSTRAINT pk_patient PRIMARY KEY (Name,Geburtsdatum)
);