REPLACE into keeping a value - mysql

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=?;

Related

INSERT INTO statement in a specific column

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.

Adding only one value to the table in sql

I have a table named student which consists of (rollno, name, sem, branch)
If I want to INSERT only one value (i.e only name has to be entered) what is the query?
To insert values into specific columns, you first have to specify which columns you want to populate. The query would look like this:
INSERT INTO your_table_name (your_column_name)
VALUES (the_value);
To insert values into more than one column, separate the column names with a comma and insert the values in the same order you added the column names:
INSERT INTO your_table_name (your_column_name_01, your_column_name_02)
VALUES (the_value_01, the_value_02);
If you are unsure, have a look at W3Schools.com. They usually have explanations with examples.
insert into student(name) values("The name you wan to insert");
Be careful not to forget to insert the primary key.
First, if the table is all empty, just wanna make the column 'name' with values, it is easy to use INSERT INTO. https://www.w3schools.com/sql/sql_insert.asp.
`INSERT INTO TABLE_NAME (COLUMN_NAME) VALUES ("the values")`
Second, if the table is already with some values inside, and only wanna insert some values for one column, use UPDATE. https://www.w3schools.com/sql/sql_update.asp
UPDATE table_name SET column1 = value1 WHERE condition;
insert into student (name)
select 'some name'
or
insert into student (name)
values ('some name')
Following works if other columns accept null or do have default value:
INSERT INTO Student (name) VALUES('Jack');
Further details can be found from the Reference Manual:: 13.2.5 INSERT Syntax.
Execute this query, if you want the rest of columns as "#", do insert # inside the single quote which is left blank in query.
Also, there is no need of defining column name in the query.
insert into student values('','your name','','');
Thanks...

Trouble with MAX(col)+1 INSERT into same MySQL table

I need two id columns in the same table that create unique values upon insert. Since MySQL allows only one column to auto-increment, I need to do something like max(id)+1 for the other column.
Here's the SQL I expected to work:
INSERT INTO invoices (invoiceid)
VALUES ((SELECT MAX(invoiceid)+1 FROM invoices))
The select statement works independently, but within my INSERT, it's not allowed. I get the error : You can't specify target table 'invoices' for update in FROM clause
You want to use INSERT INTO .... SELECT FROM instead of INSERT INTO...VALUES():
INSERT INTO invoices (invoiceid)
SELECT MAX(invoiceid)+1
FROM invoices
My question for you would be why are you not use an AUTO INCREMENT field to generate the invoiceid value? That is what it is for, then you will not have to create this when inserting data.

MYSQL: update a row without mentioning fields name

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

Access Auto-Increment Value During INSERT INTO Statement

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();