A MySql 5.3 table with 100K rows has a primary key.
There is also an integer column which is not part of the key. I would like to update this column to contain a unique number for the table. E.g. for the first record it should contain 1, for the second 2 etc.
This could as well be an auto-increment column, but MySql does not allow auto-increment on non-key columns. I don't want this column to be part of the key, because of the way it gets populated from a file etc.
So how such a query would look like?
I don't know why do you want to do something like this, but a possible solution is this:
set #rownum:=0;
update <table> set column = #rownum:=rownum+1 order by <field>
Related
Is it possible to write a query like the one below?
UPDATE sale SET sale_order='123456789' WHERE **COLUMN_1** = 2
where I don't explicitly pass the column name? Only its position?
I could get the column names but I am trying to avoid querying the database only to get them.
Thanks.
To answer your question, no, there is no syntax in SQL to reference the column by its position. This goes back to relational theory, in the sense that a table is a set of columns, and members of a set are unordered.
You will either have to know the column name, or else query it from the database:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA=SCHEMA() AND TABLE_NAME='sale'
AND ORDINAL_POSITION=1;
It looks like you are trying to design a query that updates a row by primary key, by assuming the first column is the primary key. The primary key isn't necessarily the first column. It isn't necessarily an integer. It isn't necessarily a single column.
So you are already making assumptions about the table definition. You might as well assume the primary key column is named id or some other convention.
This seems like it should be simple, but I couldn't figure out a way to do it. Let's say I have a table with 5,000 rows, each with an ID (primary key) of 1–5000. I am blindly inserting a new value with an existing ID, and it could be something like 2677. What I want to happen is that if the ID already exists, it will use the auto_increment value, in this case 5001. That or the maximum existing value + 1.
Most importantly, I can't use PHP (or anything else other than SQL) to do this, because the output is a query that needs to be directly importable without errors.
I have looked at two similar questions on SO:
Can you use aggregate values within ON DUPLICATE KEY
– the problem here is that they're selecting from an existing table which I can't do.
on duplicate key update with a condition? – the problem here is that I have no information on the table I'm importing to (except the basic structure), and don't know what the maximum value is.
INSERT INTO table (column1,column2) VALUES (1,2) ON DUPLICATE KEY UPDATE id=VALUES(id)
Obviously this requires an id column with AUTO_INCREMENT.
Moreover if you later need to select the inserted id just like if it was a new Insert, you do:
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(VALUES(id));
I have a MySQL table called settings. It has multiple columns, where every column is an item with a single value. So it has only one row and no id column. The design is final (I don't plan to add more columns).
How can I update the value in a single column (change one setting's value)?
What's the problem with using this --> http://dev.mysql.com/doc/refman/5.0/en/update.html
?
UPDATE table1 SET column1 = value
In case you have more than one row, you can add:
WHERE table1.column = matching_value;
making sure the match criteria is only the row you need.
to update a value of certain item column you must specify the row contains the value to be updated, since you don't have a primary key, you can depend on the nature of item values to act as row identifier and i don't recommend that.. the best way is to update your design and add column for the primary key
I'm trying to create a UNIQUE INDEX constraint for two columns, but only when another column contains the value 1. For example, column_1 and column_2 should be UNIQUE only when active = 1. Any rows that contain active = 0 can share values for column_1 and column_2 with another row, regardless of what the other row's value for active is. But rows where active = 1 cannot share values of column_1 or column_2 with another row that has active = 1.
What I mean by "share" is two rows having the same value(s) in the same column(s). Example: row1.a = row2.a AND row1.b = row2.b. Values would be shared only if both columns in row1 matched the other two columns in row2.
I hope I made myself clear. :\
You can try to make multi-column UNIQUE index with column_1, column_2 and active, and then set active=NULL for the rows where uniqueness not required. Alternatively, you can use triggers (see MySQL trigger syntax)
and check for each inserted/updated row if such values are already in the table - but I think it would be rather slow.
I'm trying to create a UNIQUE INDEX constraint for two columns, but only when another column contains the value 1
You can set the value of "another column" to a unique value that does not equal to 1. for example the id of a record.
Then the unique index constraint could be applied to all three columns including the "another column". Let's call the "another column" columnX.
Set the value of columnX to 1 if you want to apply the unique constraint to a record. Set the value of columnX to a unique value if you don't want to apply the unique constraint.
Then no extra work/triggers needed. The unique index to all three columns could solve your problem.
I am not sure about MySQL syntax, but it should have pretty much the same thing that SQL Server has:
CREATE UNIQUE INDEX [UNQ_Column1Column2OnActive_MyTable]
ON dbo.[MyTable]([column1,column2)
WHERE ([active] = 1);
This index will make sure if active=1 then column1 and column2 combination is unique across the table.
In SQL Server this could be accomplished with check constraints, however I do not know if MySQL supports anything similar.
What will work on any database, is that you can split the table in two. If the records where active =0 are just history records, and will never become active again, you could just move them to another table, and set a simple unique constraint on the original table.
I am not sure I understand you 100% but lets say you have a table that has a status column and you want to make sure there is only one raw with a status of 'A' (Active). You are OK with many rows with statuses of 'I' or 'Z' or anything else. Only one row is allowed with status of 'A'.
This will do the trick.
CREATE UNIQUE INDEX [Idx_EvalHeaderOnlyOneActive]
ON [dbo].[EvalHeader]([Hdr Status])
WHERE [Hdr Status] = 'A';
indexes are agnostic of external influences. This kind of constraint would have to be implemented outside your database.
Appears that MySQL's UNIQUE constraint is on a columns by column basis, I'm looking for a way to make sure the row are UNIQUE on a row by row basis; guessing the answer is to create a hash from by concatenating the columns per row I want to be UNIQUE then but a UNIQUE on the column storing the hash for that row. Also, the rows themselves unless I create a control will always be UNIQUE, since the ID for the row is a SURROGATE_KEY; meaning it's an integer sequential growing by +1 of the ID of the last row's integer.
You can create a multiple-column UNIQUE index.
The main ways to ensure a row is completely unique is to use the UNIQUE constraint on every column or create an analagous UNIQUE index on every column. If you can allow two columns to accept the same input but you need to uniquely distinguish them, there's no reason why you would need anything more than a primary key (basically an ID column). The other thing you could do is switch to a column-store database such as InfiniDB, which has a MySQL front-end, or MonetDB, both of which are open source alternatives.