Is there any mysql query, to update a table and set numbers starting from 1?
For example, the table "item" has 100000 rows, the query would just update first row and set id ="1", next to 2, 3, 4 etc.
Try:
ALTER TABLE item MODIFY id INT PRIMARY KEY NOT NULL AUTO_INCREMENT
If you have id in your item table.
But it will show error on id column having duplicate values.
add auto increment column (INT) and it should do what you want
Is there already a primary key in the table? If not then create one with AUTO_INCREMENT.
This would do the job done.
ALTER TABLE `your_table`
ADD COLUMN `id_primary` int NOT NULL AUTO_INCREMENT FIRST ,
ADD PRIMARY KEY (`id_primary`);
Note: Changing an existing column to primary key AUTO_INCREMENT would raise error if that column contains duplicate values.
Given this
MariaDB [sandbox]> select * from posts;
+------+--------+----------+
| id | userid | category |
+------+--------+----------+
| NULL | 1 | a |
| NULL | 1 | b |
| NULL | 1 | c |
| NULL | 2 | a |
| NULL | 1 | a |
+------+--------+----------+
5 rows in set (0.00 sec)
This code
use sandbox;
update posts p,(select #rn:=0) rn
set id=(#rn:=#rn+1)
where 1 = 1;
Results in
MariaDB [sandbox]> select * from posts;
+------+--------+----------+
| id | userid | category |
+------+--------+----------+
| 1 | 1 | a |
| 2 | 1 | b |
| 3 | 1 | c |
| 4 | 2 | a |
| 5 | 1 | a |
+------+--------+----------+
5 rows in set (0.00 sec)
Related
PREPARATION
Consider this script to create a MySQL dummy-database:
CREATE SCHEMA `zzz_dummy` ;
CREATE TABLE `zzz_dummy`.`subtable2` (
`id` INT NOT NULL UNIQUE,
`col1` VARCHAR(45) NULL,
PRIMARY KEY (`id`));
CREATE TABLE `zzz_dummy`.`subtable1` (
`id` INT NOT NULL UNIQUE,
`ref_subtab2` INT NULL,
PRIMARY KEY (`id`));
CREATE TABLE `zzz_dummy`.`maintable` (
`id` INT NOT NULL UNIQUE,
`ref_subtab1` INT NULL,
PRIMARY KEY (`id`));
ALTER TABLE `zzz_dummy`.`maintable`
ADD INDEX `fk_subtab1_idx` (`ref_subtab1` ASC);
ALTER TABLE `zzz_dummy`.`maintable`
ADD CONSTRAINT `fk_subtab1`
FOREIGN KEY (`ref_subtab1`)
REFERENCES `zzz_dummy`.`subtable1` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
ALTER TABLE `zzz_dummy`.`subtable1`
ADD INDEX `fk_subtab2_idx` (`ref_subtab2` ASC);
ALTER TABLE `zzz_dummy`.`subtable1`
ADD CONSTRAINT `fk_subtab2`
FOREIGN KEY (`ref_subtab2`)
REFERENCES `zzz_dummy`.`subtable2` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
INSERT INTO zzz_dummy.subtable2 VALUES
(1,'ref_val_1'),
(2,'ref_val_2'),
(3,'no_ref');
INSERT INTO zzz_dummy.subtable1 VALUES
(1,'1'),
(2,'2'),
(3,'3');
INSERT INTO zzz_dummy.maintable VALUES
(1,'1'),
(2,'2'),
(3,'1'),
(4,'1'),
(5,'2'),
(6,'1');
This will produce the following tables and entries:
maintable:
+----+-------------+
| id | ref_subtab1 |
+----+-------------+
| 1 | 1 |
| 3 | 1 |
| 4 | 1 |
| 6 | 1 |
| 2 | 2 |
| 5 | 2 |
+----+-------------+
subtable1:
+----+-------------+
| id | ref_subtab2 |
+----+-------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+----+-------------+
subtable2:
+----+-----------+
| id | col1 |
+----+-----------+
| 1 | ref_val_1 |
| 2 | ref_val_2 |
| 3 | no_ref |
+----+-----------+
PROBLEM
As you can see, the column ref_subtab1 in maintable references id in subtable1, which column ref_subtab2 finally references id in subtable2. I want to select all rows in subtable2 that are indirectly referenced in aforementioned manner.
I have tried
SELECT subtable2.* FROM zzz_dummy.subtable2
INNER JOIN zzz_dummy.maintable
INNER JOIN zzz_dummy.subtable1
WHERE zzz_dummy.maintable.ref_subtab1=zzz_dummy.subtable1.id
AND zzz_dummy.subtable1.ref_subtab2=zzz_dummy.subtable2.id;
but this returns 6 results, one for every match in maintable:
+----+-----------+
| id | col1 |
+----+-----------+
| 1 | ref_val_1 |
| 1 | ref_val_1 |
| 1 | ref_val_1 |
| 1 | ref_val_1 |
| 2 | ref_val_2 |
| 2 | ref_val_2 |
+----+-----------+
I do not want redundant values, I would like it to return:
+----+-----------+
| id | col1 |
+----+-----------+
| 1 | ref_val_1 |
| 2 | ref_val_2 |
+----+-----------+
Can this be done efficiently with a MySQL statement?
As already commented use distinct to get the unique result combination and also move those conditions from WHERE clause to JOIN ON condition like
SELECT distinct subtable2.*
FROM zzz_dummy.subtable2
INNER JOIN zzz_dummy.subtable1
ON zzz_dummy.subtable1.ref_subtab2 = zzz_dummy.subtable2.id
INNER JOIN zzz_dummy.maintable
ON zzz_dummy.maintable.ref_subtab1 = zzz_dummy.subtable1.id;
I had two tables running in 2 different databases but the structure is identical. I want to import data of one table into the other but the id of the rows was autoincrement. This causes id's in both tables to have the same value but their content is different.
How do I insert the content of table1 into table 2 and auto update the id to a value that doesnt exist yet?
Because the table contains around 1000 rows I can't manually change the numbers or declare each individual row.
Something like ON DUPLICATE 'id' AUTO INCREMENT 'id'
?
this could be the way
Hitesh> desc test;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| name | varchar(200) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
Hitesh> desc test_new;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| name | varchar(200) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
Hitesh> insert into test_new(name) select name from test;
Query OK, 9 rows affected (0.03 sec)
Records: 9 Duplicates: 0 Warnings: 0
Hitesh> select * from test_new;
+-------------------------+----+
| name | id |
+-------------------------+----+
| i am the boss | 1 |
| You will get soon | 2 |
| Happy birthday bro | 3 |
| the beautiful girl | 4 |
| oyee its sunday | 5 |
| cat and dog in a park | 6 |
| dog and cat are playing | 7 |
| cat | 8 |
| dog | 9 |
+-------------------------+----+
9 rows in set (0.00 sec)
INSERT INTO new_db.new_tbl SELECT * FROM old_db.old_tbl;
Above will not generate new ids for new_tbl.
Let me explain it a little further, we consider you have both tables with id as auto increment enabled.
Override the auto increments
insert into B select * from A;
If you insert a value into new_tbl's (B) id column. i.e. if you select all columns, This will override the auto increment for the new table.
Activate the auto increment
insert into B (col1, col2) select col1, col2 from A;
insert into B select 0, col1, col2 from A;
If you want activate the auto increment on new_tbl (B) you can not pass ids to the insert stmnt, so you will need to skip the id (chose the columns you want to migrate without id column) or send DEFAULT/NULL/0 for the id.
I am having issues inserting Id fields from two tables into a single record in a third table.
mysql> describe ing_titles;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| ID_Title | int(10) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(64) | NO | UNI | NULL | |
+----------+------------------+------+-----+---------+----------------+
2 rows in set (0.22 sec)
mysql> describe ing_categories;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| ID_Category | int(10) unsigned | NO | PRI | NULL | auto_increment |
| category | varchar(64) | NO | UNI | NULL | |
+-------------+------------------+------+-----+---------+----------------+
2 rows in set (0.02 sec)
mysql> describe ing_title_categories;
+-------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------+----------------+
| ID_Title_Category | int(10) unsigned | NO | PRI | NULL | auto_increment |
| ID_Title | int(10) unsigned | NO | MUL | NULL | |
| ID_Category | int(10) unsigned | NO | MUL | NULL | |
+-------------------+------------------+------+-----+---------+----------------+
3 rows in set (0.04 sec)
Let's say the data from the tables is:
mysql> select * from ing_titles;
+----------+-------------------+
| ID_Title | title |
+----------+-------------------+
| 3 | Chicken |
| 2 | corn |
| 1 | Fettucini Alfredo |
+----------+-------------------+
3 rows in set (0.00 sec)
mysql> select * from ing_categories;
+-------------+----------+
| ID_Category | category |
+-------------+----------+
| 1 | Dinner |
| 3 | Meat |
| 2 | Veggie |
+-------------+----------+
3 rows in set (0.00 sec)
I want to insert into ing_title_categories the record "corn, Veggie" or where ID_Title = 2 and ID_Category = 2.
Here's what I tried:
INSERT INTO ing_title_categories (ID_Title, ID_Category)
SELECT ing_titles.ID_Title, ing_categories.ID_Category
FROM ing_title_categories
LEFT JOIN ing_titles ON ing_title_categories.ID_Title=ing_titles.ID_Title
LEFT JOIN ing_categories ON ing_title_categories.ID_Category=ing_categories.ID_Category
WHERE (ing_titles.ID_Title=2) AND (ing_categories.ID_Category = 2);
There is no data inserted into the table ing_title_categories, and here is the reply from MySQL:
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
What is the correct syntax for inserting the ing_titles.ID_Title and ing_categories.ID_Category into the table ing_titles_categories?
Please, no PHP or Python examples. Use SQL that I can copy and paste into the MySQL prompt. I will be adding this to a C++ program, not PHP, JavaScript or Python.
Edit 1:
The ing_title_categories.ID_Title and ing_title_categories.ID_Category are foreign keys into the other tables.
INSERT INTO
ing_title_categories (ID_Title, ID_Category)
SELECT
ing_titles.ID_Title, ing_categories.ID_Category
FROM
ing_titles, ing_categories
WHERE
ing_titles.ID_Title = ing_categories.ID_Category AND
ing_titles.ID_Title = 2 AND ing_categories.ID_Category = 2;
SQL Fiddle demo
After taking advice from #DrewPierce and #KaiserM11, here is the MySQL sequence:
mysql> INSERT INTO ing_title_categories (ID_Title, ID_Category)
-> SELECT
-> ing_titles.ID_Title,
-> ing_categories.ID_Category
-> FROM ing_titles, ing_categories
-> where (ing_titles.ID_Title = 2) AND (ing_categories.ID_Category = 2)
-> ;
Query OK, 1 row affected (0.07 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from ing_title_categories;
+-------------------+----------+-------------+
| ID_Title_Category | ID_Title | ID_Category |
+-------------------+----------+-------------+
| 17 | 2 | 2 |
+-------------------+----------+-------------+
1 row in set (0.00 sec)
In this case, only possible way I see is using a UNION query like
INSERT INTO ing_title_categories (ID_Title, ID_Category)
SELECT Title, NULL
FROM ing_title WHERE ID_Title = 2
UNION
SELECT NULL, category
FROM ing_categories
WHERE ID_Category = 2
(OR)
You can change your table design and use an AFTER INSERT trigger to perform the same in one go.
EDIT:
If you can change your table design to something like below (No need of that extra chaining table)
ing_titles(ID_Title int not null auto_increment PK, title varchar(64) not null);
ing_categories( ID_Category int not null auto_increment PK,
category varchar(64) not null,
ing_titles_ID_Title int not null,
FOREIGN KEY (ing_titles_ID_Title)
REFERENCES ing_titles(ID_Title));
Then you can use a AFTER INSERT trigger and do the insertion like
DELIMITER //
CREATE TRIGGER ing_titles_after_insert
AFTER INSERT
ON ing_titles FOR EACH ROW
BEGIN
-- Insert record into ing_categories table
INSERT INTO ing_categories
( category,
ing_titles_ID_Title)
VALUES
('Meat' NEW.ID_Title);
END; //
DELIMITER ;
mysql> select * from emp;
+-----+---------+------+------+------+
| eno | ename | dno | mgr | sal |
+-----+---------+------+------+------+
| 1 | rama | 1 | NULL | 2000 |
| 2 | kri | 1 | 1 | 3000 |
| 4 | kri | 1 | 2 | 3000 |
| 5 | bu | 1 | 2 | 2000 |
| 6 | bu | 1 | 1 | 2500 |
| 7 | raa | 2 | NULL | 2500 |
| 8 | rrr | 2 | 7 | 2500 |
| 9 | sita | 2 | 7 | 1500 |
| 10 | dlksdgj | 2 | 2 | 2000 |
| 11 | dlksdgj | 2 | 2 | 2000 |
| 12 | dlksdgj | 2 | 2 | 2000 |
| 13 | dlksdgj | 2 | 2 | 2000 |
| 14 | dlksdgj | 2 | 2 | 2000 |
+-----+---------+------+------+------+
Here is my table. I want to eliminate or prevent insertion of the duplicate records, as the eno field is auto increment total row never be duplicate, but the records are duplicates. How can I prevent inserting those duplicate records?
I tried using INSERT IGNORE AND ON DUPLICATE KEY UPDATE (I think I have not used them properly).
The way I used them is,
mysql> insert into emp(ename,dno,mgr,sal) values('dlksdgj',2,2,2000);
Query OK, 1 row affected (0.03 sec)
mysql> insert ignore into emp(ename,dno,mgr,sal) values('dlksdgj',2,2,2000);
Query OK, 1 row affected (0.03 sec)
mysql> insert into emp(ename,dno,mgr,sal) values('dlksdgj',2,2,2000) ON DUPLICATE KEY UPDATE eno=eno;
Query OK, 1 row affected (0.03 sec)
mysql> insert into emp(ename,dno,mgr,sal) values('dlksdgj',2,2,2000) ON DUPLICATE KEY UPDATE eno=eno;
Query OK, 1 row affected (0.04 sec
mysql> desc emp;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| eno | int(11) | NO | PRI | NULL | auto_increment |
| ename | varchar(50) | YES | | NULL | |
| dno | int(11) | YES | | NULL | |
| mgr | int(11) | YES | MUL | NULL | |
| sal | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
alter the table by adding UNIQUE constraint
ALTER TABLE employee ADD CONSTRAINT emp_unique UNIQUE (ename,dno,mgr,sal)
but you can do this if the table employee is empty.
or if records existed, try adding IGNORE
ALTER IGNORE TABLE employee ADD CONSTRAINT emp_unique UNIQUE (ename,dno,mgr,sal)
UPDATE 1
Something went wrong, I guess. You only need to add unique constraint on column ename since eno will always be unique due to AUTO_INCREMENT.
In order to add unique constraint, you need to do some cleanups on your table.
The queries below delete some duplicate records, and alters table by adding unique constraint on column ename.
DELETE a
FROM Employee a
LEFT JOIN
(
SELECT ename, MIN(eno) minEno
FROM Employee
GROUP BY ename
) b ON a.eno = b.minEno
WHERE b.minEno IS NULL;
ALTER TABLE employee ADD CONSTRAINT emp_unique UNIQUE (ename);
Here's a full demonstration
SQLFiddle Demo
Create a UNIQUE CONSTRAINT on which you think the duplicacy exist .
like
ALTER TABLE MYTABLE ADD CONSTRAINT constraint1 UNIQUE(column1, column2, column3)
This will work regardless of whether you clean up your table first (i.e. you can stop inserting duplicates immediately and clean up on separate schedule) and without having to add any unique constraints or altering table in any other way:
INSERT INTO
emp (ename, dno, mgr, sal)
SELECT
e.ename, 2, 2, 2000
FROM
(SELECT 'dlksdgj' AS ename) e
LEFT JOIN emp ON e.ename = emp.ename
WHERE
emp.ename IS NULL
The above query assumes you want to use ename as a "unique" field, but in the same way you could define any other fields or their combinations as unique for the purposes of this INSERT.
It works because it's an INSERT ... SELECT format where the SELECT part only produces a row (i.e. something to insert) if its left joined emp does not already have that value. Naturally, if you wanted to change which field(s) defined this "uniqueness" you would modify the SELECT and the LEFT JOIN accordingly.
I have a relational table that connects two other tables based on their IDs. There can be duplicates for both columns - but there CANNOT be the same row twice. I handle the checking code side.
How do I remove duplicate rows (see below):
select * from people:
a | b
1 2
1 3
1 3
1 7
2 3
2 5
2 5
2 9
I want the result to be:
a | b
1 2
1 3
1 7
2 3
2 5
2 9
This should work:
ALTER IGNORE TABLE people ADD UNIQUE (a,b);
If you don't want to add an index, then this should work:
DROP TABLE IF EXISTS people_old;
DROP TABLE IF EXISTS people_new;
CREATE TABLE people_new LIKE people;
INSERT INTO people_new SELECT DISTINCT * FROM people;
RENAME TABLE people TO people_old, people_new TO people;
There can be duplicates for both columns - but there CANNOT be the same row twice
That's a constraint on the table that you have not implemented. The constraint is a unique index on (a,b). If you had the index you would not have duplicates.
IMHO your best approach is to add the unique index to the table, using a temporary table to first remove the duplicates:
Copy person to person_temp
Delete all from person
Add unique index to person
Copy unique a,b from person_temp to `person.
This is how you can delete duplicate rows... I'll write you my example and you'll need to apply to your code. I have Actors table with ID and I want to delete the rows with repeated first_name
mysql> select actor_id, first_name from actor_2;
+----------+-------------+
| actor_id | first_name |
+----------+-------------+
| 1 | PENELOPE |
| 2 | NICK |
| 3 | ED |
....
| 199 | JULIA |
| 200 | THORA |
+----------+-------------+
200 rows in set (0.00 sec)
-Now I use a Variable called #a to get the ID if the next row have the same first_name(repeated, null if it's not).
mysql> select if(first_name=#a,actor_id,null) as first_names,#a:=first_name from actor_2 order by first_name;
+---------------+----------------+
| first_names | #a:=first_name |
+---------------+----------------+
| NULL | ADAM |
| 71 | ADAM |
| NULL | AL |
| NULL | ALAN |
| NULL | ALBERT |
| 125 | ALBERT |
| NULL | ALEC |
| NULL | ANGELA |
| 144 | ANGELA |
...
| NULL | WILL |
| NULL | WILLIAM |
| NULL | WOODY |
| 28 | WOODY |
| NULL | ZERO |
+---------------+----------------+
200 rows in set (0.00 sec)
-Now we can get only duplicates ID:
mysql> select first_names from (select if(first_name=#a,actor_id,null) as first_names,#a:=first_name from actor_2 order by first_name) as t1;
+-------------+
| first_names |
+-------------+
| NULL |
| 71 |
| NULL |
...
| 28 |
| NULL |
+-------------+
200 rows in set (0.00 sec)
-the Final Step, Lets DELETE!
mysql> delete from actor_2 where actor_id in (select first_names from (select if(first_name=#a,actor_id,null) as first_names,#a:=first_name from actor_2 order by first_name) as t1);
Query OK, 72 rows affected (0.01 sec)
-Now lets check our table:
mysql> select count(*) from actor_2 group by first_name;
+----------+
| count(*) |
+----------+
| 1 |
| 1 |
| 1 |
...
| 1 |
+----------+
128 rows in set (0.00 sec)
it works, if you have any question write me back