Mysql. ERROR 1064(42000) in syntax - mysql

Hello! I can't understand what's a problem?
CREATE TABLE expenses(
num INT,
paydate DATE DEFAULT DATE(),
receiver INT NOT NULL DEFAULT 1,
value DEC(10,2) NOT NULL,
PRIMARY KEY(num)
);
And I have a problem:
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 '(), receiver INT NOT NULL DEFAULT 1, value
DEC(10,2) NOT NULL, PRIMARY KEY(num))' at line 4

Unfortunately, MySQL does not let you default only the date. You need to default a datetime:
CREATE TABLE expenses (
num INT,
paydate datetime DEFAULT now(),
receiver INT NOT NULL DEFAULT 1,
value DEC(10,2) NOT NULL,
PRIMARY KEY (num)
);
Here is a SQL Fiddle.

Related

MYSQL (WAMP SERVER): Error 1064 when using AUTO_INCREMENT=1000 to create table column?

I have this code:
CREATE TABLE Company ( Comp_Code INT(5) NOT NULL AUTO_INCREMENT=1000, Comp_Name VARCHAR(15) NOT NULL, PRIMARY KEY (Comp_Code) );
but when i run it in MYSQL from WAMPServer it gives an error:
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 '=1000,
Comp_Name VARCHAR(15) NOT NULL,
PRIMARY KEY (Comp_Code)
)' at line 2
Why is this happening? I can't seem to find any solution to this particular issuer.
AUTO_INCREMENT is column option.
Initial AUTO_INCREMENT=1000 value is table option.
CREATE TABLE Company (
Comp_Code INT(5) NOT NULL AUTO_INCREMENT, -- specify that the column is AI
Comp_Name VARCHAR(15) NOT NULL,
PRIMARY KEY (Comp_Code)
) AUTO_INCREMENT=1000; -- specify initial AI value

What is the syntax error in this simple SQL query for creating a table?

I am trying out IntelliJ's DataGrip to do some SQL work on a MariaDB database.
Somehow i cannot execute the query that was automatically created by DataGrip itself....
Can you help me find the error in it ?
create table currencyIndex
(
id int auto_increment,
isoCode VARCHAR2(3) not null,
isoCodeNumeric SMALLINT not null,
currencyName VARCHAR2(100) not null,
countryName VARCHAR2(100) not null,
constraint currencyIndex_pk
primary key (id)
);
The error is
[42000][1064] (conn=246) 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 '(3) not null,
[42000][1064] 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 '(3) not null,
isoCodeNumeric SMALLINT not null,
currencyName VARCHAR2(100) ...' at line 4.
I tried to validate the query with an online validator and it seems fine... Any suggestions ?
MariaDB and MySQL do not have a VARCHAR2 type (but Oracle does). Use plain VARCHAR and the error should go away:
CREATE TABLE currencyIndex (
id INT AUTO_INCREMENT,
isoCode VARCHAR(3) NOT NULL,
isoCodeNumeric SMALLINT NOT NULL,
currencyName VARCHAR(100) NOT NULL,
countryName VARCHAR(100) NOT NULL,
CONSTRAINT currencyIndex_pk PRIMARY KEY (id)
);
Demo

Generated SQL script from workbench doesn`t work on MariaDB server

I created a scheme in mysql workbench and exported this scheme into SQL code.
.
When I want to generate table user on my server I am getting error.
Script:
CREATE TABLE IF NOT EXISTS 'user' (
'id' INT NOT NULL AUTO_INCREMENT,
'registerDate' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
'login' VARCHAR(255) NOT NULL,
'password' VARCHAR(255) NOT NULL,
'status' TINYINT NOT NULL,
'credits' INT NOT NULL DEFAULT 0,
PRIMARY KEY ('id'),
UNIQUE INDEX 'login_UNIQUE' ('login' ASC) VISIBLE)
ENGINE = InnoDB;
Error #1064 - 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 ''user' ( 'id' INT NOT NULL AUTO_INCREMENT, 'registerDate' TIMESTAMP NOT NU' at line 1
I dont understand where is an error? I didnt see anything wrong.
Version on server: 10.1.32-MariaDB

Table cannot be created in mysql -Error 1064

I am trying to create a table in MySQL with the query
CREATE TABLE ofRosterGroups (
rosterID BIGINT NOT NULL,
rank TINYINT NOT NULL,
groupName VARCHAR(255) NOT NULL,
PRIMARY KEY (rosterID, rank),
INDEX ofRosterGroup_rosterid_idx (rosterID)
);
but seems like it is throwing error everytime I made updates too. I don't know what is going wrong with it.
Error coming up is
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 'rank TINYINT NOT NULL, groupName
VARCHAR at line 3
MySQL 8.0.2 added support for the window rank function, making it a reserverd word.
You could escape it using backticks (`):
CREATE TABLE ofRosterGroups (
rosterID BIGINT NOT NULL,
`rank` TINYINT NOT NULL, -- Here
groupName VARCHAR(255) NOT NULL,
PRIMARY KEY (rosterID, `rank`), -- And here
INDEX ofRosterGroup_rosterid_idx (rosterID)
);
But it may be a better idea to just use a name that isn't a reserved word, such as rosterRank instead of rank:
CREATE TABLE ofRosterGroups (
rosterID BIGINT NOT NULL,
rosterRank TINYINT NOT NULL, -- Here
groupName VARCHAR(255) NOT NULL,
PRIMARY KEY (rosterID, rosterRank), -- And here
INDEX ofRosterGroup_rosterid_idx (rosterID)
);

SQL Syntax error when using sysdate

so i'm having an issue using sysdate function as my default data value in MySQL. my code to create the table is as follows:
CREATE TABLE orders
(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
odr_date DATE DEFAULT sysdate() NOT NULL
);
i get the error
[42000][1064] 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 'sysdate()
)' at line 4
instead of sysdate() try CURRENT_TIMESTAMP for mysql
reference: Type date default sysdate in Mysql
CREATE TABLE orders
(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
odr_date DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
);