why does my foreign key code show a syntax error? - mysql

I'm attempting to create a table in a relational database that has a foreign key and a SQL syntax error consistently occurs.
I am using PHP and mySQL (i've removed any non interacting tables that only hold data and don't contribute to the relations)
CREATE table IF NOT EXISTS logins(
loginID int UNSIGNED AUTO_INCREMENT PRIMARY KEY,
buildingID int FOREIGN KEY REFERENCES buildings(buildingID))
buildings is another table, which has the primary key "buildingID"
the error that shows is
Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FOREIGN KEY REFERENCES buildings(buildingID), login varchar(255)...'
this error doesn't happen when the foreign key is removed, the table creates with no errors; and I create this table after i create the table that the foreign key references.
i have tried using the other syntax:
CREATE table IF NOT EXISTS logins(
loginID int UNSIGNED AUTO_INCREMENT,
buildingID int,
PRIMARY KEY(loginID),
FOREIGN KEY (buildingID) REFERENCES buildings(buildingID))
to no avail: the same error happens
what is my mistake here, and how can I change my code to fix it? Thanks.

the error is that primary keys are not permitted to be unsigned, i changed this one thing in my code and the error went away

Few typos.
CREATE table IF NOT EXISTS logins(
loginID int UNSIGNED AUTO_INCREMENT ,
buildingID int,
PRIMARY KEY(loginID),
FOREIGN KEY (buildingID) REFERENCES buildings(buildingID))
this works fine

Related

Trouble with mysql, does not allow multiple foreign keys

I have three tables in my database
Books(table)
create table books (book_id int auto_increment, bookName varchar(10), qty int,primary key(book_id));
Students(table)
create table students (student_id int auto_increment,studentName varhchar(10),primary key(student_id));
issuedBooks(table)
create table issuedBooks(book_id int ,student_id int ,issued_date date, foreign key (book_id) references books(book_id), foreign key (students) references students(student_id));
mysql report an error that
ERROR 1064 (42000): 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 students(student_id))' at line 1
When I remove second foreign key, It works. I don't understand what mistake did I do?
Works(without any error, I just removed second foreign key)
create table issuedBooks(book_id int ,student_id int,issued_date date, foreign key (book_id) references books(book_id));

mysql: add foreign key after table creation

I am quite new at mysql.
I am trying to add a foreign key after having created two tables.
Here are the query used to create the tables
CREATE TABLE Categorie_article (
categorie_id INT UNSIGNED,
article_id INT UNSIGNED,
PRIMARY KEY (categorie_id, article_id)
);
CREATE TABLE Article (
id INT UNSIGNED AUTO_INCREMENT,
titre VARCHAR(150) NOT NULL,
texte LONGTEXT NOT NULL,
extrait TEXT,
FULLTEXT KEY (texte),
PRIMARY KEY (id)
);
and here is the query used to create the foreign key constraint:
ALTER Categorie_article ADD CONSTRAINT fk_categorie_article FOREIGN KEY (article_id) REFERENCES Article(id);
And here is the message I got :
ERROR 1064 (42000): 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 'Categorie_article
ADD CONSTRAINT fk_categorie_article FOREIGN KEY (article_' at line 1
Can please someone tell me what I am doing wrong?
I tried to look up at others similar questions but it didn't help.
Thanks in advance ;-)!
The syntax is ALTER TABLE <table_name> not just ALTER <table_name>.
So try:
ALTER TABLE Categorie_article ADD CONSTRAINT fk_categorie_article FOREIGN KEY (article_id) REFERENCES Article(id);
db<>fiddle

sql error that i cant solve (7)

can anyone help with this error , when i run my code it comes up with error 1005 can't create table my code looks like this can anyone point out the source of this error im using codio mysql if that helps
CREATE TABLE IF NOT EXISTS entries (
entries_id INT NOT NULL AUTO_INCREMENT,
students_id INT UNSIGNED NOT NULL,
Date_of_exam DATETIME NOT NULL,
subjects_id INT UNSIGNED NOT NULL,
PRIMARY KEY (entries_id),
FOREIGN KEY (students_id) REFERENCES students(students_id),
FOREIGN KEY (subjects_id) REFERENCES subjects(subjects_id));
this is the error
mysql> SOURCE task7
ERROR 1005 (HY000): Can't create table 'exams.entries' (errno: 150)
This error related to foreign keys. Check following items:
Name of tables and columns that exist on foreign keys are correct.
Type of foreign keys columns are same.
Data of foreign keys not conflict with them.
Note: You can disable check foreign key on query. Sure enable this option after query.
SET FOREIGN_KEY_CHECKS=0;
CREATE TABLE IF NOT EXISTS entries (
entries_id INT NOT NULL AUTO_INCREMENT,
students_id INT UNSIGNED NOT NULL,
Date_of_exam DATETIME NOT NULL,
subjects_id INT UNSIGNED NOT NULL,
PRIMARY KEY (entries_id),
FOREIGN KEY (students_id) REFERENCES students(students_id),
FOREIGN KEY (subjects_id) REFERENCES subjects(subjects_id)
);
SET FOREIGN_KEY_CHECKS=1;
The error itself is related with the foreign keys (as removing them creates the table with no errors).
Main possible causes:
One of the referenced tables doesn't exists
The field of the referenced tables doesn't exists
The data types of the referenced table doesn't match (I assume is this).
As i can see your primary key on your table is INT, assuming you use INT as your primary keys, students_id is INT UNSIGNED, possibly the cause of your error.

