查看mysql 库信息和表结构与表创建方法

一、查看基础信息

1.查看数据库存储位置

  show global variables like "%datadir%";

2.查看数据库的版本

  select version();

3.修改root密码

  set password for root@localhost = password('123');  

二、查看mysql表结构

有三种方法:
1、desc tablename;
例如:
要查看jos_modules表结构的命令:
desc jos_modules;
查看结果:
mysql> desc jos_modules;

+------------------+---------------------+------+-----+---------------------+----------------+
| Field            | Type                | Null | Key | Default             | Extra          |
+------------------+---------------------+------+-----+---------------------+----------------+
| id               | int(11)             | NO   | PRI | NULL                | auto_increment |
| title            | text                | NO   |     | NULL                |                |
| content          | text                | NO   |     | NULL                |                |
| ordering         | int(11)             | NO   |     | 0                   |                |
| position         | varchar(50)         | YES |     | NULL                |                |
| checked_out      | int(11) unsigned    | NO   |     | 0                   |                |
| checked_out_time | datetime            | NO   |     | 0000-00-00 00:00:00 |                |
| published        | tinyint(1)          | NO   | MUL | 0                   |                |
| module           | varchar(50)         | YES | MUL | NULL                |                |
| numnews          | int(11)             | NO   |     | 0                   |                |
| access           | tinyint(3) unsigned | NO   |     | 0                   |                |
| showtitle        | tinyint(3) unsigned | NO   |     | 1                   |                |
| params           | text                | NO   |     | NULL                |                |
| iscore           | tinyint(4)          | NO   |     | 0                   |                |
| client_id        | tinyint(4)          | NO   |     | 0                   |                |
| control          | text                | NO   |     | NULL                |                |
+------------------+---------------------+------+-----+---------------------+----------------+

2、show create table tablename;
例如:
要查看jos_modules表结构的命令:
show create table jos_modules;
查看结果:
mysql> show create table jos_modules;

jos_modules | CREATE TABLE `jos_modules` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` text NOT NULL,
`content` text NOT NULL,
`ordering` int(11) NOT NULL DEFAULT '0',
`position` varchar(50) DEFAULT NULL,
`checked_out` int(11) unsigned NOT NULL DEFAULT '0',
`checked_out_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`published` tinyint(1) NOT NULL DEFAULT '0',
`module` varchar(50) DEFAULT NULL,
`numnews` int(11) NOT NULL DEFAULT '0',
`access` tinyint(3) unsigned NOT NULL DEFAULT '0',
`showtitle` tinyint(3) unsigned NOT NULL DEFAULT '1',
`params` text NOT NULL,
`iscore` tinyint(4) NOT NULL DEFAULT '0',
`client_id` tinyint(4) NOT NULL DEFAULT '0',
`control` text NOT NULL,
PRIMARY KEY (`id`),
KEY `published` (`published`,`access`),
KEY `newsfeeds` (`module`,`published`)
) ENGINE=MyISAM AUTO_INCREMENT=145 DEFAULT CHARSET=utf8


3、use information_schema;select * from columns where table_name='tablename'
例如:
要查看jos_modules表结构的命令:
use information_schema;
select * from columns where table_name='jos_modules';
查看结果:
略。
如果要查看怎么建立数据表的命令用第二种方法最佳。

三、编码格式

1.查看数据库编码格式

show variables like 'character_set_database';

 2.查看数据表的编码格式

show create table <表名>;

 3.创建数据库时指定数据库的字符集

create database <数据库名> character set utf8;

4.创建数据表时指定数据表的编码格式

create table tb_books (
    name varchar(45) not null,
    price double not null,
    bookCount int not null,
    author varchar(45) not null ) default charset = utf8;

5.修改数据库的编码格式

alter database <数据库名> character set utf8;

6.修改数据表格编码格式

alter table <表名> character set utf8;

7.修改字段编码格式

alter table <表名> change <字段名> <字段名> <类型> character set utf8;

alter table user change username username varchar(20) character set utf8 not null;

四、修改表结构

1.添加外键

alter table tb_product add constraint fk_1 foreign key(factoryid) references tb_factory(factoryid);
alter table <表名> add constraint <外键名> foreign key<字段名> REFERENCES <外表表名><字段名>;

2.删除外键

mysql>alter table tb_people drop foreign key fk_1;
mysql>alter table <表名> drop foreign key <外键名>;

相关文章:

https://www.cnblogs.com/shootercheng/p/5836657.html

原文地址:https://www.cnblogs.com/xcsn/p/4678402.html