I'm upgrading an Access 2016 back-end database to MySQL 8.0 and having problems with the generated columns. I used a third-party tool to migrate my Access tables to MySQL, and for the most part it worked fine. The Access calculated columns are not migrated. Or rather, they are, but as standard columns. I need to go in and redefine the calculations in MySQL.
In Access, I have a Heading column which combines title, last name, first name, spouse name, spouse last name into a single heading. Here is the Access calculation which works just fine.
IIf([UseTitle],[Title] & " ","") & IIf(IsNull([DonorFirstName]),[DonorLastName],[DonorFirstName] & IIf(IsNull([SpouseName])," " & [DonorLastName],IIf(IsNull([SpouseLastName])," and " & [SpouseName] & " " & [DonorLastName]," " & [DonorLastName] & " and " & [SpouseName] & " " & [SpouseLastName])))
If this person wants to use a title (such as "Mr. & Mrs."), insert the title. If no first name, the last name's a company, so add the LastName field, otherwise add the FirstName field. If no spouse, add space and LastName field. If spouse with same last name, add " and " plus SpouseLastName. Otherwise, add LastName plus " and " plus SpouseFirstName plus space plus SpouseLastName. So now you either have "Mr. & Mrs. John and Jane Doe" or "Mr. & Mrs. John Doe and Jane Smith".
I tried doing this in MySQL using a combination of CONCATs and IFs like this:
`Heading` varchar(509) CHARACTER SET utf8 COLLATE utf8_general_ci GENERATED ALWAYS AS (concat(if(((`UseTitle` = 0) or (`Title` is null)),_utf8mb3'',concat(`Title`,_utf8mb3' ')),if((ifnull(`DonorFirstName`,_utf8mb3'') = _utf8mb3''),`DonorLastName`,concat(`DonorFirstName`,_utf8mb3' ',if((ifnull(`SpouseName`,_utf8mb3'') = _utf8mb3''),`DonorLastName`,if((ifnull(`SpouseLastName`,_utf8mb3'') = _utf8mb3''),concat(_utf8mb3'and ',`SpouseName`,_utf8mb3' ',`DonorLastName`),concat(`DonorLastName`,_utf8mb3' and ',`SpouseName`,_utf8mb3' ',`SpouseLastName`))))))) STORED,
This does NOT work, and in fact causes an error in the table DDL. It seems to update properly, but when I try to open the table design in MySQL Workbench, it gives me an error saying Error parsing DDL for tableName
When I choose to view the DDL in another tab, the generated field line is identified with an asterisk, and shows this:
`Heading` varchar(509) CHARACTER SET utf8 COLLATE utf8_general_ci GENERATED ALWAYS AS (concat(if(((`UseTitle` = 0) or (`Title` is null)),_utf8mb3'',concat(`Title`,_utf8mb3' ')), `DonorFirstName`,_utf8mb3' ',`DonorLastName`)) VIRTUAL,
The '' after the _utf8mb3 is underlined, and hovering over it shows:
"Syntax error: extraneous input found - expected 'comma'"
Luckily, this is a brand new database, and I can simply drop and recreate the table, but not if I cannot figure out how to properly generate that column.
I'm obviously doing something wrong here. Is there a correct way to make this generated column properly work?
Your alter table command is incorrect. A valid command would look like this. Remove all _utf8mb3 and correct the if and concat native functions.
ALTER TABLE tableName
`Heading` varchar(509) CHARACTER SET utf8 COLLATE utf8_general_ci GENERATED ALWAYS AS (
concat(
if( ((not `UseTitle`) or IsNull(`Title`)),
'',
concat(`Title`,' ', `DonorFirstName`, ' ',`DonorLastName`)
)
)
) VIRTUAL,
PS: concat(value1, value2) will return null if any value within the function is null.
concat_ws('', value1, value2) will ignore nulls and add all non null values.
Thanks to those who answered or commented.
If I make the changes in the SQL editor, it works just fine. The changes are accepted as expected. Still, I get a hard error trying to view the table definition in MySQL Workbench. So, the problem seems to be there instead. I will re-post the question differently to solve that issue.
create table WEL
(
pipe_type varchar(30),date DATE
)
insert into WEL values(H.T.NO.2,....,....)
getting error multi-part identifier h.t.no.2 could not be bound and 2 is a incorrect syntax
is there any problem with varchar or any other way to insert H.T.NO.2 into table
For Character Type Data Types we must give single quotes
For inserting a single record we have two options
First one
insert into WEL values('H.T.NO.2','2013-07-07');
Second one
insert into WEL(pipe_type,date) values('H.T.NO.2','2013-07-07');
This will helpful when default value is given.For example
If i give default value for date like '2000-02-02'.Then we will write query like this
insert into WEL(pipe_type) values('H.T.NO.2');
Then system takes default value which you give(like 2000-02-02 ) for that one.
Importance of Delimiter(;):
I observe that you didn't give ";" .If so, The database checks for another query.So for one query its not an important,But for multiple its very Important.
You have to enclose varchar and date values in single quotes:
insert into WEL values('H.T.NO.2', '2013-12-31')
Use the following format to insert the string into database:
insert into WEL values('H.T.NO.2',....,....);
Always enclose the string with Quota mark.
As given in other answers you need to put values in single qoutes.
The error message
multi-part identifier h.t.no.2 could not be bound
is coming because MySQL is reading h.t.no.2 as a column name rather than a string value
I am using below code to add constraint on CreatID column. But its not working. I can still add text like ' xyz' which I don't want to. Also when only spaces are there in text it should be considered as 'null' value. Like if I enter '<space><space><space>' this then it should be considered as null.
ALTER TABLE [dbo].[OneC_Gadget_AppDetails] WITH CHECK
ADD CONSTRAINT [chk_CreateID_val]
CHECK ((len(ltrim(rtrim([AppActionContent])))>(1)))
So I have a form that can get data from a database by its ID (auto-incremented column(Primary Key)) and display all fields in the <input> tags via value properties. And when I submit the form I want it to either INSERT a new row if the ID from the ID column doesn't already exist and if it does I want to UPDATE the rest of the data in the row with the same ID.
I have been trying to research this, but no one seems to be doing the same thing I am trying to do, its always slightly different. I found a REPLACE INTO and created it like below:
$sqlString = 'REPLACE INTO coursework
SET cwID=`'. $cwID .'`,
cwTitle=`'. $cwTitle .'`,
cwContent=`'. $cwContent .'`,
cwProgress=`'. $cwProgress .'`,
cwDue=`'. $cwDue .'`;
All the $cw[] variables being content received from $_POST method.
I keep getting a Error code: 1054-Unknown column '6' in 'field list' -
the "Unknown column '6'" is mysql trying to call $cwID (value of $_POST['cwID']) instead of the cwID column(which is the Primary Key for my table). I feel like there is something simple and stupid I am missing but I have never used this REPLACE INTO method before.
I saw a post about INSERT IGNORE INTO and INSERT ... ON DUPLICATE KEY UPDATE but both of those sound more destructive than what I am looking for.
I just want to make sure that the table is updated if the cwID exists and the auto-increment is kept in tact, or a new row is added if there is no ID. Should I just run a SELECT query to see if it exists and INSERT/ UPDATE appropriately?
Remove Replace the back-ticks (`) surrounding the strings with single quotes (').
MySql is trying to find a column named by the strings you are using as values.
See http://dev.mysql.com/doc/refman/5.5/en/identifiers.html
Replace the backticks with single quotes:
$sqlString =
"REPLACE INTO coursework SET cwID='$cwID', cwTitle='$cwTitle', cwContent='$cwContent', cwProgress='$cwProgress', cwDue='$cwDue'";
Also, note that " will interpolate your variables.
I want a column to have only two values. For example I want to make the column active can only contain the values "Y" and "N" I don`t want to use boolean data type.
I`m looking for a way similar to the Look Up Wizard of the MS Access how can this be done?
Use a non-nullable bit
What if you want J and N for German? Or other languages? This is client formatting
Ditto "true", "false"
What about y/Y/n/N? Unicode Ys and Ns?
You'd need a check constraint to restrict to Y or N: why when you have this anyway with bit?
Finally, SQL Server has no boolean type as such: client code will interpret bit as boolean though
Edit, after comment on question.
If you need to add more values, then I suggest a lookup table and foreign key. This means you can support new values without changing code (CHECK constraint) and/or datatypes.
What you're looking for are Check Constraints
e.g.
ALTER TABLE dbo.Vendors ADD CONSTRAINT CK_Vendor_CreditRating
CHECK (CreditRating >= 1 AND CreditRating <= 5)
Or for you
ALTER TABLE dbo.MyTableName ADD CONSTRAINT CK_MtTable_FieldName_YN
CHECK (FieldName = 'Y' OR FieldName = 'N')
You could use a varchar(1) or nvarchar(1). Put a constraint on the column in which you state that only Y and N are possible as input to keep data integrity.
Grz, Kris.