Error While Creating Table In MySQL - php

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

Related

How do i make my database auto-generate unique Certificate number that will also be auto-incremented for example 00001

I am creating a database that will store 'graduate details'. I have been able to set the ID to auto increment, Unique and also my primary key that start from 1...
I also want to make the certificate number field unique and auto-increment value in an increasing order that will start from 00001.
Please any help on how to go about this will be very appreciated. Thank you.
This is the MYSQL code for the database below;
CREATE TABLE IF NOT EXISTS `graduates` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`f_name` varchar(25) NOT NULL,
`l_name` varchar(25) NOT NULL,
`gender` varchar(6) DEFAULT NULL,
`address` varchar(100) DEFAULT NULL,
`city` varchar(15) DEFAULT NULL,
`region` varchar(30) DEFAULT NULL,
`phone` varchar(15) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`certificate_number` int(50) NOT NULL AUTO_INCREMENT,
`programme` varchar(50) CHARACTER SET utf8mb4 NOT NULL,
`marks` int(10) NOT NULL,
`college_name` varchar(50) CHARACTER SET utf8mb4 NOT NULL,
`date_of_birth` date DEFAULT NULL,
`created_by` int(10) UNSIGNED NOT NULL DEFAULT 0,
`created_at` date DEFAULT NULL,
`updated_by` int(10) UNSIGNED NOT NULL DEFAULT 0,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `certificate_number` (`certificate_number`)
) ENGINE=InnoDB AUTO_INCREMENT=90 DEFAULT CHARSET=utf8;

Want to populate sets of records from sales table by joining deliveries

