bitsCN.com
mysql数据库动态创建表 大家一般可能很少有这种需求吧,我以前也没有遇到过,但这次做项目需要这么做。 就是表的字段名和字段数都不是固定的,要根据需要来创建。 这是我的创建形成过程,大家照着演示一下就知道了我的动态表的来龙去脉了。 第一步.创建相关表 /*---建立所有指标信息的临时表---*/ drop table if exists INTERBANKBONDQUOTE_SClass; create table INTERBANKBONDQUOTE_SClass ( Name varchar(50) not null, id int Primary key, Parent int, Value varchar(50) );
insert into INTERBANKBONDQUOTE_SClass(Name,id,Value)values('最新成交', 0,''); insert into INTERBANKBONDQUOTE_SClass(Name,id,Value)values('买入信息', 1,''); insert into INTERBANKBONDQUOTE_SClass(Name,id,Value)values('买卖价差', 2,''); insert into INTERBANKBONDQUOTE_SClass(Name,id,Value)values('卖出信息', 3,''); insert into INTERBANKBONDQUOTE_SClass(Name,id,Parent,Value)values('中债最新估值', 4, -1,''); insert into INTERBANKBONDQUOTE_SClass(Name,id,Value)values('含权债行权指标', 5,''); insert into INTERBANKBONDQUOTE_SClass(Name,id,Value)values('基础指标', 6,''); insert into INTERBANKBONDQUOTE_SClass(Name,id,Value)values('双边报价笔数', 7,''); insert into INTERBANKBONDQUOTE_SClass(Name,id,Parent,Value)values('报价方', 8, 1, 'col4'); insert into INTERBANKBONDQUOTE_SClass(Name,id,Parent,Value)values('报价方会员号', 9, 1, 'col5'); insert into INTERBANKBONDQUOTE_SClass(Name,id,Parent,Value)values('报价时间', 10, 1, 'col6'); insert into INTERBANKBONDQUOTE_SClass(Name,id,Parent,Value)values('匿名', 11, 1, 'col7'); 第二步:/*---建立显示数据表格标题的表---*/ drop table if exists INTERBANKBONDQUOTE_T; create table INTERBANKBONDQUOTE_T as select concat('/'',a.Name,',',b.Name,'/' __TITLE__',b.Value); 第三步: 这里有两种实现方法。因为在mysql中,这时的变量长度受到了限制 ,本来应该longtext足够长的,可实际只返回了限制长度的。如果字段太多了,就要用第二种方法。 存储过程A: 这里变量返回值长度受限,字段不多时可以。 DELIMITER $$ DROP PROCEDURE IF EXISTS `dzhappdb_bond`.`INTERBANKBONDQUOTE_TSP`$$ CREATE PROCEDURE INTERBANKBONDQUOTE_TSP () BEGIN DECLARE objs1 TEXT; DECLARE objs TEXT; SELECT GROUP_CONCAT(col1) INTO objs1 FROM T; SET objs =CONCAT('CREATE TABLE Title AS SELECT ',objs1); SET @sql_txt = objs;
PREPARE stmt FROM @sql_txt; EXECUTE stmt; DEALLOCATE PREPARE stmt; END$$ call INTERBANKBONDQUOTE_TSP; 存储过程B:这样不管字段多少,只要数据库支持就可以创建成功。但不如第一个方法简洁。 DROP PROCEDURE IF EXISTS INTERBANKBONDQUOTE_TSP_Title; drop table if EXISTS Title; CREATE PROCEDURE INTERBANKBONDQUOTE_TSP_Title() proc:begin DECLARE add_sql LONGTEXT; DECLARE insert_sql LONGTEXT; DECLARE nhh_sql varchar(200); DECLARE column_name varchar(100); DECLARE column_value varchar(100); DECLARE mycount int; DECLARE len int; DECLARE strlen int; DECLARE cursor_Title CURSOR for select col1 from INTERBANKBONDQUOTE_T; create table Title(mid int); insert into Title values (100); select count(col1) into @mycount from INTERBANKBONDQUOTE_T; OPEN cursor_Title; REPEAT FETCH cursor_Title INTO nhh_sql; begin set @mycount=@mycount-1; set @strlen=CHARACTER_LENGTH(nhh_sql); set @len=INSTR(nhh_sql,' '); set @column_name=RIGHT(nhh_sql,@strlen-@len); set @column_value=LEFT(nhh_sql,@len); set @add_sql=CONCAT('ALTER table Title add COLUMN ',@column_name,' varchar(100)'); set @insert_sql=CONCAT('update Title set ',@column_name,'=',@column_value,' where mid=100'); PREPARE stmt1 FROM @add_sql; EXECUTE stmt1; PREPARE stmt2 FROM @insert_sql; EXECUTE stmt2; DEALLOCATE PREPARE stmt1; DEALLOCATE PREPARE stmt2; end; until @mycount

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

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