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.
Related
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
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 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.
I normally found three ways of using MYSQL insert Command
INSERT INTO `table` (field1, field2, field3) VALUES ('value1', 'value2', 'value3');
or
INSERT INTO `table` VALUES ('value1', 'value2', 'value3');
or
INSERT INTO `table` SET `field1`='value1', `field2`='value2', `field3`='value3';
Is there any differences among these?
The first two are standard SQL; the third one is non-standard and specific to MySQL, derived from the standard syntax for UPDATE.
The difference between the first two is that one specifies the fields that you want to insert, which is more flexible because you can miss out fields if you're happy with them being set to NULL. Also specifying the fields means that the statement will still work if the table layout changes; if you don't specify the fields you're relying on the table never changing.
You are looking for differences in terms of what? In terms of usage, the first and third insert statements let you to set only those columns which you need whereas the second statement always tries to insert the values to all columns in order of table structure.
Only one difference, pretty obvious to me: the second one don't let you specify your own field set to insert.