1

I am planning to create a attendance system in java mysql and am stuck in the date and time function. My question is how can i insert the date automatically to my database. My table looks like this:

  Create table automatic
(
   `id` int(20) NOT NULL AUTO_INCREMENT,
   `name` varchar(500) NOT NULL,
   `class` varchar(255) NOT NULL,
   `remark` varchar(255) NOT NULL,//remarks suggest presnt or absent
   `date` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)    
) ENGINE=InnoDB;
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
rockers123
  • 11
  • 1
  • 2

2 Answers2

1

I hope you want to insert the datetime automatically when you insert a record to DB. Use DEFAULT CURRENT_TIMESTAMP

Create table automatic
(
   `id` int(20) NOT NULL AUTO_INCREMENT,
   `name` varchar(500) NOT NULL,
   `class` varchar(255) NOT NULL,
   `remark` varchar(255) NOT NULL,//remarks suggest presnt or absent
   `date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)    
) ENGINE=InnoDB;
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
  • sir hope I don't cause you any trouble can you please help me providing some example for insert function aswell!!!! – rockers123 Sep 09 '14 at 16:28
0

CURDATE() returns the current date.

You can use it like this:

CREATE TABLE Orders
(
OrderId int NOT NULL,
ProductName varchar(50) NOT NULL,
OrderDate datetime NOT NULL DEFAULT CURDATE(),
PRIMARY KEY (OrderId)
)

Many shopping sites use this if the want to track the ti me of the order by customer. Similarly it has tonnes of other uses.

get more details here

CREATE TABLE dates
(
    id int NOT NULL,
    id_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
);
INSERT INTO dates (id) VALUES (1);
SELECT id, DATE(id_date) AS id_date FROM dates;

id  id_date
1   2010-09-12
VedantK
  • 9,728
  • 7
  • 66
  • 71
  • it displays synatx error nea CURDATE() what can I do to solve this? – rockers123 Sep 10 '14 at 04:13
  • I apologize,as You can't use CURDATE() as a default value.Instead you can use a TIMESTAMP column with DEFAULT CURRENT_TIMESTAMP. Then you will have to ignore the time part of it. – VedantK Sep 10 '14 at 04:22