I know that
SELECT * FROM Table
will list all columns in the table, but I am interested in listing the columns in alphabetical order.
You may just specify columns you wish to select:
SELECT age, name, sex FROM Table
Columns will be shown in the same order as you specified them in query.
Yes. It is possible with the following command.
SELECT column_name FROM user_tab_cols WHERE table_name=UPPER('Your_Table_Name') order by column_name;
It will display all columns of your table in alphabetic order.
A different approach would be to arrange all columns alphabetically by altering the table via a SQL procedure. I created one for a couple of the tables in which my users prefer the alphabetic layout while still using the simplified SELECT *
statement.
This code should arranged my index first and then organise all other columns from A-Z. It may be different for your instance but is a good starting point.
DELIMITER ;;
DROP PROCEDURE IF EXISTS ALPHABETISE_TABLE_COLUMNS;
CREATE PROCEDURE ALPHABETISE_TABLE_COLUMNS(IN database_name VARCHAR(64), IN table_name_string VARCHAR(64), IN index_name_string VARCHAR(64))
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
DECLARE col_name VARCHAR(30) DEFAULT "";
DECLARE col_datatype VARCHAR(10) DEFAULT "";
DECLARE previous_col VARCHAR(30) DEFAULT col_name;
SELECT COUNT(*)
FROM
(SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = table_name_string) AS TEMP
INTO n;
SET @Q= CONCAT('ALTER TABLE `',database_name,'`.`',table_name_string,'` CHANGE COLUMN `',index_name_string,'` `',index_name_string,'` BIGINT(20) NOT NULL FIRST');
PREPARE exe FROM @Q;
EXECUTE exe;
DEALLOCATE PREPARE exe;
SET n = n-1;
SET i=1;
WHILE i<n DO
SELECT COLUMN_NAME FROM
(SELECT COLUMN_NAME, @row_num:= @row_num + 1 as ind_rows
FROM INFORMATION_SCHEMA.COLUMNS, (SELECT @row_num:= 0 AS num) AS c
WHERE TABLE_NAME = table_name_string AND COLUMN_NAME <> index_name_string
ORDER BY COLUMN_NAME ASC) as TEMP
WHERE ind_rows = i
INTO col_name;
SELECT DATA_TYPE
FROM
(SELECT DATA_TYPE, @row_num:= @row_num + 1 as ind_rows
FROM INFORMATION_SCHEMA.COLUMNS, (SELECT @row_num:= 0 AS num) AS c
WHERE TABLE_NAME = table_name_string AND COLUMN_NAME <> index_name_string
ORDER BY COLUMN_NAME ASC) as TEMP
WHERE ind_rows = i
INTO col_datatype;
IF i = 1 THEN
SET previous_col = index_name_string;
ELSE
SELECT COLUMN_NAME
FROM
(SELECT COLUMN_NAME, @row_num:= @row_num + 1 as ind_rows
FROM INFORMATION_SCHEMA.COLUMNS, (SELECT @row_num:= 0 AS num) AS c
WHERE TABLE_NAME = table_name_string AND COLUMN_NAME <> index_name_string
ORDER BY COLUMN_NAME ASC) as TEMP
WHERE ind_rows = i-1
INTO previous_col;
END IF;
IF col_datatype = 'varchar' THEN
SET col_datatype = 'TEXT';
END IF;
select col_name, previous_col;
IF col_name <> index_name_string OR index_name_string = '' THEN
SET @Q= CONCAT('ALTER TABLE `',database_name,'`.`',table_name_string,'` CHANGE COLUMN `',col_name,'` `',col_name,'` ',col_datatype,' NULL DEFAULT NULL AFTER `',previous_col,'`');
PREPARE exe FROM @Q;
EXECUTE exe;
DEALLOCATE PREPARE exe;
END IF;
SET i = i + 1;
END WHILE;
END;
;;
DELIMITER ;
# NOTE: ASSUMES INDEX IS BIGINT(20), IF OTHER PLEASE ADAPT IN LINE 22 TO MEET DATATYPE
#
# CALL ALPHABETISE_TABLE_COLUMNS('database_name', 'column_name', 'index_name')
Hope this helps!
SELECT *
is not recommended and will not sort column namesSELECT age, name, sex FROM
At the SQL level, it does not matter. Not does it matter to any client code object-
If it's important, then sort when you present the data to the client.
Sorry, it just is that way...
If you just trying to find a column, on SQL Server.
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'MyTableThatCouldMaybeNeedNormalising'
order by COLUMN_NAME
SQL-92 Standard specifies that when using SELECT *
the columns are referenced in the ascending sequence of their ordinal position within the table. The relevant sections are 4.8 (columns) and 7.9 (query specification). I don't know of any vendor extensions to the Standard that would allow columns to be returned in any other order, probably because use of SELECT *
is generally discouraged.
You can use SQL DDL to ensure that columns' ordinal positions match the desired alphabetical order. However, this will only work in the way you want when referening a sinlge table in the FROM
clause. If two tables are referenced, SELECT *
will return the columns from the first table in ordinal position order followed by the second table's columns in ordinal position, so the complete resultset's columns may not be in alphabetical order.