MySQL Default Keyword usage errors - sql

When am trying to set default date and default sysdate am getting following errors:
MySQL Query:
create table product_offer_type(object_id INT(19), snapshot_id INT(19), PRIMARY KEY(object_id,snapshot_id), enum_value VARCHAR(64) NOT NULL, external_name VARCHAR(64) NOT NULL, description VARCHAR(255), business_validation INT(1), valid_for_start_date_time DATE DEFAULT '1900-01-10', valid_for_end_date_time DATE DEFAULT '4712-01-01', mutation_date_time DATE SYSDATE, mutation_user VARCHAR(32) DEFAULT 'USER');
Error Message:
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 'SYSDATE, mutation_user VARCHAR(32) DEFAULT 'USER')' at line 1
Any pointer's would be highly appreciated.

If you're trying to track the last modification time of the row, I usually use something like
mutation_date_time timestamp default current_timestamp, on update current_timestamp
You might need slight modification if you really want Date and not timestamp.

Related

Error in simple SQL when executing in python

I'm getting an error with this SQL code when I execute it in my Flask app. I swear I've done this exact thing before and it worked, so I'm not sure what's happening.
Here is the SQL:
DROP TABLE IF EXISTS user;
CREATE TABLE order (
id TEXT PRIMARY KEY,
plan_id INTEGER NOT NULL,
placed TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
ssh_key TEXT NOT NULL,
region INTEGER NOT NULL,
operating_system INTEGER NOT NULL,
enable_ipv6 INTEGER NOT NULL
expires INTEGER NOT NULL
);
Here is the relevant part of my python error:
sqlite3.OperationalError: near "order": syntax error
Thank you for the help
Order is a reserved keyword for sqlite. If you want to use a keyword as a name, you need to quote it.
https://www.sqlite.org/lang_keywords.html

SQL syntax error

I use phpMyAdmin and want to create a table. I use the visual interface for creating the table but I'm gonna post the code from "Preview SQL" option:
CREATE TABLE `baza`.`koncert` (
`koncert_id` INT(10) NOT NULL AUTO_INCREMENT ,
`koncert_naziv` VARCHAR(50) NULL ,
`koncert_lokacija` VARCHAR(50) NOT NULL ,
`koncert_datum` DATE NULL DEFAULT NULL ,
`koncert_cijena` DOUBLE(10) NOT NULL ,
`koncert_slika` VARCHAR(500) NOT NULL )
ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_croatian_ci;
And I get this error:
1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL version for the right syntax to use near ')' NOT NULL, 'koncert_slika' VARCHAR(500) NOT NULL ) ENGINE=InnoDB CHARSET=ut
I tried setting the 'koncert_datum' default value to CURRENT_TIMESTAMP, but then I get an error "Invalid default value for 'koncert_datum'". I just don't understand what could possibly be wrong (and I used the phpMyAdmin visual interface to try create the table!)
According to the documentation https://dev.mysql.com/doc/refman/5.7/en/floating-point-types.html the DOUBLE type needs total digits and decimal digits. Something like
`koncert_cijena` DOUBLE(12,2) NOT NULL ,
Check the schema and ensure baza.koncert is present and try running them by removing "ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_croatian_ci" this.

PHPmyadmin won't accept CREATE TABLE statement

I'm trying to use a simple CREATE TABLE statement to create a new table in my very first SQL-database. PHPmyadmin won't accept the code and gives me an error statement.
There doesn't appear to be anything wrong with the syntax of my SQL command. In fact, I receive the same error statement when I copy and past an example code from any internet tutorial to create a table.
this is my SQL command:
CREATE TABLE Guestbook(
ID int AUTO_INCREMENT PRIMARY KEY NOT NULL,
Name varchar(50) NOT NULL,
Message TEXT NOT NULL,
Date datetime,
Sport varchar(30),
Practicioner BOOLEAN default 0,
)
This is the error statement:
Static analysis:
3 errors were found during analysis.
A symbol name was expected! (near "Name" at position 74)
Unexpected beginning of statement. (near "50" at position 87)
Unrecognized statement type. (near "NOT NULL" at position 91)
SQL query:
CREATE TABLE Guestbook( ID int AUTO_INCREMENT PRIMARY KEY NOT NULL, Name varchar(50) NOT NULL, Message TEXT NOT NULL, Date datetime, Sport varchar(30), Practicioner BOOLEAN default 0, )
MySQL said: Documentation
#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 ')' at line 8
I can't imagine why this won't work. I'd like to be able to use to command line in phpMyadmin. and this seems pretty straigtht forward. Yet I've been fiddling around with it for ages and I can't figure out how to create even the simplest possible table.
Can anyone help me out?
You should remove the last comma in your CREATE statement:
CREATE TABLE Guestbook(
ID int AUTO_INCREMENT PRIMARY KEY NOT NULL,
Name varchar(50) NOT NULL,
Message TEXT NOT NULL,
Date datetime,
Sport varchar(30),
Practicioner BOOLEAN default 0
)

