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
Related
I'm attempting to create a table in a relational database that has a foreign key and a SQL syntax error consistently occurs.
I am using PHP and mySQL (i've removed any non interacting tables that only hold data and don't contribute to the relations)
CREATE table IF NOT EXISTS logins(
loginID int UNSIGNED AUTO_INCREMENT PRIMARY KEY,
buildingID int FOREIGN KEY REFERENCES buildings(buildingID))
buildings is another table, which has the primary key "buildingID"
the error that shows is
Fatal error: Uncaught mysqli_sql_exception: 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 REFERENCES buildings(buildingID), login varchar(255)...'
this error doesn't happen when the foreign key is removed, the table creates with no errors; and I create this table after i create the table that the foreign key references.
i have tried using the other syntax:
CREATE table IF NOT EXISTS logins(
loginID int UNSIGNED AUTO_INCREMENT,
buildingID int,
PRIMARY KEY(loginID),
FOREIGN KEY (buildingID) REFERENCES buildings(buildingID))
to no avail: the same error happens
what is my mistake here, and how can I change my code to fix it? Thanks.
the error is that primary keys are not permitted to be unsigned, i changed this one thing in my code and the error went away
Few typos.
CREATE table IF NOT EXISTS logins(
loginID int UNSIGNED AUTO_INCREMENT ,
buildingID int,
PRIMARY KEY(loginID),
FOREIGN KEY (buildingID) REFERENCES buildings(buildingID))
this works fine
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 got the following table:
CREATE TABLE `unsub_counts` (
`count_id` int(11) NOT NULL AUTO_INCREMENT,
`unsub_date` date DEFAULT NULL,
`unsub_count` int(11) DEFAULT NULL,
`store_id` smallint(5) DEFAULT NULL,
PRIMARY KEY (`count_id`),
UNIQUE KEY `uc_unsub_date` (`unsub_date`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
where column unsub_date is unique. Now I want to drop that uniqueness, because I need to have a unique index on unsub_date + store_id.
I found suggestions on the net, but is failing:
ALTER TABLE `unsub_counts` DROP CONSTRAINT `unsub_date`
gives me: 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 unsub_date' at line 1
Is this related to MyISAM? Any other suggestions?
Use drop index with constraint name:
ALTER TABLE unsub_counts DROP INDEX uc_unsub_date;
or just:
drop index uc_unsub_date on unsub_counts;
help me please. I get the following message when I try to run the sql commands:
ERROR 1064 (42000) at line 194: 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 '(
ADD CONSTRAINT VFSGRPUSR_REF_VFSGRP
FOREIGN KEY (VFSGRP)
REFERENCES `VFS' at line 1
I'm trying to implement a many-to-many relationship.
----------------------------------------------------------------------------------------
DROP TABLE IF EXISTS VFSGRPUSR;
CREATE TABLE VFSGRPUSR
(
VFSGRP INTEGER REFERENCES VFSGRP(VFSGRP_ID),
VFSUSR INTEGER REFERENCES VFSUSR(VFSUSR_ID),
PRIMARY KEY (VFSGRP, VFSUSR)
)ENGINE=InnoDB DEFAULT CHARSET utf8;
----------------------------------------------------------------------------------------
DROP TABLE IF EXISTS VFSUSR;
CREATE TABLE VFSUSR
(
VFSUSR_ID INTEGER NOT NULL auto_increment,
DEPARTMENT INTEGER,
FIRSTNAME VARCHAR(255) NOT NULL,
SURNAME VARCHAR(255) NOT NULL,
LOGIN VARCHAR(255) NOT NULL,
PASSWORD BLOB,
TOKEN BLOB,
PRIMARY KEY (VFSUSR_ID),
CONSTRAINT VFSUSR_REF_DEPARTMENT FOREIGN KEY (DEPARTMENT) REFERENCES DEPARTMENT (DEPARTMENT_ID)
)ENGINE=InnoDB DEFAULT CHARSET utf8;
----------------------------------------------------------------------------------------
DROP TABLE IF EXISTS VFSGRP;
CREATE TABLE VFSGRP
(
VFSGRP_ID INTEGER NOT NULL auto_increment,
GROUPNAME VARCHAR(255) NOT NULL,
PRIMARY KEY (VFSGRP_ID)
)ENGINE=InnoDB DEFAULT CHARSET utf8;
----------------------------------------------------------------------------------------
ALTER TABLE VFSGRPUSR(
ADD CONSTRAINT VFSGRPUSR_REF_VFSGRP
FOREIGN KEY (VFSGRP)
REFERENCES VFSGRP(VFSGRP_ID)
)ENGINE=InnoDB DEFAULT CHARSET utf8;
ALTER TABLE VFSGRPUSR(
ADD CONSTRAINT VFSGRPUSR_REF_VFSUSR
FOREIGN KEY (VFSUSR)
REFERENCES VFSUSR(VFSUSR_ID)
)ENGINE=InnoDB DEFAULT CHARSET utf8;
Why are you trying to also set ENGINE and CHARSET? Just use:
ALTER TABLE VFSGRPUSR
ADD CONSTRAINT VFSGRPUSR_REF_VFSGRP
FOREIGN KEY (VFSGRP)
REFERENCES VFSGRP(VFSGRP_ID);
And refer to the official documentation for available syntax: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html