473,657 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

changing the info in a table with multiple index columns

I have a MySQL question about changing the info in a table:

I am using MySQL Server version: 3.23.58

I have a table called sign_region2 and it contains thousands of
references to signs and the regions they are in.

mysql> desc sign_region2;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| id_sign | int(11) | | PRI | 0 | |
| id_region | int(11) | | PRI | 0 | |
+-----------+---------+------+-----+---------+-------+

This table keeps track of which signs are in which regions. Note that
the Primary Key is a multiple-column index (id_sign and id_region).

mysql> select * from sign_region2 limit 20;
+---------+-----------+
| id_sign | id_region |
+---------+-----------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 1 | 5 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
| 2 | 4 |
| 3 | 5 |
| 4 | 5 |
| 5 | 5 |
| 6 | 5 |
| 7 | 5 |
| 8 | 2 |
| 10 | 2 |
| 11 | 1 |
| 11 | 2 |
| 11 | 3 |
| 11 | 4 |
+---------+-----------+
20 rows in set (0.00 sec)

I need to take all "id_region = 4" and make them "id_region = 2"
except where "id_region = 2" already exists. In that case I'd just
need to delete where "id_region = 4".

Remember that there are thousands of references in this table. Some
signs are in region 2, some are in region 4, some both, some neither.
What query would I use to accomplish this?

The problem is that the DB won't allow identical instances of the
id_sign and id_region combinations (because they must be unique
because they are primary keys).

An example:
Let's say I've got a table that looks like this...
+---------+-----------+
| id_sign | id_region |
+---------+-----------+
| 12 | 1 |
| 12 | 3 |
| 12 | 4 |
| 12 | 5 |
+---------+-----------+

But I need it to look like this...
+---------+-----------+
| id_sign | id_region |
+---------+-----------+
| 12 | 1 |
| 12 | 2 |
| 12 | 3 |
| 12 | 5 |
+---------+-----------+

But for thousands of records not just the ones with "id_sign = 12".
Jul 23 '05 #1
1 1285
Tony W. wrote:
I need to take all "id_region = 4" and make them "id_region = 2"
except where "id_region = 2" already exists. In that case I'd just
need to delete where "id_region = 4".

The problem is that the DB won't allow identical instances of the
id_sign and id_region combinations (because they must be unique
because they are primary keys).


Probably the simplest solution is to do it in two steps:

insert ignore into sign_region2 (id_sign, id_region)
select id_sign, 2 from sign_region2 where id_region = 4;
delete from sign_region2 where id_region = 4;

The "ignore" keyword is a MySQL extension to SQL. It allows the insert
operation to treat key violations as warnings instead of fatal errors.
So the row inserts that aren't collisions succeed, even though those
that would result in collision fail. See the docs on INSERT for more
details.

Regards,
Bill K.
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2888
by: Margaret MacDonald | last post by:
It appears that the 'table.field' dot notation is not preserved by php's mysql library. I can create some join 'SELECT a.id, b.id FROM table1 AS a, table2 AS b' but when I try to refer to the fields as 'a.id' and 'b.id' the reference fails because the table aliases have been discarded from the dataset as managed by php. 'a.id' has become merely 'id', and 'b.id' no longer has a fieldname -- the only way to refer to it is by the numeric...
9
1871
by: elyob | last post by:
Hi, I'm looking at storing snippets of details in MySQL about what credit cards a business excepts. Rather than have a whole column for Visa, another for Amex etc ... I am looking at having a column called payment types and inserting multiple codes ... e.g. ViAmBcCa Is this a good way of doing things? To me it'd be a lot cleaner and limit amount of Db work to be done. Is this a sensible way in your opinion? What's the best way of...
7
10801
by: Bing Wu | last post by:
Hi Folks, I have a very large table containing 170 million rows of coordinats: CREATE TABLE "DB2ADMIN"."COORDINATE" ( "FID" INTEGER NOT NULL , "AID" INTEGER NOT NULL , "X" REAL NOT NULL , "Y" REAL NOT NULL , "Z" REAL NOT NULL )
4
15819
by: maricel | last post by:
I have the following base table structure - DDL: CREATE TABLE "ADMINISTRATOR"."T1" ( "C1" INTEGER NOT NULL ) IN "TEST_TS" ; ALTER TABLE "ADMINISTRATOR"."T1" ADD PRIMARY KEY
33
2478
by: Geoff Jones | last post by:
Hiya I have a DataTable containing thousands of records. Each record has a primary key field called "ID" and another field called "PRODUCT" I want to retrieve the rows that satisy the following criteria: I have a list of about 100 numbers which correspond to the ID field and also another 40 say numbers corresponding to the numbers in the PRODUCT field. I want to show the rows that correspond to both these criteria.
1
3433
by: B | last post by:
Hello All, This is my first time using this list, but hopefully I got the right one for the question I need to ask :). I have a table which has about 4 million records. When I do a search (as I will explain below) it takes about 1.35 secs to get me back what I am looking for. Since I am doing multiple types of these searches, the total time goes in minutes, therefore, I am trying to see if I can get any help in reducing this time...
10
3554
by: shsandeep | last post by:
DB2 V8.2 (not Viper yet and no range partitioning!!) I have created a table T1 (col1, col2) with col1 as the primary key. When I try to create a partitioning key on col2, it gives me error that it should have all primary keys included. So, I created table T1 again with col2 as the partitioning key. Now, I do not have col1 as the primary key. When I try to create col1 as the primary key, I get the following error: 1 The primary key, each...
4
3712
by: Hemant Shah | last post by:
Folks, Our client has a program that browses whole table from begining to end. The table has 1 million rows in it. REORGCHK does not show any problems. It has unique index defined on KEY0 column. If I use SELECT statement without OPTIMIZE FOR clause, then it uses temporary table to sort the data, but if I use OPTIMIZE clause then it uses index access without temporary table. If I use OPTIMIZE FOR more than 700 rows then it uses...
12
2439
by: Michel Esber | last post by:
Hello, Db2 Linux LUW FP 15. Consider table A (ID varchar, EXECUTION_DATE date). a) I want to first retrieve all IDs that have not executed during the last 90 days: select distinct ID from table_B where NOT EXISTS
0
8403
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8833
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8737
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8610
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.