Insert code:
INSERT INTO CalendarAppointment (`codiCategory`, `codiPersona`, `nomePersona`, `comment`, `day`, `description`, `from`, `shortDescription`, `to`, `modificatoDa`, `dataModifica`, `idModifica`) VALUES ('oUQz/ejwAfoAdAw7Jm+W1cFKeNH8N26ZBBcvhwwXPAA=', 'oUQz/ejwAfoAdAw7Jm+W1cFKeNH8N26ZBBcvhwwXPAA=', 'HuWZQyWSAnY9ReZ0q0+ZrBVRhReagvUG9Th1q3TtLPw=', 'q46vm3AsgJsEO6NOJ3r5xKhLMvvy+Sy05E76krKhiNo=', '2014-10-29', 'q46vm3AsgJsEO6NOJ3r5xKhLMvvy+Sy05E76krKhiNo=', '2014/10/13 09:00', 'q46vm3AsgJsEO6NOJ3r5xKhLMvvy+Sy05E76krKhiNo=', '2014/10/13 09:30', '2014-10-29 11:11:34', '2014-10-29 11:11:34', '1')
Show create table CalendarAppointment:
| CalendarAppointment | CREATE TABLE `CalendarAppointment` (
`idAppointment` int(10) NOT NULL AUTO_INCREMENT,
`codiCategory` blob,
`codiPersona` blob,
`nomePersona` blob,
`comment` blob,
`day` datetime NOT NULL,
`description` blob,
`from` datetime NOT NULL,
`shortDescription` blob,
`to` datetime NOT NULL,
`modificatoDa` varchar(80) NOT NULL DEFAULT '',
`dataModifica` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`idModifica` int(11) NOT NULL DEFAULT '0',
UNIQUE KEY `idAppointment` (`idAppointment`)
) ENGINE=InnoDB AUTO_INCREMENT=104 DEFAULT CHARSET=utf8
What is wrong?
Related
CREATE DATABASE vehicle_system;
USE vehicle_system;
USE vehicle_system; CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) DEFAULT NULL,
`email` varchar(255) NOT NULL,
`password_hash` text NOT NULL,
`api_key` varchar(32) NOT NULL,
`phone` varchar(14) NOT NULL,
`status` int(1) NOT NULL DEFAULT '1',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `phone` (`phone`)
);
CREATE TABLE IF NOT EXISTS `vehicle_model` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) DEFAULT NULL,
`manufacturer_id` int(11) NOT NULL,
`manufacturer_name` varchar(250) NOT NULL,
`model` varchar(32) NOT NULL,
`model_no` varchar(32) NOT NULL,
`type` varchar(32) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `manufacturer_id` (`manufacturer_id`)
);
CREATE TABLE IF NOT EXISTS `user_vehicles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) DEFAULT NULL,
`user_id` int(11) NOT NULL,
`manufacturer_id` int(11) NOT NULL,
`vehicle_no` varchar(32) NOT NULL,
`rc_no` text,
`engine_type` varchar(32),
`year` int(4),
`insurance_exp_date` timestamp,
`pollution_exp_date` timestamp,
`rc_renew_date` timestamp,
`insurance_company` varchar(32),
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `vehicle_no` (`vehicle_no`),
KEY `user_id` (`user_id`),
KEY `manufacturer_id` (`manufacturer_id`)
);
While executing above query I get
#1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
But I do not see two default time stamp in the query, Can any one point out what could be the possible error. Is it because current time stamp is present in the users table ? If so how to get rid of this ?
I think the problem is that you do not have a default for all the fields of type timestamp.
Running your query I got the following error:
Error Code: 1067. Invalid default value for 'updated_at'
Then I got:
Error Code: 1067. Invalid default value for 'pollution_exp_date'
Everything works fine when all the timestamp field have a default value:
CREATE DATABASE vehicle_system;
USE vehicle_system;
USE vehicle_system; CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) DEFAULT NULL,
`email` varchar(255) NOT NULL,
`password_hash` text NOT NULL,
`api_key` varchar(32) NOT NULL,
`phone` varchar(14) NOT NULL,
`status` int(1) NOT NULL DEFAULT '1',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `phone` (`phone`)
);
CREATE TABLE IF NOT EXISTS `vehicle_model` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) DEFAULT NULL,
`manufacturer_id` int(11) NOT NULL,
`manufacturer_name` varchar(250) NOT NULL,
`model` varchar(32) NOT NULL,
`model_no` varchar(32) NOT NULL,
`type` varchar(32) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `manufacturer_id` (`manufacturer_id`)
);
CREATE TABLE IF NOT EXISTS `user_vehicles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) DEFAULT NULL,
`user_id` int(11) NOT NULL,
`manufacturer_id` int(11) NOT NULL,
`vehicle_no` varchar(32) NOT NULL,
`rc_no` text,
`engine_type` varchar(32),
`year` int(4),
`insurance_exp_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`pollution_exp_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`rc_renew_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`insurance_company` varchar(32),
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `vehicle_no` (`vehicle_no`),
KEY `user_id` (`user_id`),
KEY `manufacturer_id` (`manufacturer_id`)
);
This is on MySql 5.7.10.
There is an interesting discussion on this here:
https://bugs.mysql.com/bug.php?id=41137
Creation of timestamp fields is different from any other!
Practically up to 5.6.6 you do not have to declare NOT NULL as by definition that field is NOT NULL and with neither DEFAULT nor ON UPDATE clauses, it is the same as DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.
After 5.6.6 you have to explicit defaults. Look also:
https://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html
and
http://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html
Finally, in your case removing NOT NULL is the best approach.
Regards
i have a problem in my examination system. In the admin panel, in the results page displays the correct score but the firstname and the last name not. I dont know what to do.
Here is the code:
$result1=executeQuery("select s.nume_student,s.prenume_student,s.stdname,s.stdid, IFNULL((select sum(q.marks) from studentquestion as sq,question as q where q.qnid=sq.qnid and sq.testid=".$_REQUEST['testid']." and sq.stdid=st.stdid and sq.stdanswer=q.correctanswer),0) as om from studenttest as st, student as s where s.stdid=st.stdid and st.testid=".$_REQUEST['testid'].";" );
$result3=executeQuery("SELECT sq.stdid, sq.testid, COUNT(*) AS correctAnswers, SUM(q.marks) AS studentScore,(SELECT SUM(marks) FROM question WHERE testid=".$_REQUEST['testid'].") AS totalScore FROM question q, studentquestion sq WHERE sq.testid=".$_REQUEST['testid']." AND q.testid = sq.testid AND q.qnid = sq.qnid AND sq.answered = 'answered' AND q.correctanswer = sq.stdanswer GROUP BY sq.stdid");
without $result1 doesen't display anything
And here are the tables from database:
question table:
CREATE TABLE IF NOT EXISTS `question` (
`testid` bigint(20) NOT NULL DEFAULT '0',
`qnid` int(11) NOT NULL DEFAULT '0',
`question` varchar(500) DEFAULT NULL,
`optiona` varchar(100) DEFAULT NULL,
`optionb` varchar(100) DEFAULT NULL,
`optionc` varchar(100) DEFAULT NULL,
`optiond` varchar(100) DEFAULT NULL,
`correctanswer` enum('optiona','optionb','optionc','optiond') DEFAULT NULL,
`marks` float(3,2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
studentquestion table:
CREATE TABLE IF NOT EXISTS `studentquestion` (
`stdid` bigint(20) NOT NULL DEFAULT '0',
`testid` bigint(20) NOT NULL DEFAULT '0',
`qnid` int(11) NOT NULL DEFAULT '0',
`answered` enum('answered','unanswered','review') DEFAULT NULL,
`stdanswer` enum('optiona','optionb','optionc','optiond') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
student table:
CREATE TABLE IF NOT EXISTS `student` (
`stdid` bigint(20) NOT NULL,
`stdname` varchar(40) DEFAULT NULL,
`stdpassword` varchar(40) DEFAULT NULL,
`emailid` varchar(40) DEFAULT NULL,
`contactno` varchar(20) DEFAULT NULL,
`fname` varchar(40) DEFAULT NULL,
`lname` varchar(40) DEFAULT NULL,
`pincode` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
student test table:
CREATE TABLE IF NOT EXISTS `studenttest` (
`stdid` bigint(20) NOT NULL DEFAULT '0',
`testid` bigint(20) NOT NULL DEFAULT '0',
`starttime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`endtime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`correctlyanswered` int(11) DEFAULT NULL,
`status` enum('over','inprogress') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
and test table:
CREATE TABLE IF NOT EXISTS `test` (
`testid` bigint(20) NOT NULL,
`testname` varchar(30) NOT NULL,
`testdesc` varchar(100) DEFAULT NULL,
`testdate` date DEFAULT NULL,
`testtime` time DEFAULT NULL,
`subid` int(11) DEFAULT NULL,
`testfrom` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`testto` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`duration` int(11) DEFAULT NULL,
`totalquestions` int(11) DEFAULT NULL,
`attemptedstudents` bigint(20) DEFAULT NULL,
`testcode` varchar(40) NOT NULL,
`tcid` bigint(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I resolved the same problem of here:
How to get sql WHERE statement auto update from localhost link
but i'm stucked here with fname and last name.
If your second query returns the correct student ids, you can just change it to add student names:
SELECT s.lastname, s.firstname, e.*
from
students s join (
... your second query ... ) e
on s.stdid=e.stdid
you can change your join type depends on what you want.
Thanks, but i resolved it like this:
SELECT ***s.fname, s.lname***, sq.stdid, sq.testid, COUNT(*) AS correctAnswers,
SUM(q.marks) AS studentScore,(SELECT SUM(marks) FROM question
WHERE testid=1) AS totalScore
FROM question q, studentquestion sq, ***student s***
WHERE sq.testid=1 AND q.testid = sq.testid
AND q.qnid = sq.qnid AND sq.answered = 'answered' ***and sq.stdid=s.stdid***
AND q.correctanswer = sq.stdanswer GROUP BY sq.stdid
I exported the sql from one phpmyadmin db and imported same at another phpmyadmin db.
The create table statement is as follows
CREATE TABLE IF NOT EXISTS `h_stats` (
`Id` int(11) DEFAULT NULL,
`Category` varchar(50) DEFAULT NULL,
`Success` int(11) DEFAULT NULL,
`OutcomeFailure` int(11) DEFAULT NULL,
`ThCount` int(11) DEFAULT NULL,
`OCount` int(11) DEFAULT NULL,
`HDate` varchar(50) DEFAULT '0',
`Count` int(11) DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The insert sql file also I have exported it as follows
INSERT INTO `h_stats` (`Id`, `Category`, `Success`, `Failure`, `ThCount`, `OCount`, `HDate`, `Count`) VALUES
(13, 'Hits', 31303, 8828, 8893, 30372, '2015-04-07', 40151),
Note :
When I try to insert new rows at the place of id NULL value is coming.
Can anyone suggest me what to change in create table statement.
You need to define the id as primary key and auto-increment .
CREATE TABLE IF NOT EXISTS `h_stats` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Category` varchar(50) DEFAULT NULL,
`Success` int(11) DEFAULT NULL,
`OutcomeFailure` int(11) DEFAULT NULL,
`ThCount` int(11) DEFAULT NULL,
`OCount` int(11) DEFAULT NULL,
`HDate` varchar(50) DEFAULT '0',
`Count` int(11) DEFAULT '0',
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
So when you add null for id, the auto-increment will come in to play and set it the next value in insert.
Note that this will enforce you to have unique value for id so while doing insert leave id into the insert statement. Something as
insert into h_stats (`Category`,`Success` ...) values ( ....);
This query is for an IPN script integrated with PayPal. Everything works in the script except the insertion of data into the table. Here's the code to insert data, all of the objects have their appropriate value.
if ($item == "RuneCoins") {
mail("admin#sallesy.com", "Item purchased!", "$acc has just purchased $item for $paid.");
mysql_query("INSERT INTO `purchases` (`id`, `email`, `price`, `product`, `fname`, `lname`, `time`, `transaction_id`, `acc`) VALUES (NULL, '$email', '$paid', '$item', '$fname', '$lname', CURRENT_TIMESTAMP, '$trans', '$acc');");
} else {
mail("admin#sallesy.com", "Invalid item!", "$acc purchased $item, and it was invalid.");
}
I am receiving the email that the user has purchased the item, but the data is not inserted into the table. Why is this happening?
Here is the SQL structure for the table
CREATE TABLE IF NOT EXISTS `purchases` (
`id` int(11) NOT NULL,
`email` longtext COLLATE utf8_unicode_ci NOT NULL,
`price` int(11) NOT NULL,
`product` longtext COLLATE utf8_unicode_ci NOT NULL,
`fname` longtext COLLATE utf8_unicode_ci NOT NULL,
`lname` longtext COLLATE utf8_unicode_ci NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`transaction_id` bigint(11) NOT NULL,
`acc` longtext COLLATE utf8_unicode_ci NOT NULL,
`delivered` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
What am I doing wrong?
I see the problem. You cannot use NULL as the value for id, because it is specifically prohibited in your create table statement. What you need to do is two fold.
First, change your insert to this:
mysql_query("INSERT INTO `purchases` (`id`, `email`, `price`, `product`, `fname`, `lname`, `time`, `transaction_id`, `acc`) VALUES (0, '$email', '$paid', '$item', '$fname', '$lname', CURRENT_TIMESTAMP, '$trans', '$acc');");
The only change I made there is changing NULL to 0, since 0 would be valid, and works nicely with the next change. Change two, modify your create table:
CREATE TABLE IF NOT EXISTS `purchases` (
`id` int(11) NOT NULL auto_increment,
`email` longtext COLLATE utf8_unicode_ci NOT NULL,
`price` int(11) NOT NULL,
`product` longtext COLLATE utf8_unicode_ci NOT NULL,
`fname` longtext COLLATE utf8_unicode_ci NOT NULL,
`lname` longtext COLLATE utf8_unicode_ci NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`transaction_id` bigint(11) NOT NULL,
`acc` longtext COLLATE utf8_unicode_ci NOT NULL,
`delivered` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
You need to make sure your id field is auto-incremented. If it is not auto-incremented, then you have to maintain a counter in your code, which is annoying. Changing it to auto-increment will allow you to put a 0 as the id, which will be interpreted as <the next available id>.
Hope this helps.
I'm working on a codeigniter project and I'm trying to troubleshoot a sql issue. I have a query that updates a date field in my table and it's not updating it at all.
I have this table
CREATE TABLE `Customer` (
`customer_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(55) NOT NULL DEFAULT '',
`last_name` varchar(55) NOT NULL DEFAULT '',
`city` varchar(255) DEFAULT '',
`state` char(2) DEFAULT '',
`zip` int(5) DEFAULT NULL,
`title` varchar(255) NOT NULL DEFAULT '',
`image` varchar(255) DEFAULT '',
`blurb` blob,
`end_date` date DEFAULT NULL,
`goal` int(11) DEFAULT NULL,
`paypal_acct_num` int(11) DEFAULT NULL,
`progress_bar` enum('full','half','none') DEFAULT NULL,
`page_views` int(11) NOT NULL DEFAULT '0',
`total_pages` int(11) NOT NULL DEFAULT '0',
`total_conversions` int(11) NOT NULL DEFAULT '0',
`total_given` int(11) NOT NULL DEFAULT '0',
`conversion_percentage` int(11) NOT NULL DEFAULT '0',
`avg_contribution` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`customer_id`)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
and when I run this query to insert data, it runs fine and sets the date to 2012-11-01
INSERT INTO `Customer` (`first_name`, `last_name`, `end_date`) VALUES ('John', 'Smith2', '2012-11-01');
Then I get the customer_id and try to run this query
UPDATE `Customer` SET `end_date` = '2012-14-01' WHERE `customer_id` = '18';
and it sets the date end_date field to 0000-00-00.
Why is it changing the end date to 0000-00-00 rather than 2012-14-01?
2012-14-01 is first day of fourteenth month :)
(so its invalid date, thus casted to 0000-00-00 and Data truncated for column 'end_date' at row 1 warning was returned by mysql, which you can see by querying SHOW WARNINGS to mysql immediately after badly behaving query)
2012-01-14 is 14th of January.
use this:
UPDATE `Customer` SET `end_date` = date('Y-m-d') WHERE `customer_id` = '18';
Use date function to update this field.