Auto-store datetime() value in select insert query mysql - mysql

need an advice, how to auto-store datetime value for my historyActivity table in select insert mysql query. This is an example:
INSERT INTO history_sequence(CODE, LAST_MOUNTH, LAST_VALUE) SELECT CODE, MOUNTH, VALUE FROM seq WHERE CODE = CODEVALUE
i just want to add datetime to see time when the data inserted. Need help please

You can do this in the MySQL table definition:
ALTER TABLE history_sequence ADD inserted TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
When records are inserted into the table table, the inserted column gets automatically populated with the current timestamp.

Related

MySQL - stop updating DEFAULT CURRENT TIMESTAMP column

I have two date time columns in my database:
inserted and updated
inserted timestamp DEFAULT CURRENT_TIMESTAMP
updated timestamp -------------------------
Primary column is ID.
I run SQL command:
REPLACE INTO TABLE1 (id,name,updated) VALUES ('1','test1',NOW())
And this is OK:
inserted column get current time upon the first INSERT
updated columns is also set to current time because it is set in REPLACE command.
If I run second command to update name, then updated column is being updated properly but also and INSERTED column is updated also. I do not want that!!!
REPLACE INTO TABLE1 (id,name,updated) VALUES ('1','test2',NOW())
How to stop inserted column to be updated automatically once the REPLACE command is being run? What change on column INSERTED in mysql database should be done? I must use REPLACE sql or any synonym for my needs.
replace deletes the old record and inserts a new one.
If you want to update an existing record use update instead:
UPDATE TABLE1
SET name = 'test2',
updated = now()
where id = 1
If you want to update a field without write-in SQL command. You need to create that column like that:
`inserted` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
But this one works for Update operations not Replace operations.
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted. See Section 13.2.6, “INSERT Syntax”.
Detailed explanations here, see: https://dev.mysql.com/doc/refman/8.0/en/replace.html

Convert column[Time] to Timestamp mysql

Seeing the question you might find it as duplicate. But I have researched and its not.
The problem is I have a column in my table with "TIME" data-type. Now I want to convert the column to "TIMESTAMP".
I have tried using Modify too.
The query
ALTER TABLE `mydb`.`temp_table` MODIFY COLUMN `time` TIMESTAMP
But this doesn't work. The Error I got
Error Code: 1292. Incorrect datetime value: '20:00:00' for column 'time' at row 1
Alter table change column too doesn't work. Is there any way which I can convert the column TIMESTAMP.
The last option that I can see is convert the column to VARCHAR then update appending a date and then convert to timestamp.
UPDATE:
Tried coverting Time to DATETIME. Which gave no error but the data is not correct. After changing the column the time "20:00:00" changed to "2020-00-00 00:00:00"
I think you should add another "TIMESTAMP" column to your table and transfer data from you "TIME" column to new "TIMESTAMP" column. Then you could drop original "TIME" column and rename new column to "TIME".
ALTER TABLE `mydb`.`temp_table` ADD COLUMN `time_stamp` TIMESTAMP;
UPDATE table SET time_stamp = TIMESTAMP(CURDATE(), `time`);
ALTER TABLE `mydb`.`temp_table`
DROP COLUMN `time`;
ALTER TABLE `mydb`.`temp_table`
CHANGE COLUMN `time_stamp` `time` TIMESTAMP;
Try this:
create table test(`time` time);
insert into test values('10:00:00'), ('20:00:00');
alter table test add ts timestamp null;
update test set ts=`time`;
alter table test drop `time`;
alter table test change ts `time` timestamp not null;

Data truncated for column startDate1 at row

I have 2 columns startdate and enddate of type int. These columns are used to store timestamp data.
Now I have to extract the date component from this timestamp, convert it back to timestamp an store it in another column startdate1 of type int
But on doing this, I get a warning 'Data truncated for column startDate1 at row'.
The sql queries are:-
ALTER TABLE `ServiceRule` ADD COLUMN `startDate1` INT(11) NULL DEFAULT NULL AFTER `endDate` , ADD COLUMN `endDate1` INT(11) NULL DEFAULT NULL AFTER `startDate1`;
update `ServiceRule` set `startDate1`= TIMESTAMP(DATE(from_unixtime(`startDate`)));
update `ServiceRule` set `endDate1`= TIMESTAMP(DATE(from_unixtime(`endDate`)));
Now if i change the datatype of startDate1 and endDate1 to TIMESTAMP, the first update query of startDate1 runs successfully.
But the endDate1 update query shows the warning 'Out of range value for column 'endDate1' at row'.
After browsing for solution, i got know that this occurs if the input value is greater than the column datatype range.
Can anybody please try to help me out?
Thanks in Advance. :)
I think what you want is UNIX_TIMESTAMP instead of TIMESTAMP.
UNIX_TIMESTAMP is the inverse function of FROM_UNIXTIME.
What means that one of the rows contains a value that cannot be converted to int because indeed it is either too more or too less than expected.
Can't you just convert the columns to timestamp and do the extraction from there? Try to query per set of 100 for example and narrow down the faulting row.
When the field startDate is of type int, then alter table statement used is not correct to achieve what you wanted.
Change it as below:
-- keeping the added fields as is, execute the following
ALTER TABLE `ServiceRule` MODIFY COLUMN `startDate1` DATETIME DEFAULT NULL;
ALTER TABLE `ServiceRule` MODIFY COLUMN `endDate1` DATETIME DEFAULT NULL;
update `ServiceRule` set `startDate1`= TIMESTAMP(DATE(from_unixtime(`startDate`)));
update `ServiceRule` set `endDate1`= TIMESTAMP(DATE(from_unixtime(`endDate`)));

how to store date default values to table.but it takes null values

i am fresher in Sql server 2008.
i create table as:
-- Table structure for [xyz]
-- ----------------------------
DROP TABLE [xyz]
GO
CREATE TABLE [xy] (
[abc] DATETIME DEFAULT GETDATE() NOT NULL
)
in inserted time date values as:2013-08-07 00:00:00.000
i want store time value as it is present time.
You can also use the time stamp for current time for each inserted record.
DROP TABLE [xyz]
GO
CREATE TABLE [xy] (
[abc] DATETIME default CURRENT_TIMESTAMP
)
Try something like this:-
ALTER TABLE myTable ADD CONSTRAINT_NAME DEFAULT GETDATE() FOR myColumn
Your Java code is not including the date - the default value only applies when you do not specify a value - it does not "magically" add the time component to the date you pass in. If you want to add the date you'll have to create an UPDATE/INSERT trigger to add the current time to the date that's pass in.
However, I would just update your Java code (if you can) to include the time.

SQL - Creating a Table with Two Timestamp Column with No Default Value

I want to create a table with two column, one "created" and one "modified", with a timestamp type. I don't want them to have a default value. But when I create them, it automatically puts CURRENT_TIMESTAMP as default for at least one of them, and also on update.
How can I just have type timestamp with no default or on update value for any of my column?
Edit: I use MySQL
If you are using SQL Server, the timestamp type is used for row versioning, not for storing an actual date or time value. See MSDN.
You could create the columns with a datetime datatype and then set your created and modified values with triggers.
CREATE TRIGGER trg_SetDateCreated ON MyTable
FOR INSERT AS
UPDATE MyTable
SET created = CURRENT_TIMESTAMP
WHERE MyTable.id = (SELECT Id FROM Inserted);
GO
CREATE TRIGGER trg_SetDateModified ON MyTable
FOR UPDATE AS
UPDATE MyTable
SET modified = CURRENT_TIMESTAMP
WHERE MyTable.id = (SELECT Id FROM Inserted);
GO