We are going to change something that we have an enum to an id of a linked table instead.
How would we go about that?
Our current enum name: strat
Our new linked name: stratid
What I was thinking was something along the lines of:
UPDATE table_name
SET stratid = (SELECT id FROM link_table WHERE stratname = table_name.strat);
I have not created the link table yet, right now it is all theory.
Will the above work?
Is there anything I should change in order to transfer from an enum to the linked table?
No gotchas for making the update other than that you need want to make triple sure that your link_table.ids are populated strictly in the order that table_name.strat options are defined.
For example, if strat is enum('FOO', 'BAR') then in linked_table the record with id == 1 should be the "FOO" record.
After that, you 'd perhaps want to make the stratid column NON NULL; this is not strictly equivalent to your previous arrangement, but it would probably be closer to what you want.
Yes, create the link table first,
set the stratname as unique,
use an auto increment ID
A lazy solution for insert link_table:
insert into link_table
select distinct strat from table_name order by strat;
However, I not sure is all the predefined enum is being used.
Also, without knowing size of the enum,
I can't suggest you do a manual insert.
If you look at enum ...
enum('...', '...', ...) <-- is just a comma separated value
So, here is the query to get the CSV :-
select column_type from information_schema.columns
where schema_name="table_name" and column_name = "strat";
You can combine with an programming language to do the link_table insertion.
Lastly, you UPDATE query is not very optimize, you can switch to use INNER JOIN.
But I assume is one-time job, so be it!
Related
I'm looking to create a new column with values that correspond with keywords in an existing column in mysql.
I'm trying to determine if a practice is private or public based on keywords in their establishment's name.
In plain english what i'm trying to get is a new column called Practice and insert value private if keywords are specialist, gleneagles.
Would appreciate any help on this matter.
If I understand what you're asking, you want to add a column to an existing table and then set that column's value (for each row) based on another column's value in that same row?
First, to create the column named Practice you can use MySQL's ALTER TABLE command:
ALTER TABLE YourTable ADD Practice VARCHAR(20);
I'm not sure what type you are aiming for here, so I'm just going with VARCHAR(20); you may also benefit from an enum based on the values you're using (or even a foreign-key to a lookup table).
After you've created your column, you can set its value:
UPDATE YourTable
SET Practice = CASE
WHEN SomeColumn = 'specialist' THEN
'private'
WHEN SomeColumn = 'gleneagles' THEN
'private'
ELSE
'public'
END
If you don't want records that don't match to be set, simply drop the ELSE 'public' and they'll be defaulted to null.
UPDATE (to select with wildcards)
If you need to update rows with the related data being contained within larger text and you need wildcards, you can use MySQL's LIKE operator:
UPDATE YourTable
SET Practice = CASE
WHEN SomeColumn LIKE '%specialist%' THEN
'private'
WHEN SomeColumn LIKE '%gleneagles%' THEN
'private'
ELSE
'public'
END
Alternatively, if you're only going to update to a single-value based on text containing multiple values, you can use a Regular Expression (via REXEXP) instead:
UPDATE YourTable
SET Practice = CASE
WHEN SomeColumn REGEXP 'specialist|gleneagles' THEN
'private'
ELSE
'public'
END
You are asking about denormalization, which means storing derived data. My initial reaction would be to not add another column, unless there were significant performance problems, instead I would create a view that gas the calculated value.
However, if you need the column, I would create a boolean column:
alter table mytable
add is_private boolean;
Then populate it like this:
update mytable
set is_private = (name like '%specialist%' or
name like '%gleneagles%')
OK, this is what I want to do :
If an entry already exists (e.g. based on field name), then just return its id
If it doesn't, add it
This is what I've managed so far (for the "if doesn't exist, create it" part) :
INSERT INTO `objects` (`id`,`name`)
SELECT NULL,'someObj2' FROM `objects`
WHERE NOT EXISTS
(SELECT name FROM `objects` WHERE `name`='someObj2');
SELECT LAST_INSERT_ID();
How can I get the id (instead of LAST_INSERT_ID()) if the entry does exist?
P.S. Yep, I know that the main reason I can't get my head around SQL is the degree at which I'm used to the more classical if-then-else approach of regular programming languages... lol
UPDATE :
I keep trying and trying and this what I've managed so far (as a stored procedure) :
IF EXISTS (SELECT * FROM `objects` WHERE `name` = NAME)
THEN
SELECT `id` FROM `objects` WHERE `name` = NAME;
ELSE
INSERT INTO `objects` (`id`,`name`) VALUES(NULL,NAME);
SELECT LAST_INSERT_ID() AS 'id';
END IF
and calling it like: CALL insertObject("someObj2");
However, it's not working as expected - neither does it add the entry, nor does it return the id (instead it returns all ids in the table...). Any idea what could be going wrong?
It looks like you are trying to enforce a unique constraint on name. If so, you can also do this by just declaring the column to be unique or equivalently creating a unique index:
create unique index objects_name on objects(name);
If this is true, then change the question from getting the last inserted id to just getting the id for name:
select id
from objects o
where o.name = 'someObj2';
I hasten to add that in a high-transaction environment where things are being added and deleted quickly, any approach might have a problem. Consider your code, the row could be inserted and then deleted, even before the last_insert_id() is executed. If you are dealing with a high transaction environment with potential race conditions, then you need to use transactions and locking to do what you want.
I am currently using MySQL. I have a table that has an auto_increment 'id' field, and an 'imgname' field containing a string that is the file name of an image.
I need to generate the 'imgname' value using the auto_increment value that is create by an INSERT INTO statement. The problem is, I don't know this value until I can use mysql_insert_id, AFTER the insert query has run. I would like to know if it's possible to access this value DURING the insert query somehow and then use it to generate my string in the query statement.
Thanks in advance.
I would keep the id and imgname independent of each other and combine the two on SELECT when needed. If the need is frequent enough, create a view.
Have a look at LAST_INSERT_ID() function. If performance is not an issue, INSERT regularly, and then UPDATE using LAST_INSERT_ID(), like:
UPDATE table SET name = CONCAT(name, "-", LAST_INSERT_ID()) WHERE id = LAST_INSERT_ID();
EDIT: This actually works fine, no idea why I thought otherwise.
I have a prices table which includes a column price_was which needs to contain the highest ever value for prices.
Is it possible to do a REPLACE query which would update this if required?
The following (which is simplified and built dynamically in PHP) doesn't seem to work.
REPLACE prices
SET price = 1.99,
price_was = IF(1.99 > price_was, 1.99, price_was)
id_product = 1
I'm thinking perhaps it's not possible, but would love to hear otherwise since I'm updating many records and need to be as efficient as possible.
The query you posted is indeed valid, try it for yourself. I would use an UPDATE though since you're only updating one field and the REPLACE can possible over-write other column data you want left alone.
Try INSERT ... ON DUPLICATE KEY UPDATE instead:
INSERT INTO prices (price, price_was, id_product)
VALUES (1.99, 1.99, 1)
ON DUPLICATE KEY UPDATE
price_was = IF(VALUES(price) > price_was, VALUES(price), price_was)
id_product = VALUES(id_product)
This will do either an INSERT or an UPDATE, while the REPLACE statement does either an INSERT or a DELETE followed by an INSERT. You are not able to reference old values in a REPLACE statement, probably because of the DELETE/INSERT semantics. From the docs:
Values for all columns are taken from
the values specified in the REPLACE
statement. Any missing columns are set
to their default values, just as
happens for INSERT. You cannot refer
to values from the current row and use
them in the new row. If you use an
assignment such as SET col_name =
col_name + 1, the reference to the
column name on the right hand side is
treated as DEFAULT(col_name), so the
assignment is equivalent to SET
col_name = DEFAULT(col_name) + 1.
How can I edit the latest row in the database. I only know it's the last one. I don't know its id.
I don't know which language you are working with, in PHP's mySQL functions you can use
mysql_insert_id()
there are similar function in every other mySQL client library I know of.
Also, there is a native mySQL function!
LAST_INSERT_ID() (with no argument)
returns the first automatically
generated value that was set for an
AUTO_INCREMENT column by the most
recently executed INSERT statement to
affect such a column. For example,
after inserting a row that generates
an AUTO_INCREMENT value, you can get
the value like this:
mysql> SELECT LAST_INSERT_ID();
-> 195
Of course, a primary key with AUTO_INCREMENT is required for these functions to work.
For a table with an auto_increment id field:
UPDATE tbl SET col1 = 'val1' WHERE id = MAX(id);
If it's a row that has been inserted in your script (the same script from which you want to update it) and there is an auto_increment column on your table, you can get that auto_increment value, using functions such as those, for PHP :
mysql_insert_id
mysqli_insert_id
PDO::lastInsertId
There should be an equivalent for probably any language you can possibly be using for your application.
If your are trying to do an update from another script than the one in which you did the insert, and still have an auto_increment column, the best way will probably be to update the row that has the biggest value for that column :
update your_table
set your_column = ...
where id = max(id)
Or, in two steps (not sure it'll work in one) :
select max(id) as id from your_table
update your_table set your_column = ... where id = [what you got with thr first query]
You can also use UPDATE table SET ... WHERE id=LAST_INSERT_ID() (supposing the last insert was on the table you want to query).
I would not use TWO steps to find the last insert ID simply because a new record could be added in the mean time.
Depending on your version, you should be able to call $handle->last_id(); or $handle->{mysql_insertid};
Chris