Difference in behavior between two MySQL servers (Error 1292) - php

We use two servers (devsrv & qualifsrv), one for development and one for the qualification of our applications.
I have exactly the same Zend application on both servers (files and configuration of the application are the same).
I have a log table in which I insert the treatments carried out and the time required to achieve them.
When I insert a row into my table "Log" on the server devsrv, no problem.
When I execute exactly the same query in the table "Log" on the qualifsrv server, MySQL returns an error stating that in 1292 the value for the log_duration field is incorrect.
My log table :
CREATE TABLE `T_log` (
`log_id` int(11) NOT NULL AUTO_INCREMENT,
`log_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`log_priority` varchar(10) DEFAULT NULL,
`log_priority_name` varchar(20) DEFAULT NULL,
`log_event` varchar(255) DEFAULT NULL,
`log_commentaire` longtext,
`log_impacted_row` int(10) DEFAULT NULL,
`log_duration` time DEFAULT NULL,
`vag_id` int(11) DEFAULT NULL,
PRIMARY KEY (`log_id`),
KEY `idx_log_priority` (`log_priority`) USING BTREE,
KEY `idx_log_date` (`log_date`) USING BTREE,
KEY `idx_log_event` (`log_event`) USING BTREE,
KEY `idx_impacted_row` (`log_impacted_row`) USING BTREE,
KEY `fk_vag_id` (`vag_id`) USING BTREE,
CONSTRAINT `T_log_ibfk_1` FOREIGN KEY (`vag_id`) REFERENCES `T_vague` (`vag_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4479 DEFAULT CHARSET=utf8;
The query :
INSERT INTO `T_log` (
`log_priority`,
`log_priority_name`,
`log_event`,
`log_commentaire`,
`log_impacted_row`,
`log_duration`,
`vag_id`
)
VALUES
(
6,
"INFO",
"TEST",
"UPDATE",
87552,
"20s",
15
)
When I insert on devsrv :
Affected rows: 1
(log_duration value is : 00:00:20 in Log table)
When I insert on qualifsrv :
[Err] 1292 - Incorrect time value: '20s' for column 'log_duration' at row 1
Why this difference in behavior between the two servers ?

I found the difference. It is because of the globale variable sql_mode that was configured to not allow other value than the one expected by the field.
More informations here : http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html

Related

Sql query will give weird 'unrecognized data type (near key) error in mysql

I haven't worked with php and mysql for a long time and my return isn't so well-received by the platform.
I created tables using phpmyadmin and then I used the code below the generate scripts for my tables:
show create table recipes
I dropped the tables and then started to use the generated scripts to recreate my tables, only to see that they are giving me syntax errors. This is the generated script.
CREATE TABLE recipes (
 id int NOT NULL AUTO_INCREMENT,
 name varchar(50) NOT NULL,
 serving varchar(50) NOT NULL,
 description text NOT NULL,
 image_url varchar(100) NOT NULL,
 calories float DEFAULT NULL,
 tag varchar(100) NOT NULL,
 meal int NOT NULL,
 user_id int NOT NULL,
 PRIMARY KEY (id),
 KEY user_id (user_id),
 FULLTEXT KEY name_full_text (name),
 FULLTEXT KEY tag_full_text (tag),
 CONSTRAINT recipes_ibfk_1 FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
What I want from my table is that it use id as the primary key, user_id as the foreign key to the users table (which is already created), and I need two FULLTEXT indexes on two of the columns.
The strange thing is that I am getting syntax errors in lines 11, 12, and 13. With the main one being unrecognized data type. (near key). I am also getting a comma or a closing bracket was expected (near name_full_text). Then again,phpmyadmin` generated this query itself and I don't know why it is not accepting it.
Any Suggestions?

MYSQL table and column naming convention

I am curious to know what is best naming convention in terms of performance for mysql table names and column names. I am designing a new database for my project.
What I have used so far is use descriptive table/column names which sometimes seems long but I think it helps in easily understanding the use/function of a table.
For example see below DDL:
CREATE TABLE `product_configuration` (
`product_configuration_id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(20) NOT NULL,
`product_size_id` int(20) NOT NULL,
`product_color_id` int(20) NOT NULL,
`price` float NOT NULL,
`image` varchar(255) DEFAULT NULL,
`locked` tinyint(1) DEFAULT '0' COMMENT '1=locked, 0 =unlocked. if locked then this row can''t be deleted/updated',
`active` tinyint(1) DEFAULT '1' COMMENT '1=active, 0=inactive and wont display on frontend',
PRIMARY KEY (`product_configuration_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2342 DEFAULT CHARSET=latin1
And another DDL in which I use the primary key from above DDL as foreign key :
CREATE TABLE `product` (
`product_id` int(11) NOT NULL AUTO_INCREMENT,
`product_name` varchar(255) NOT NULL,
`product_description` varchar(255) NOT NULL,
`product_image` varchar(255) NOT NULL,
`price` float NOT NULL,
`active` tinyint(1) NOT NULL COMMENT '1=active, 0=inactive',
`date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`product_type_id` int(11) DEFAULT NULL,
`date_modified` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`product_id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=latin1
Basically I use singular table names with table name as prefix in most of the column names inside that table and I keep the same name and datatype for primary and foreign keys so that I can easily know which foreign key relates to which primary key/tables.
But I wonder, do using long table/column names have performance impact when database size grows. Like instead of just using "id" as primary key I am using long "product_configuration_id".
Also if I name tables/columns in uppercase and lowercase mixed like
"ProductConfiguration"
for table name and
"ProductConfigurationId"
for column name will that have any performance impact or linux/windows environment compatibility issue.
Long table and column names do not have (any significant) performance impact. All tables and column references are turned into internal locators during the compilation phase of the query. So pretty much the only impact is having to query a longer query string. The parsing part of query compilation is usually ignored from a performance perspective.
The following is opinion-based. As a general rule, I follow these conventions for naming:
Table names are in the plural, because they contain multiple entities.
Each table (almost always) has an auto-incremented numeric primary key, which is the singular form of the table followed by Id.
This column is the first column defined, so I can use order by 1 desc to get the most recent rows added to the table.
The table name is not (generally) part of the column name. I always (try to) use table aliases, so including the table name would be redundant.
Foreign key references use the same column name as the primary key they are referring to, when possible, so I can use using for joins.
I admit that these are "opinion-based", so the real answer to your question is in the first paragraph.

what is use of 'KEY' in mysql , it is not allowing me edit record in phpmyadmin

Here is my database mapping table definition,
you can try this, when I create this table and add some records in to it, it is not let me edit or delete the records by phpmyadmin although by query it should be possible,
CREATE TABLE IF NOT EXISTS `map2` (
`map_table_a` varchar(25) DEFAULT NULL,
`map_id_a` int(10) DEFAULT NULL,
`map_table_b` varchar(25) DEFAULT NULL,
`map_id_b` int(10) DEFAULT NULL,
KEY `map_table_b` (`map_table_b`,`map_id_b`),
KEY `map_table_a` (`map_table_a`,`map_id_a`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I don't know reason behind this behavior
Depending on your phpMyAdmin version, you should see this error message:
While you do have KEY columns, you have no PRIMARY or UNIQUE columns defined. This is why phpMyAdmin cannot edit your data - it has no way to be sure it is editing the correct row.
Suggested solution: Add the following into your table definition, preferably as the first column:
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY

Datetime and Timestamp Issue while creating MYSQL Table

I am trying to create mysql table in PhpMyAdmin in Hostgator server with the following information but it is showing error
Error : #1067 - Invalid default value for 'CreatedDate'
Table
CREATE TABLE `tbl_sample` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Domain` varchar(100) DEFAULT NULL,
`ClickUrl` varchar(600) DEFAULT NULL,
`CreatedDate` datetime NULL DEFAULT now(),
`ModifyDate` timestamp NULL DEFAULT NULL on update now(),
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The purpose of table is I need to enter only 'Domain' and 'ClickUrl' data using insert command remaining Id(autoincrement),CreatedDate(current date when inserting row),ModifyDate(when update the row) will automatically insert.
The above table is executed successfully in mysql environment in my local system but it is not executing in the mysql environment in hostgator
Check this link]1
Problem with creating Two column with timestamps. have to use trigger to get it done.

How to speed up the SELECT query of the following table?

I have a mysql table whose create code is as follows :
CREATE TABLE image_ref (
region VARCHAR(50) NULL DEFAULT NULL,
district VARCHAR(50) NULL DEFAULT NULL,
district_name VARCHAR(100) NULL DEFAULT NULL,
lot_no VARCHAR(10) NULL DEFAULT NULL,
sp_no VARCHAR(10) NULL DEFAULT NULL,
name VARCHAR(200) NULL DEFAULT NULL,
form_no VARCHAR(50) NOT NULL DEFAULT '',
imagename VARCHAR(50) NULL DEFAULT NULL,
updated_by VARCHAR(50) NULL DEFAULT NULL,
update_log DATETIME NULL DEFAULT NULL,
ip VARCHAR(50) NULL DEFAULT NULL,
imgfetchstat VARCHAR(1) NULL DEFAULT NULL,
PRIMARY KEY (form_no)
)
COLLATE='latin1_swedish_ci'
ENGINE=MyISAM;
This table contains approximately 7,00,000 number of rows. I have an application developed using PHP. Somewhere I need to run the following query :
SELECT
min(imagename) imagename
FROM
image_ref
WHERE
district_name = '$sess_district'
AND
lot_no = '$sess_lotno'
AND
imgfetchstat = '0';
which is taking on average 1.560 sec. The form_no field only has unique values. After some job is done with the result set fetched, the imgfetchstat is required to be updated with a value 1. Now my requirement is that, whether I should use InnoDB or MyISAM? Also, the application is accessed by around 50 numbers of users in LAN. Is there any way out to run the above query little bit faster? because the imagename fetched is being used to load an image of resolution 500 x 498 into the browser and the it is taking enough time to load the image. Thanks in advance.
You can add indexes to your table (be aware that this will make the storage larger - but given your query, you should be able to use the following:
ALTER TABLE `table` ADD INDEX `product_id` (`product_id`)
For more information see http://dev.mysql.com/doc/refman/5.0/en/create-index.html
You can add an index on a single column (which makes things nice for the DB, even it it uses a few of them on a query) but if you have a secific query that needs to REALLY run fast, you can add a multi-column index which is specific to your query:
ALTER TABLE image_ref ADD INDEX `someName`
(`district_name`, `lot_no`, `imgfetchstat`)

Categories