方法1:
desc 表名;
方法2:
show columns from 表名;
方法3:
describe 表名;
方法4:
show create table 表名;
# 此命令是实时反映当前表结构,不是说后期改了表结构了,它就不变的
show create table alert;
CREATE TABLE `alert` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`model` varchar(20) NOT NULL DEFAULT '',
`stat` int(11) NOT NULL DEFAULT '0',
`level` int(11) NOT NULL DEFAULT '0',
`msg` varchar(1000) NOT NULL DEFAULT '',
`target` varchar(50) NOT NULL DEFAULT '',
`create_time` datetime NOT NULL,
`end_time` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
# 我加一个字段
alter table `alert`
Add column ss int not null default 0;
show create table alert;
CREATE TABLE `alert` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`model` varchar(20) NOT NULL DEFAULT '',
`stat` int(11) NOT NULL DEFAULT '0',
`level` int(11) NOT NULL DEFAULT '0',
`msg` varchar(1000) NOT NULL DEFAULT '',
`target` varchar(50) NOT NULL DEFAULT '',
`create_time` datetime NOT NULL,
`end_time` datetime NOT NULL,
`ss` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
# 发现多了‘ss’字段的说明了吧
还有一种情况是以上方法都不适用的:数据库里的表名是数据库命令的关键字。
desc databases;
# 表名叫‘databases’,错误信息如下,这时候想查表结构,要用方法5
ERROR 1064 (42000): You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near 'databases' at line 1
方法5:
select * from information_schema.columns where table_schema = '库名' and table_name = '表名';
方法6:
使用第三方可视化工具,网上很多