Mysql new Table - mysql

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

Related

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)

Error 1005 in MySQL upon execution of ALTER TABLE

This is a long question please read through. I have listed some of the links I have referred, but I have read all the suggestion thrown up while posting this question. This is not a duplicate question as marked here https://stackoverflow.com/questions/31341481/unable-to-figure-out-the-cause-for-error-1005-in-mysql-database.
I've been trying to write a small MySQL database, which contains two tables role and user defined as follows:
role
CREATE TABLE `role` (
`roleid` varchar(20) NOT NULL,
`role_name` varchar(255) NOT NULL,
`permission` int(11) NOT NULL,
PRIMARY KEY (`roleid`),
UNIQUE KEY `roleid` (`roleid`),
UNIQUE KEY `role_name` (`role_name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
user
CREATE TABLE `user` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`uname` varchar(255) NOT NULL,
`slug` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`rid` varchar(20) NOT NULL,
UNIQUE KEY `uid` (`uid`),
UNIQUE KEY `uname` (`uname`),
UNIQUE KEY `slug` (`slug`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `password` (`password`),
KEY `rid` (`rid`),
CONSTRAINT `user_ibfk_1` FOREIGN KEY (`rid`) REFERENCES `role` (`roleid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
So now when I issue the ALTER TABLE 'user' DROP FOREIGN KEY;
I get an error:
ERROR 1005 (HY000): Can't create table 'parth.#sql-418_24' (errno: 150)
parth is the name of the database.
I've consulted following discussions:
Error 1005 in MySQL
Error 1005 in MySQL (from foreign key syntax?)
Foreign Key in MySQL : ERROR 1005
Foreign key issue in mysql (error 1005)
Special Characters in MySQL Table Name
MySQL Reference https://dev.mysql.com/doc/refman/5.7/en/alter-table.html
The SHOW ENGINE INNODB STATUS gave the following output regarding the above situation:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
150710 18:20:35 Error in foreign key constraint of table parth/#sql-418_24:
foreign key:
Syntax error close to:
Please help me figure this out. Thanks.
The table parth/#sql-418_24 generated automatically when I executed the ALTER TABLE command. According to the reference manual ALTER TABLE works by copying the contents of the original table into a temporary table, which in this case is #sql-418_24 which is then renamed to the name of the table as specified in the schema. So it doesn't seem like a problem with special characters in table name. Please help.
MySql Version 5.5.43-0ubuntu0.14.04.1
operating system Ubuntu 14.04
Thanks for helping.
try this ...
ALTER TABLE `user` DROP FOREIGN KEY `user_ibfk_1`;
you are supposed to drop a constraint, here the constraint is named user_ibfk_1

MySQL statement syntax

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;

Syntax error trying to insert an SQL clause [duplicate]

This question already has answers here:
1064 error in CREATE TABLE ... TYPE=MYISAM
(5 answers)
Closed 9 years ago.
I'm getting this error:
CREATE TABLE `libro`
(
`id` INTEGER NOT NULL AUTO_INCREMENT,
`autor_id` INTEGER(11),
`titulo` VARCHAR(255),
`paginas` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
INDEX `libro_FI_1` (`autor_id`),
CONSTRAINT `libro_FK_1`
FOREIGN KEY (`autor_id`)
REFERENCES `autor` (`id`)
)Type=InnoDB
[propel-sql-exec] SQLSTATE[42000]: Syntax error or access violation: 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 'Type=InnoDB' at line 12
Any idea?
Regards
Javi
Shouldn't it be ENGINE=InnoDB?
Use ENGINE instead of Type
CREATE TABLE `libro`
(
`id` INTEGER NOT NULL AUTO_INCREMENT,
`autor_id` INTEGER(11),
`titulo` VARCHAR(255),
`paginas` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
INDEX `libro_FI_1` (`autor_id`),
CONSTRAINT `libro_FK_1`
FOREIGN KEY (`autor_id`)
REFERENCES `autor` (`id`)
)ENGINE=InnoDB