Search code examples
mysqlsqlcalculated-columns

MySql Set Up Computed Column does not work


My Goal is to have 3 Columns: "Date" ProductName" "ProductID" - ProductID consists of Date+ProductID (like: YY-MM-DD_Socks);

I tried using "Computed Columns" but I am getting the error that "AS" is an unrecognized data type (I have my Database in PhpMyAdmin)

Here is my Code:

CREATE TABLE origin (   
  Date DATE,    
  ProductName TEXT,     
  ProductID as (Concat(Date,'_',ProductName))
);

I am just learning MySQL so I guess that this is a easy problem that I face ;)

Thanks in advance for looking into this


Solution

  • MySQL requires a type:

    CREATE TABLE origin (   
      Date DATE,    
      ProductName TEXT, 
      Ticker TEXT,    
      ProductID TEXT GENERATED ALWAYS as (Concat(Date, '_', Ticker))
    );
    

    And Ticker needs to be a column.