mysql數據庫動態創建表
大家一般可能很少有這種需求吧,我以前也沒有遇到過,但這次做項目需要這麼做。
就是表的字段名和字段數都不是固定的,要根據需要來創建。
www.2cto.com
這是我的創建形成過程,大家照著演示一下就知道了我的動態表的來龍去脈了。
第一步.創建相關表
/*---建立所有指標信息的臨時表---*/
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,''); www.2cto.com
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: 這裡變量返回值長度受限,字段不多時可以。
www.2cto.com
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;
www.2cto.com
存儲過程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;
www.2cto.com
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; www.2cto.com
until @mycount<=0
END REPEAT;
CLOSE cursor_Title;
ALTER table Title drop column mid;
end proc;
call INTERBANKBONDQUOTE_TSP_Title;