MySQL CONSTRAINT FOREIGN KEY syntax - mysql

I am new to MySQL and am messing with a database I created just to learn.
I created my first table and it worked OK:
mysql> CREATE TABLE Pizzas(ItemID INTEGER PRIMARY KEY, Size INTEGER, Price DECIMAL);
Query OK, 0 rows affected (0.90 sec)
then I tried to create another table using CONSTRAINT (I was simply copying the way foreign keys were created in a class I am taking) and I got an error:
mysql> CREATE TABLE Customers(CustomerID PRIMARY KEY, Name CHAR(20), ItemID INTEGER, CONSTRAINT FOREIGN KEY (ItemID) REFERENCES Pizzas (ItemID));
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 'PRIMARY KEY, Name CHAR(20), ItemID INTEGER, CONSTRAINT FOREIGN KEY (ItemID) REFE' at line 1
I'm using a linux computer and my MySQL version is 5.5. Why am I getting this problem?

your table definition is not correct. you are missing the datatype in CustomerID PRIMARY KEY. It should be
CREATE TABLE Customers(CustomerID int not null PRIMARY KEY,
<-- Here
Name CHAR(20), ItemID INTEGER,
CONSTRAINT FOREIGN KEY (ItemID) REFERENCES Pizzas (ItemID));

Related

why does my foreign key code show a syntax error?

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

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

Unable to execute a foreign key in MySQL 5.5 (using XAMPP)

I am unable to execute a foreign key in MySQL 5.5 (using XAMPP).
Here is the code I am trying to execute:
create table Category (
Category_ID int,
CategoryName varchar(50),
Primary Key (Category_ID)
);
create table SubCategory (
SubCategory_ID int,
Category_ID int,
SubCategoryName varchar(50),
Primary Key (SubCategory_ID),
Foreign Key Category_ID references Category(Category_ID)
);
I tried replacing int with int(10) but it did not help.
Also, I tried adding ON CASCADE suff but it did not work.
Even adding CONSTRAINT inside and outside the table did not work.
Error I keep on getting is:
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 'references Category(Category_ID))'
Please help.
you need to wrap your foreign key with parenthesis like:
create table SubCategory (
SubCategory_ID int,
Category_ID int,
SubCategoryName varchar(50),
Primary Key (SubCategory_ID),
Foreign Key (Category_ID) references Category(Category_ID)
------------^-----------^
);

Foreign Key Error on MySQL CREATE TABLE statement (err: 150)

I feel I've tried everything possible on a very simple pair of create table statements.
The types match, I tried using ENGINE = InnoDB, etc and am stumped why I'm getting the foreign key error.
I've been away from SQL for some time, so this is probably an easy one.
mysql> CREATE TABLE foo_ent(yyy_no VARCHAR(80),
-> zoo VARCHAR(80),
-> PRIMARY KEY (yyy_no));
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE cat_ent(some_item INTEGER,
-> yyy_no VARCHAR(80),
-> apple DECIMAL(6,2),
-> PRIMARY KEY (some_item),
-> FOREIGN KEY (yyy_no) REFERENCES foo_ent);
ERROR 1005 (HY000): Can't create table 'test.cat_ent' (errno: 15
0)
Sorry about the poor variable names, safe to over-write company stuff.
You don't reference to a field, only a table, which is incorrect.
...
foreign key (yyy_no) references foo_ent(yyy_no)
And according to your error number, the MySQL documentation also states;
If you re-create a table that was
dropped, it must have a definition
that conforms to the foreign key
constraints referencing it. It must
have the right column names and types,
and it must have indexes on the
referenced keys, as stated earlier. If
these are not satisfied, MySQL returns
error number 1005 and refers to error
150 in the error message.
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
You should provide the name of the referenced column explicitly:
CREATE TABLE cat_ent
(
some_item INTEGER NOT NULL,
yyy_non VARCHAR(80),
apple DECIMAL(6,2),
PRIMARY KEY (some_item),
FOREIGN KEY (yyy_non) REFERENCES foo_ent(yyy_no)
);
Consider rewriting your FOREIGN KEY declaration to explicitly list the column in foo_ent that you want to key on:
CREATE TABLE cat_ent(some_item INTEGER,
yyy_no VARCHAR(80),
apple DECIMAL(6,2),
PRIMARY KEY (some_item),
FOREIGN KEY (yyy_no) REFERENCES foo_ent(yyy_no))
ENGINE=InnoDB;
works for me.