creating a table in the database with a foreign key - mysql

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.

Related

ENGINE=InnoDB' at line 10 in SQL

I'm trying to create a table with this sql:
CREATE TABLE angestellte (
PersonalNr int(11) NOT NULL AUTO_INCREMENT,
Vorname varchar(50) NOT NULL,
Nachname varchar(50) NOT NULL,
Beruf varchar(50) NOT NULL,
Gehalt int(11) NOT NULL,
arbeitetInAbteilung int(11) NOT NULL,
PRIMARY KEY (PersonalNr),
FOREIGN KEY abteilung (AbteilungID)
)
ENGINE=InnoDB;
But I get only the message
#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 ')
ENGINE=InnoDB' at line 10
I looked really can't find my mistake but think it's probably something obvious I just don't see.
Supposing you have a table where a field (arbeitetInAbteilung) references a row in another table (say arbeiten), you need to define it like this:
CREATE TABLE `angestellte` (
`PersonalNR` INT(11) NOT NULL AUTO_INCREMENT,
...other fields...
`arbeitetInAbteilung` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`PersonalNR`),
INDEX `FK__arbeiten` (`arbeitetInAbteilung`),
CONSTRAINT `FK__arbeiten` FOREIGN KEY (`arbeitetInAbteilung`)
REFERENCES `arbeiten` (`arbeitID`)
ON UPDATE CASCADE
ON DELETE CASCADE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
This means that an index will be created to index the field you specify (arbeitetInAbteilung). Then a constraint will be set in place so that this index is linked to the values of another field in a different table, which could be defined like this:
CREATE TABLE `arbeiten` (
`arbeitID` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`arbeitID`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
Note that the two fields must be absolutely identical; if they are text fields, they need to have the same collation; they must be both either NULL or NOT NULL; et cetera. The slightest difference will yield a "Foreign key constraint is incorrectly formed" error, and you'll need to show the engine status for InnoDB and parse its output to understand why.
The ON UPDATE and ON DELETE define what happens when you change a value in the master (arbeiten) table, or you delete it. If the ID 123 becomes 456, CASCADE means that all the rows that referred 123 will now refer 456. Another possibility would be to prevent the operation (RESTRICT), or set the mismatched rows to NULL (SET NULL).
As already commented, your FK syntax is total wrong.
FOREIGN KEY abteilung (AbteilungID)
should be
FOREIGN KEY some_column REFERENCES abteilung (AbteilungID)
Here, some_column should be replaced by the column which you want to designate as the referencing key and the definition of this column should exactly match with column AbteilungID of table abteilung

Mysql syntax error for creating table

I want to create a table in mysql:--
create table app_own(app_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
sp_id bigint(20),
title varchar(30),
description varchar(60),
details LONGTEXT(1000),
primary key(app_id ),
FOREIGN KEY (user_id) REFERENCES student(sp_id));
where app_id is the primary key and sp_id is the foreign key reference from student table..
but I am getting 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 '(1000)0,primary key(app_id ),FOREIGN KEY (sp_id)
REFERENCE' at line 1
why the error is occuring?
Try to Run This one:-
use db;
CREATE TABLE parent (
id INT UNSIGNED NOT NULL,
PRIMARY KEY (id)
) ;
CREATE TABLE Persons
(
PID int UNSIGNED NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City longtext,
PRIMARY KEY (PID),
FOREIGN KEY (PID)
REFERENCES parent(id)
ON DELETE CASCADE
);
LONGTEXT [CHARACTER SET charset_name] [COLLATE collation_name]
A TEXT column with a maximum length of 4,294,967,295 or 4GB (232 − 1) characters. The effective maximum length is less if the value contains multibyte characters. The effective maximum length of LONGTEXT columns also depends on the configured maximum packet size in the client/server protocol and available memory. Each LONGTEXT value is stored using a 4-byte length prefix that indicates the number of bytes in the value.
You should user varchar(1000) in place of longtext(1000).
Moreover user_id column doesn't exist in you table.
You can´t specify the LONGTEXT in this context. Have a look in the manual.
Change your code to:
create table app_own(app_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
sp_id bigint(20),
title varchar(30),
description varchar(60),
details LONGTEXT,
primary key(app_id),
FOREIGN KEY (user_id) REFERENCES student(sp_id));
Also, you shouldn't be using spaces inbetween the brackets at primary key(app_id).

Have error trying to modify a table in 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);

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
);