Current_Timestamp on different time zone - mysql

I made a table like this:
CREATE TABLE options(
id MEDIUM INT NOT NULL AUTO_INCREMENT,
email VARCHAR(254) NOT NULL,
created_at DATETIME DEFAULT NULL,
PRIMARY KEY(id)
);
however.. after inserting a value like:
INSERT INTO options VALUES(0,'test#test.com',CURRENT_TIMESTAMP)
I got the created_at column with this value : 10:29 but right now the time is 16:29.
I believe that's because the server/db is located in the US and I'm in Brazil.
Is there a way to get my current time when inserting values into the table?

Related

Can I have a string of TIMESTAMPS in MySQL?

I am just wondering to know can I have a string of TIMESTAMPs in a MySQL table schema like following:
CREATE TABLE IF NOT EXISTS auth (
id BINARY(16) PRIMARY KEY,
email VARCHAR(64) UNIQUE NOT NULL,
password VARCHAR(64) NOT NULL,
admin INT NOT NULL DEFAULT 0,
created_at [TIMESTAMP]
);
Unfortunately the above code gives me a syntax error!
My idea is to have a list of TIMESTAMs to store every future updates inside of it, something like ['2023-01-01' , '2023-02-02' , ....]
If this is not possible in my suggested way, how can I store any changes of one column of a table like created_at column? Should I do it in the web-server?

MYSQL InnoDB engine indexing (B-Tree)

I have the following user registration table:
ID INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
User_name VARCHAR(35) NOT NULL,
Full_name VARCHAR(55) NOT NULL,
User_vars VARCHAR(255) NOT NULL,
BirthDay DATE NOT NULL,
password CHAR(70) NOT NULL,
Security_hint VARCHAR(27) NOT NULL,
Email VARCHAR(225) NOT NULL,
userType ENUM ('a','b','c','d') NOT NULL DEFAULT 'a',
Signup_Date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
activation TINYINT(1) UNSIGNED NOT NULL,
Ip BINARY(16) NOT NULL,
PRIMARY KEY (ID),
UNIQUE KEY User_name (User_name,Email)
I am really confused to whether I SHOULD index the fields BirthDay, Signup_Date, Full_name, User_vars and userType as this information will have to be displayed in any page the user visits.
User_varsis a field that the user might be updating on daily basis (or even weekly); I think that indexing this field might not be good idea as indexes slow down write operations; correct me if wrong. I have many other fields like this one in other tables (fields that user updates on daily basis and their updates have toble displayed in any page they visit ), and don't know if they have to be indexed as well
Note: I know that I can query them once then cache them, however I am afraid that the first query to the table will be slow to return the result-set.
Many thanks
For performance change the VARCHAR to TINYTEXT which allows the rows to have a fixed size thereby speeding up table scans. Based on the table likely queries are for User_name and Email. Change those to CHAR and index those.

Set a datetime column with a SQL trigger

