MySQL schema can be exported to GitHub markdown format with the help of stored procedures. Here's an example using two stored procedures, which can be chained together to achieve the desired output.
Chain of Stored Procedures
The first stored procedure prepares the data for reporting and assigns a session number to keep the output isolated.
CREATE PROCEDURE `Reporting101a`.`describeTables_v2a`( IN dbName varchar(100), -- the dbname to report table structures OUT theSession int, -- OUT parameter for session# assigned IN deleteSessionRows BOOL, -- true for delete rows when done from main reporting table for this session# IN callTheSecondStoredProc BOOL -- TRUE = output is from Pretty output in Second Stored Proc. FALSE= not so pretty output ) BEGIN DECLARE thisTable CHAR(100); DROP TEMPORARY TABLE IF EXISTS Reporting101a.tOutput; CREATE TEMPORARY TABLE Reporting101a.tOutput ( id int auto_increment primary key, tblName varchar(100) not null, ordVal int not null, cField varchar(100) not null, cType varchar(100) not null, cNull varchar(100) not null, cKey varchar(100) not null, cDefault varchar(100) null, cExtra varchar(100) null ); INSERT Reporting101a.tOutput(tblName,ordVal,cField,cType,cNull,cKey,cDefault,cExtra) SELECT TABLE_NAME,ORDINAL_POSITION,COLUMN_NAME AS Field, COLUMN_TYPE AS TYPE, RPAD(IS_NULLABLE,4,' ') AS 'Null', RPAD(COLUMN_KEY,3,' ') AS 'Key',RPAD(COLUMN_DEFAULT,7,' ') AS 'DEFAULT',EXTRA AS Extra FROM information_schema.columns WHERE table_schema = dbName ORDER BY table_name,ordinal_position; -- select * from information_schema.columns WHERE table_schema = '57security' order by table_name,ordinal_position; ... END$$
The second stored procedure utilizes a cursor and a special table to generate the pretty output, resembling MySQL's DESCRIBE command.
CREATE PROCEDURE `Reporting101a`.`Print_Tables_Like_Describe`( pSessionId INT ) BEGIN DECLARE done INT DEFAULT FALSE; ... CREATE TABLE IF NOT EXISTS Reporting101a.reportOutput ( lineNum INT AUTO_INCREMENT PRIMARY KEY, sessionId INT NOT NULL, lineOut varchar(100) NOT NULL ); ... END$$
Usage
Call the first stored procedure with the required parameters.
SET @theOutVar =-1; -- A variable used as the OUT variable below -- See **Note3** -- Note: with `TRUE` as the 4th parameter, this is a one call deal. Meaning, you are done. call Reporting101a.describeTables_v2a('stackoverflow',@theOutVar,false,true); -- See **Note4** -- Primarily used if the 4th parameter above is false call Reporting101a.Print_Tables_Like_Describe(@theOutVar); -- loads data for prettier results in chunk format.
Output
The output will be generated in the reportOutput table and can be retrieved as follows:
select lineOut as '' from Reporting101a.reportOutput where sessionId=pSessionId order by lineNum;
The above is the detailed content of How Can I Export a MySQL Schema to GitHub Markdown Using Stored Procedures?. For more information, please follow other related articles on the PHP Chinese website!