SQL - how to create a new table? - sql

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,
)

Related

Alter column length in SQL

ALTER TABLE tableName
MODIFY COLUMN columnName VARCHAR (256);
Error :
Incorrect syntax near 'VARCHAR'
The syntax for modifying a column definition is depending on used database. For Microsoft SQL Server for example, you would need to replace MODIFY COLUMN with ALTER COLUMN.
For better help you should specify your database type.

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.

Mariadb SQL syntax error while creating table

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 ...

OUTPUT TO FILE LOCAL SYBASE IQ 16

I'm trying to write my existing tables to files and save it locally.
I'm using T-SQL with SQuirrel JDBC Connection to Sybase
I tried the following Code to write results into a file:
SELECT * FROM date_dimension;
OUTPUT TO "C:\Users\temp\output.txt" FORMAT ASCII;
I dont know why it doesn't work but I get a Syntax error while trying this.
Error: SQL Anywhere Error -131: Syntax error near 'OUTPUT' on line 1
SQLState: 42W04
ErrorCode: 102
Can someone see a mistake in the code? Is there another way to write into file from Sybase IQ?
I'm new to all this Tools and I'm sry for such a question
Please help me :)
CREATE TABLE DATE_DIMENSION
( [DateKey] INT primary key,
[Date] DATETIME,
[FullDateUK] CHAR(10), -- Date in dd-MM-yyyy format
[FullDateUSA] CHAR(10),-- Date in MM-dd-yyyy format
}
That should work, your syntax is correct, can you provide the ddl of the table?
Edit: try this select statement:
select DateKey,("Date"),FullDateUK,FullDateUSA from DATE_DIMENSION;
OUTPUT TO "C:\Users\temp\output.txt" FORMAT ASCII;

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.