Mariadb SQL syntax error while creating table - sql

Manually creating a table while in mariadb console;
got the following error:
MariaDB [mysql]> CREATE TABLE learningdb(command varchar(255) text2disp varchar(255));
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near 'varchar(255))' at line 1
MariaDB [mysql]> CREATE TABLE learningdb(command varchar(2048) text2disp varchar(2048));
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near 'varchar(2048))' at line 1
Cannot find out the correct syntax.

Dont you think a comma separator is required between each of type definition!?
Example:
CREATE TABLE learningdb(command varchar(255), -- <--- this comma was missing
text2disp varchar(255));
Refer to Documentation:
Create Table - Syntax
CREATE [OR REPLACE] [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...) ...
Tutorial Point: MariaDB - Create Tables
... Multiple columns separated by commas ...

Related

MariaDB does not accept specific date as 'default on update' value

Using MariaDB 10.5.8.
The following commands work :
CREATE TABLE t1 (
dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE t1 (
dt DATETIME DEFAULT '2100-01-01 00:00:000' ON UPDATE CURRENT_TIMESTAMP
);
But the following command does not work :
CREATE TABLE t1 (
dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE '2100-01-01 00:00:000'
);
It returns ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '`2100-01-01 00:00:000` )' at line 1.
The documentation is really clear on what syntax is allowed:
[ON UPDATE [NOW | CURRENT_TIMESTAMP] [(precision)]]
This does not include putting in a custom value.
To be honest, I'm not sure if the designers would have even considered a constant value for such a column. After all, you can just have two columns and check an update if updatedAt <> createdAt.

Add generated column to an existing table Postgres

I am trying to add a generated column to an existing table with this script.
alter table Asset_Store add column
md5_hash VARCHAR(100) GENERATED ALWAYS AS
(CAST(UPPER(
case
when OR_ID is not null then MD5(cast(OR_ID as varchar(100)))
when Asset_ID is not null then MD5(Asset_ID)
else null
end
) as VARCHAR(100)))
STORED
;
but I am getting an error:
SQL Error [42601]: ERROR: syntax error at or near "("
Position: 88
ERROR: syntax error at or near "("
Position: 88
ERROR: syntax error at or near "("
Position: 88
What is the issue? I don't get it.
In the schema of my Asset_Store table the column
OR_ID is int and Asset_ID is varchar(100).
I guess it expects a slightly different syntax... but what is the right syntax?
Your syntax is correct. Your version of PostgreSQL apparently is not.
In version 12:
create table asset_store(or_id text, asset_id text);
alter table Asset_Store add column
md5_hash VARCHAR(100) GENERATED ALWAYS AS
(CAST(UPPER(
case
when OR_ID is not null then MD5(cast(OR_ID as varchar(100)))
when Asset_ID is not null then MD5(Asset_ID)
else null
end
) as VARCHAR(100)))
STORED
;
ALTER TABLE
Time: 17.678 ms
More general, simplified command
ALTER TABLE "items"
ADD COLUMN "revenue" numeric
GENERATED ALWAYS AS ("price" * (1-"discount")) STORED;

SQL - how to create a new table?

I am self-studying how to write SQL. I am following the documentation here.
SQL Error [1064] [42000]: 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 ')' at line 1
How can I fix this?
Can you add what SQL you have tried? Otherwise, this is how you create a new table:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
)

multiply two varchars in sql

This what I have tried but it is giving errors:
MariaDB [test]> create table table1 (length varchar, breadth varchar);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' breadth varchar)' at line 1
MariaDB [test]> create table table1 (length varchar(20), breadth varchar(20));
Query OK, 0 rows affected (0.06 sec)
MariaDB [test]> insert into table1 (length, breadth) values ('12','11');
Query OK, 1 row affected (0.05 sec)
MariaDB [test]> select * from table1;
+--------+---------+
| length | breadth |
+--------+---------+
| 12 | 11 |
+--------+---------+
1 row in set (0.02 sec)
MariaDB [test]> select convert (int, length)*convert(int, breadth) as T from table1;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'int, length)*convert(int, breadth) as T fr
om table1' at line 1
MariaDB [test]> select convert(int, length)*convert(int, breadth) as T from table1;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'int, length)*convert(int, breadth) as T fr
om table1' at line 1
Use cast():
select cast(length as int) * cast(breadth as int) as T
from table1;
As explained in the documentation, convert() is for converting between different character sets. You are confusing MySQL's (and MariaDB's) use of convert() with SQL Server's.
By the way, you do not even need the explicit cast() (at least in MySQL). The engine will do an implicit cast for you:
select length * breadth as T from table1;
Although this is supported, I don't really advocate depending on implicit casts.
Here is a SQL Fiddle.

SQL Server Alter Table Query (Incorrect Syntax)

I'm stumped, and I know this is probably something very simple. I am trying to add two columns to an existing table.
I am receiving the following syntax error:
Incorrect syntax near 'PublishedDate' Expecting '(', or SELECT.
Here is my SQL:
ALTER TABLE my_table ADD (PublishedDate DATETIME, UnpublishedDate DATETIME)
Try without the parentheses:
ALTER TABLE my_table
ADD PublishedDate DATETIME, UnpublishedDate DATETIME
Here is a sqlfiddle with a demo for you to try.