MySQL primary key must be written in parentheses - mysql

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.

Related

Creation of MySQL's generated column utilizing column that is used for foreign key constraint causes error

I try to add a generated stored column to existing table running the following SQL command:
ALTER TABLE routes ADD routes_hash char(32)
AS (MD5(CONCAT(`traveler_id`, `track`))) stored;
MySQL returns the error message:
ERROR 1025 (HY000): Error on rename of './travelers/#sql-558_69' to './travelers/routes'
(errno: 150 - Foreign key constraint is incorrectly formed)
Mysql version is 5.7.20. Here is a database structure:
CREATE TABLE `travelers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `routes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`traveler_id` int(11) NOT NULL,
`track` multilinestring NOT NULL,
PRIMARY KEY (`id`),
KEY `traveler_id` (`traveler_id`),
CONSTRAINT `routes_ibfk_1` FOREIGN KEY (`traveler_id`)
REFERENCES `travelers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I have not found any restrictions in MySQL documentation that forbid usage of the column that participates in foreign key constraint as a part of expression used for build a generated column. However, if I exclude traveler_id from the generated column expression for routes_hash the column is created fine. Is this a bug of MySQL or I am missing something about generated column usage?
Unfortunately, it is a bug of MySQL that affects even current 5.7 and 8.0 versions. I have found description on MySQL's bug tracker https://bugs.mysql.com/bug.php?id=88111 . Please be aware.

I don't get whats wrong with the mysql syntax

CREATE TABLE hoofdtoonder
(
id INT NOT NULL,
idondersoorten INT FOREIGN KEY REFERENCES `ondersoort`(`id`) NOT NULL,
)
//making table but the error is with the references its on a mysql database someone please help
It says error at FOREIGN KEY REFERENCES ondersoort(id) NOT NULL. But I don't know what's wrong with the syntax.
There are several things wrong here:
First, for an inline constraint, you don't need to specify foreign key, just references.
The not null clause should come before the references clause.
You have a redundant comma at the end of the last column's specification.
To put it all together:
CREATE TABLE hoofdtoonder (
id INT NOT NULL,
idondersoorten INT NOT NULL REFERENCES `ondersoort`(`id`)
);
MySQL does not support inline foreign key references.
It's true that the SQL language allows for syntax like #Mureinik suggested:
idondersoorten INT NOT NULL REFERENCES `ondersoort`(`id`)
But you will find that MySQL parses this and ignores it. InnoDB does not support inline foreign key syntax. If you now run SHOW CREATE TABLE hoofdtoonder, it'll show this:
CREATE TABLE `hoofdtoonder` (
`id` int(11) NOT NULL,
`idondersoorten` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Where did the REFERENCES go? It was silently discarded. This is actually a beef I have with MySQL, that it recognizes some valid constraint syntax, but ignores it. It doesn't even show you a warning. It just defines the table without the constraint.
In MySQL, you must declare a foreign key as a table-level constraint, like this:
CREATE TABLE hoofdtoonder (
id INT NOT NULL,
idondersoorten INT NOT NULL,
FOREIGN KEY (idondersoorten) REFERENCES `ondersoort`(`id`)
);

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

sql DROP CONSTRAINT UNIQUE not working

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;

INNODB Cascade Syntax / Logic Problem

I have a problem with an SQL database I'm creating. I'm trying to switch it over to use INNODB but I can't seem to get the syntax (or possibly logic) correct for cascading.
Here's a part of the code erroring. It does not like line 40.
Error output in the usual cryptic (to me at least) form:
ERROR 1005 (HY000) at line 36: Can't create table './school/staff.frm' (errno: 150)
Edit:
Here's the whole SQL file I'm trying to pipe in if that helps.
Foreign keys have to be indexes. (role.name)
Try this:
CREATE TABLE IF NOT EXISTS `role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(30) NOT NULL,
`description` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`)
) ENGINE=InnoDB;
Did you mean for the "role" column's foreign key constraint to be using the name instead of the ID? It looks like the following line (line 43):
FOREIGN KEY (role) REFERENCES role(name)
should be:
FOREIGN KEY (role) REFERENCES role(id)
This executes without errors.
Alternatively, the "staff.role" column's data type (line 40, role INT NOT NULL) could be changed to CHAR(30) NOT NULL.