I was a student of Oracle SQL where i used to use "AS"
keyword for certain columns to automatically fill the values to selected columns based on mathematical computations. If you see in the example below:
CREATE TABLE CHIT_CHAT_BILING
(
BILL_NO NUMBER(3) NOT NULL,
CHAT_NAME VARCHAR2(20) NOT NULL,
PRICE NUMBER(3),
PURCHASE_TIME TIMESTAMP NOT NULL,
SERVICE_TAX AS (PRICE*(12.5/100)),
VAT AS (PRICE*(4.5/100))
);
Values of SERVICE_TAX
field & VAT
field is computed from PRICE
field. The above DDL command was working fine in Oracle SQL, but not in MySQL database.
What is the alternative? How to achieve this in MySQL database? Please guide me
IN MySql AS
keyword is not working.
3
Answers
The problem is not with the
AS
keyword or syntax for the calculation. You need to use data-types that MySQL supports and set the data-type for the generated columns.In MySQL 8.0:
fiddle
Your problem is not the computed columns. You have to change the data types.
https://dbfiddle.uk/76dfOd3l
The problem is with the datatypes.
VARCHAR2
in MySQL. UseVARCHAR
instead.NUMBER
in MySQL. UseNUMERIC
orDECIMAL
(or any other appropriate numeric data type) instead.The corrected statement:
Demo: https://dbfiddle.uk/HKO9EHXo
More infos here: