Heim >Datenbank >MySQL-Tutorial >mysql数据库动态创建表_MySQL

mysql数据库动态创建表_MySQL

WBOY
WBOYOriginal
2016-06-01 13:40:431555Durchsuche

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn