how to output a parameter using a stored procedure in mysql? - mysql

I have two tables say
table1:
create table Person(
Id int primary key auto_increment,
name varchar(20),
Gender char(1)
);
table2:
create table Details(
Age int,
Phone int(10) primary key,
Address varchar(100)
Id foreign key(Id) references Person(Id),
Name foreign key(Name) references Person(Name)
);
i will have to create a stored procedure to insert data into table:'Details'
create procedure usp_insert(
IN Name varchar(100),
IN Age int,
IN Phone int(10),
IN Address varchar(100),
OUT Id int
)
begin
//want to output the Id as QW001 on inserting the data into the Details table.
insert into Details(Name,Age,Phone,Address) values(name,age,phone,address)
end
How can i achieve the Id in the following format 'QW001' as output parameter.
Can someone help me out in correcting the above stored procedure,since i'm new to this. Any help is appreciated.
TYIA!

you need to use LAST_INSERT_ID() like this :
create procedure usp_insert(
IN Name varchar(100),
IN Age int,
IN Phone int(10),
IN Address varchar(100),
OUT Id int
)
begin
insert into Details(Name,Age,Phone,Address) values(name,age,phone,address);
set Id =CONCAT("QW00", LAST_INSERT_ID()) AS ConcatenatedString;
end

Related

How to update a column value by another column(foreign key) in another table

I had made a student table link with report table by foreign key to join them.
CREATE TABLE Students (
st_id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(st_id),
student_name varchar(100),
student_id varchar(9),
email varchar(100),
pass varchar(100),
tp_id INT,
FOREIGN KEY (tp_id) REFERENCES Training_programs(tp_id)
);
CREATE TABLE Report (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
st_id INT,
FOREIGN KEY (st_id) REFERENCES Students(st_id),
status VARCHAR(100)
);
But my problem is when i insert status that i can't insert st_id in the same time because i just insert status in my web. So that i try to auto insert st_id from Students to Report after insert status report:
CREATE TRIGGER thistrigger BEFORE INSERT
ON Students FOR EACH ROW
UPDATE Report, Students SET Report.st_id = Students.st_id WHERE Report.st_id IS NULL;
But it can't work. So that do every ones have any solution with my problem?

Trying to change the name of a foreign key column

I have these two tables:
CREATE TABLE Collaborators (
CustomerID INT NOT NULL,
FirstName VARCHAR(25),
LastName VARCHAR(25),
Street VARCHAR(50),
City VARCHAR(50),
State VARCHAR(25),
ZipCode INT,
Telephone VARCHAR(15),
PRIMARY KEY(CustomerID));
CREATE TABLE Orders (
OrderID INT NOT NULL,
CustomerID INT NOT NULL,
SKU VARCHAR(20),
Description VARCHAR(50),
PRIMARY KEY(OrderID),
FOREIGN KEY(CustomerID) REFERENCES Customers(CustomerID));
My goal is to change any instance of "Customer" to "Collaborator". I tried to go one at a time and use an alter table statement.
ALTER TABLE Collaborators CHANGE CustomerID CollaboratorID INT;
ALTER TABLE Orders CHANGE CustomerID CollaboratorID INT;
Here is the error code MySQL spits out at me:
ERROR 1025 (HY000): Error on rename of './QuantigrationUpdates/#sql-668_24' to './QuantigrationUpdates/Collaborators' (errno: 150)
Any help would be greatly appreciated. I've figured that I can't simply change the column names because they have key constraints, but I don't know how to work around that. Thanks.
Update: Am I able to use CREATE VIEW to work around the key constraints?

What's the right way to insert data to a table with a relationship using plain mysql

This is the database I've created so far
CREATE TABLE app_user(
id INT AUTO_INCREMENT,
username VARCHAR(20),
password VARCHAR(40),
first_name VARCHAR(20),
last_name VARCHAR(20),
PRIMARY KEY(id)
);
CREATE TABLE app_videos(
id INT AUTO_INCREMENT,
video_path VARCHAR(20),
description VARCHAR(20),
likes INT(20),
dislikes INT(20),
PRIMARY KEY(id)
);
CREATE TABLE vid_uid
(
video_id INT,
user_id INT
);
ALTER TABLE vid_uid ADD FOREIGN KEY(video_id) REFERENCES app_videos(id); (..)
This far I've only managed to insert data, by using two queries.
One of the inserts data to the app_users/app_videos table and the other one inserts the data into the relationship table.
How can I add data to a manytomany relationship without using several queries?
is any "right" way to do it?

Error Code: 1215 in mysql cannot add foreign key constraint

