Duplicate entry '2147483647' for key 'PRIMARY' - php

Based on what I've read here and on the web, you get this error if you try to store an integer > INT's maximum signed value; 2147483647. As you can see below, I used bigint(20) for user_id. So, I don't know why I'm getting this error.
CREATE TABLE IF NOT EXISTS `users` (
`user_id` bigint(20) unsigned NOT NULL,
`screen_name` varchar(20) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`profile_image_url` varchar(200) DEFAULT NULL,
`created_at` datetime NOT NULL,
`followers_count` int(10) unsigned DEFAULT NULL,
`friends_count` int(10) unsigned DEFAULT NULL,
`statuses_count` int(10) unsigned DEFAULT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`user_id`),
KEY `user_name` (`name`),
KEY `last_update` (`last_update`),
KEY `screen_name` (`screen_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
The php script that handles the insertion:
/* Prepare an insert statement */
$query = "INSERT INTO users (user_id, screen_name, name, profile_image_url, created_at, followers_count, friends_count, statuses_count) VALUES (?,?,?,?,?,?,?,?)";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("issssiii", $user_id, $screen_name, $name, $profile_image_url, $created_at, $followers_count, $friends_count, $statuses_count);
/* Execute the statement */
$stmt->execute()
FYI: the purpose of this script & table is to store some of Twitter's users data

INT has a maximum signed value of 2147483647. Any number greater than that will be truncated to that value. What you need to do is change that column to be a user_id varchar(20) unsigned NOT NULL, which will hold strings of up to 20 characters since cards numbers are strings and not actually numbers (you don't do math with the). You also should remove AUTO_INCREMENT as that is not a value you will be incrementing.

Related

#1062 - Duplicate entry

I am working on a project of my own but I have an issue.
I created a simple table with the query:
$sql_art = "CREATE TABLE articoli (
id INT(3) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
cod_int VARCHAR(6) NOT NULL,
nome VARCHAR(50) NOT NULL,
descr VARCHAR(100) NULL,
cod_barre INT(20) NOT NULL,
prezzo INT(5) NULL,
note VARCHAR(100) NULL,
reg_date TIMESTAMP,
UNIQUE (cod_barre)
)";
when I try to add a new record with
$sql_ins = "INSERT INTO `gestionale_db`.`articoli`
(`id`, `cod_int`, `nome`, `descr`, `cod_barre`, `prezzo`, `note`, `reg_date`)
VALUES
(NULL, 'A001', 'Cazzilli', 'ancora caldi', '4545415615456',
'215', 'su tisi', CURRENT_TIMESTAMP)";
it return the error
#1062 - Duplicate entry '2147483647' for key 'cod_barre'
but obviously cod_barre is not equal to this value.
If I try to modify the record with UPDATE, it modify all the value except cod_barre that remain the same.
By default in php.ini I use InnoDB es ENGINE and latin1_swedish_ci as char_set.
Can anyone help me to find the error?
In your case you go to INT maximum 2147483647 = 2^{31}-1, because 4545415615456 is greater then 2147483647.
So you really insert row with max int value.
You need to create this table to fix your error:
$sql_art = "CREATE TABLE articoli (
id INT(3) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
cod_int VARCHAR(6) NOT NULL,
nome VARCHAR(50) NOT NULL,
descr VARCHAR(100) NULL,
cod_barre BIGINT(20) NOT NULL,
prezzo INT(5) NULL,
note VARCHAR(100) NULL,
reg_date TIMESTAMP,
UNIQUE (cod_barre)
)";

mysql unique for multiple columns

When payment happen, sometimes its captured double entry in table.
I want to ignore double entry capture so i want to insert records when these created, user_id, amount fields should be unique.
How do i make it ? Below is my table.
CREATE TABLE `transactions` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`user_id` int(20) NOT NULL,
`project_id` int(20) DEFAULT NULL,
`foreign_id` int(20) NOT NULL,
`class` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`transaction_type_id` int(20) DEFAULT NULL,
`amount` float(10,2) NOT NULL,
`description` text COLLATE utf8_unicode_ci,
`payment_gateway_id` int(20) DEFAULT NULL,
`gateway_fees` float(10,2) NOT NULL,
`is_old` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=266 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
To strictly answer your question, you create a unique composite key on the combination of those 3 columns. That way no two rows can exist with a combination of the 3 of them in the composite index.
CREATE TABLE `transactions2` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`user_id` int(20) NOT NULL,
`project_id` int(20) DEFAULT NULL,
`foreign_id` int(20) NOT NULL,
`class` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`transaction_type_id` int(20) DEFAULT NULL,
`amount` float(10,2) NOT NULL,
`description` text COLLATE utf8_unicode_ci,
`payment_gateway_id` int(20) DEFAULT NULL,
`gateway_fees` float(10,2) NOT NULL,
`is_old` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
unique key(created,user_id,amount) -- <------------------- right here
);
insert some data to test it:
insert transactions2 (created,modified,user_id,project_id,foreign_id,class,
transaction_type_id,amount,description,payment_gateway_id,gateway_fees,is_old) values
('2009-01-01 12:00:00','2009-01-01 12:00:00',666,1,1,'a',1,100,'desc',1,12,1);
-- inserts fine (above)
Try it again with exactly the same data:
insert transactions2 (created,modified,user_id,project_id,foreign_id,class,
transaction_type_id,amount,description,payment_gateway_id,gateway_fees,is_old) values
('2009-01-01 12:00:00','2009-01-01 12:00:00',666,1,1,'a',1,100,'desc',1,12,1);
-- error 1062: Duplicate entry
-- change it barely:
insert transactions2 (created,modified,user_id,project_id,foreign_id,class,
transaction_type_id,amount,description,payment_gateway_id,gateway_fees,is_old) values
('2009-01-01 13:00:00','2009-01-01 12:00:00',666,1,1,'a',1,100,'desc',1,12,1);
-- inserts fine
Also, use ENGINE=INNODB. Read about that Here.
Please read Mysql multi column indexes a.k.a. composite indexes.
Lastly, the concept of what you are talking about is not far off from Insert on Duplicate Key Update. Just throwing that reference out there for you.
You can achieve the same using ,handling at the time of inserting,
You can try WHERE NOT EXISTS with INSERT.
Something like this.(You need to specify column name who has NOT NULL constraint,i missed all those columns)
INSERT INTO table_name(`created`,`user_id`,`amount`) VALUES =
'$created','$user_id','$amount'
WHERE NOT EXISTS
(SELECT *
FROM table_name
WHERE created ='$created' AND user_id='$user_id' AND amount='$amount')
Hope this helps.

MySQL insert statement odd behaviour

I am trying to write insert a row into a database with a column that is a composite of other columns to help with searching. Here is my PHP code:
$compose=$house." ".$street." ".$district." ".$posttown." ".$county." ".$postcode;
$sql=sprintf("INSERT INTO Address (House,Street,District,PostTown,County,PostCode,Active,Composite) VALUES ('%s','%s','%s','%s','%s','%s',%s,'%s')",
$house,
$street,
$district,
$posttown,
$county,
$postcode,
$binactive,
$compose);
(I know I should be using prepared statements, that is part of the next refactoring)
The statement executes without errors but the Composite column is being evaluated as a number. That is, if $house starts with a number that number is inserted, if $house is a name then 0 is inserted.
$compose is being concatenated properly.
The column is definitely a varchar. Here is the table create script:
CREATE TABLE `Address` (
`idAddress` int(11) NOT NULL AUTO_INCREMENT,
`House` varchar(45) NOT NULL,
`Street` varchar(45) NOT NULL,
`District` varchar(45) DEFAULT NULL,
`PostTown` varchar(45) NOT NULL,
`County` varchar(45) NOT NULL,
`PostCode` varchar(45) NOT NULL,
`Composite` varchar(1000) NOT NULL,
`Active` binary(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`idAddress`),
UNIQUE KEY `idAddress_UNIQUE` (`idAddress`)
) ENGINE=InnoDB AUTO_INCREMENT=46 DEFAULT CHARSET=latin1;
So...where am I going wrong?

MySql Properly Join Complex Data/Tables

Abstract:
Every client is given a specific xml ad feed (publisher_feed table). Everytime there is a query or a click on that feed, it gets recorded (publisher_stats_raw table) (Each query/click will have multiple rows depending on the subid passed by the client (We can sum the clicks together)). The next day, we pull stats from an API to grab the previous days revenue numbers (rev_stats table) (Each revenue stat might have multiple rows depending on the country of the click (We can sum the revenue together)). Been having a hard time trying to link together these three tables to find the average RPC for each client for the previous day.
Table Structure:
CREATE TABLE `publisher_feed` (
`publisher_feed_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`alias` varchar(45) DEFAULT NULL,
`user_id` int(10) unsigned DEFAULT NULL,
`remote_feed_id` int(10) unsigned DEFAULT NULL,
`subid` varchar(255) DEFAULT '',
`requirement` enum('tq','tier2','ron','cpv','tos1','tos2','tos3','pv1','pv2','pv3','ar','ht') DEFAULT NULL,
`status` enum('enabled','disabled') DEFAULT 'enabled',
`tq` decimal(4,2) DEFAULT '0.00',
`clicklimit` int(11) DEFAULT '0',
`prev_rpc` decimal(20,10) DEFAULT '0.0000000000',
PRIMARY KEY (`publisher_feed_id`),
UNIQUE KEY `alias_UNIQUE` (`alias`),
KEY `publisher_feed_idx` (`remote_feed_id`),
KEY `publisher_feed_user` (`user_id`),
CONSTRAINT `publisher_feed_feed` FOREIGN KEY (`remote_feed_id`) REFERENCES `remote_feed` (`remote_feed_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `publisher_feed_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=124 DEFAULT CHARSET=latin1$$
CREATE TABLE `publisher_stats_raw` (
`publisher_stats_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`unique_data` varchar(350) NOT NULL,
`publisher_feed_id` int(10) unsigned DEFAULT NULL,
`date` date DEFAULT NULL,
`subid` varchar(255) DEFAULT NULL,
`queries` int(10) unsigned DEFAULT '0',
`impressions` int(10) unsigned DEFAULT '0',
`clicks` int(10) unsigned DEFAULT '0',
`filtered` int(10) unsigned DEFAULT '0',
`revenue` decimal(20,10) unsigned DEFAULT '0.0000000000',
PRIMARY KEY (`publisher_stats_id`),
UNIQUE KEY `unique_data_UNIQUE` (`unique_data`),
KEY `publisher_stats_raw_remote_feed_idx` (`publisher_feed_id`)
) ENGINE=InnoDB AUTO_INCREMENT=472 DEFAULT CHARSET=latin1$$
CREATE TABLE `rev_stats` (
`rev_stats_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`date` date DEFAULT NULL,
`remote_feed_id` int(10) unsigned DEFAULT NULL,
`typetag` varchar(255) DEFAULT NULL,
`subid` varchar(255) DEFAULT NULL,
`country` varchar(2) DEFAULT NULL,
`revenue` decimal(20,10) DEFAULT NULL,
`tq` decimal(4,2) DEFAULT NULL,
`finalized` int(11) DEFAULT '0',
PRIMARY KEY (`rev_stats_id`),
KEY `rev_stats_remote_feed_idx` (`remote_feed_id`),
CONSTRAINT `rev_stats_remote_feed` FOREIGN KEY (`remote_feed_id`) REFERENCES `remote_feed` (`remote_feed_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=58 DEFAULT CHARSET=latin1$$
Context:
Each remote_feed has a specific subid/typetag given to it. So we need to match up the both the remote_feed_id and the subid columsn from the publisher_feed table to the remote_feed_id and typetag columns in the revenue stats table.
My current, non working, implementation:
SELECT
pf.publisher_feed_id, psr.date, sum(clicks), sum(rs.revenue)
FROM
xml_network.publisher_feed pf
JOIN
xml_network.publisher_stats_raw psr
ON
psr.publisher_feed_id = pf.publisher_feed_id
JOIN
xml_network.rev_stats rs
ON
rs.remote_feed_id = pf.remote_feed_id
WHERE
pf.requirement = 'tq'
AND
pf.subid = rs.typetag
AND
psr.date <> date(curdate())
GROUP BY
psr.date
ORDER BY
psr.date DESC
LIMIT 1;
The above keeps pulling the wrong data out of the rev_stats table (pulls the sum of the correct stats, but repeats it over because of a join). Any help with how I would be able to properly pull the correct data would be greatly helpful ( I could use multiple queries and PHP to get the correct results, but what's the fun in that!)
Figured out a way to get this accomplished. Its def not a fast method by any means, needing 4 selects to get it done, but it works flawlessly =)
SELECT
pf.publisher_feed_id,
round(
(
SELECT
SUM(rs.revenue)
FROM
xml_network.rev_stats rs
WHERE
rs.remote_feed_id = pf.remote_feed_id
AND
rs.typetag = pf.subid
AND
rs.date = subdate(current_date, 1)
),10)as revenue,
(
SELECT
MAX(rs.tq)
FROM
xml_network.rev_stats rs
WHERE
rs.remote_feed_id = pf.remote_feed_id
AND
rs.typetag = pf.subid
AND
rs.date = subdate(current_date, 1)
) as tq,
(
SELECT
SUM(psr.clicks)-SUM(psr.filtered)
FROM
xml_network.publisher_stats_raw psr
WHERE
psr.publisher_feed_id = pf.publisher_feed_id
AND
psr.date = subdate(current_date, 1)
) as clicks
FROM
xml_network.publisher_feed pf
WHERE
pf.requirement = 'tq';

Partitioning of table in mysql for MYISAM eninge based on year

I am trying to partition the table based on the year range.
but i'm getting the following error "#1503 - A PRIMARY KEY must include all columns in the table's partitioning function".
Following is my query
CREATE TABLE IF NOT EXISTS `t2123` (
`j_id` int(11) NOT NULL AUTO_INCREMENT,
`u_id` int(11) NOT NULL,
`w_id` int(5) NOT NULL,
`p_id` int(5) NOT NULL,
`s_p_id` int(5) NOT NULL,
`p_t` tinyint(3) unsigned NOT NULL,
`a_n` int(5) NOT NULL,
`type` enum('GP','NGP','PGP','PAGP') NOT NULL,
`p_p` int(11) NOT NULL,
`s_p` int(11) NOT NULL,
`p_c` float NOT NULL,
`s_c` float NOT NULL,
`s_p_p_c` float NOT NULL DEFAULT '0',
`g_d` date NOT NULL,
`datetimes` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
`c_c` double DEFAULT '0',
`c_m` double DEFAULT '0',
`c_y` double DEFAULT '0',
`c_k` double DEFAULT '0' ,
`c_total` double DEFAULT '0' ,
`p_a` float DEFAULT '0',
PRIMARY KEY (`j_id`),
UNIQUE KEY(j_id, g_d),
KEY `u_id` (`u_id`),
KEY `w_id` (`w_id`),
KEY `p_id` (`p_id`),
KEY `s_p_id` (`s_p_id`),
KEY `a_n` (`a_n`),
KEY `g_d` (`g_d`)
) engine=myisam PARTITION BY RANGE (j_id + year(g_d)) (
PARTITION t2_2012 VALUES LESS THAN (2012),
PARTITION t2_2013 VALUES LESS THAN (2013)
);
Please suggest me how can I create the partition?
As the error message advises:
A PRIMARY KEY must include all columns in the table's partitioning function
Your partitioning function refers to j_id and g_d while your primary key only covers j_id.
By the way, your UNIQUE constraint is useless (UNIQUE KEY(j_id, g_d)) because j_id is already unique by definition (it is primary key).
In fact you probably want your primary key on (j_id, g_d)

Categories