Stored procedure to insert new column into table - mysql

I have a table:
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`score` int(11) NOT NULL,
PRIMARY KEY (`id`)
Now i want to create a stored procedure that will insert new column into this table with the next id (which is automaticly created), name (value which I will write), and score (which i will write).
I have tryed
DELIMITER ;;
CREATE PROCEDURE insertStudent (IN ime varchar(100),IN score int(11))
BEGIN
insert into student (name,score) values (in_score,in_score);
END
;;
But it doesnt seem to work.
I know this is basic but i need help.

Are you inserting a column or a row? It looks like you're trying to insert a row, which is much more conventional.
As for "doesn't seem to work", I imagine you're getting an error from the database. What is that error? I would guess it has something to do with unknown identifiers in the query or syntax errors. Take a look at your procedure declaration:
insertStudent (IN ime varchar(100),IN score int(11))
And at your query:
insert into student (name,score) values (in_score,in_score);
What is in_score in the query? Where do you get that? You have a couple of problems here:
in_score is never defined, instead you have values called ime and score
You're trying to insert the same value into columns of two different types, that's not going to work.
There may be additional problems with your procedure declaration, I'm not familiar enough with SQL to say with certainty. But IN and int(11) look suspicious to me.
Maybe your query should be this?:
insert into student (name,score) values (ime,score);
At the very least, even if it doesn't work that may change the error message you're seeing. Which would be a step in the right direction. At that point continue to examine the error message to help debug what's wrong.

CREATE PROCEDURE insertStudent #ime varchar(100),score int
as
BEGIN
insert into student
values (#a1, #a2);
END
You forgot to put # before the parameter and you don't need to use in
and forgot to put as

Related

Stored procedures that dont work?

I am trying to create some stored procedures in my database for my assignment and I can't work out why they don't work correctly as they run, they just don't have the desired effect.
The first is attempting to add members to my table. It runs fine but nothing is added. It just add's 0's for everything.
CREATE DEFINER=`root`#`localhost` PROCEDURE `AddMember`(IN `iFirstname` VARCHAR(15) CHARSET utf8, IN `iLastname` VARCHAR(15) CHARSET utf8, IN `iCPR` INT(10) ZEROFILL, IN `iPhone` INT(8) ZEROFILL, IN `iAddress` VARCHAR(50) CHARSET utf8, IN `iPostcode` INT(4) UNSIGNED, IN `iDateJoined` DATE, IN `iNewsletter` BOOLEAN)
MODIFIES SQL DATA
SQL SECURITY INVOKER
INSERT INTO members (FirstName,
LastName,
CPR,
PhoneNumber,
Address,
Postcode,
DateJoined,
Newsletter)
VALUES (iFirstname,
iLastname,
iCPR,
iPhone,
iAddress,
iPostcode,
iDateJoined,
iNewsletter)
The second is deleting a member. If I run the DELETE FROM WHERE line by it's self it works fine but when its put into the stored procedure it doesn't work.
CREATE DEFINER=`root`#`localhost` PROCEDURE `DeleteMember`(IN mID INT)
BEGIN
DELETE FROM members WHERE MemberID = mID;
END
I am a bit confused as to what is wrong with them as I am new to this and finding specific answers is difficult especially as most examples are even more complex.
Hello Create an id Field in database table with auto incremented values then you will be able to store values via Stored Procedure returned above

PLS HELP MySql Error 1054: Unknown column 'column name' in field list

I have this table:
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(25) NOT NULL,
`pass` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8
I made a stored procedure like this:
create procedure uppdateUser(newname varchar(25), newpass varchar(50), newname2 varchar(25), newpass2 varchar(50))
-> begin
-> while name=newname && pass=newpass do
-> update users set name=newname2, pass=newpass2;
-> end while;
-> end
But I get an error unknown column name in field list when I call the procedure like this:
call uppdateUser('marky','mark','ice','cube');
I have searched questions here on StackOverflow but haven't found an answer. Can anyone explain to me what is the issue here in my query and how to fix it?
I think the issue is the references to name and pass in the while condition. Those references aren't valid. There aren't any procedure parameters or variables with those names.
It's not clear what you are trying to achieve, but I don't think there's any need for a while loop.
It looks like you want a WHERE clause on the UPDATE statement. Without a WHERE clause, the UPDATE statement is going to update every row in the table. And there's no need to run that more than once.
I'd expect the update statement to be of the form something like this:
UPDATE users
SET name = v_newname
, pass = v_newpass
WHERE name = v_oldname
AND pass = v_oldpass
But again, I don't really understand what you are trying to achieve.
First, get your UPDATE statement right. Figure out the actual SQL statements you want to execute.
Once you've got that, then you can put it into a stored procedure.

how to create unique sequence in mysql

How to create unique sequence number in MySQL?
The scenario goes like, that in table1 the data say "A" in row1 can appear more than once.
So when it is first occurring a sequence no will be assigned to it, and the same will be assigned to it each time it appears again.
But the data "B" (say the next data entered) will have the next sequence no.
So i cant use auto_increment in this scenario. Say, i have to check the conditions c1 and c2 for this unique sequence no.
Looking for a stored procedure to implement this. Hope i am clear with my problem.
CREATE TABLE `seq` (
`n` BIGINT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`n`)
);
DELIMITER $$
DROP PROCEDURE IF EXISTS getseq$$
CREATE FUNCTION getseq() RETURN BIGINT
BEGIN
DECLARE r BIGINT;
INSERT INTO `seq` (`n`) VALUES (NULL);
SELECT MAX(`n`) INTO r FROM `seq`;
COMMIT;
RETURN r;
END$$
DELIMITER ;
Concurrent transactions should be revised, but I think it would work, because indeed the mark of auto-increment is shared across transactions, but not the id resulting from the insert you made into the table.

MySQL trigger (replacing assertion) does not work

Can you help me with this problem? I have two tables in a MySQL database:
ServiceProvider(SPID, Name, ... etc.)
hasTag(SPID, TagID)
Each service provider is supposed to have at least one tag, and a maximum of five tags. The max-constraint is not a problem, but the min-constraint refuses to work properly. I first tried to implement this via assertions, but then I found out, that MySQL does not support assertions. Thus, I wrote the following trigger:
delimiter |
CREATE TRIGGER MinTags BEFORE INSERT
ON ServiceProvider
FOR EACH ROW BEGIN
IF EXISTS (SELECT SPID FROM ServiceProvider
WHERE NOT EXISTS (SELECT DISTINCT SPID FROM hasTag))
THEN INSERT INTO stop_action VALUES(1, 'Assert Failure');
END IF;
END;
|
delimiter ;
The insert of 'Assert Failure' into the stop_action table is only to create a constraint violation, so that the DB would abort the action.
Now, normally, when I insert any value into the ServiceProvider table, without inserting anything in to the hasTag table, I should get an error, right? But, somehow it doesn't work ... I can insert anything I want into the ServiceProvider table, without receiving any kind of error. Do you know, what is wrong with my code?
How about denormalising a tad:
ALTER TABLE ServiceProvider
ADD COLUMN TagID1 BIGINT UNSIGNED NOT NULL,
ADD COLUMN TagID2 BIGINT UNSIGNED NULL,
ADD COLUMN TagID3 BIGINT UNSIGNED NULL,
ADD COLUMN TagID4 BIGINT UNSIGNED NULL,
ADD COLUMN TagID5 BIGINT UNSIGNED NULL;
Include foreign key constraints, if appropriate.
As written, this trigger does not even use the values from the row to be inserted.
The syntax to get the value of the SPID column is:
NEW.SPID
Also, consider using the SIGNAL statement to raise an error.
If you want to use ASSERT in SQL, this post may help:
SQL Scripts - Does the equivalent of a #define exist?

MySql autoincrement column increases by 10 problem

I am a user of a some host company which serves my MySql database. Due to their replication problem, the autoincrement values increses by 10, which seems to be a common problem.
My question is how can I simulate (safely) autoincrement feature so that the column have an consecutive ID?
My idea was to implement some sequence mechanism to solve my problem, but I do not know if it is a best option. I had found such a code snipset over the web:
DELIMITER ;;
DROP TABLE IF EXISTS `sequence`;;
CREATE TABLE `sequence` (
`name` CHAR(16) NOT NULL,
`value` BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;;
DROP FUNCTION IF EXISTS `nextval`;
CREATE FUNCTION `nextval`(thename CHAR(16) CHARSET latin1)
RETURNS BIGINT UNSIGNED
MODIFIES SQL DATA
SQL SECURITY DEFINER
BEGIN
INSERT INTO `sequence`
SET `name`=thename,
`value`=(#val:=##auto_increment_offset)+##auto_increment_increment
ON DUPLICATE KEY
UPDATE `value`=(#val:=`value`)+##auto_increment_increment;
RETURN #val;
END ;;
DELIMITER ;
which seems quite all correct. My second question is if this solution is concurrent-safe? Of course INSERT statement is, but what about ON DUPLICATE KEY update?
Thanks!
Why do you need to have it in the first place?
Even with auto_increment_increment == 1 you are not guaranteed, that the autoincrement field in the table will have consecutive values (what if the rows are deleted, hmm?).
With autoincrement you are simply guaranteed by the db engine, that the field will be unique, nothing else, really.
EDIT: I want to reiterate: In my opinion, it is not a good idea to assume things like concurrent values of an autoincrement column, because it is going to bite you later.
EDIT2: Anyway, this can be "solved" by an "on insert" trigger
create trigger "sequence_b_ins" before insert on `sequence`
for each row
begin
NEW.id = select max(id)+1 from `sequence`;
end
Or something along these lines (sorry, not tested)
Another option would be to use a stored proc to do the insert and have it either select max id from your table or keep another table with the current id being used and update as id's are used.