I am a novice to MySQL
I have a database where I want to populate sets of records from 'sales' table where condition is sales is not delivered by comparing with 'deliveries' table of 'deliveries.reference_no'.
Both Tables has reference_no in common which is the invoice ref number.
I tried few SQL and got all common filed as it is and tried this one below but its displaying #1052 - Column 'date' in field list is ambiguous.
SELECT sales.id AS sid, date, reference_no, biller_name, customer_name, total_tax, total_tax2, total, internal_note FROM sales LEFT JOIN deliveries ON (sales.reference_no = deliveries.reference_no)
For more information below is my two table schema
Sales
CREATE TABLE IF NOT EXISTS `sales` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`reference_no` varchar(55) NOT NULL,
`warehouse_id` int(11) DEFAULT NULL,
`biller_id` int(11) NOT NULL,
`biller_name` varchar(55) NOT NULL,
`customer_id` int(11) NOT NULL,
`customer_name` varchar(55) NOT NULL,
`date` date NOT NULL,
`note` varchar(1000) DEFAULT NULL,
`internal_note` varchar(1000) DEFAULT NULL,
`inv_total` decimal(25,2) NOT NULL,
`total_tax` decimal(25,2) DEFAULT NULL,
`total` decimal(25,2) NOT NULL,
`invoice_type` int(11) DEFAULT NULL,
`in_type` varchar(55) DEFAULT NULL,
`total_tax2` decimal(25,2) DEFAULT NULL,
`tax_rate2_id` int(11) DEFAULT NULL,
`inv_discount` decimal(25,2) DEFAULT NULL,
`discount_id` int(11) DEFAULT NULL,
`user` varchar(255) DEFAULT NULL,
`updated_by` varchar(255) DEFAULT NULL,
`paid_by` varchar(55) DEFAULT 'cash',
`count` int(11) DEFAULT NULL,
`shipping` decimal(25,2) DEFAULT '0.00',
`pos` tinyint(4) NOT NULL DEFAULT '0',
`paid` decimal(25,2) DEFAULT NULL,
`cc_no` varchar(20) DEFAULT NULL,
`cc_holder` varchar(100) DEFAULT NULL,
`cheque_no` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
Deliveries
CREATE TABLE IF NOT EXISTS `deliveries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` date NOT NULL,
`time` varchar(10) NOT NULL,
`reference_no` varchar(55) NOT NULL,
`customer` varchar(55) NOT NULL,
`address` varchar(1000) NOT NULL,
`note` varchar(1000) DEFAULT NULL,
`user` varchar(255) DEFAULT NULL,
`updated_by` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
Currently I have a CI program and I am doing like this no Luck what I want to populate
CI PHP
function getdatatableajax()
{
if($this->input->get('search_term')) { $search_term = $this->input->get('search_term'); } else { $search_term = false;}
$this->load->library('datatables');
$this->datatables
->select("sales.id as sid, date, reference_no, biller_name, customer_name, total_tax, total_tax2, total, internal_note")
->from('sales');
$this->datatables->add_column("Actions",
"<center><a href='#' title='$2' class='tip' data-html='true'><i class='icon-folder-close'></i></a> <a href='#' onClick=\"MyWindow=window.open('index.php?module=sales&view=view_invoice&id=$1', 'MyWindow','toolbar=0,location=0,directories=0,status=0,menubar=yes,scrollbars=yes,resizable=yes,width=1000,height=600'); return false;\" title='".$this->lang->line("view_invoice")."' class='tip'><i class='icon-fullscreen'></i></a>
<a href='index.php?module=sales&view=add_delivery&id=$1' title='".$this->lang->line("add_delivery_order")."' class='tip'><i class='icon-road'></i></a>
<a href='index.php?module=sales&view=pdf&id=$1' title='".$this->lang->line("download_pdf")."' class='tip'><i class='icon-file'></i></a>
<a href='index.php?module=sales&view=email_invoice&id=$1' title='".$this->lang->line("email_invoice")."' class='tip'><i class='icon-envelope'></i></a>
</center>", "sid, internal_note")
->unset_column('sid')
->unset_column('internal_note');
echo $this->datatables->generate();
}
Wow, no one answered this yet? Strange.
Your error is because the date field is in both tables. You have to specify which table you want to select date from. So you must write either sales.date or deliveries.date. This goes with the rest of them, once you fix sales.date, your next field reference_no will generate the same error. If you want data from BOTH tables you'll need to assign aliases to them as you did with the first column.
sales.id AS sid, sales.date AS sales_date, deliveries.date AS deliveries_date etc...

MySQL Stored Procedure [Copy table 1 -> table 2]

My knowledge of MySQL/ SQL apart from the simple CRUD operations is basic.
If I had to use a stored procedure to move certain attributes (not in a specific order) to another table how could it be done?
These are the following tables. I want to move from the 1st to the 2nd table.
As you can see the datatype sizes are different for certain columns.
CREATE TABLE IF NOT EXISTS `source_cdr` (
`callstart` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`src` varchar(80) NOT NULL DEFAULT '',
`dst` varchar(80) NOT NULL DEFAULT '',
`accountcode` varchar(50) NOT NULL,
`uniqueid` varchar(100) NOT NULL,
`ID` int(11) NOT NULL AUTO_INCREMENT,
`callanswer` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`callend` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`disposition` varchar(50) NOT NULL,
`cdr_id` int(11) unsigned NOT NULL DEFAULT '0',
`pin_code` varchar(4) NOT NULL,
`provider` int(11) NOT NULL,
PRIMARY KEY (`ID`),
KEY `calldate_idx` (`callstart`) USING BTREE,
KEY `idx_acc_code_calldate` (`accountcode`,`callstart`) USING BTREE,
KEY `uniqueid` (`uniqueid`),
KEY `cdr_id` (`cdr_id`),
KEY `idx_uniqueid_cdr_id` (`uniqueid`,`cdr_id`)
) ENGINE=MyISAM;
--
CREATE TABLE IF NOT EXISTS `destination_cdr` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`calldate` datetime NOT NULL,
`source` varchar(80) NOT NULL,
`destination` varchar(80) NOT NULL,
`account_code` varchar(30) DEFAULT NULL,
`pincode` varchar(45) NOT NULL,
`duration_call` bigint(20) NOT NULL DEFAULT '0',
`duration_talk` bigint(20) NOT NULL,
`disposition` varchar(255) NOT NULL,
`clid` varchar(80) DEFAULT NULL,
`cdr_id` bigint(20) DEFAULT NULL,
`vxcdr_id` bigint(20) DEFAULT NULL,
`provider` int(11) NOT NULL DEFAULT '0'
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
EDIT 1
An example of a row
('2012-03-18 20:54:49', '5796', '0761100866', '103f0124ad510516f33cab132c0a695b', 'call-F1884808-6753-2F10-181C-3A#10.217.164.33', 308006367, '2012-03-18 20:55:05', '2012-03-18 20:55:51', '200 OK', 2, '', 0),
Thanks
You can use MySQL: INSERT ... SELECT Syntax to copy data from one table to the other.
Decide common fields in both and copy the same.
Example:
INSERT INTO TABLE2( COL1, COLx, ... ) SELECT colM, colY FROM TABLE1;
If the column sizes mismatch, data truncation takes place, and you can't overcome that but redefine the destination table columns.

trying to make a foreign key but MySql won't allow me, is there any way?

i have these 3 tables
CREATE TABLE IF NOT EXISTS `enrollment` (
`STUDENT_NUM` varchar(10) NOT NULL,
`SUBJECT_NUM` varchar(10) NOT NULL,
`UNITS` int(10) NOT NULL,
`DAYS` varchar(50) NOT NULL,
`TIME_START` time NOT NULL,
`TIME_END` time NOT NULL,
`ROOM_ID` int(11) DEFAULT NULL,
`PRELIM` float(10,2) DEFAULT NULL,
`MIDTERM` float(10,2) DEFAULT NULL,
`FINALS` float(10,2) DEFAULT NULL,
`FINAL_GRADE` float(10,2) DEFAULT NULL,
`SEMESTER` varchar(50) NOT NULL,
`SCHOOL_YEAR` varchar(50) NOT NULL,
`DATE_ADDED` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`STUDENT_NUM`,`SUBJECT_NUM`),
KEY `SUBJECT_NUM` (`SUBJECT_NUM`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `subjects` (
`SUBJECT_NUM` varchar(10) NOT NULL,
`EMPLOYEE_NUM` varchar(10) NOT NULL,
`SUBJECT_TITLE` varchar(100) DEFAULT NULL,
`DEPARTMENT` varchar(100) DEFAULT NULL,
`UNITS` int(10) NOT NULL,
`DAYS` varchar(50) NOT NULL,
`TIME_START` time NOT NULL,
`TIME_END` time NOT NULL,
`room_id` int(11) DEFAULT NULL,
`SEMESTER` varchar(50) NOT NULL,
`SCHOOL_YEAR` varchar(50) NOT NULL,
`COUNT` int(10) DEFAULT NULL,
`STATUS` varchar(50) DEFAULT NULL,
`FLAG` varchar(50) NOT NULL,
`DATE_ADDED` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`SUBJECT_NUM`),
UNIQUE KEY `SUBJECT_NUM` (`SUBJECT_NUM`),
KEY `EMPLOYEE_NUM` (`EMPLOYEE_NUM`),
KEY `EMPLOYEE_NUM_2` (`EMPLOYEE_NUM`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `room` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`room` varchar(255) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `room` (`room`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
what i am trying to do is make the field ROOM_ID from enrollment and subjects foreign key and the reference be the ID from room.. the ROOM_ID must not be unique...
i am getting this error
#1452 - Cannot add or update a child row: a foreign key constraint fails (`enrollmentdb`.`#sql-277_164`, CONSTRAINT `#sql-277_164_ibfk_3` FOREIGN KEY (`ROOM_ID`) REFERENCES `room` (`ID`))
when i am using this SQL command:
ALTER TABLE enrollment
ADD FOREIGN KEY (room_id)
REFERENCES room(ID)
Try below code if its working :
ALTER TABLE enrollment
ADD CONSTRAINT ROOM_ID_fk
FOREIGN KEY(ROOM_ID)
REFERENCES room(ID);
Thanks!
room_id is capitalised in your table. Try:
ALTER TABLE enrollment
ADD FOREIGN KEY (ROOM_ID)
REFERENCES room(ID)
replace 'room_id' with 'ROOM_ID'

Confusing mysql problem

I have got a problem with two tables:
CREATE TABLE IF NOT EXISTS `addresses` (
`adr_id` int(11) NOT NULL auto_increment,
`per_id` int(11) NOT NULL,
`adr_street` varchar(50) NOT NULL,
`adr_houseno` int(11) default NULL,
`adr_housenoadd` varchar(10) default NULL,
`adr_postalcode` varchar(25) NOT NULL,
`adr_city` varchar(20) NOT NULL,
`adr_type` varchar(45) default NULL,
`cnt_id` int(11) NOT NULL,
`adr_date` date default NULL,
`sys-mut-dt` timestamp NULL default NULL,
`sys-mut-user` varchar(20) default NULL,
`sys-mut-id` int(11) NOT NULL default '0',
PRIMARY KEY (`adr_id`),
KEY `per_id` (`per_id`),
KEY `cnt_id` (`cnt_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
--
-- RELATIES VOOR TABEL `addresses`:
-- `cnt_id`
-- `countries` -> `cnt_id`
-- `per_id`
-- `persons` -> `per_id`
--
CREATE TABLE `events` (
`evt_id` int(11) NOT NULL auto_increment,
`evt_name` varchar(50) NOT NULL,
`evt_description` varchar(100) default NULL,
`evt_startdate` date NOT NULL,
`evt_enddate` date default NULL,
`evt_starttime` time default NULL,
`evt_endtime` time default NULL,
`evt_amtpersons` int(11) default NULL,
`sts_id` int(11) NOT NULL,
`adr_id` int(11) default NULL,
`evt_amtPersonsSubs` tinyint(4) NOT NULL default '0',
`evt_photo` varchar(50) default NULL,
`sys-mut-dt` timestamp NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`sys-mut-user` varchar(20) default NULL,
`sys-mut-id` int(11) NOT NULL default '0',
PRIMARY KEY (`evt_id`),
KEY `sts_id` (`sts_id`),
KEY `adr_id` (`adr_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The problem is, when i try to delete a record from address table I get the following error
DELETE
FROM addresses
WHERE per_id = 45
1451 - Cannot delete or update a parent row: a foreign key constraint fails (`site/events`, CONSTRAINT `adr_id` FOREIGN KEY (`adr_id`) REFERENCES `addresses` (`adr_id`) ON DELETE NO ACTION ON UPDATE NO ACTION) ;
The weird thing is that there is no record with the same per_id or adr_id in the events table.
So what's going on over here?
How Can I solve this? ;-)
/* I think it means "How can I solve this?" */
It may sound stupid, but are you absolutely sure there are no child rows?
Could you please run this:
SELECT e.*
FROM addresses a
JOIN events e
ON e.adr_id = a.adr_id
WHERE a.per_id = 45

Categories