Stored Procedures
[Create is to create a stored procedure, alter is to modify and change a stored procedure]
[Use create when writing a stored procedure for the first time. If you modify the stored procedure program, replace create with alter and then execute]
[In the database, begin and end mean braces]
·Format for creating stored procedures:
--(procedure can be abbreviated as proc) proc means program and step. Followed by the stored procedure name
create proc The stored procedure name
as
Code block
Go
--exec means execution. Execute stored procedure
Exec Stored procedure name
---------Modify stored procedure
alter proc hehe ---alter means change, change
as
select student number, Chinese score from fenshu
go
exec hehe
-------------Query multiple tables
create proc chaxun
as
begin
select * from fenshu
select * from jiaoshi
select * from xuesheng
end
go
exec chaxun
--------------Stored procedure with parameters
create proc chucunguocheng
@yican varchar(20), @yican means formal parameter
@ercan varchar(20)
as
begin
print @yican+@ercan
end
go
exec chucunguocheng 'Hello','China'
Example question:
-------Enter the student number to determine whether the student is excellent, graduated, or not (passing three courses is considered excellent, passing two courses is considered completed)
alter proc biye
@xuehao int — Create input variables
as
begin
declare @y int
declare @s int
declare @w int
declare @zongshu int
select @y=COUNT(*) from fenshu where student ID=@xuehao and Chinese score>=60
select @s=COUNT(*) from fenshu where student number=@xuehao and math score>=60
select @w=COUNT(*) from fenshu where student number=@ xuehao and English score>=60
set @zongshu=@y+@s+@w
if @zongshu=3
print 'Excellent'
if @zongshu =2
print 'Complete'
if @ zongshu=1
print'Not completed'
if @zongshu=0
Print''Input error'
end
go
exec biye 1
The result is:
----- --- Comprehensive exercises
(Comprehensive training of stored procedures)
Create a goods table: number, goods name, unit, price, inventory quantity, remarks. (10 pieces of data) After
, purchase the goods. If the goods already exist, increase the quantity. Otherwise, add them to the database table.
Shipping, if someone wants the goods, judge whether the quantity is sufficient and reduce the inventory enough, otherwise we will inform you of the shortage.
Delete the data in the database at any time based on the name. If there is any, delete it. If not, we will notify you.
------------Create database and data table, and insert data----------
create database notebook
go
create table bijiben
(
Number int,
Name nvarchar(20),
Remarks varchar(20),
Price int,
Inventory int,
Unit nvarchar(10)
)
go to -----(Random ranking)------
insert into bijiben values(1,'Apple','macbook',12000,10,'United States')
insert into bijiben values(2,' Acer','acer',3500,20,'China Taiwan')
insert into bijiben values(3,'Asus','asus',3500,25,'China')
insert into bijiben values(4, 'Dell','dell',4300,30,'United States')
insert into bijiben values(5,'Shenzhou','hass',4000,20,'China')
insert into bijiben values(6, 'Lenovo','lenovo',4200,30,'China')
insert into bijiben values(7,'HP','ph',3600,20,'United States')
insert into bijiben values(8, 'Samsung','samsung',3700,10,'Japan')
insert into bijiben values(9,'Sony','sony',7000,10,'Japan')
insert into bijiben values(10, 'Toshiba','toshiba',3200,10,'Japan')
select *from bijiben
-------------------------Incoming stock- -----------------------
create proc jinhuo --Create purchase stored procedure
@bianhao int, --Purchasing number
@bjbn nvarchar( 20),--notebook name
@beizhu nvarchar(20),--remarks
@jiage int,--price
@jinhuo int,--how many units
@danwei nvarchar(20)--unit
as
begin
declare @ybjbn nvarchar(20),@ykc int --@ykc is the original inventory number in the data
select @ybjbn=count(name) from bijiben where name=@bjbn
if @ybjbn=0 - -When there is no entered data in the database
begin
insert into bijiben values(@bianhao,@bjbn,@beizhu,@jiage,@jinhuo,@danwei)
print'New computer added successfully! '
end
else if @ybjbn=1 --When there is input data in the database
begin
select @ykc=stock from bijiben where name=@bjbn
set @ykc=@ykc+@jinhuo
B Update bijiben set inventory =@ykc where name =@bjbn prop' This computer inventory is successfully added! ' Endendgoexec jinhuo 11,'Dell','dell',4200,10,'United States'---------------- -----Shipping--------------------------------create proc chuhuo --Create shipping stored procedure@name nvarchar(20 ), --The name of the notebook to be shipped@shuliang int --The quantity to be shippedasbegin declare @ygeshu int,@hgeshu int --@ygeshu is the original inventory of the database, @hgeshu transaction The remaining inventory after select @ygeshu=stock from bijiben where name=@name if @shuliang>@ygeshu --When the quantity shipped is greater than the quantity in stock print 'Sorry, insufficient stock~~'else begin set @hgeshu=@ygeshu-@shuliang update bijiben set inventory=@hgeshu where name=@name --modify the inventory number after the transaction print 'Transaction successful! ' Endendgoexec chuhuo 'Apple',11---------------------------Delete one Notebook data -------create proc qingchu@scbjbn nvarchar(20) --The name of the notebook to be deletedasbegin declare @sgeshu int --The individual of the notebook to be found Count select @sgeshu=COUNT(*) from bijiben where name=@scbjbn if @sgeshu=1 begin delete from bijiben where name=@scbjbn print'The notebook's data was deleted successfully! ' end if @sgeshu=0 print 'No notebook with this name found~~'endexec qingchu 'Apple'

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)