I am new to MySQL and taking an online community college course for work. I keep getting the message Error Code 1062 Duplicate entry '1' for key Primary My understanding would be that the Primary Key cannot be duplicated but it isn't so I am not sure what the issue is. I have tried changing the Primary keys but it just keeps coming back with the same message but a different primary key number.
Here is the script (part of it - there are alot more entries but they are all formatted the same):
Insert into pres_term values ( 1, '1789-07-01', '1797-03-04', 2, 'Did not seek re-election');
Insert into pres_term values ( 2, '1797-03-04', '1801-03-04', 1, 'Lost reelection');
Insert into pres_term values ( 3, '1801-03-04', '1809-03-04', 2, 'Did not seek reelection');
Insert into pres_term values ( 4, '1809-03-04', '1817-03-04', 2, 'Did not seek reelection');
Insert into pres_term values ( 5, '1817-03-04', '1825-03-04', 2, 'Did not seek reelection');
Insert into pres_term values ( 6, '1825-03-04', '1829-03-04', 1, 'Lost reelection');
Insert into pres_term values ( 7, '1829-03-04', '1837-03-04', 2, 'Did not seek reelection');
Insert into pres_term values ( 8, '1837-03-04', '1841-03-04', 2, 'Did not seek reelection');
Insert into pres_term values ( 9, '1841-03-04', '1841-04-04', 1, 'Died in office');
Related
I have 2 unique columns. and I want to put another value if I find a duplicate.
can i make it by adding select like this?
INSERT INTO `invoices` (`id_invoice`, `inv_code`, `inv_date`, `customer_id`, `outlet_id`, `marketing_source_id`, `consultant_id`, `therapist_id`, `staff_id`, `agent_id`, `expired_date`, `total`, `remaining`, `remarks`, `inv_remarks`, `status_inv`, `void_invoice`, `customer_sign`, `created_at`, `updated_at`)
VALUES
(4, 'HD-I-202002-0002', '2020-02-08', 2, 1, 3, 1, 3, 3, 0, '2021-02-08', 440000, 0, NULL, NULL, 1, 0, NULL, '2020-02-08 04:54:09', '2020-02-08 04:54:09'),
....
....
....
SELECT CONCAT(LEFT(`inv_code`, 12), LPAD(RIGHT(`inv_code`, 4)+1, 4 ,'0')) as inv_codes from invoices where id_invoice = id_invoice;
ON DUPLICATE KEY UPDATE
id_invoice=VALUES(id_invoice+1),
inv_code = VALUES(inv_codes)
;
I'm new to mysql, and can not figure out why this error keeps coming up. It's a simple table and I want id to be 1, 2, 3, 4 etc. alongside two other columns. Why does it keep reading, column count doesn't match value count at row 1?
CREATE DATABASE thedatabase;
USE thedatabase;
CREATE TABLE cars (
id INTEGER AUTO_INCREMENT,
model INTEGER NOT NULL,
mileage INTEGER NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO thedatabase.cars (
model,
mileage
) VALUES (
(45, 34598),
(22, 23847),
(10, 3847),
(487, 93229),
(237, 238975),
(23, 23987),
(34, 3498),
(57, 34984),
(56, 34983),
(20, 9845);
You have got an extra opening bracked in INSERT statement, after VALUES below should work fine:
INSERT INTO thedatabase.cars ( model,
mileage ) VALUES (45, 34598), (22, 23847), (10, 3847), (487, 93229), (237, 238975), (23, 23987), (34, 3498), (57, 34984), (56, 34983), (20, 9845);
Can't name it less confusing, sorry...
Imagine the DB table with 3 columns:
object_id - some entity,
relation_key - some property of the object,
bundle_id - we must generalize different objects with this id.
Table has unique key for [object_id, relation_key]:
single object can't have duplicated relation_key,
but different objects can have equal relation_key
Some oxygen understanding with the picture:
Plenty objects can have deep relations by relation_key, all this objects will be related with bundle_id
How can I update bundle_id column with correct values using just single query?
I can write procedure but this way is unsuitable for me.
I look for statement like:
"UPDATE example [join example ON ...] SET bundle_id = ... WHERE ..."
there is "before" schema for mysql:
CREATE TABLE `example` (
`bundle_id` INT(11) DEFAULT NULL,
`object_id` INT(11) NOT NULL,
`relation_key` INT(11) NOT NULL,
PRIMARY KEY (`object_id`,`relation_key`)
);
INSERT INTO `example`(`object_id`, `relation_key`)
VALUES (1, 4), (1, 5), (1, 6), (2, 6), (2, 7), (2, 8), (3, 4),
(3, 9), (3, 10), (4, 11), (4, 12), (4, 13), (5, 14), (5, 15), (5, 16), (6, 17), (6, 11), (6, 18);
Here is the example "before": fiddle example (sqlfiddle stuck for this moment)
And "after" will look like like if you do the queries :
UPDATE `example` SET `bundle_id` = 1 WHERE `object_id` IN (1, 2, 3);
UPDATE `example` SET `bundle_id` = 2 WHERE `object_id` IN (4, 6);
UPDATE `example` SET `bundle_id` = 3 WHERE `object_id` IN (5);
object1 related to object2 by key=6,
object3 related TO object1 by key=4,
so ... objs 1, 2, 3 are related together.
here must be first bundle_id=1.
there is no other keys linking another objects to 1, 2, 3
object_id=4 related to object_id=6 by key=11
so ... obj [4, 6] are related together.
here must be second bundle_id=2,
there is no other keys linking another objects to 4, 6
object_id=5 has no relations to other objects
all object's key belong to itself.
here must be second bundle_id=3,
there is no other keys linking another objects to 5
I'm trying to better wrap my head around PIVOT in MySQL. I made a test table and query in SQLfiddle but I'm not getting it to work. I get the error:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'PIVOT ( COUNT p.[stat] FOR p.[title] IN (p.[title], p.[weight],
p.[length]' at line 3
Table and data:
CREATE TABLE pivoter (
amount int,
title varchar(20),
weight int,
length int,
stat varchar(10)
);
INSERT INTO pivoter VALUES (5, 'Blip', 13, 5, 'hold');
INSERT INTO pivoter VALUES (2, 'Crog', 10, 5, 'full');
INSERT INTO pivoter VALUES (2, 'Gump', 22, 1, 'hold');
INSERT INTO pivoter VALUES (10, 'Yark', 7, 12, 'fun');
INSERT INTO pivoter VALUES (0, 'Blim', 6, 1, 'full');
INSERT INTO pivoter VALUES (3, 'Fron', 5, 8, 'hold');
INSERT INTO pivoter VALUES (8, 'Xand', 2, 7, 'fun');
INSERT INTO pivoter VALUES (2, 'Xnor', 7, 9, 'hold');
INSERT INTO pivoter VALUES (4, 'Rugg', 7, 8, 'fun');
INSERT INTO pivoter VALUES (9, 'Lint', 4, 10, 'fun');
INSERT INTO pivoter VALUES (9, 'Pawn', 7, 2, 'hold');
INSERT INTO pivoter VALUES (0, 'Undr', 12, 11, 'hold');
INSERT INTO pivoter VALUES (14, 'Call', 1, 6, 'full');
Pivot Query:
select stat, title, weight, length, amount from pivoter p
PIVOT
(
COUNT p.[stat]
FOR p.[title]
IN (p.[title], p.[weight], p.[length], p.[amount])
) AS testedPivot;
In case you were wondering, no, the data isn't supposed to make sense. I just made garbage to try this out.
Is the problem with my query or sqlfiddle?
If it's my query what can I do to make this work?
If the problem is sqlfiddle, do you know of an alternative?
The query in the question looks like T-SQL syntax for Microsoft SQL Server.
This syntax is not valid in MySQL, for a couple of reasons.
PIVOT is not a valid keyword in MySQL.
And MySQL uses backticks to escape identifiers, not square brackets.
I have a table from which I would like the update the value of a certain colum of fields.
Basicly moving one value down and those under it should inherit the previous value of the one about them.
I wonder if this action is possible using a single SQL query.
My table:
CREATE TABLE `menu` (
`slot_index` int(2) NOT NULL,
`language_ID` int(2) NOT NULL,
`menuItem` text NOT NULL,
PRIMARY KEY (`slot_index`,`language_ID`),
KEY `language_ID` (`language_ID`)
)
The content in it:
INSERT INTO `menu` (`slot_index`, `language_ID`, `menuItem`) VALUES
(1, 1, 'Home'),
(2, 1, 'Wie zijn wij'),
(21, 1, 'Missie'),
(22, 1, 'Doelen'),
(23, 1, 'Visie'),
(24, 1, 'Test'),
(3, 1, 'Wat doen wij'),
(31, 1, 'Medische kaart voor op reis'),
(32, 1, 'Huisartsenkaart'),
(33, 1, 'Huisartsenkaart anderstaligen'),
(4, 1, 'Perskamer'),
(5, 1, 'Beheer'),
(6, 1, 'FAQ'),
(7, 1, 'Ervaringen'),
(8, 1, 'Contact'),
(81, 1, 'Disclaimer'),
(9, 1, 'Links'),
(91, 1, 'Adresgegevens')
I would like to move slot_index 5 to 9, and make the fields under it move up inheriting the value from the field above.
Is this possible with a single query at all, or should I just write a script for this?
Thanks in advance.
Wolfert
Maybe using auto increment will help for future uses.
As for the current one I doubt that this can go with single query, because you cant query the table you are trying to update.