error creating table on my DB

I am trying to create a table that references another table in my db however I am receiving this error.
#1064 - 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 'UInt32 PRIMARY KEY, name VARCHAR(20), handle VARCHAR(20), countryName VARCHAR(20' at line 1
I have had a look at error #1064 on the mySQL refrence page but its abit of a vuage error just giving the outline of its structure.
Error: 1064 SQLSTATE: 42000 (ER_PARSE_ERROR)
Message: %s near '%s' at line %d
this is the code I have tried to execute
CREATE TABLE User (userID UInt32 PRIMARY KEY, name VARCHAR(20), handle VARCHAR(20), countryName VARCHAR(20) references Countries(SID))engine=innodb;
Countries table has already been created, so I am not entirely sure what is causing the problem
any help would be greatly appreciated.
added not this is how I have defined countries
CREATE TABLE Countries (countryName VARCHAR(20) PRIMARY KEY)engine=innodb;
UPDATED response:
Moving the constraint definition out of the table definition helped isolate the problem:
CREATE TABLE User(
userID UInt32 PRIMARY KEY
,name VARCHAR(20)
,handle VARCHAR(20)
,countryName VARCHAR(20)
)engine=innodb;
ALTER TABLE user ADD CONSTRAINT user_country_name_fk FOREIGN KEY(countryName) REFERENCES Countries(SID);
Table creation failure was caused by the use of the UInt32 data type. Changed to int, table creation succeeded.
Foreign key constraint was referring to the wrong column in Countries. It was referring to SID but needed to refer to countryName.

Error Code: 1005 can't create table

please see my create statement:
CREATE TABLE INTERNAL_MEDICINE_DETAIL(DIAGNOSE_ITEM VARCHAR(15) NOT NULL,
EXM_RESULT VARCHAR(50),
IDENTIFY_ID INT AUTO_INCREMENT PRIMARY KEY,
SUMMARY_ID INT NOT NULL,
FOREIGN KEY(SUMMARY_ID) REFERENCES INTERNAL_MEDICINE_SUMMARY(SUMMARY_ID),
FOREIGN KEY(DIAGNOSE_ITEM) REFERENCES INTERNAL_MEDICINE_ITEM_DEF(DIAGNOSE_ITEM))
The referenced two tables are created successfully:
CREATE TABLE INTERNAL_MEDICINE_ITEM_DEF(DIAGNOSE_ITEM VARCHAR(15) NOT NULL,
ITEM_DESCRIPTION VARCHAR(50) NOT NULL,
IDENTIFY_ID INT AUTO_INCREMENT PRIMARY KEY)
CREATE TABLE INTERNAL_MEDICINE_SUMMARY(SUMMARY_ID INT AUTO_INCREMENT PRIMARY KEY,
SUMMARY VARCHAR(1000),
R_IDENTIFYID INT NOT NULL,
FOREIGN KEY(R_IDENTIFYID) REFERENCES BASICINFO(IDENTIFY_ID))
I have seen the mean of error code 1005 on MySQL web site, it says:
Cannot create table. If the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed. If the error message refers to error –1, table creation probably failed because the table includes a column name that matched the name of an internal InnoDB table.
my error is 150 currently, but I cannot find the difference of the two foreign keys defined in INTERNAL_MEDICINE_DETAIL table with the two fields defined in INTERNAL_MEDICINE_SUMMARY AND INTERNAL_MEDICINE_ITEM_DEF respectively.
So, could you please help me and tell me the reason?
From the InnoDB foreign key constraints documentation:
InnoDB permits a foreign key to reference any index column or group of columns. However, in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.
You'll need to create an index on INTERNAL_MEDICINE_ITEM_DEF that has DIAGNOSE_ITEM as its first column. (No need to change anything for INTERNAL_MEDICINE_SUMMARY since the referenced column is that table's primary key.)