mysql 儲存過程 多個表新增主鍵 自增

2021-09-26 13:16:43 字數 1497 閱讀 3205

傳入資料庫名 以id為主鍵

create definer=`root`@`localhost` procedure `newproc`(in `dataname` varchar(50))

begin

declare taname varchar(50) default '';

declare `@sql2` varchar(2048) default '';

declare countnum int default 0;

declare i int default 1;

declare hasprimarynum int default 0;

drop temporary table if exists tablename;

create temporary table tablename(id int primary key not null auto_increment,tablenames varchar(50));

#select databasesname;

#查詢資料庫所有表名

select dataname;

insert tablename(tablenames) select table_name from information_schema.tables where table_schema = dataname;

set countnum = found_rows();

select *,countnum from tablename;

#select countnum;

#迴圈獲取表名

while i <= countnum do

select tablenames into taname from tablename where id = i;

select taname;

#判斷表是否有主鍵

select count(*) from information_schema.`key_column_usage` where table_name='classes' and constraint_name='primary' and constraint_schema=dataname;

select hasprimarynum;

if hasprimarynum < 1 then

#沒有主鍵

#新增主鍵 自增 id 自己選擇合適的

set @sqlstr = concat("alter table ",`taname`," change id id int not null auto_increment primary key");

prepare stmt from @sqlstr;

execute stmt;

set @sqlstr='';

end if;

set i = i + 1;

end while;

end

mysql 儲存過程建立多個表

要建立64個表,聽到這一需求,必然要用儲存過程。直接上 delimiter use table drop procedure if exists p create definer root localhost procedure p begin declare i int declare table...

MySQL新增儲存過程

1.mysql的儲存過程與sqlserver是不同的。而且mysql相對比較輕量化,在寫的過程中,對函式與儲存過程的區分不是特別明顯。標準語法結構 delimiter 或 delimiter 這個地方沒別的意思,就是重新定義結束標識。將 變成了 begin delete from matches w...

mysql儲存過程表 mysql儲存過程和表命令

show procedure status 2.顯示某個儲存過程的詳細資訊 sp為儲存過程名稱 show create procedure sp 3.顯示當前庫中所有表 show tables 4.顯示某個表的建表語句 test為表名 show create table test 5.刪除儲存過程 ...