MySQL sorting table by column names - mysql

I have already built a table with field names in arbitrary order. I want those field names to be in alphabetical order so that I can use them in my dropdown list. Is it possible with a query?

Select columns from a specific table using INFORMATION_SCHEMA.COLUMNS and sort alphabetically with ORDER BY:
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = '[schemaname]'
AND table_name = '[tablename]'
ORDER BY column_name

Note: The following code will alter the specified table and reorder the columns in alphabetical order
This should do the trick. It's a bit messy and lengthy, and you'll have to change the database name and table name, but for this one, the only requirement is that there is a database named "test" and that you are running these commands in it:
Let's create the tables we need:
-- CREATE TESTING TABLE IN A DATABASE NAMED "test"
DROP TABLE IF EXISTS alphabet;
CREATE TABLE alphabet (
d varchar(10) default 'dee' not null
, f varchar(21)
, e tinyint
, b int NOT NULL
, a varchar(1)
, c int default '3'
);
-- USE A COMMAND STORAGE TABLE
DROP TABLE IF EXISTS loadcommands;
CREATE TABLE loadcommands (
id INT NOT NULL AUTO_INCREMENT
, sqlcmd VARCHAR(1000)
, PRIMARY KEY (id)
);
Now let's create the two stored procedures required for this to work:
Separating them since one will be responsible for loading the commands, and including a cursor to immediately work with it isn't plausible (at least for me and my mysql version):
-- PROCEDURE TO LOAD COMMANDS FOR REORDERING
DELIMITER //
CREATE PROCEDURE reorder_loadcommands ()
BEGIN
DECLARE limitoffset INT;
SET #rank = 0;
SET #rankmain = 0;
SET #rankalter = 0;
SELECT COUNT(column_name) INTO limitoffset
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'test'
AND table_name = 'alphabet';
INSERT INTO loadcommands (sqlcmd)
SELECT CONCAT(t1.cmd, t2.position) AS commander FROM (
SELECT #rankalter:=#rankalter+1 AS rankalter, CONCAT('ALTER TABLE '
, table_name, ' '
, 'MODIFY COLUMN ', column_name, ' '
, column_type, ' '
, CASE
WHEN character_set_name IS NOT NULL
THEN CONCAT('CHARACTER SET ', character_set_name, ' COLLATE ', collation_name, ' ')
ELSE ' '
END
, CASE
WHEN is_nullable = 'NO' AND column_default IS NULL
THEN 'NOT NULL '
WHEN is_nullable = 'NO' AND column_default IS NOT NULL
THEN CONCAT('DEFAULT \'', column_default, '\' NOT NULL ')
WHEN is_nullable = 'YES' THEN 'DEFAULT NULL '
END
) AS cmd
, column_name AS columnname
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'test'
AND table_name = 'alphabet'
ORDER BY columnname
) t1
INNER JOIN (
SELECT #rankmain:=#rankmain+1 AS rownum, position FROM (
SELECT 0 AS rownum, 'FIRST' AS position
, '' AS columnname
UNION
SELECT #rank:=#rank+1 AS rownum, CONCAT('AFTER ', column_name) AS position
, column_name AS columnname
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'test'
AND table_name = 'alphabet'
ORDER BY columnname
LIMIT limitoffset
) inner_table
) t2 ON t1.rankalter = t2.rownum
;
END//
DELIMITER ;
If anyone thinks/sees that I'm missing to include any important column attributes in the ALTER command, please hesitate not and mention it! Now to the next procedure. This one just executes the commands following the order of column id from the loadcommands table. :
-- PROCEDURE TO RUN EACH REORDERING COMMAND
DELIMITER //
CREATE PROCEDURE reorder_executecommands ()
BEGIN
DECLARE sqlcommand VARCHAR(1000);
DECLARE isdone INT DEFAULT FALSE;
DECLARE reorderCursor CURSOR FOR
SELECT sqlcmd FROM loadcommands ORDER BY id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET isdone = TRUE;
OPEN reorderCursor;
read_loop:LOOP
FETCH reorderCursor INTO sqlcommand;
IF isdone THEN
LEAVE read_loop;
END IF;
SET #sqlcmd = sqlcommand;
PREPARE stmt FROM #sqlcmd;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP read_loop;
CLOSE reorderCursor;
END//
DELIMITER ;
The SQL is long, so if someone can point out ways (and has tested them) to make this shorter I'd gladly do it, but for now, this at least works on my end. I also didn't need to put dummy data in the alphabet table. Checking the results can be done using the SHOW... command.
The last part:
-- TO TEST; AFTER RUNNING DDL COMMANDS:
SHOW CREATE TABLE alphabet; -- SEE ORIGINAL ORDER
CALL reorder_loadcommands(); -- PREPARE COMMANDS
CALL reorder_executecommands(); -- RUN COMMANDS
SHOW CREATE TABLE alphabet; -- SEE NEW ORDER
Perhaps later on I could make reorder_loadcommands dynamic and accept table and schema parameters, but I guess this is all for now..