I using a MySQL server (5.5.27 - Community Server). I have a table with this definition:
CREATE TABLE IF NOT EXISTS tbl_messages (
`msg_id` VARCHAR(50) NOT NULL ,
`msg_text` VARCHAR(50) NULL ,
PRIMARY KEY (`msg_id`);
I write a trigger that, when I do an insert, the server sets the msg_id column with the current time including microseconds with this format "yyyymmddhhnnssuuuuuu". "u" is for microseconds.
I created a trigger:
create trigger tbl_messages_trigger
before insert on tbl_messages
for each row
BEGIN
SET NEW.msg_id = DATE_FORMAT(NOW(),'%Y%m%d%H%i%s%f');
END;$$
But the msg_id column only gets values like this: 20130302144818*000000*, with microseconds in zero. ¿Is it possible capture the microseconds?
TIA,
From the code provided I guess that you are trying to use microsecond to minimize probability of getting same msg_id for different rows.
Also, msg_id is the primary key, which should not present any object-specific data but only be unique. There is a good link about: http://en.wikipedia.org/wiki/Surrogate_key
The best way to deal with primary keys in MySql is AUTO_INCREMENT column attribute. If you need insert time for messages, you may provide column for it:
CREATE TABLE tbl_messages
(
`msg_id` bigint(20) NOT NULL AUTO_INCREMENT,
`msg_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`msg_text` VARCHAR(50) NULL,
PRIMARY KEY (`msg_id`)
);

Convert from SQLite to MySQL

This table I created in a SQLite database:
CREATE TABLE [tickets] (
[id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
[coupon_id] INTEGER NULL,
[size] FLOAT NULL,
[phone] VARCHAR(10) NULL,
[date] DATE DEFAULT CURRENT_DATE NULL,
[time] TIME DEFAULT CURRENT_TIME NULL,
[product] TEXT NULL
);
Now INSERT operation is:
INSERT INTO "tickets" VALUES(429,9,18.16,'949-893-5032','2010-11-30','17:46:39','Kids’ Kups Berry Interesting™');
INSERT INTO "tickets" VALUES(430,9,12.04,'847-188-1359','2010-11-25','10:54:00','Raspberry Collider™');
INSERT INTO "tickets" VALUES(431,9,14.1,'204-682-5560','2010-12-08','15:34:07','Celestial Cherry High™');
Now the same table I created in MySQL:
CREATE TABLE tickets (
id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
coupon_id INTEGER NULL,
size FLOAT NULL,
phone VARCHAR(10) NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
product TEXT NULL
);
INSERT operation for MySQL is:
INSERT INTO tickets VALUES(429,9,18.16,'949-893-5032','2010-11-30','17:46:39','Kids’ Kups Berry Interesting™');
INSERT INTO tickets VALUES(430,9,12.04,'847-188-1359','2010-11-25','10:54:00','Raspberry Collider™');
INSERT INTO tickets VALUES(431,9,14.1,'204-682-5560','2010-12-08','15:34:07','Celestial Cherry High™');
When i am inserting those values I got an error :-there can be only one TIMESTAMP column with current_timestamp in default of on update clause
…but I am not able to insert all those values into MySQL. Help me?
In SQLite you have two columns
[date] DATE DEFAULT CURRENT_DATE NULL,
[time] TIME DEFAULT CURRENT_TIME NULL,
while on MySQL you have only one
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
and you're trying to insert two values on it...
You should try
INSERT INTO tickets VALUES(..., '2010-11-30 17:46:39', ...)
At first glace, your varchar column is size 10, but you are inserting greater than length 10 data into it. Make sure your varchar column is wide enough for your data.
Your MySQL Schema appears to be incorrect for what you're trying to insert.
Excerpt from this post: Should I use field 'datetime' or 'timestamp'?
...Timestamps in MySQL generally used to track changes to records, and are updated every time the record is changed. If you want to store a specific value you should use a datetime field.
Change your MySQL schema to something closer to:
...
phone VARCHAR(12) NULL,
date DATE DEFAULT CURRENT_DATE NULL,
time TIME DEFAULT CURRENT_TIME NULL,
...

Time & Date Stamp in a MySQL table row

I would like to put a time & date stamp on each row added to a MySQL table. If I understand correctly, I need to create a column for the time & date stamp. How do I do that for the CREATE TABLE query below?
"CREATE TABLE `$table` (id INT(11) NOT NULL auto_increment, site VARCHAR(1000) NOT NULL, actions1 BIGINT(9) NOT NULL, actions2 BIGINT(9) NOT NULL, PRIMARY KEY(id), UNIQUE (site))"
Also, if I understand correctly, I would then need to add the stamp each time a row is inserted to the table. How would I do that for the INSERT INTO query below?
"INSERT INTO `$find` VALUES (NULL, '$site',1,0)"
Thanks in advance,
John
You need to add a TIMESTAMP column like this:
CREATE TABLE `$table` (
id INT(11) NOT NULL auto_increment,
site VARCHAR(1000) NOT NULL,
actions1 BIGINT(9) NOT NULL,
actions2 BIGINT(9) NOT NULL,
CreatedDateTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP
PRIMARY KEY(id), UNIQUE (site))
That will make a column CreatedDateTime which contains the (server) time the row was created.
You can do the insert as:
INSERT INTO `$find` (site, actions1, actions2) VALUES ('$site', 1, 0)
For further reference see here