I am getting an issue and I feel like I fixed everything. MySQL workbench is still giving me this error. If you can please explain what is wrong I would be greatly appreciate it. I did check for key constraints in the Account table. It is giving the error in the account table.
CREATE TABLE Account(
AcctNum int AUTO_INCREMENT,
MemberID int,
Balance double,
PIN int,
creationDate date,
InitialBalance double,
CreatedByEmployee int,
type VARCHAR(20),
PRIMARY KEY(AcctNum),
FOREIGN KEY(MemberID) REFERENCES Member(MemNum),
FOREIGN KEY(CreatedByEmployee) REFERENCES Employee(EmpId)
);
CREATE TABLE Member(
MemNum int AUTO_INCREMENT,
DOB date,
CreditScore int,
AcctOpened date,
SSN VARCHAR(11),
Address VARCHAR(255),
PRIMARY KEY(MemNum)
);
CREATE TABLE Employee(
EmpId int AUTO_INCREMENT,
DOB date,
SSN VARCHAR(11),
HireDate date,
Salary double,
EmpLevel VARCHAR(50),
PRIMARY KEY(EmpId)
);
You need to create the referencing table first before creating the table which is referring other tables with FOREIGN key.
The order of table creation should be as
CREATE TABLE Member(
MemNum int AUTO_INCREMENT,
DOB date,
CreditScore int,
AcctOpened date,
SSN VARCHAR(11),
Address VARCHAR(255),
PRIMARY KEY(MemNum)
);
CREATE TABLE Employee(
EmpId int AUTO_INCREMENT,
DOB date,
SSN VARCHAR(11),
HireDate date,
Salary double,
EmpLevel VARCHAR(50),
PRIMARY KEY(EmpId)
);
CREATE TABLE Account(
AcctNum int AUTO_INCREMENT,
MemberID int,
Balance double,
PIN int,
creationDate date,
InitialBalance double,
CreatedByEmployee int,
type VARCHAR(20),
PRIMARY KEY(AcctNum),
FOREIGN KEY(MemberID) REFERENCES Member(MemNum),
FOREIGN KEY(CreatedByEmployee) REFERENCES Employee(EmpId)
);
You are creating the Foreign key for Member before creating the table so it does not exist yet. Change the order you are creating the tables.
CREATE TABLE Member(
MemNum int AUTO_INCREMENT,
DOB date,
CreditScore int,
AcctOpened date,
SSN VARCHAR(11),
Address VARCHAR(255),
PRIMARY KEY(MemNum)
);
CREATE TABLE Employee(
EmpId int AUTO_INCREMENT,
DOB date,
SSN VARCHAR(11),
HireDate date,
Salary double,
EmpLevel VARCHAR(50),
PRIMARY KEY(EmpId)
);
CREATE TABLE Account(
AcctNum int AUTO_INCREMENT,
MemberID int,
Balance double,
PIN int,
creationDate date,
InitialBalance double,
CreatedByEmployee int,
type VARCHAR(20),
PRIMARY KEY(AcctNum),
FOREIGN KEY(MemberID) REFERENCES Member(MemNum),
FOREIGN KEY(CreatedByEmployee) REFERENCES Employee(EmpId)
);
Ofcourse the order of table creation is important here.
Create the Member and Employee tables first, followed by the Account. It should work fine.
you have wrong order of creating tables
try this
1- Create member table first
2- employer table second
3- account table in last
http://www.sqlfiddle.com/#!2/47d58

Creating a table, SQL. Basic stuff

So I'm having a small problem with creating a table. The problem is in my attempt to create a foreign key to another table. I'm currently using MYSQL2008 Management R2 express, so no designer. Heres my two tables
use teckDB;
CREATE TABLE inventory
(
primId int NOT NULL PRIMARY KEY,
prodName VarChar(255),
quantity int,
prodCost MONEY,
prodDesc VARCHAR(255)
);
CREATE TABLE orderTB
(
primId INT NOT NULL PRIMARY KEY,
orderId INT NOT NULL,
created date,
prodId INT,
);
Those two tables ran without a problem. When create the thrid one however causes this error message.
Msg 1769, Level 16, State 1, Line 3 Foreign key 'orderTB' references
invalid column 'orderTB' in referencing table 'CustomerTB'. Msg 1750,
Level 16, State 0, Line 3 Could not create constraint. See previous
errors.
On the thrid table of....
CREATE TABLE CustomerTB
(
primId INT NOT NULL PRIMARY KEY,
orderId INT, FOREIGN KEY (orderTB) REFERENCES orderTB(orderId),
fName VARCHAR(50),
lName VARCHAR(50),
addLN1 VARCHAR(255),
addLN2 VARCHAR(255),
addCity VARCHAR(255),
addPro VARCHAR(255),
addPST VARCHAR(7)
);
try this
FOREIGN KEY (iparent_id) REFERENCES innodb_parent (iparent_id)
I think this should help to solve your query
http://blog.sqlauthority.com/2008/09/08/sql-server-%E2%80%93-2008-creating-primary-key-foreign-key-and-default-constraint/
You have an extra comma after "orderId INT", and you have got the foreign key syntax wrong.
This should work:
CREATE TABLE CustomerTB
(
primId INT NOT NULL PRIMARY KEY,
orderId INT REFERENCES orderTB(orderId),
fName VARCHAR(50),
lName VARCHAR(50),
addLN1 VARCHAR(255),
addLN2 VARCHAR(255),
addCity VARCHAR(255),
addPro VARCHAR(255),
addPST VARCHAR(7)
);
tested on SQLFiddle here