I am using SQL Server 2008 :
I want to keep date and time separated
I want both of them to have default value like NOW() when record is created
How can I control format of date and time
So far I have this code :
CREATE TABLE [Table1]
(
[RECORD_ID] int IDENTITY (1, 1) NOT NULL,
[INPUT_ID] int NOT NULL,
[DATE] date DEFAULT GETDATE(),
[TIME] time(7) DEFAULT GETDATE(),
PRIMARY KEY ([RECORD_ID])
)
I just don't know how to set time condensed data type to time(0). Is rest of my code ok, or there is a better approach ?
Related
Basically I have a MySQL database with a table that stores requests from the users of my website. I would like to automatically perform a query that removes all completed requests (completed means that the column Status = 3) older than 180 days basing on the column ArchivingDate.
My Requests table:
Requests(
RequestID INT AUTO_INCREMENT PRIMARY KEY,
Ticket VARCHAR(40) NOT NULL UNIQUE,
RequestDate DATETIME NOT NULL,
ReplacementOrRefund INT NOT NULL,
ItemName VARCHAR(200) NOT NULL,
Total NUMERIC(15, 2) NOT NULL,
AccountName VARCHAR(200) NOT NULL,
Email VARCHAR(254) NOT NULL,
BillingAddress VARCHAR(200) NOT NULL,
OrderNumber VARCHAR(50) NOT NULL,
OrderDate DATETIME NOT NULL,
DeliveryDate DATETIME NOT NULL,
WhoSigned VARCHAR(200),
WhereLeft VARCHAR(200),
Status INT NOT NULL,
ArchivingDate DATETIME,
PayPalTransactionID VARCHAR(100),
Toggle TINYINT(1) NOT NULL,
StoreID_FK INT,
FOREIGN KEY (StoreID_FK) REFERENCES Stores(StoreID)
);
I have already written the query to remove rows that are older than 180 days:
$remove = "UPDATE Requests SET Toggle = 0 WHERE Status = 3 AND ArchivingDate < DATE_SUB(NOW(), INTERVAL 180 DAY)";
NOTE: I do not remove data from the table, I simply "hide" it by setting the column Toggle to 0.
Question: How do I automatically make MySQL perform this query once a day (if it's possible)?
Thank you very much in advance!
you can create a stored procedure to delete the records based on your criteria of 180 days on "RequestDate" and schedule your stored procedure on daily basis.
Now MySQL will handle the deletion automatically.
yes, it's possible, see this article: http://www.sitepoint.com/how-to-create-mysql-events/
It depends witch OS you have.
Linux
Use file /etc/crontab, add line where specify time and command what to execute.
The columns in crontab file are:min,hour,day of month,month,day of week,run as user, command.(Colums are seperated by space)
For example:
30 22 * * * apache php /usr/remove_old.php
It will execute every evening in 22:30.
It is good practice to put the execution result to log file.
30 22 * * * apache php /usr/remove_old.php >> \var\log\removed_requests.log 2>&1
The you can analyze later if somethings goes wrong.
Windows
You can use Task Scheduler programm.
I want to make a column in mysql database that when user login first time in system, it stores that datetime in mysql table. And since that day in other column days will add according to his register date. Like 1, 2, 3,....and so on. So, is there any way I can achieve the results? Please guide me soon.
You can do this with just one column (to hold the registration / first login date) and the DATEDIFF function:
CREATE TABLE users (
ID int(11) NOT NULL AUTO_INCREMENT,
name varchar(20) NOT NULL,
registered_at datetime NOT NULL,
PRIMARY KEY (ID)
);
INSERT INTO users SET
name = 'myname',
registered_at = NOW();
SELECT registered_at, DATEDIFF(NOW(), registered_at) AS days_since
FROM users
WHERE name = 'myname';
I am familiar with DATE_FORMAT function which can display the the record from my date field in the format specified. However, I would like to create a table with a date field that only accepts my format.
Here's what I have done so far:
CREATE TABLE test_table (
id INT AUTO_INCREMENT,
f_name VARCHAR(40) NOT NULL,
l_name VARCHAR(25) NOT NULL,
date_hired DATE NOT NULL
);
Inserting a record with a date_hired value of '2013-03-01' will be inserted as '1/03/2013 12:00:00 AM' which is far from my expected result (I would like the format the way it was inserted). Any feedback? Did I miss something?
Thanks,
Michael
You can't change the format during table create, you can change the format of date for displaying user by using you programming logic like if you are using PHP as your server side language the you can convert it your desired format.
I have data in the format of both "2013-01-17 18:46:47 -0800" and "1358477089" ...I'm wondering what is the best way to store this in a mysql db, that allows me to select results within a certain month, week, day etc.. using mysql's own functions.
Currently my create table code is like this.. the "timestamp" needs changing.
visible
CREATE TABLE IF NOT EXISTS `votes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`from` varchar(32) NOT NULL,
`username` varchar(32) NOT NULL,
`address` varchar(16) NOT NULL,
`timestamp` varchar(32) NOT NULL,
PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Best way is to use MySQL built-in DATETIME type.
MySQL offers lots of function which will allow you to select results within a certain month, week, day, whatever you need.
See great list of functions here:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
As hek2mgl and other guys mentioned, there is also TIMESTAMP.
MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.)
If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different from the value you stored.
I preffer and advice you to use DATETIME.
If you use a timestamp your field should be an "integer" not a varchar. This provides better perfomance (for example if you use an index for this column).
If you do not need to have dates before 1970 I would suggest to use a timestamp, not a datetime. It is easier to use.
PHP
$timestamp = date('U');
MySQL
INSERT INTO table SET timestamp = UNIX_TIMESTAMP()
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,
...