I there a way to update a row without mentioning fields name ?
I mean something like:
UPDATE table SET VALUES(1, 'name', 'family')
instead of:
UPDATE table SET id=1, name='name', family='family'
update
I'm using INSERT ON DUPLICATE KEY UPDATE and don't want to use REPLACE function because REPLACE function will cause a record to be removed, and inserted at the end, which will cause the indexing to get broken apart, decreasing the efficiency of the table.
If you specify the values in the same order as the table definition you could use
REPLACE INTO table VALUES(1, 'name', 'family');
Note that this will replace the entire row, so you must specify all the values you need!
You cannot do like that with mysql, as set clause indicates which columns to modify and the values they should be given
FYI: http://dev.mysql.com/doc/refman/5.0/en/update.html
Related
For example i have table with a different field names(column), lets say 5 columns and all of them are empty. And i wanted to insert data in one specific column. Is it possible? I'm looking for example of this, but unlucky to find one. Most of insert into statements examples required all columns to be filled. If possible, can you give me the correct syntax? I'm sorry if i'm lacking research or it's already been asked, it's ok if you will redirect me to the link.
If you want insert on column3, leaving empty the other:
INSERT INTO table_name (column1,column2,column3,column4,column5)
VALUES ("","","VALUE","","");
The other part of program would UPDATE the other columns:
UPDATE table_name
SET column1=value1,column2=value2,column4=value4,column5=value5
WHERE some_column=some_value;
The documentation on how to construct an INSERT INTO statement is here: INSERT INTO Statement (Microsoft Access SQL).
But basically, you just need to be explicit about which columns you want to insert values for, and omit the other ones, like this:
INSERT INTO table (colname) VALUES ('colvalue')
What happens to the fields you omit? The documentation says:
When you do not specify each field, the default value or Null is inserted for missing columns.
In my table "accounts" I have four columns like
user, pass, column1, column2
I need to insert value into column2, where user='special_user_value'.
How can I do this?
UPDATE accounts
SET column2 = 'New Value'
WHERE user = 'special_user_value';
You don't "insert values" into a column. You insert a row, that has a value for all the columns you specified in the table creation; Just like a real table, or excel sheet for that matter.
If you need to change a column value for a specific row, you can use UPDATE:
UPDATE table_name SET column2='new value' WHERE user='special_user_value'
This is a really basic example. If you follow the link I provided for UPDATE, you may learn more about changing table values for a specific row.
If are you looking for actually inserting a new row with a specific value for that column, there's INSERT INTO you could follow to achieve that.
Use update Query like:
Update table_nm set field1=value1, Field2=value2 Where condition;
UPDATE Accounts SET column2='NewValue' WHERE user='special_user_value'
I find that there are only after and instead of triggers in sql server. And it is illegal to modify the values in the inserted pesudo table. Then my problem occurs: If I want to check the data which is going to be inserted into my table, and when the data violates my constraints I should modify these values to default values, how to do it ? How about updateing the values after inserted ? However, if there's no primary key or colum which is unique in my table, how can I locate the row just inserted and then update it ?
Basically, with an INSTEAD OF INSERT trigger, you can achieve what you're looking for - just read out the data from the INSERTED pseudo table, modify it, and insert it into the table
So your trigger would look something like this:
CREATE TRIGGER YourTrigger ON dbo.YourTable
INSTEAD OF INSERT
AS
SET NOCOUNT ON
-- do the INSERT based on the INSERTED pseudo table, modify data as needed
INSERT INTO dbo.YourTable(Col1, Col2, ....., ColN)
SELECT
Col1, 2 * Col2, ....., N * ColN
FROM
INSERTED
Of course, you could also add e.g. checks in the form of WHERE clause to that SELECT .... FROM INSERTED statement to e.g. ignore certain rows - the possibilities are endless!
I want to make a replace into in a table where cust_id is the primary key, but I do not want to modify the date field. So, a normal insert on this table would look like:
REPLACE INTO emails(cust_id, email, date)
VALUES(55, 'email#email.com', '2011-08-07 00:00');
Now, without having to modify the date field, it would be something such as:
REPLACE INTO emails(cust_id, email, date)
VALUES(55, 'email#email.com', date.value?);
But how do I exactly keep the date value?
Short answer, You can't keep the dates that way. from Mysql documentation
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
perhaps you want to use http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html instead.
insert ignore will skip insertion if any duplication
if you need to update certain fields ,
you can do
insert into some_table values (...)
on duplicate update email=?;
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.