How to define a column that can automate capitalizing? - mysql

So that when you insert 'abc',it'll be converted to 'ABC' automatically.
I'm using MySQL,is it possible?

You could write an INSERT trigger that does this for you.
Something like:
CREATE TRIGGER Capitalize BEFORE INSERT ON MyTable
SET NEW.MyColumn = UPPER(NEW.MyColumn)

What about using Upper function while executing query ?
lets say You are using a SP to insert the values ... cast the value to upper case using UPPER function and then insert ?

this really should be done before it is passed to the DB

Related

Insert into a specific row multiple values with Where clause?

I want to insert two dates into a specifc row on my table using MySql but have a syntax error on Workbench (Error 1064):
INSERT INTO atable (Activated,Expiry) WHERE Access_Code = 'accesscode'
VALUES('$current_date','$Expiration_Date')
What would be the correct version for this?
It would be something like this:
UPDATE atable
SET Activated='$current_date',
Expiry='$Expiration_date'
WHERE Access_Code='accesscode';
I'm assuming you will be setting $current_date and $Expiration_date via (what looks like PHP) code.
Hope this helps.

How can I use MySQL variable with POINT

I am trying to use a MySQL variable together with POINT like this (simplified):
SET #lat=145.033667; SET #long=-37.932000; INSERT INTO Location(position) values (GeomFromText(‘POINT(#lat #long)'));
This works fine:
SELECT 'POINT(145.033667 -37.932000)';
As does this:
SELECT GeomFromText('POINT(145.033667 -37.932000)');
Is there anyone who know how I can make this work?
I think you have a syntax error:
SET #lat=145.033667;
SET #long=-37.932000;
INSERT INTO Location(position)
values (GeomFromText('POINT(#lat #long)'));
^^^ Here you need '

Custom format example for serial number creation in mysql

I have recently switched over from OpenOffice Base to MySQL community. In ooBase you can customize integers upon entering them into the database. For example I could type 2013_00000 and then every number in this column would be formatted in this way (2013_00001, 2013_00002, ...). After playing around with MySQL community for a while I noticed that there is no obvious way to format a custom integer like this. I might be overlooking something basic, but if anyone knows how to do this please let me know. Thank you
In order to manipulate data before inserting them you need a trigger.
Something like that:
CREATE TRIGGER ins_sn BEFORE INSERT ON tbl
FOR EACH ROW
SET NEW.sn = CONCAT(LEFT(NEW.sn, 4), '_', RIGHT(NEW.sn,5));
If you need to update your rows, you need a second trigger:
CREATE TRIGGER upd_sn BEFORE UPDATE ON tbl
FOR EACH ROW
SET NEW.sn = CONCAT(LEFT(NEW.sn, 4), '_', RIGHT(NEW.sn,5));
See http://sqlfiddle.com/#!2/01428/1 for the complete example.
Here, I use the CONCAT(), LEFT() and RIGHT() functions for simplicity. Maybe your data require more complex manipulations (i.e.: padding). MySQL has a rich set of string functions. Anyway this is the principle.
Not the most flexible (here locked down to an underscore separator), but you can do it with a trigger, something like this;
CREATE TRIGGER trig_MyTable BEFORE INSERT ON MyTable
FOR EACH ROW
BEGIN
SELECT seq into #seq FROM MyTable_Seq FOR UPDATE;
SET NEW.id = #seq, #cutoff = LOCATE('_', #Seq);
UPDATE MyTable_Seq SET Seq =
CONCAT(LEFT(#seq, #cutoff),
LPAD(SUBSTR(#seq, #cutoff+1)+1, LENGTH(#seq)-#cutoff, '0'));
END;
//
An SQLfiddle to test with.

Update MySQL without specifying column names

I want to update a mysql row, but I do not want to specify all the column names.
The table has 9 rows and I always want to update the last 7 rows in the right order.
These are the Fields
id
projectid
fangate
home
thanks
overview
winner
modules.wallPost
modules.overviewParticipant
Is there any way I can update the last few records without specifying their names?
With an INSERT statement this can be done pretty easily by doing this:
INSERT INTO `settings`
VALUES (NULL, ...field values...)
So I was hoping I could do something like this:
UPDATE `settings`
VALUES (NULL, ...field values...)
WHERE ...statement...
But unfortunately that doesn't work.
If the two first columns make up the primary key (or a unique index) you could use replace
So basically instead of writing
UPDATE settings
SET fangate = $fangate,
home = $home,
thanks = $thanks
overview = $overview,
winner = $winner,
modules.wallPost = $modules.wallPost,
modules.overviewParticipant = $modules.overviewParticipant
WHERE id = $id AND procjectId = $projectId
You will write
REPLACE INTO settings
VALUES ($id,
$projectId,
$fangate,
$home,
$thanks
$overview,
$winner,
$modules.wallPost,
$modules.overviewParticipant)
Of course this only works if the row already exist, otherwise it will be created. Also, it will cause a DELETE and an INSERT behind the scene, if that matters.
You can't. You always have to specify the column names, because UPDATE doesn't edit a whole row, it edits specified columns.
Here's a link with the UPDATE syntax:
http://dev.mysql.com/doc/refman/5.0/en/update.html
No, it works on the INSERT because even if you didn't specify the column name but you have supplied all values in the VALUE clause. Now, in UPDATE, you need to specify which column name will the value be associated.
UPDATE syntax requires the column names that will be modified.
Are you always updating the same table and columns?
In that case one way would be to define a stored procedure in your schema.
That way you could just do:
CALL update_settings(id, projectid, values_of_last_7 ..);
Although you would have to create the procedure, check the Mysql web pages for how to do this, eg:
http://docs.oracle.com/cd/E17952_01/refman-5.0-en/create-procedure.html
I'm afraid you can't afford not specifying the column names.
You can refer to the update documentation here.

How do I remove slashes during a select statement

Please I am new. I am performing an insert select and I want to remove the slashes in a particular field say field b at the select portion of the query.
eg. insert into mytable(a,b,c) select a, stripslashes(b),c from mysecondtable;
Please help.
you can use REPLACE like this:
insert into mytable(a,b,c)
select a, REPLACE(b, '\\', '\') as b, c
from mysecondtable;
The REPLACE expression might have to be refined, but I hope this gets you started.
You can use MySQL String functions, e.g. replace.
Also consider sanitizing or preparing any input to the SELECT in the calling application before you execute the query.
use the SUBSTR function of mysql or do something like this:
"insert into table set value = " . stripslashes('whatever')
Are you doint this with PHP? Then a simple $fieldB = strip_slashes($_POST['b']); should do. You should then use $fieldB in your query.