MySQL and Visual Studio not recognizing primary key - mysql

I've reduced this as basic as I can. I'm getting the error: "dynamic sql generation for the update command is not supported against a selectcommand that does not return any key column information".
This would normally mean I don't have a primary key. But I do and here is the create table for it (MySQL):
CREATE TABLE `testtable1` (
`test_id` bigint unsigned NOT NULL AUTO_INCREMENT,
`test_label` varchar(10) NOT NULL,
PRIMARY KEY (`test_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
MySQL and such are a fresh install of the latest stable release. My Visual Studio is Community 2019.
I can add the Data Connection in Server Explorer just fine and I can see all two columns. This is the only table in the testdb. The above create statement comes from that connection in fact and it identifies that there is a primary key (test_id). Looking at the column properties in Server Explorer shows the Column Key property having "PRI" in it. Seems like it knows it's the primary key to me.
I go and add a new winform and only put a DataGridView on it. From the Wizard on "Choose Data Source I select Add Project Data Source and pick database, dataset, select the connection string, pick all columns from the table, and click finish.
That's when I get the error.
Can't get much more basic than that. What am I screwing up? Is there some setting in MySQL or Visual Studio that's set wrong? This is my first time trying MySQL and I'm not having any luck.
Gurr, Greg.
:)

Found an answer of sorts. The Unsigned in the MySQL definition was definitely causing the Connector or Visual Studio to choke. Removing it from the column definition allowed it to work correctly.
I can live with a Signed BIGINT primary key column for my table.
-gg

Related

Importing a database script into my database throws an #1064 error

could you please help me? I bought a domain just for learning databases etc. and I created my model of a database in MySQL Workbench. I generated a script and tried importing it into my database using phpMyAdmin. This is the script:
CREATE TABLE IF NOT EXISTS `knight` (
`idKnight` INT NOT NULL AUTO_INCREMENT,
`strength` INT NOT NULL DEFAULT 1,
`agility` INT NOT NULL DEFAULT 1,
`vitality` INT NOT NULL DEFAULT 1,
`attack` INT GENERATED ALWAYS AS (agility*strength) STORED,
`defense` INT GENERATED ALWAYS AS (vitality*strength) STORED,
`idUser` INT NOT NULL,
`idTavern` INT NULL,
PRIMARY KEY (`idKnight`),
INDEX `fk_user_idx` (`idUser` ASC),
INDEX `fk_tavern_idx` (`idTavern` ASC),
CONSTRAINT `fk_user` FOREIGN KEY (`idUser`)
REFERENCES `user` (`idUser`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tavern` FOREIGN KEY (`idTavern`)
REFERENCES `tavern` (`idTavern`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
And this is the 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 'GENERATED ALWAYS AS (agility*strength) STORED,
defense INT GENERATED ALWAYS ' at line 6
Now the question is how do I synchronize the MySQL Workbench version of a database for which the script is generated, and the database itself. The database is Inno DB.
Thanks for your help
EDIT: MySQL version of my server is: 5.6.28
This type of problem shows the importance of using the same version of database in development as you will eventually use when you deploy to your production server. So you don't get surprised by incompatibilities.
You can run that script against your MySQL 5.6 server only if you avoid SQL features introduced in more recent versions of MySQL. This includes generated columns, which were first introduced in MySQL 5.7.
So you need to remove these columns, or else change them to plain INT columns, without the generated option.
`attack` INT GENERATED ALWAYS AS (agility*strength) STORED,
`defense` INT GENERATED ALWAYS AS (vitality*strength) STORED,
If you need those columns in query result sets, you have a few alternative solutions:
Add them as expressions in the select-list of a SELECT query:
SELECT (agility*strength) AS `attack`, (vitality*strength) AS `defense`
FROM `knight` ...
Or you could create a VIEW to encode a query with those expressions.
Or you could add those columns as plain integers, and write TRIGGERs on INSERT and UPDATE to keep them in sync with the other columns.
MySQL 5.6.28 was released in December 2015, and the whole 5.6 branch is past its end-of-support date. That means if any security bugs are discovered from now on, they won't be fixed. Besides, you're already using an outdated release of 5.6, with many bugs. The last 5.6 release was 5.6.51 in January 2021.

How to create a composite self referential foreign key in MySQL?

1215 - Cannot add foreign key constraint
PhpMyAdmin is giving me this error whenever I try to execute the following script:
It's self referencing, so it shouldn't have anything to do with its type and the syntax seems to be right, given the other examples on stackoverflow and MySQL's documentation.
Not sure if it'll help, but the code is a migration script generated by MySQL Workbench: Moving from Microsoft SQL to MySQL.
Does anyone have any clue as to what might be causing this error?
CREATE TABLE IF NOT EXISTS `clinicalTrialEmployee` (
clinicalTrialId INT NOT NULL,
employeeId INT NOT NULL,
clinicalTrialEmployeeTypeId INT NULL,
roleName VARCHAR(50) CHARACTER SET 'utf8' NULL,
PRIMARY KEY (clinicalTrialId, employeeId),
CONSTRAINT FK_clinicalTrialEmployee_clinicalTrialEmployee
FOREIGN KEY (clinicalTrialId , employeeId)
REFERENCES clinicalTrialEmployee (clinicalTrialId , employeeId)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
What in the world does it even mean for a field to reference itself? That's a new one on me. I've had one column on a table be a foreign key for another column on the same table, but I've never heard of a column referencing itself. Do you understand what such a foreign key constraint is supposed to accomplish? I mean that quite literally: what is the actual point of this? Most likely the answer is that MySQL doesn't support this. There are many variations between different "dialects" of SQL, and just because an export from a Microsft SQL server created this SQL doesn't mean that MySQL supports either the syntax or the principle in general.
Someone else might come along with more information, but in the meantime I wouldn't try to just reproduce this exactly as is. I would figure out what it was actually supposed to accomplish, figure out if that even still matters, and then find a way to do the equivalent in MySQL (if it is even possible).
Usually there is another field in the table to reference a related record. For instance, you may have a supervisor_id pointing to a supervisor employee that is also in the clinicalTrialEmployee table.
The way the foreign key is written now, it is just referencing itself, which becomes an infinite loop.

Installation error creating table tuserremotesessions

Installing Mura on a brand new machine and local MySQL 5.7 database. Per the install instructions I browse to the Mura index.cfm file to complete the installation. I enter in the database and DSN info. After a few seconds I get an error message.
Error Executing Database Query.
Datasource: muracms
SQL: CREATE TABLE
IF NOT EXISTS tuserremotesessions ( userID char(35) default NULL,
authToken char(32) default NULL, data text, created datetime
default NULL, lastAccessed datetime default NULL, PRIMARY KEY
(userID) )
Code: 42000
Type: 42000
All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead
Refreshing browser page results in this error again. I can see that tables have already been created in the database. I have been unsuccessful at attempts to internet search for a solution.
Does anyone have an idea of what I can do to get past this error? I have successfully installed Mura on other servers before so I'm really stumped.
For those who run into this error, it is due to a change in MySql 5.7 from how MySql 5.6 worked. See http://dev.mysql.com/doc/relnotes/mysql/5.7/en/news-5-7-3.html. Specifically
Columns in a PRIMARY KEY must be NOT NULL, but if declared explicitly
as NULL produced no error. Now an error occurs. For example, a
statement such as CREATE TABLE t (i INT NULL PRIMARY KEY) is rejected.
I edited the create table statements for several tables in {murahome}/requirements/mura/dbUpdates/5.2.0.cfm to remove the default NULL statement on two tables and then everything worked fine.

Create table category_path opencart 1.5.6.4

I have a problem. When I try to edit something in the category area of my opencart isntallation an error shows up saying the table category_path does not exist.
This error usually happens when an upgrade went wrong. Can I simply use a CREATE_TABLE query in my phpmyadmin to fix this, or would that not work?
Hope someone can help me with this.
If you only need to create the category path table it can be created like this (replacing oc_ with your database prefix if necessary):
CREATE TABLE `oc_category_path` (
`category_id` int(11) NOT NULL,
`path_id` int(11) NOT NULL,
`level` int(11) NOT NULL,
PRIMARY KEY (`category_id`,`path_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
You can run that SQL from phpMyAdmin or any other MySQL client. Afterward you can navigate to the Admin > Catalog > Category and click Repair which should generate the necessary records.
Please note, that table is generated by install/upgrade script and it's exactly as you say, probably a botched database upgrade or none at all. If that's the case, bear in mind that you may have other database problems as well and you may benefit by running the upgrade script on your database.

mySQL corruption "Cannot add foreign key constraint" trying to CREATE table

EDIT This is a corrupt mySQL DB problem. Pls see "EDIT:" below
Trying to add a table called client to a new mySQL DB:
CREATE TABLE `client` (
`id` INT NOT NULL AUTO_INCREMENT,
`person_id` INT NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
Every time I get: #1215 - Cannot add foreign key constraint.
I have looked at all the reserved word lists I can find. None of them seem to have "client".
I can use "clients" (but do not like having table as plurals -0 all the others are singular), "clientxxx" etc. I REALLY want to use "client". Is there a way round this? It is important that they are clients not patients.
PS I have deleted all other tables from the DB this still will not fly.
PPS Although not listed CLIENT is a ****** reserved word in SQL in reality (or at least HeidiSQL lists it. Is there any way around it? (I think I know the answer to that one). And why the heck is it not in the mySQL reserved words list?
EDIT: OK dropped old DB. Tried with a DB called testxxx and worked fine. Tried to rename DB to old name = betadb01 and got:
Database "betadb01" exists. but it does not contain objects with the same names as in "testxxx", so it's uncritical to move everything. Move all objects to "betadb01"?
... said yes and then got SQL error (1025) ... errno 150 - foreign key constraint is incorrectly formed
I have run all the repair options in HeidiSQL. Looking through the repair and analysis logs the DB in question is nowhere to be seen. I can pretty easily rename the DB and get past the problem but this is a bit nervous making. Any ideas how I can clean out this problem? Complete reinstall of mySQL after exporting my other databases???