I have this table: cd_biblio which has a list of books.
When I insert a new book from my php page, I want to increment a field ('Ingresso') based on the value 'Class' I inserted.
For example:
I insert a new book with class 'A', his 'Ingresso' value will be the current max of 'Ingresso' for the class 'A' + 1.
How can I do this using MyISAM?
use prodedures or triggers f.e:
delimiter ~
CREATE PROCEDURE AddBook(...)
BEGIN
DECLARE gIngresso INT DEFAULT 0;
SELECT Max(Ingresso) INTO gIngresso FROM Table;
INSERT INTO Table VALUE(... , gIngresso)
END~
delimiter ;
Related
Can you insert an autoincrement value into several columns at once?
Example table `zoo`:
id (int autoincrement primary_key);
parent (int);
data (varchar,10).
INSERT INTO zoo (id, parent, data) VALUES (NULL, id, "tiger");
Or do I have to do a second query?
UPDATE TABLE zoo SET parent=id WHERE parent IS NULL;
You might be able to do this via a before insert trigger:
DELIMITER //
CREATE TRIGGER same_id_trigger
BEFORE INSERT
ON zoo FOR EACH ROW
BEGIN
DECLARE id int DEFAULT 0;
SELECT auto_increment INTO id
FROM information_schema.tables
WHERE table_name = 'zoo' AND table_schema = database();
SET NEW.parent = id;
END; //
DELIMITER ;
Assuming the above trigger works, then it is working by directly querying information schema to find the current auto increment value for the zoo table. Then, the trigger effectively intercepts the insert, and swaps in that auto increment value for the parent column.
When I insert a new row, an auto increment ID will be saved like 1,2,3..
I need to custom it using a Trigger so it will be saved like 20171,20172,20173..
This trigger would do this.
CREATE DEFINER=`root`#`%` TRIGGER `test_before_insert` BEFORE INSERT ON `test` FOR EACH ROW BEGIN
SET NEW.id = (
SELECT CONCAT(YEAR(CURDATE()),IFNULL(MAX(CAST(ids.id AS UNSIGNED))+1,1))
FROM (
SELECT RIGHT(t.id,LENGTH(t.id)-4) AS id
FROM test t
WHERE LEFT(t.id,4) = YEAR(CURDATE())
) ids
);
END
But there are many reason you would not want to. This will get exponentially more expensive as rows are inserted, and provides no viable sorting etc.
How can I set my custom value in auto increment column when insert query fire in phpMyAdmin
I want like when I insert a data in MySQL at that time the auto increment column value add with like something ABC001 and next record will be ABC002
It there any way to setup this functionality
This is my table structure
I want new code for every insert query for red mark field
Yes, you can use a TRIGGER like this:
CREATE DEFINER=`root`#`localhost` TRIGGER `setDrinkk` BEFORE INSERT ON `yourTable`
FOR EACH ROW BEGIN
SET NEW.DrinkCode = CONCAT('ABC-', LPAD(NEW.db_id,4,'0'));
END
You'll have to add 2 more fields to the table..One field(eg:drink_text)containing the text 'ABC' and other field (Eg:drink_no) with autoincrement.
Set default value of drink_text as ABC.
Set autoincrement primary key for drink_no
ALTER TABLE tablename AUTO_INCREMENT=101;
Now in insert query you have to concatenate both the fields to drink_code
//your insert query
INSERT INTO tablename( user_id,club_id) VALUES ('xyz','abc');
//drink_code with custom string
UPDATE tablename SET drink_code = concat( drink_text, drink_no ) ;
I have a requirement where in my sql a column 2 would always have the uppercase value that is column 1,
Not sure ow to do this in mysql. I want to do something like this , I know the syntax below is incorrect but writing some psuedo code as to make it clear what I am trying to achieve
create table sakila.testupper(name varchar(50),
uppername varchar(50) not null default as select upper(#name));
I believe you could accomplish this across most version of MySQL with a trigger on insert or update: http://dev.mysql.com/doc/refman/5.7/en/triggers.html
The syntax you're after looks like this: "create table sakila.testupper (name varchar(50), uppername varchar(50) generated always as (upper(name)));". However, you'd need a recent release of MySQL version 5.7.
You can't have the table do this automatically but you can create a trigger for all future INSERTs. With a trigger you will need to maintain a separate table.
drop table if exists p;
drop table if exists q;
create table q (name nvarchar(59),uppername nvarchar(59));
create table p (name nvarchar(59));
create trigger trig_thing after insert on p
for each row
begin
insert into q set name = new.name, uppername = upper(new.name);
end;
insert into p (name) values ('Some nice gentleman');
insert into p (name) values ('A sweet old lady');
select * from q;
Your original table will keep only the name, but each insert will cause a trigger to insert the same data into your new table with name and uppername.
If you already have names stored in a table you should insert into your second table to get them in line before you set up the trigger:
insert into q name, upper(name) from p;
here is a functional example
DELIMITER $$
CREATE DEFINER=`root`#`localhost` TRIGGER `grade_one_BINS`
BEFORE INSERT ON `grade_one` FOR EACH ROW
set new.student_no = concat(new.letter, ' - ',new.num)
there is a problem in concatenating the num column that has auto increment value since the trigger is for before insert cause it will show a 0 value since the auto increment is still 0 until you insert some values...can you help me???
I suggest you don't store data again in another column that you already have in the num and letter columns.
You can generate the student_no column on-the-fly in your selects like this
select *,
concat(letter, ' - ', num) as student_no
from your_table