Updating the timestamp of a row when copied from another table - mysql

I've been working at this for a while now and I haven't been able to come up with a solution, so i'm not sure if this may even be possible but any help is greatly appreciated.
I have two tables with a very basic structure as follows:
CREATE TABLE test ( time timestamp NULL DEFAULT NULL );
CREATE TABLE test_copy_to ( time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP );
I then create an entry in the "test" table that has a NULL value i.e. no given time:
INSERT INTO test VALUES (NULL);
I then would like to copy the NULL value from the "test" table to the "test_copy_to" table as follows:
INSERT INTO test_copy_to SELECT * FROM test;
The first problem is that the "test_copy_to" won't accept NULL values which is what is initialized in the first table. However what I would like to achieve is being able to copy the value from "test" (while having the null value) to "test_copy_to" such that the time-stamp is updated to the time at which it was copied from one table to the other. I've tried using UPDATE on the variable time however that does not seem to work. Is there anyway I can create the table such that it behaves in this manner?

Instead use COALESCE() function like below to provide some default values where the value is null
INSERT INTO test_copy_to
SELECT coalesce(`time`, CURRENT_TIMESTAMP ) FROM test;

Related

Insert INTO table where one table has a NOT NULL

I have two identical tables bar one column, (one live, one test)
The test table has an extra not null Column called "MatchOrderNo"
I'm trying to extract data from Live to test, how can i populate this not Null for all rows?
basically my current statement is
INSERT INTO test SELECT * FROM live;
Depends what do you want to insert to the NOT NULL column? For a constant value :
INSERT INTO test
SELECT t.* , 'Val_For_NotNull_Col' FROM live t
You can use a Default value like:
INSERT INTO test SELECT live.col1, live.co2, '1' FROM live;

INSERT INTO ... SELECT if destination column has a generated column

Have some tables:
CREATE TABLE `asource` (
`id` int(10) unsigned NOT NULL DEFAULT '0'
);
CREATE TABLE `adestination` (
`id` int(10) unsigned NOT NULL DEFAULT '0',
`generated` tinyint(1) GENERATED ALWAYS AS (id = 2) STORED NOT NULL
);
I copy a row from asource to adestination:
INSERT INTO adestination
SELECT asource.*
FROM asource;
The above generates an error:
Error Code: 1136. Column count doesn't match value count at row 1
Ok, quite strange to require me to mention generated query. But ok, I add that column to the query:
INSERT INTO adestination
SELECT asource.*, NULL AS `generated`
FROM asource;
This has worked fine in 5.7.10. However, it generates an error in 5.7.11 (due to a fix:
Error Code: 3105. The value specified for generated column 'generated' in table 'adestination' is not allowed.
Ok, next try:
INSERT INTO adestination
SELECT asource.*, 1 AS `generated`
FROM asource;
But still the same error. I have tried 0, TRUE, FALSE but the error persists.
The DEFAULT value which is stated as the only allowed value (specs or docs). However, the following generates a syntax error (DEFAULT is not supported there):
INSERT INTO adestination
SELECT asource.*, DEFAULT AS `generated`
FROM asource;
So, how can I copy a row from one table to another using INSERT INTO ... SELECT if the destination table adds some columns where some of them are GENERATED?
The code calling this query is generic and has no knowledge what columns that particular tables have. It just knows which extra columns the destination table has. The source table is a live table, the destination table is a historical version of the source table. It has few columns extra like user id made the change, what type of the change it is (insert, update, delete) when etc.
Sadly this is just how MySQL works now to "conform to SQL standards".
The only value that the generated column can accept in an update, insert, etc. is DEFAULT, or the other option is to omit the column altogether.
My poor mans work around for these are to just disable the generated column while I'm working with the data (like for importing a dump) and then go back and add the generated column expression afterwards.
You must declare the columns
Insert into adestination (id, generated)
select id, 1
from asource;
It is best practice to list out the columns, and use null as field1 for the auto incremented id field.
INSERT INTO adestination
(id,
field1,
field2)
SELECT
null AS generated,
asource.field1,
asource.field2
FROM asource;

Auto-store datetime() value in select insert query 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.

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