MySql stored procedure dynamically creates a table and inserts data _ MySQL-mysql tutorial

Source: Internet
Author: User
Tags mysql tutorial
The MySql stored procedure dynamically creates a table and inserts data to a recently created Project. The database uses MySql, which is not very skillful in MySql, but can be simply used, after all, simple SQL statements are still the same, but as the project goes deeper into complicated SQL statements, MySql stored procedures are gradually used to dynamically create tables and insert data.

In a recent project, the database uses MySql, which is not very skilled in MySql, but simple application. after all, simple SQL statements are still the same, however, as the project goes deeper into complex SQL statements, one of the minor difficulties is to dynamically create a table based on the date of the day and insert data into it.

The specific logic can be understood as: first go to the database to check whether there is a record table of the day, if not, create a table, and then execute the log writing of the day.

Mysql visualization tools we use navicat, which is relatively easy to use. of course, it is not as user-friendly as sqlserver (many shortcut keys are not supported, and the operation is cumbersome ). Next I will talk about how to use navicat to dynamically create database tables and insert data.

I. create a stored procedure

II. use the concat function to concatenate and execute statements for dynamic table creation

III. use the concat function to concatenate and execute a statement to write data to a dynamically created table.

Because database tables are in the form of a fixed prefix and the current date, no matter whether you create a table or write data, it is based on the dynamic table, therefore, you cannot write simple insert statements like the original fixed table. In addition, the parameter id passed to the stored procedure cannot be directly passed to the stored insert statement, and an SQL statement error will be reported, therefore, it may not be the best way to introduce local parameters, but the author has not yet found a better way to write the code. if you have any better ideas or want to improve your skills, we hope to talk more.

For mysql, we can't ignore its power. mysql still has many things to learn. since the record table is generated dynamically every day, a job is required, to clear or back up database tables according to certain time rules. next we will study mysql job.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.