Change TIMESTAMP when execute update query - mysql

I try to update some values with an update query but have problems to create this table on reg_update:
CREATE TABLE Product (
id INT(3) PRIMARY KEY,
product VARCHAR(20) NOT NULL,
reg_date TIMESTAMP,
reg_update NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
I get this error:
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 'NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)' at line 5
Is it posible to update in this case reg_update ?

try this
CREATE TABLE Product (
id INT(3) PRIMARY KEY,
product VARCHAR(20) NOT NULL,
reg_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
reg_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

Related

Getting syntax error on sql database create table

So i'm trying to create a table using MySQL Workbench like this:
DROP TABLE IF EXISTS document_categories;
CREATE TABLE document_categories (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL DEFAULT '',
created_at timestamp NOT NULL DEFAULT current_timestamp(),
updated_at timestamp NULL DEFAULT current_timestamp(),
deleted_at datetime DEFAULT NULL,
PRIMARY KEY (id)
);
But im getting this error:
11:54:19 ​ CREATE TABLE document_categories ( id int(10) unsigned NOT NULL AUTO_INCREMENT, name varchar(64) NOT NULL DEFAULT '', created_at timestamp NOT NULL DEFAULT current_timestamp(), updated_at timestamp NULL DEFAULT current_timestamp(), deleted_at datetime DEFAULT NULL, PRIMARY KEY (id) ) Error Code: 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 '​ CREATE TABLE document_categories ( id int(10) unsigned NOT NULL AUTO_IN...' at line 1 0.000 sec
I don't really see what the problem is here. Also I'm using MariaDB as the database

making a Mysql table and I keep getting the same syntax error

Im very new to mysql and I keep getting the same error whenever i try to make a table
CREATE TABLE tasks {
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT PRIMARY KEY ,
description VARCHAR(64) NOT NULL,
completed TINYINT(2) NOT NULL DEFAULT 0,
created DATETIME NOT NULL DEFAULT NOW(),
last_updated DATETIME NOT NULL DEFAULT NOW() ON UPDATE NOW()
};
I wanted it to make a table but instead I got:
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 '{
id INT NOT NULL PRIMA' at line 1
You have a syntax error. CREATE TABLE uses parentheses not braces.
CREATE TABLE foo(col1 int, col2 varchar(25), col3 timestamp);
Here is the right syntax:
CREATE TABLE tasks (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, description VARCHAR(64) NOT NULL, completed TINYINT(2) NOT NULL DEFAULT 0, created DATETIME NOT NULL DEFAULT NOW(), last_updated DATETIME NOT NULL DEFAULT NOW() ON UPDATE NOW())

#1064 - You have an error in your SQL syntax; Timestamp table

I've searched the page for answers, however, I am not able to solve this.
I receive the following error:
#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 5
When I am pasting this in the database:
CREATE TABLE IF NOT EXISTS `Votes_History` (
ID int(11) NOT NULL AUTO_INCREMENT,
Username varchar(100) NOT NULL,
Coin varchar(30) NOT NULL,
Timestamp int(11) NOT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6;
This is what I used to enter data into the Votes_History table:
INSERT INTO `Votes_History` (`ID`, `Username`, `Coin`, `Timestamp`)
VALUES
What am I doing wrong here?
Robin
If the query you are using is the same as the one in this sqlfiddle : http://sqlfiddle.com/#!9/01dcb/1 then you query is valid
What I remarked is you are declaring Timestamp as int(11), if you give to your INSERT statement Timestamp value as NOW() or DATE() then its a syntax error.
For TIMESTAMP type example:
# This table used TIMESTAMP type
CREATE TABLE IF NOT EXISTS `Votes_History_Timestamp` (
ID int(11) NOT NULL AUTO_INCREMENT,
Username varchar(100) NOT NULL,
Coin varchar(30) NOT NULL,
Timestamp TIMESTAMP NOT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6;
INSERT INTO `Votes_History_Timestamp` (`Username`, `Coin`, `Timestamp`)
VALUES ('Hamza', '50MAD', NOW());
SQLFiddle link for timestamp : http://sqlfiddle.com/#!9/7878c9/1

Mysql : Insert null timestamp into NOT NULL column

Mysql version is 5.1.73,there can be only one timestamp column with current_timestamp in deafault or on update clause,so I tried to create table:
create table `temp`(
`id` INTEGER NOT NULL auto_increment PRIMARY KEY,
`created_date` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
`last_updated_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
And after I executed this sql:
insert into `temp`(id,created_date, last_updated_date) values(1,null, null);
I got this document:
1 2017-02-10 18:17:16 2017-02-10 18:18:13
Here is my question:
I set created_date NOT NULL,why did the insert sql work well?Should it return Error: Column 'xx' cannot be null?
Why did created_date column get a value and it is not the default value?
Thanks.
That's how the MySql's TIMESTAMP work, if you want to get an error on NULL the way to go is to use DATETIME instead.
So if you create the following table
create table `temp`(
`id` INTEGER NOT NULL auto_increment PRIMARY KEY,
`created_date` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`last_updated_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
And then execute the below query you'll get the error.
insert into `temp`(id,created_date, last_updated_date) values(1,null, null);
Now you'll start getting this error #1048 - Column 'created_date' cannot be null.

Please help sort out a syntax error when I try to define a MySQL table

The error is this;
#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 ''order'(
orderID VARCHAR(6) NOT NULL DEFAULT 0,
orderDate DATE NOT NULL DEFAUL' at line 1
My DDL is this:
CREATE TABLE 'order'(
orderID VARCHAR(6) NOT NULL DEFAULT 0,
orderDate DATE NOT NULL DEFAULT 1,
claimCondition VARCHAR(30) NOT NULL DEFAULT ''
);
The name order is a reserved word, so you'll have to name the table something else. Also, the value 1 isn't a valid default for the type date.
This works:
CREATE TABLE order1 (
orderID VARCHAR(6) NOT NULL DEFAULT 0,
orderDate DATE NOT NULL,
claimCondition VARCHAR(30) NOT NULL DEFAULT ''
);
CREATE TABLE `order`(
`orderID` VARCHAR(6) NOT NULL DEFAULT 0,
`orderDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`claimCondition` VARCHAR(30) NOT NULL DEFAULT ' '
);
You cant assign default 1 to the date field ,since the data type is date the default value must be the date value(use a default date or Current date instead)
create table orders(orderId varchar(6 ) NOT NULL DEFAULT 0,order_Date DATE NOT NULL DEFAULT "0000-00-00",claimCondition VARCHAR(30) NOT NULL DEFAULT '');
The above query is working know without default value to the date field
Refer this link you will get the idea of datatypes and and its default values
http://kimbriggs.com/computers/computer-notes/mysql-notes/mysql-data-types-50.file