Related

modify format of multiple database columns at once

I have imported a csv table into sql db using phpmyadmin. I guess the default format is decimal(8,5), or at least that is how it comes out. Seems overkill and I thought I could reduce to 4,1. Problem is there are around 470 fields. I know how to change one at a time but this would take a long time. Is there a faster way?
I only have Sql Server 2008 R2, but it seems the MySQL solution may be similar. I've tried to align it as closely with MySQL as I can, but without a MySQL engine I can't check it ... So for what it's worth, here's my script (tested in Sql Server):
-- I believe this is 'CREATE TEMPORARY TABLE' in MySql
create table #tbl
(
ownerName sysname -- `sysname` is nvarchar(128) the max identifier length
, tableName sysname
, colName sysname
, colType sysname
, colPrecision int
, colScale int
)
insert #tbl
select table_schema, table_name, column_name, data_type, numeric_precision, numeric_scale
FROM information_schema.columns
where data_type = 'decimal' and numeric_precision = 8 and numeric_scale = 5
-- Note: It is 'MODIFY COLUMN' in MySQL
declare #newPrecision int = 4, #newScale int = 1
DECLARE #sql varchar(max) = ''
select #sql = #sql + CHAR(13) + CHAR(10) + 'ALTER TABLE ' + ownerName + '.' + tableName + ' ALTER COLUMN '
+ colName + ' decimal(' + cast(#newPrecision as varchar(max)) + ',' + cast(#newScale as varchar(max)) + ')'
from #tbl
/**
-- In MySql, GROUP_CONCAT() may work
select #sql = GROUP_CONCAT( 'ALTER TABLE ' + ownerName + '.' + tableName + ' MODIFY COLUMN '
+ colName + ' decimal(' + cast(#newPrecision as varchar(max)) + ',' + cast(#newScale as varchar(max)) + ')' SEPARATOR ' ' )
from #tbl
**/
print #sql
execute ( #sql )
I'd never used SqlFiddle before so thought it was a good time to start, to give a complete answer specifically for MySQL 5.6. Please see http://sqlfiddle.com/#!9/19f46/1
At the bottom of Build Schema panel, select Delimiter = [ // ]. SqlFiddle uses this to decide when to send off a chunk of code to MySQL. It is necessary because the CREATE PROCEDURE has to be sent off as one chunk. If you use the default Delimiter = [ ; ], then it will send off only part of the CREATE PROCEDURE up to the first ; it finds.
The table cols selects columns of type decimal with the precision and scale you want to change. Currently hardcoded as 8 and 5 as the OP requested, but change as required to identify only the columns you want to modify. It would be a good idea before you do the table modifications to run this select to verify which columns you will be modifying.
The stored procedure exec_multiple uses the table cols to generate ALTER TABLE statements which are then dynamically executed.
EXECUTE only handles one statement at a time, so you need to iterate through the rows of cols and apply each ALTER TABLE individually. The auto_increment column id in cols allows you to select each row in turn without the use of a cursor.
The table test_log captures any debugging information you may want to examine after the Build Schema is complete.
The following goes in the Build Schema panel on the left.
All of the logic needs to be in this panel because SqlFiddle does not allow Data Definition Language or table insert/update/delete statements in the Run SQL panel.
create table if not exists cols
(
id int auto_increment primary key
, ownerName varchar(128)
, tblName varchar(128)
, colName varchar(128)
, colType varchar(128)
, colPrecision int
, colScale int
) //
create table if not exists test_table
(
testDecimal1 decimal(8,5)
, testDecimal2 decimal(8,5)
) //
create table if not exists test_table2
(
testDecimal3 decimal(8,5)
, testDecimal4 decimal(8,5)
) //
create table if not exists test_log
(
msg varchar(1024)
) //
INSERT INTO cols
( ownerName, tblName, colName, colType, colPrecision, colScale )
SELECT TABLE_SCHEMA, `TABLE_NAME`, COLUMN_NAME, DATA_TYPE, NUMERIC_PRECISION, NUMERIC_SCALE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE = 'decimal' AND NUMERIC_PRECISION = 8 AND NUMERIC_SCALE = 5 //
insert test_log( msg) select database() //
CREATE PROCEDURE `exec_multiple` ( newPrecision int, newScale int)
BEGIN
declare n int;
declare nrows int;
declare sql_stmt varchar(1024);
set n = 1;
select count(*) from cols into nrows;
while n <= nrows do
select CONCAT('ALTER TABLE '
, ownerName, '.', tblName, ' MODIFY COLUMN '
, colName, ' decimal(', newPrecision, ','
, newScale, ')') into sql_stmt from `cols` where id = n;
SET #sql_stmt := sql_stmt; -- not sure why this is necessary
insert test_log( msg ) select #sql_stmt;
PREPARE dynamic_statement FROM #sql_stmt;
EXECUTE dynamic_statement;
DEALLOCATE PREPARE dynamic_statement;
set n = n + 1;
end while;
END //
call exec_multiple(4, 1) //
This goes in the Run SQL panel on the right
select * from test_log;
SELECT TABLE_SCHEMA, `TABLE_NAME`, COLUMN_NAME, DATA_TYPE, NUMERIC_PRECISION, NUMERIC_SCALE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE = 'decimal'
;
select * from cols;

How to loop through all the tables on a database to update columns

I'm trying to update a column (in this case, a date) that is present on most of the tables on my database. Sadly, my database has more than 100 tables already created and full of information. Is there any way to loop through them and just use:
UPDATE SET date = '2016-04-20' WHERE name = 'Example'
on the loop?
One painless option would be to create a query which generates the UPDATE statements you want to run on all the tables:
SELECT CONCAT('UPDATE ', a.table_name, ' SET date = "2016-04-20" WHERE name = "Example";')
FROM information_schema.tables a
WHERE a.table_schema = 'YourDBNameHere'
You can copy the output from this query, paste it in the query editor, and run it.
Update:
As #PaulSpiegel pointed out, the above solution might be inconvenient if one be using an editor such as HeidiSQL, because it would require manually copying each record in the result set. Employing a trick using GROUP_CONCAT() would give a single string containing every desired UPDATE query in it:
SELECT GROUP_CONCAT(t.query SEPARATOR '; ')
FROM
(
SELECT CONCAT('UPDATE ', a.table_name,
' SET date = "2016-04-20" WHERE name = "Example";') AS query,
'1' AS id
FROM information_schema.tables a
WHERE a.table_schema = 'YourDBNameHere'
) t
GROUP BY t.id
You can use SHOW TABLES command to list all tables in database. Next you can check if column presented in table with SHOW COLUMNS command. It can be used this way:
SHOW COLUMNS FROM `table_name` LIKE `column_name`
If this query returns result, then column exists and you can perform UPDATE query on it.
Update
You can check this procedure on sqlfiddle.
CREATE PROCEDURE UpdateTables (IN WhereColumn VARCHAR(10),
IN WhereValue VARCHAR(10),
IN UpdateColumn VARCHAR(10),
IN UpdateValue VARCHAR(10))
BEGIN
DECLARE Finished BOOL DEFAULT FALSE;
DECLARE TableName VARCHAR(10);
DECLARE TablesCursor CURSOR FOR
SELECT c1.TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS c1
JOIN INFORMATION_SCHEMA.COLUMNS c2 ON (c1.TABLE_SCHEMA = c2.TABLE_SCHEMA AND c1.TABLE_NAME = c2.TABLE_NAME)
WHERE c1.TABLE_SCHEMA = DATABASE()
AND c1.COLUMN_NAME = WhereColumn
AND c2.COLUMN_NAME = UpdateColumn;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET Finished = TRUE;
OPEN TablesCursor;
MainLoop: LOOP
FETCH TablesCursor INTO TableName;
IF Finished THEN
LEAVE MainLoop;
END IF;
SET #queryText = CONCAT('UPDATE ', TableName, ' SET ', UpdateColumn, '=', QUOTE(UpdateValue), ' WHERE ', WhereColumn, '=', QUOTE(WhereValue));
PREPARE updateQuery FROM #queryText;
EXECUTE updateQuery;
DEALLOCATE PREPARE updateQuery;
END LOOP;
CLOSE TablesCursor;
END
This is just an example how to iterate through all tables in database and perform some action with them. Procedure can be changed according to your needs.
Assuming you are using MySQL, You can use Stored Procedure.
This post is a very helpful.
Mysql-loop-through-tables

Check MySQL database for unique value over many tables

I'm looking for a way to easily check each table of a MySQL database and make sure that a certain field contains one value only. I have tables named Authors, Titles, Places, etc.
Each table contains a field called xuser and it needs to ask "does the field xuser contain the value xy in all records of all tables".
Can someone push me in the right direction how to do this with a SQL query if this is possible?
Thanks for reading, regards
Nico
I've created stored procedure which checks all table for provided db:
DELIMITER $$
DROP PROCEDURE IF EXISTS `UTL_CHECK_BACKUP_FOR_USER` $$
CREATE PROCEDURE `UTL_CHECK_BACKUP_FOR_USER`(
IN i_database_name VARCHAR(255),
IN i_user_column_name VARCHAR(255),
IN i_user_column_value VARCHAR(255),
OUT o_result TINYINT(1)
)
BEGIN
DECLARE v_table_name VARCHAR(255);
DECLARE v_last_row_fetched TINYINT(3) DEFAULT 0;
DECLARE tables_cursor CURSOR FOR
SELECT table_name
FROM information_schema.tables
WHERE table_schema = i_database_name
;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_last_row_fetched = 1;
SET v_last_row_fetched = 0;
OPEN tables_cursor;
SET #query =
CONCAT(
'SELECT SUM(IF(user_column=''',
i_user_column_value,
''', 1, -1)) = 1 INTO #o_result FROM ( SELECT ''test'' AS user_column FROM information_schema.tables WHERE 1<>1 '
)
;
table_loop: LOOP
FETCH tables_cursor INTO v_table_name;
IF (v_last_row_fetched = 1) THEN
LEAVE table_loop;
END IF;
SET #query =
CONCAT(
#query,
' UNION SELECT DISTINCT(',
i_user_column_name,
') AS user_column FROM ',
v_table_name
)
;
END LOOP table_loop;
CLOSE tables_cursor;
SET v_last_row_fetched=0;
SET #query =
CONCAT(
#query,
' ) all_xusers;'
)
;
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET o_result = COALESCE(#o_result, 0);
END $$
DELIMITER ;
Just deploy this stored procedure to database.
And then it could be executed in the following way:
-- db_name, user_column_name, user_column_value, result
call UTL_CHECK_BACKUP_FOR_USER('test', 'xuser', 'xxx', #result);
select #result;
To get the rows from all three tables where xuser has the same value in all three tables you could use:
SELECT *
FROM authors a
JOIN titles t
ON t.xuser = a.xuser
JOIN places p
ON p.xuser = t.xuser
If you want to look at a specific xuser value you could add the following WHERE clause:
WHERE a.xuser = 'xy'
The first thing comes to my mind:
select sum(if(xuser='xxx', 1, -1)) = 1
from (
select distinct(xuser) from authors
union
select distinct(xuser) from titles
union
select distinct(xuser) from places
) all_xusers;
This will return 1 (true) if all tables contains records belonging ONLY to 'xxx' user. Otherwise (if there is no 'xxx' records or there is some other user records) it will return 0 (false).

Cursor is becoming an never ending loop

I want to create an SP to generate the metadata for all tables using cursors in SQL. Following is the code I have tried. but its becoming a never ending loop and same data is repeated. Thanks in advance.
--SELECT * FROM information_schema.columns
ALTER PROCEDURE p1
AS
SET NOCOUNT ON;
DECLARE #id INT
, #tablename VARCHAR(100)
, #columnname VARCHAR(100)
, #datatype VARCHAR(100)
, #isnullable VARCHAR(100)
BEGIN
DECLARE CURSOR_1 CURSOR FOR
SELECT TABLE_NAME
, COLUMN_NAME
, DATA_TYPE
, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Employee' -- group BY table_name
OPEN CURSOR_1
FETCH NEXT FROM CURSOR_1 INTO
#tablename,
#columnname,
#datatype,
#isnullable
WHILE ##fetch_status = 0
BEGIN
INSERT INTO table_schema_detail (TABLE_NAME, COLUMN_NAME, DATA_TYPE, isnullable)
VALUES (#tablename, #columnname, #datatype, #isnullable)
END
FETCH NEXT FROM CURSOR_1 INTO
#tablename,
#columnname,
#datatype,
#isnullable
CLOSE CURSOR_1
DEALLOCATE CURSOR_1
SET NOCOUNT OFF;
END
GO
I don't really understand why you need to store this information on a table, since it's already available on the system views (and if you still need to store this data, why are you using a cursor?). If, like your comment says, you need to store data from 3 tables, then you could simply do:
INSERT INTO table_schema_detail(table_name,column_name,data_type,isnullable)
SELECT table_name, column_name, data_type, is_nullable
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name IN ('Employee','OtherTable1','OtherTable2')
But again, I don't see the point. At least you could store the date when this was done:
INSERT INTO table_schema_detail(table_name,column_name,data_type,isnullable,DateInserted)
SELECT table_name, column_name, data_type, is_nullable, GETDATE()
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name IN ('Employee','OtherTable1','OtherTable2')
look where you have that end statement
insert INTO table_schema_detail(table_name,column_name,data_type,isnullable) VALUES(#tablename,#columnname,#datatype,#isnullable)
end
Suspect it never gets to this line
FETCH NEXT FROM CURSOR_1 into
And agree with other comments on is a cursor the proper approach but this is an answer to the question as stated.
I think get meta-data from sys.columns more preferable (in your case, cursor is not necessary):
INSERT INTO dbo.table_schema_detail
(
TABLE_NAME
, COLUMN_NAME
, DATA_TYPE
, IS_NULLABLE
)
SELECT
SCHEMA_NAME(o.[schema_id]) + '.' + o.name
, c.name
, TYPE_NAME(c.system_type_id)
, c.is_nullable
FROM sys.columns c
JOIN sys.objects o ON c.[object_id] = o.[object_id]
WHERE SCHEMA_NAME(o.[schema_id]) + '.' + o.name = 'dbo.Employee'
AND o.[type] = 'U'

MySQL Derterministic migration script

I am currently looking a way to have my database under version control. To achieve so, I wanted to have deterministic procedures that can only be run only once (with corresponding undo).
I have a problem building my first script which is riddled with small bugs.
Here are the 3 main parts :
Condition to execute query (if field doesn't exists)
SELECT *
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'my_database'
AND TABLE_NAME = 'my_table'
AND COLUMN_NAME = 'full_name'
The table alteration:
ALTER TABLE
my_table
ADD full_name VARCHAR(255) NOT NULL;
And finally the data migration
UPDATE candidat dest JOIN candidat src ON dest.id = src.id
SET dest.full_name = CONCAT(src.first_name, ' ', IF(src.middle_name='', '', CONCAT(src.middle_name, ' ')), src.last_name);
I'am trying to make this work in this form:
DELIMITER $$
DROP PROCEDURE IF EXISTS migration_001;
CREATE PROCEDURE migration_001()
BEGIN
IF NOT EXISTS (
SELECT *
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'my_database'
AND TABLE_NAME = 'my_table'
AND COLUMN_NAME = 'full_name')
THEN
ALTER TABLE
my_table
ADD full_name VARCHAR(255) NOT NULL;
UPDATE candidat dest JOIN candidat src ON dest.id = src.id
SET dest.full_name = CONCAT(src.first_name, ' ', IF(src.middle_name='', '', CONCAT(src.middle_name, ' ')), src.last_name);
END IF
END;
$$
Current error I am getting:
1064 : ... right syntax to use near 'CREATE PROCEDURE migration_001() BEGIN IF NOT EXISTS ( SELECT * ' at line 3
Can anyone point me in the right direction for solving this?
BTW I am using 5.5.16-log - MySQL Community Server.
Change the order of
DELIMITER $$
and
DROP PROCEDURE IF EXISTS migration_001;
Currently you are using the wrong delimiter to drop the procedure.