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

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.


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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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.

Atom editor mac version download
The most popular open source editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