postgres syntax error at or near "ON"

I created this table:
CREATE TABLE IF NOT EXISTS config_activity_log
(
id serial primary key,
activity_name varchar(100) NOT NULL,
last_config_version varchar(50) NOT NULL,
activity_status varchar(100) NOT NULL DEFAULT 'Awaiting for cofman',
cofman_last_update bigint NOT NULL DEFAULT -1,
is_error boolean DEFAULT FALSE,
activity_timestamp timestamp DEFAULT current_timestamp
);
I try to run this postgres script:
INSERT INTO config_activity_log
(activity_name, last_config_version, activity_status)
VALUES
('test awating deployment','5837-2016-08-24_09-12-22', 'Awaiting for deployment')
ON CONFLICT (activity_name)
DO UPDATE SET
activity_status = EXCLUDED.activity_status
why do i get this syntax error?
psql:upsert_test_log.sql:7: ERROR: syntax error at or near "ON"
LINE 5: ON CONFLICT (activity_name)
Supported Version
Per #klin's comment above, ON CONFLICT is only supported from PostgreSQL 9.5 onwards.
If you're on an earlier version, there's some great info in this answer: https://stackoverflow.com/a/17267423/361842
Unique Constraint
Add a unique index on activity_name. At present there's no constraints on that column, so there's no possibility for a conflict on that column.
CREATE UNIQUE INDEX UK_config_activity_log__activity_name
ON config_activity_log (activity_name);
If, however, you don't want that column to be unique, what conflict are you envisaging / what's the issue you're hoping to resolve with the on conflict action?
See conflict_target in https://www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT
An alternative syntax is to modify your create statement to include the unique condition there; e.g.
CREATE TABLE IF NOT EXISTS config_activity_log
(
id serial primary key,
activity_name varchar(100) NOT NULL UNIQUE,
last_config_version varchar(50) NOT NULL,
activity_status varchar(100) NOT NULL DEFAULT 'Awaiting for cofman',
cofman_last_update bigint NOT NULL DEFAULT -1,
is_error boolean DEFAULT FALSE,
activity_timestamp timestamp DEFAULT current_timestamp
);
According to the error code, your version does not support ON CONFLICT.
On PostreSQL 9.6 the error message is -
[Code: 0, SQL State: 42P10] ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification

MySQL Syntax for setting Default Date

How can we set element to some default date in mysql ?
In oracle we would do something like
start_date DATE DEFAULT to__date('01011900','DDMMYYYY')
mutation_date_time DATE DEFAULT SYSDATE
CONSTRAINT entity_specification UNIQUE(external_name, start_date_time, end_date_time))
Also do we have any site or resource where we can get MySQL equivalent syntax to Oracle Syntax.
Update
I tried to do as mentioned in answer but again am getting error message:
create table product_offer_type(object_id INT(19), snapshot_id INT(19), PRIMARY KEY(object_id,snapshot_id), enum_value VARCHAR(64) NOT NULL, external_name VARCHAR(64) NOT NULL, description VARCHAR(255), business_validation INT(1), valid_for_start_date_time DATE DEFAULT '1900-01-10', valid_for_end_date_timeDATE DEFAULT '4712-01-01', mutation_date_time DATE DEFAULT 'SYSDATE()', mutation_user VARCHAR(32) DEFAULT 'USER');
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 'DEFAULT '4712-01-01', mutation_date_time DATE DEFAULT 'SYSDATE()', mutation_user' at line 1
mysql>
Simply pass it a formatted string.
start_date DATE DEFAULT '1900-01-01'
For a fixed default date on a DATE field, the format is 'YYYY-MM-DD', like start_date DATE DEFAULT '2009-10-30'. DATETIME would be 'YYYY-MM-DD HH:MM:SS'.
For a dynamic date based on the current date, you can use DEFAULT CURRENT_TIMESTAMP if you are using a TIMESTAMP field; otherwise, it is not possible to use a function as a DEFAULT in MySQL. TIMESTAMP fields are formatted as 'YYYY-MM-DD HH:MM:SS'. Otherwise, you'd have to get the current date in your code before inserting the data.