Skip to content

Instantly share code, notes, and snippets.

@olivertappin
Created March 26, 2019 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olivertappin/245f8f3d612e4dabef836cf35000e6f4 to your computer and use it in GitHub Desktop.
Save olivertappin/245f8f3d612e4dabef836cf35000e6f4 to your computer and use it in GitHub Desktop.
Return the column entry for a SHOW CREATE TABLE (based on primary key or column name)
SELECT RTRIM(CONCAT(
'`',
COLUMN_NAME,
'`',
' ',
COLUMN_TYPE,
' ',
IF (COLLATION_NAME IS NULL, '', CONCAT('COLLATE ', COLLATION_NAME, ' ')),
IF (IS_NULLABLE = 'YES', '', 'NOT NULL '),
IF (COLUMN_DEFAULT IS NULL, '', CONCAT('DEFAULT ', COLUMN_DEFAULT, ' ')),
IF (IS_NULLABLE = 'YES' AND COLUMN_DEFAULT IS NULL, 'DEFAULT NULL ', ''),
EXTRA
)) AS `SQL`
FROM information_schema.columns
WHERE
TABLE_NAME = 'TABLE'
AND TABLE_SCHEMA = 'DATABASE'
AND COLUMN_KEY = 'PRI'
# AND COLUMN_NAME = 'COLUMN'
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment