MySQL statement syntax - mysql

I'm experiencing problems with this MySQL statement:
CREATE TABLE Articoli (Cod_Articolo char(10) NOT NULL,
Des_Articolo varchar(50) NOT NULL,
Cat_Articolo char(2) NOT NULL,
Ubi_Articolo char(6) NOT NULL,
PRIMARY KEY (Cod_Articolo)
FOREIGN KEY (Cat_Articolo) REFERENCES Categorie(Cod_Categoria) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB;
I get 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
(Cat_Articolo) REFERENCES Categorie(Cod_Categoria) ON DELETE NO ACTI' at line 6
I don't see why...
Thanks, Mauro

You are missing a comma here:
PRIMARY KEY (Cod_Articolo)
Try this:
CREATE TABLE Articoli (Cod_Articolo char(10) NOT NULL,
Des_Articolo varchar(50) NOT NULL,
Cat_Articolo char(2) NOT NULL,
Ubi_Articolo char(6) NOT NULL,
PRIMARY KEY (Cod_Articolo),
FOREIGN KEY (Cat_Articolo) REFERENCES Categorie(Cod_Categoria) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB;

Related

Cannot creat database tabele: syntax error

I'm trying to create mysql db table as below, but it got a syntax error at line 4.
CREATE TABLE findable_drivers (
id BIGINT NOT NULL AUTO_INCREMENT,
driver_id BIGINT NOT NULL,
current_role VARCHAR(100) NOT NULL, #here
lat DOUBLE NOT NULL,
lng DOUBLE NOT NULL,
findable BOOLEAN DEFAULT 0 NOT NULL,
CONSTRAINT findable_drivers_PK PRIMARY KEY (id),
CONSTRAINT findable_drivers_UN UNIQUE KEY (driver_id),
CONSTRAINT findable_drivers_driver_FK FOREIGN KEY (driver_id) REFERENCES driver(id)
)
ENGINE=InnoDB
DEFAULT CHARSET=latin1
COLLATE=latin1_swedish_ci ;
the error description:
#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 'current_role VARCHAR(100) NOT NULL,
lat DOUBLE NOT NULL,
lng DOUBLE NOT N...' at line 4
CURRENT_ROLE() is actually a MySQL/MariaDB system function which returns active roles for the current session. So in your CREATE TABLE statement, the database thinks you are trying to call this function. You should call the current_role column something else. If you must stick with this name, then will have to forever refer to it using backticks:
CREATE TABLE findable_drivers (
id BIGINT NOT NULL AUTO_INCREMENT,
driver_id BIGINT NOT NULL,
`current_role` VARCHAR(100) NOT NULL, -- must use backticks here
lat DOUBLE NOT NULL,
lng DOUBLE NOT NULL,
findable BOOLEAN DEFAULT 0 NOT NULL,
CONSTRAINT findable_drivers_PK PRIMARY KEY (id),
CONSTRAINT findable_drivers_UN UNIQUE KEY (driver_id),
CONSTRAINT findable_drivers_driver_FK FOREIGN KEY (driver_id) REFERENCES driver(id)
) ENGINE=InnoDB
DEFAULT CHARSET=latin1
COLLATE=latin1_swedish_ci;
Tim is correct
It would be good if you use different column name than the defined keywords.

FOREIGN KEY constraint help Mysql

I'm reviewing mysql and I am working on composing foreign keys when creating tables. However when I create the foreign key I receive a syntax error.
CREATE TABLE IF NOT EXISTS staff (
staff_id INT(5) NOT NULL,
staff_first_name VARCHAR(20) NOT NULL,
staff_last_name VARCHAR(20) NOT NULL,
staff_phone_number VARCHAR(15) NOT NULL,
staff_email_address VARCHAR(30) NOT NULL,
CONSTRAINT staff_id_pk PRIMARY KEY (staff_id)
CONSTRAINT staff_id_fk FOREIGN KEY (staff_id)
REFERENCES computer_staff (staff_id)
);
Can anyone see what I've done wrong here?
EDIT:
The error I receive reports as:
19:35:55 CONSTRAINT staff_id_fk FOREIGN KEY (staff_id), REFERENCES computer_staff (staff_id)) Error Code: 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 'CONSTRAINT staff_id_fk FOREIGN KEY (staff_id), REFERENCES computer_staff (staff_' at line 1 0.00025 sec
UPDATE: Error Code: 1215. Cannot add foreign key constraint 0.110 sec
How does one resolve error code 1215??
You are missing comma:
CREATE TABLE IF NOT EXISTS staff (
staff_id INT(5) NOT NULL,
staff_first_name VARCHAR(20) NOT NULL,
staff_last_name VARCHAR(20) NOT NULL,
staff_phone_number VARCHAR(15) NOT NULL,
staff_email_address VARCHAR(30) NOT NULL,
CONSTRAINT staff_id_pk PRIMARY KEY (staff_id), -- here
CONSTRAINT staff_id_fk FOREIGN KEY (staff_id)
REFERENCES computer_staff (staff_id)
);
Try this code
CREATE TABLE IF NOT EXISTS staff (
staff_id INT(5) NOT NULL,
staff_first_name VARCHAR(20) NOT NULL,
staff_last_name VARCHAR(20) NOT NULL,
staff_phone_number VARCHAR(15) NOT NULL,
staff_email_address VARCHAR(30) NOT NULL,
Foregn_Key int,
CONSTRAINT staff_id_pk PRIMARY KEY (staff_id),
CONSTRAINT staff_id_fk FOREIGN KEY (Foregn_Key)
REFERENCES computer_staff (Foregn_Key)
);

SQL foreign key error?

DROP TABLE IF EXISTS userRole;
DROP TABLE IF EXISTS account;
CREATE TABLE account (
lionID VARCHAR(50) NOT NULL,
firstName VARCHAR(50) NOT NULL,
hashedpass VARCHAR(255) NOT NULL,
PRIMARY KEY (lionID)
);
CREATE TABLE roles (
roleID INT UNSIGNED AUTO_INCREMENT NOT NULL,
lionID VARCHAR(50) NOT NULL,
administrator BOOLEAN NOT NULL DEFAULT False,
qRole VARCHAR(255) NOT NULL,
PRIMARY KEY (roleID),
FOREIGN KEY (lionID) REFERENCES account(lionID)
);
I am having trouble understanding why I am getting an error.. the foreign key lionID in roles is referencing the primary key lionID in account but it doesn't seem to like it. Any help is greatly appreciated.
ERROR 1064 (42000) at line 76: 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 lionID REFERENCES account lionID'
)' at line 8
I think the problem is in create statement:
You need to use Syntax:
CONSTRAINT `fk_account`
FOREIGN KEY (lionID) REFERENCES account(lionID)

Mysql new Table

I have some doubt in my table
CREATE TABLE user_roles(
user_role_id int(10) unsigned not null,
user_id int(10) unsigned not null,
authority varchar(45) not null,
PRIMARY KEY(user_role_id),
KEY FK_user_roles (user_id),
CONSTRAINT FK_user_roles FOREING KEY (user_id) REFERENCES users (user_id))
ENGINE=InnoDB DEFAULT CHARSET=utf8;
i get the following 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
'FOREING KEY (user_id) REFERENCES users (user_id)) ENGINE=InnoDB DEFAULT CHARSET=' at line 7
Simple mistake
FOREIGN not FOREING

Error 1064 <42000> on mysql when i'm trying to create table

CREATE TABLE `photos` (
`title` varchar(255) not null,
`id` int(11) not null,
`ph_path` varchar(255) not null,
`description` varchar(255) not null,
`privilange` varchar(20) not null,
`owner` varchar(60) not null,
`provoles` int(11),
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=greek;
I'm getting error 1064 <4200> and I'm not sure what is wrong.
You have a trailing comma in the primary key:
PRIMARY KEY (`id`), <--- remove that
The full error would read something like:
check the manual that corresponds to your MySQL server version for the right syntax to use near ') ENGINE=InnoDB
In MySQL, the position pointed to by the error message (near ')ENGINE) will show you the character immediately after where the error occurred. Look to the previous thing in your statement and you'll find your syntax error.
You will have to remove the comma after PRIMARY KEY (`id`).