Have error trying to modify a table in MySQL - mysql

I am trying to add a column to my table and make it a foreign key but I keep getting this error:
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 'FOREIGN KEY(idplayer) REFERENCES
players(playersid)' at line 1
below is my code before the alter statement:
CREATE TABLE transactions
(
transid INT UNSIGNED NOT NULL AUTO_INCREMENT,
type VARCHAR(20),
fromteam VARCHAR(30),
toteam VARCHAR(30),
idplayer INT UNSIGNED NOT NULL,
PRIMARY KEY(transid)
);
Now I try to alter the idplayer and make it a foreign key:
ALTER TABLE transactions
MODIFY idplayer INT UNSIGNED NOT NULL
FOREIGN KEY(idplayer) REFERENCES players(playersid)
Please an assistance would be great.

First off, you are missing a comma after NOT NULL, Second you need to tell mysql to add the CONSTRAINT. Give this a try:
ALTER TABLE `events`
MODIFY idplayer INT UNSIGNED NOT NULL,
ADD CONSTRAINT `FK_Name` FOREIGN KEY (idplayer) REFERENCES players(playersid);

Related

creating a table in the database with a foreign key

I am getting this error whenever I try to create my table:
#1064 - 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 'NOT NULL,
hehi VARCHAR NOT NULL,
adds VARCHAR NOT NULL,
aob VA...' at line 3
This is my code:
CREATE TABLE patients (
patientId int NOT NULL,
problem VARCHAR NOT NULL,
hehi VARCHAR NOT NULL,
adds VARCHAR NOT NULL,
aob VARCHAR NOT NULL,
PRIMARY KEY (patientID),
CONSTRAINT FK_userspatients FOREIGN KEY (user_id)
REFERENCES users(user_id)
);
What might be the problem?
You need to specify the lengths of your VARCHARs,
e.g. if the column problem is supposed to hold 255 characters max.:
problem VARCHAR(255) NOT NULL
see https://mariadb.com/kb/en/varchar/ for more info
Update:
As P.salmon mentioned in the comments, your foreign key definition should be updated to
CONSTRAINT FK_userspatients FOREIGN KEY (patientId)
If patientId refers to the corresponding field user_id of table users.

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 create table with foreign key

In my database I need to create a table which has two foreign keys, I am unable to figure out the source of error although I tried .Any body help me in solving this problem.
The mysql command I gave to create the table
create table book_vegetable(
id int NOT NULL AUTO_INCREMENT,
producer_offer_id int NOT NULL,
consumer_id int NOT NULL,
booked_qty varchar,
PRIMARY KEY(id),
FOREIGN KEY(producer_offer_id)
REFERENCES producer_offer(id),
FOREIGN KEY(consumer_id) REFERENCES user(id)
);
The error I am getting
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(id),FOREIGN KEY(producer_offer_id) REFERENCES
producer_offer(id),FOR' at line 1
Your first problem was caused by a missing length specification on the booked_qty varchar column.
The usual suspects for error 150:
mismatch in primary vs. foreign key type (for example int - bigint). make sure they match exactly
different table engines (for example if you try to reference a MyIsam table in a InnoDB table)
This works in MySQL, just tested (note that producer offer and user are just a mock tables).
create table producer_offer (id int(10) not null auto_increment, primary key(id));
create table user (id int(10) not null auto_increment, primary key(id));
create table book_vegetable (
id int(10) NOT NULL AUTO_INCREMENT,
producer_offer_id int(10) NOT NULL,
consumer_id int(10) NOT NULL,
booked_qty varchar(255),
PRIMARY KEY(id),
FOREIGN KEY(producer_offer_id) REFERENCES producer_offer(id),
FOREIGN KEY(consumer_id) REFERENCES user(id)
);

MySQL syntax error detected, but don't what is wrong?

I typed the following code
And I don't know what's wrong with my code.
CREATE TABLE SlotGame
(
gID CHAR(12),
jackpot DECIMAL(10,2) NOT NULL,
sID CHAR(5) NOT NULL,
PRIMARY KEY(gID),
FOREIGN KEY(gID) REFERENCES Game(gID),
FOREIGN KEY(sID) REFERENCES Slot(sID),
);
The following is shown to me:
#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 ')' at line 9
Hope someone can tell me what's wrong.
Thanks a lot.
You just need to remove the last comma:
CREATE TABLE SlotGame
(
gID CHAR(12),
jackpot DECIMAL(10,2) NOT NULL,
sID CHAR(5) NOT NULL,
PRIMARY KEY(gID),
FOREIGN KEY(gID) REFERENCES Game(gID),
FOREIGN KEY(sID) REFERENCES Slot(sID) <-- comma removed
);

MySQL InnoDB Create FK error

I am trying to create a self-referential FK:
DROP TABLE IF EXISTS `Company`;
CREATE TABLE `Company` (
`company_id` INTEGER(32) UNSIGNED AUTO_INCREMENT,
`parent_company_id` INTEGER(32),
PRIMARY KEY (`company_id`)
) ENGINE=InnoDB;
ALTER TABLE `Company`
ADD FOREIGN KEY `parent_company_id` REFERENCES `Company`(`company_id`);
I am getting the following 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 'REFERENCES Company(company_id)' at line 1
Got it.
You gotta change the definition of
parent_company_id INTEGER(32)
To match company_id except for the autoincrement and then use this statement
ALTER TABLE `Company`
ADD CONSTRAINT fk_parent_company_id FOREIGN KEY (`parent_company_id`) REFERENCES `Company`(`company_id`)
So basically remember to put unsigned on the column you are using as FK so it matches the definition of the referenced key