Home  >  Article  >  Database  >  MySQL查看表和清空表的常用命令总结_MySQL

MySQL查看表和清空表的常用命令总结_MySQL

WBOY
WBOYOriginal
2016-05-30 17:10:37910browse

查看MySQL数据库表
进入MySQL Command line client下
查看当前使用的数据库:

mysql>select database();
mysql>status;
mysql>show tables;

mysql>show databases;//可以查看有哪些数据库,返回数据库名(databaseName)

mysql>use databaseName; //更换当前使用的数据库

mysql>show tables; //返回当前数据库下的所有表的名称

或者也可以直接用以下命令

mysql>show tables from databaseName;//databaseName可以用show databases得来

mysql查看表结构命令,如下:

desc 表名;
show columns from 表名;

或者

describe 表名;
show create table 表名;

或者

use information_schema
select * from columns where table_name='表名';

查看警告:

Rows matched: 1 Changed: 0 Warnings: 1 
mysql> show warnings; 
+---------+------+-------------------------------------------+ 
| Level  | Code | Message                  | 
+---------+------+-------------------------------------------+ 
| Warning | 1265 | Data truncated for column 'name' at row 3 | 
+---------+------+-------------------------------------------+ 
1 row in set 

以上就是查看MySQL数据库表的命令介绍。

 

MySQL清空表
Mysql清空表是很重要的操作,也是最常见的操作之一,下面就为您详细介绍Mysql清空表的实现方法,希望能够对您有所帮助。

方法1:重建库和表
用mysqldump --no-data把建表SQL导出来,然后drop database再create database,执行一下导出的SQL文件,把表建上;
方法2:生成清空所有表的SQL

mysql -N -s information_schema -e "SELECT CONCAT('TRUNCATE TABLE ',TABLE_NAME,';') FROM TABLES WHERE TABLE_SCHEMA='eab12'"

输出结果如下:

TRUNCATE TABLE AUTHGROUPBINDINGS;
TRUNCATE TABLE AUTHGROUPS;
TRUNCATE TABLE AUTHUSERS;
TRUNCATE TABLE CORPBADCUSTOMINFO;
TRUNCATE TABLE CORPSMSBLACKLISYInfo;
TRUNCATE TABLE CORPSMSFILTERINFO;
TRUNCATE TABLE CORPSMSINFO;
TRUNCATE TABLE EABASEREGINFOS;
TRUNCATE TABLE EACORPBLOB;
TRUNCATE TABLE EACORPINFO;
....
....

这样就更完善了:

代码如下:


mysql -N -s information_schema -e "SELECT CONCAT('TRUNCATE TABLE ',TABLE_NAME,';') FROM TABLES WHERE TABLE_SCHEMA='eab12'" | mysql eab12


即清空eab12中所有的表。
但是如果有外键的话,很可能会报错。因此还需要加个-f

代码如下:


mysql -N -s information_schema -e "SELECT CONCAT('TRUNCATE TABLE ',TABLE_NAME,';') FROM TABLES WHERE TABLE_SCHEMA='eab12'" | mysql -f eab12


多执行几次,直到不报错。

以上就是Mysql清空表的实现方法。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn