473,378 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 1274
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
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...
9
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...
7
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 ,...
4
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
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...
1
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...
10
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...
4
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...
12
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.