Toggle navigation
TechnoWikis.com
Email or Username
Password
Remember
Login
Register
|
I forgot my password
All Activity
Questions
Hot!
Pending
Ask a Question
Privacy Policy
Contact
MYSQL stored procedures - Creation, Queries and data insertions
Home
Tutorials
Databases
MYSQL stored procedures - Creation, Queries and...
(adsbygoogle = window.adsbygoogle || []).push({});
+5
votes
264
views
MYSQL stored procedures - Creation, Queries and data insertions
asked
Jun 24, 2019
in
Databases
by
backtothefuture
(
552k
points)
reopened
Jun 24, 2019
|
264
views
answer
Your answer
Your name to display (optional):
Email me at this address if my answer is selected or commented on:
Email me if my answer is selected or commented on
Privacy: Your email address will only be used for sending these notifications.
Add answer
Cancel
1
Answer
+3
votes
Best answer
A stored procedure (Store Procedure), is a small algorithm in SQL language that is stored next to the database and allows to perform tasks on this data.
The Advantages of stored procedures are:
- They can be
accessed
from different external programs, if the need to publish the structure of the database.
- They can be
reused
, therefore we will gain time by being programmed and tested.
Use stored mysql procedures, with phpmyadmin
We will begin the work of creating and consulting stored procedures with the phpmyadmin tool, but anyone who supports SQL queries from MYSQL 5.0 can be used.
In this case we will take the database of a car or vehicle agency.
1) We enter phpmyadmin and from there to the database
There are 2 type engines that handle data in Mysql
MyISAM:
default engine, very fast for queries, does not provide data integrity, nor referential protection.
Ideal systems with many queries
InnoDB:
provides referential protection and data integrity in addition to blocking records, ideal if you are going to insert, edit or delete a lot of information constantly.
Generally for stored procedures it is better to use InnoDB.
In the SQL tab we create our first stored procedure to check the types of vehicles.
Write to the text field of sql.
CREATE PROCEDURE consult_type_home () ---> name of the procedure
SELECT * FROM vehicle_type ---> SQL to be resolved
If we execute the SQL query, we will receive a success message when the procedure is created.
To see the procedures created from the SQL tab, consult the SHOW PROCEDURE STATUS command, which will not show all the stored procedures.
Now we will show how to execute the stored procedure with the
CALL
command procedure_name (each programming language has its own library to access a stored procedure but they are all similar.
Here we can see the result of executing the stored procedure CALL pa_tipo_vehiculo, returned the result and nobody sees what commands have been executed.
In the following example we will list vehicles but by brand, the procedure would be:
CREATE PROCEDURE pa_vehiculos_por_marca (marca varchar (50))
SELECT * FROM
vehicles, brands
WHERE vehiculos.marca = marcas.id
AND brands.brand = brand
To the name of the procedure we add a variable to be able to look for example vehicles Honda brand
To execute the example we call the stored procedure in a SQL tab
CALL pa_vehiculos_por_marca ("Honda")
CALL pa_vehiculos_por_marca ("Ford")
You can also use the stored procedures for insertion tasks, example a procedure to record data from a client
CREATE PROCESS pa_cliente_insertar (
vname VARCHAR (64),
Vungal noun (64)
)
INSERT INTO client (name, surname) VALUES (vname, surname
);
To use it we call it in the following way
CALL pa_cliente_insertar (
'José', 'Gonzales');
Another could be Consult quantity of provinces
CREATE PROCEDURE `pa_provinces_quantity` ()
SELECT COUNT (*) as provincias FROM provinces
To remove any procedure, use
DROP PROCEDURE
procedure_name
Calls to procedures from different languages.
In
PHP
, assuming that the data comes from a form
$ mysqli = new mysqli ("localhost", "root", "root");
$ mysqli-> select_db ("auto-agency");
$ mysqli-> query ("CALL pa_cliente_insertar ('$ nombre', '$ surname')");
Now we execute the procedure stored in
Java
(the code has been cut
)
conn = ConnectionMySQL.connect ("127,0,0,1", "root", "*******", "root");
CallableStatement Procedure = conn.prepareCall ("{CALL pa_client_insertar ('$ name', '$ surname'))}");
Procedure.setString ("vname", $ name);
Procedure.setString ("surname", $ surname);
Procedure.execute ();
connM.commit ();
In this way it is demonstrated that the same procedures have been used in different environments and languages, in a transparent manner for the user.
The procedure topic is extensive but anyone with knowledge of SQL will be able to investigate and achieve great achievements with the use of stored procedures, thus being able to optimize their projects in time and data security.
answered
Jun 24, 2019
by
stackoverflow
(
3.5m
points)
edited
Jun 24, 2019
ask related question
comment
Your comment on this answer:
Your name to display (optional):
Email me at this address if a comment is added after mine:
Email me if a comment is added after mine
Privacy: Your email address will only be used for sending these notifications.
Add comment
Cancel
Related questions
+5
votes
1
answer
MySQL Conditional stored procedures IF - CASE
asked
Jun 23, 2019
in
Databases
by
backtothefuture
(
552k
points)
|
439
views
+4
votes
1
answer
How to install MySQL and MariaDB on Debian 10
asked
Aug 21, 2019
in
Databases
by
backtothefuture
(
552k
points)
|
372
views
+4
votes
1
answer
Mysql triggers or trigger - Defining events and moments
asked
Jun 23, 2019
in
Databases
by
backtothefuture
(
552k
points)
|
234
views
+5
votes
1
answer
Warehouse procedures with parameters from PHP
asked
Jun 24, 2019
in
Databases
by
backtothefuture
(
552k
points)
|
219
views
+4
votes
1
answer
Disable MySQL Installer is running in Community mode Windows 10 CMD window | ManifestUpdate
asked
Feb 18, 2021
in
Databases
by
backtothefuture
(
552k
points)
|
396
views
Sponsored articles cost $40 per post. You can contact us via
Feedback
All categories
Tutorials
7.5k
Microsoft
1.9k
Android
1.4k
Security
120
Linux / Unix
543
Internet
757
Virtualization
104
Apple
611
Networks
64
Other Devices
321
Other Applications
184
Hardware
19
Development
53
Digital Marketing
47
Databases
14
Graphic Design
30
Guides
794
GraphicDesign
54
Networking
4
PlayStation
186
Gaming
55
Linux/Unix
85
Manzana
33
Otherdevices
38
Otherapps
49
Digitalmarketing
39
Safety
1
Developing
2
Help
685
Social Networks
34
Android Tutorial
549
iPhone Tutorial
267
News
17
Social
6
Phone
11
Telephone
9
Applications
167
Smartphones
3
Cell Phones
11
Applications
25
Travels
6
Photo
21
Education
18
Games
25
Internet
14
Music
8
Technique
10
Video
6
Windows
5
Apple
2
Cell Phones
3
TikTok
216
Trips
2
House
1
Operating System
5
Tips & Tricks
892
Solutions
6
Tutorials
3
FAQ
1
Applications
5
Cell Phones
2
Tutorials
4
Computers
6
Tutoriales
2
8
Technology
2
In Computers
1
In Applications
2
Tutorials Tutorials
9
Applications Applications
25
Aplicaciones Applications
4
Devices Devices
5
Tutoriales Tutorials
1
Tutorials u00a0 Tutorials
2
Applications u00a0 Applications
2
Devices u00a0 Devices
1
OS OS
1
Etc Etc
2
Most popular questions within the last 30 days
Please leave a comment about whether the solution works or not (with device model)
[X]Close
10,659
questions
10,791
answers
510
comments
3
users