I have a table where I have an ID column(primary key auto increment) and one more column for name.
I want to fill name column's value automatically while insertion based on generated ID column value in format
<IDColumnValue>_School
I am aware of the two ways to do this
using trigger
inserting the row first and then update its column value based on the inserted row id column value
But actually I want to make this field Non Nullable but to use the second option I will have to make it nullable.
Is there any direct way to do this while inserting row so that I can have the field non nullable?
As I said in the comment, you can use a temporary value for the name column. You can use a request like :
INSERT INTO `table` VALUES('', 'name') /*let's assume name is the real name you want to insert*/
I'm not sure then how to use a trigger, but you may want to write something like this :
delimiter #
CREATE TRIGGER update_name_after_insert INSERT ON `table`
for each row
begin
update `table` set name = CONCAT_WS("_", id, name)
end#
It work for firebird but I think it must work in MySQL because MySQL have new/old operators. Bellow trigger for table XYZ with fields B and C (not null).
CREATE OR ALTER trigger xyz_bi0 for xyz
active before insert position 0
AS
begin
new.c=new.b||' some text';--this construction must work in MySQL
end
Related
I've this MySQL database table called "DataField". It has an incrementing Primary Key column called "ID", another column called "Name" and another column called "LocType". What I would like to do is, as soon as the ID and Name fields are updated in the "DataField" table I would like to also update the "LocType" table automatically with value "1".
Can someone explain me how to do this?
You can use Trigger to achieve this. Try something like the following code,
CREATE TRIGGER update_LOC_TYPE
BEFORE UPDATE ON DataFiled
FOR EACH ROW BEGIN
UPDATE LocTYPE
SET Field_Name = 'New Value';
I have two columns in my mysql table, equipment and orderno, Here equipment number is manually inserted, and they are in the form C1234,C3212 etc.
I want to strip the C from equipment column and insert the remaining number to orderno column. I have seen that mysql substring_index() can effectively get substring but I am not sure how to make it automtically do the changes, when the equipment column changes.
You could use two triggers, one that's fired before an INSERT, and one that's fired before an UPDATE, to automatically update orderno based on equipment column:
CREATE TRIGGER upd_your_table BEFORE UPDATE ON your_table
FOR EACH ROW
SET new.orderno=substring(new.equipment, 2)
;
CREATE TRIGGER ins_your_table BEFORE INSERT ON your_table
FOR EACH ROW
SET new.orderno=substring(new.equipment, 2)
;
To update existing values, you could use this:
UPDATE your_table SET orderno=substring(equipment, 2)
See this fiddle
Try this ::
UPDATE myTable set orderno= REPLACE(equipment, 'C', '')
My table has 3 columns ID, ISOCode and CountryName.
Column ID is an IDENTITY column.
When I insert new records into this table, I want to populate the ISOCode field - on occasion - with the same value as the ID field.
I've tried SCOPE_IDENTITY(), ##IDENTITY and IDENT_CURRENT but none of these seems to work.
Is there a way I can do this?
you could write a trigger to update the ISOCode column
In Trigger use the code below
update T set T.ISOCode=I.ID
from your_table T
join INSERTED I
on T.ID=I.ID
Can I, and, if I can, how can I set the default value of a field in a MySQL table to the value of another field?
Thing is: I have data, and each data object has its ID in the table. But, I would like the possibility to rearrange the data, changing their sorting index, without altering their ID. Thus, the field sort_num should by default be set to the value given to the auto-incremented indexed field ID.
Thanks in advance!
I see two possible solutions for this:
1. Possibility:
You use a function to simply ignore sort_num if it is not set:
`SELECT * FROM mytable ORDER BY coalesce(sort_num, id)`
coalesce() returns the first non-null value, therefore you would insert values for sort_num if you really need to reorder items.
2. Possibility:
You write a trigger, which automatically sets the value if it is not set in the insert statement:
DELIMITER //
CREATE TRIGGER sort_num_trigger
BEFORE INSERT ON mytable
FOR EACH ROW BEGIN
DECLARE auto_inc INT;
IF (NEW.sort_num is null) THEN
-- determine next auto_increment value
SELECT AUTO_INCREMENT INTO auto_inc FROM information_schema.TABLES
WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME = 'mytable';
-- and set the sort value to the same as the PK
SET NEW.sort_num = auto_inc;
END IF;
END
//
(inspired by this comment)
However, this might run into parallelization issues (multiple queries inserting at the same time)
Its a bad idea to have an auto-increment column rearranged, hence better idea would be
Add a column sort_num to the table
ALTER TABLE data ADD sort_num column-definition; (column definition same as ID)
UPDATE data SET sort_num = ID
Now play with sort_num column as it has no effect on column ID
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