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
);
Related
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.
CREATE TABLE MK_Dosen (
No INT NOT NULL,
Kode CHAR(6),
Kelas CHAR(2),
NIK CHAR(7),
FOREIGN KEY (Kode)
);
I have tried and seen the material presented by the lecturer but I still can't find the reason for this syntax error. Can anyone help me?
this is the picture
You should give the foreign key a reference column of others table
ex: table demo and field is field (type shoulde be same with MK_Dosen.Kode)
CREATE TABLE MK_Dosen (
No INT NOT NULL,
Kode CHAR(6),
Kelas CHAR(2),
NIK CHAR(7),
FOREIGN KEY (Kode) REFERENCES demo(field)
);
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
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);
I have tried the following query (it was not written by myself, but was included in a source code from the Internet)
CREATE TABLE `city_list_count` (
`city_created` date NOT NULL,
`count_created` int(8) unsigned NOT NULL,
PRIMARY KEY `city_created`
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
It gave me 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 ') ENGINE=InnoDB DEFAULT CHARSET=utf8' at line 5
According to MySQL CREATE TABLE syntax, you need parentheses before and after a column list for a constraint:
[CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
[index_option] ...
Trying different things I've pointed out that the reason of the error is that primary key field name should be wrapped in parentheses:
CREATE TABLE `city_list_count` (
`city_created` date NOT NULL,
`count_created` int(8) unsigned NOT NULL,
PRIMARY KEY (`city_created`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I couldn't find the explanation for this anywhere. However, it worked for me.
Just wanted to share this with others if someone came across the same trouble.