1

I am trying to concatenate two integers as the default value in a third field. My create table in SQL Server works fine:

CREATE TABLE MEI_Tbl
(
MEI_ID int PRIMARY KEY IDENTITY (1,1),
SRC tinyint NOT NULL DEFAULT '2',
HEI_ID AS (Cast (SRC as varchar)+ Cast (MEI_ID as varchar))
);

but when I try to create it in MySQL, I cannot find the equivalent for the concatenation of the two integers (Line 5 HEI_ID...). ** I am aware of changing IDENTITY (1,1) to AUTO_INCREMENT for MySQL. ** I have also tried several concat methods, but to no avail.

MySQL seems happier if I define the datatype for HEI_ID, and I have done so as varchar and int but again no success.

I have spent too much time reading about tool kits to convert SQL Server to MySQL. I just want to create the table in MySQL.

Any input would be appreciated.

0

1 Answer 1

1

MySQL does not support computed columns. Instead, you can use a view:

CREATE TABLE MEI_Tbl (
    MEI_ID int PRIMARY KEY AUTO_INCREMENT,
    SRC tinyint NOT NULL DEFAULT 2
);

CREATE VIEW v_MEI_Tbl as
    SELECT MEI_ID, SRC,
           CONCAT(src, mei_d) as HEI_ID 
    FROM MEI_Tbl
);

Then query from the view.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, sir! Your first sentence ("MySQL does not support computed columns.") is not something I had read. Nor did I come across it in my research. Your suggestion for a workaround also makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.