Is it possible to auto generate a number if nothing is supplied for a column in the table
#SurveyId,Name,PhoneNumber
1,David,071234234
1,John, NULL
expected output
#SurveyId,Name,PhoneNumber
1,David,071234234
1,John, 3274985AUTO
I would like to write a trigger for my table to carry out the auto generation option; i would like to use UUID() for auto generating number
syntax error:
15:10:23 CREATE TRIGGER mobilecheck BEFORE INSERT ON reg02_maininfo `FOR EACH ROW IF NEW.farmermobile IS NULL THEN SET NEW.farmermobile = floor(rand()*900000)+100000 Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 2 0.0015 sec`
You could do it this way
SELECT SurveyId, Name, IFNULL(PhoneNumber,floor(rand()*900000)+100000) as PhoneNumber FROM table
I did not add an AUTO at the end as if the field only accepts an integer it will give you an error.
You could also update your table to directly replace all null values
UPDATE Table SET PhoneNumber = floor(rand()*900000)+100000
WHERE PhoneNumber IS NULL;
For a trigger you could use the following :
CREATE TRIGGER phonecheck BEFORE INSERT ON tablename FOR EACH ROW IF NEW.PhoneNumber IS NULL THEN SET NEW.PhoneNumber = floor(rand()*900000)+100000; END IF;
COALESCE to fetch it if it's non-null, FLOOR+RAND to get a random number otherwise, CONCAT to add AUTO at the end?
SELECT `#SurveyId`,`Name`,COALESCE(`PhoneNumber`,CONCAT((FLOOR(RAND()*(9999999-1000000+1))+1000000),'AUTO')) AS PhoneNumber
Related
I've never used IF's before in SQL. I need to update a row where institution is a specific number if it exists and insert it if it doesn't. In order to avoid using first a select and then a insert or update I wanted to try my hand at an IF statement. I figured from what I've read in the documentation that it should go something like this:
IF (NOT EXISTS(SELECT evaluations FROM tEvaluations WHERE institution = 0))
BEGIN
INSERT INTO tEvaluations (institution,evaluations) VALUES (0,0)
END
ELSE
BEGIN
UPDATE tEvaluations SET evaluations = 10 WHERE institution = 0
END
However I get this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'BEGIN
INSERT INTO tEvaluations (institution,evaluations) VALUES (0,0)
END' at line 2
I'm trying to run this query in phpmyadmin to test out how the query should be.
You can't have if..else block in normal SQL statement unless it's inside a procedural block. To me looks like you are looking for INSERT ON DUPLICATE KEY UPDATE like
INSERT INTO tEvaluations (institution,evaluations) VALUES (0,0)
ON DUPLICATE KEY UPDATE evaluations = 10;
Per documentation, either of your column should have a UNIQUE constraint defined against it. Quoting from documentation
If you specify an ON DUPLICATE KEY UPDATE clause and a row to be
inserted would cause a duplicate value in a UNIQUE index or PRIMARY
KEY, an UPDATE of the old row occurs. For example, if column a is
declared as UNIQUE and contains the value 1
I have a problem with a stored procedure, both in syntax and logic. I got the error below when trying it on sqlfiddle.com.
Also I'm not sure if the logic is right. Is there anyway I can check by entering the ID and the query will return a table which row that contains the ID has been deleted?
create a procedure named delete_id which deletes the row that goes by the ID
CREATE PROCEDURE DELETE_ID (ID_INPUT IN INT)
AS
BEGIN
DELETE FROM TABLE ALBUM WHERE ID=ID_INPUT;
END;
Expected result: Enter an Id and SQL will delete the row which contains the ID
Actual Result:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TABLE ALBUM WHERE ID=ID_INPUT' at line 3
You need to add "DELIMITER //" at the start on the code and "//" at the end. So your code should look like
DELIMITER //
CREATE PROCEDURE `DELETE_ID`(IN `ID_INPUT` INT)
BEGIN
DELETE FROM `ALBUM` WHERE `ID` = ID_INPUT;
END //
I'm trying to create a trigger whereby an insertion on one table updates another. This is my SQL Query:
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END;
No matter what I do I get the following error:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 6
I don't think it's related to the SUM, because trying a basic = 1 on the SET command gives me the exact error. There is no '' at line 6 which is very confusing?
If you're entering this query directly into MySQL you will need to change the delimiter prior to the query using (e.g.) DELIMITER //, otherwise it thinks the query ends at the ; at the end of your UPDATE statement. MySQL then sees an END with nothing before it and complains about the nothing (''). So try this:
DELIMITER //
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END; //
DELIMITER ;
I am trying to create in MySQL a 'custom' unique ID based on existing primary key (auto-incremented) 'id' field
delimiter //
CREATE TRIGGER update_id AFTER INSERT ON test
BEGIN
UPDATE test SET PN="PN-"+NEW.id;
END;
//
delimiter ;
I am using PhpMyAdmin and I really don't know if it's a UI problem (I encountered some problems in the past while creating triggers in PhpMyAdmin) or I really do something wrong.
What I need is a custom PN field that is automatically updated when insert new records in table, based on some text prefix "PN-"
id PN other fields
-------------------------
...
...
...
1253 PN-1253
1254 PN-1254
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BEGIN
UPDATE test SET PN="PN-"+NEW.id;
END' at line 2
I read in some other StackOverflow post that this will be impossible (to update) on the same table AFTER insert.
There is a solution with this? Thanks.
Get the new value and add it before insert
delimiter //
CREATE TRIGGER update_id BEFORE INSERT ON test
BEGIN
SET #pn:= ( SELECT AUTO_INCREMENT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME='test'
AND TABLE_SCHEMA=DATABASE() );
SET new.PN= CONCAT('PN-',#pn);
END;
//
delimiter ;
Here
I am trying to use IF EXISTS in MySQL but i keep getting syntax errors and I have researched for correct syntax but everything isnt working...
What i need is:
If query exists then UPDATE else INSERT new...
$queryString = "IF EXISTS (SELECT * FROM $ONCALL_TABLE WHERE uid='$contextUser' AND submitid='$submitid' AND submitstatus=3) THEN UPDATE $ONCALL_TABLE SET uid='$contextUser', start_time='$onStr', end_time='$offStr', amount='$amount' ELSE INSERT INTO $ONCALL_TABLE (uid, start_time, end_time, amount) VALUES ('$contextUser','$onStr', '$offStr', '$amount') END IF";
Error message:
Can't perform query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS (SELECT * FROM timesheet_oncall WHERE uid='admin' AND submitid='136545' at line 1
REPLACE INTO is what you need. http://dev.mysql.com/doc/refman/5.0/en/replace.html
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.
In your case
REPLACE INTO
$ONCALL_TABLE (uid, start_time, end_time, amount)
VALUES ('$contextUser','$onStr', '$offStr', '$amount')
WHERE uid='$contextUser';
Assuming uid is a PRIMARY KEY or UNIQUE KEY
NOTE: Since the code in your question contains SQL injection flaws I would recommend you to read this article. http://php.net/manual/en/security.database.sql-injection.php