首页>建站相关>Mysql基础命令与Typecho的数据库字段

Mysql基础命令与Typecho的数据库字段

纯手工搭建了这个“小鸟数据”博客,没有安装第三方管理面板,存取文件基本靠putty,输入指令基本靠双手,满足不了的需求则基本靠跳过。因为曾经有过误删数据库的惨痛经历,对于手工操作数据库一直是拒绝的。某天尝试了一下在Typecho中通过操作数据库的方式更新文章,就想看看数据库里有没有成功被写入,然后就很尴尬的发现自己的mysql指令就只记得一个“show databases”了。

登陆数据库

以root用户登陆,需求密码,linux的输入密码过程不会有任何的提示,如果没成功进入数据库,那估计就是密码输错了,在命令行按方向键“上”可以回溯上一条命令,再来一遍没准就输对了。

mysql -u root -p

数据库一览

成功进入数据库后会出现mysql的前缀,centos8默认安装的是同源的MariaDB,前缀是“MariaDB [(none)]> ”,下面这条指令会显示所有已建立的数据库名,需要注意的是,从现在开始,末尾的分号必不可少;

show databases;

进入某个库

比如博主建立了一个测试用的库“Abddb_Theme”,进入这个库可以用“use”命令,成功后命令行前缀也会被变更,变成类似下面这样的形式“MariaDB [Abddb_Theme]>”;

use Abddb_Theme;

数据库有几张表?哪些字段?

typecho的数据库结构很精简,我们看一下它一共包含哪些表单,先输入:

//显示一共有几张表
show tables;
//显示表中包含的所有字段
show columns from 具体表名;

利用typecho的数据库实际操作一下,顺便了解一下其字段构成;

//页面实际显示类似这样
MariaDB [Abddb_Theme]> show tables;
//程序输出如下:
+-----------------------+
| Tables_in_Abddb_Theme |
+-----------------------+
| typecho_comments      |
| typecho_contents      |
| typecho_fields        |
| typecho_metas         |
| typecho_options       |
| typecho_relationships |
| typecho_users         |
+-----------------------+

来都来了,看下typecho表的字段吧!

comments

这张应该是放评论数据的表单;

show columns from typecho_comments;

+----------+------------------+------+-----+----------+----------------+
| Field    | Type             | Null | Key | Default  | Extra          |
+----------+------------------+------+-----+----------+----------------+
| coid     | int(10) unsigned | NO   | PRI | NULL     | auto_increment |
| cid      | int(10) unsigned | YES  | MUL | 0        |                |
| created  | int(10) unsigned | YES  | MUL | 0        |                |
| author   | varchar(200)     | YES  |     | NULL     |                |
| authorId | int(10) unsigned | YES  |     | 0        |                |
| ownerId  | int(10) unsigned | YES  |     | 0        |                |
| mail     | varchar(200)     | YES  |     | NULL     |                |
| url      | varchar(200)     | YES  |     | NULL     |                |
| ip       | varchar(64)      | YES  |     | NULL     |                |
| agent    | varchar(200)     | YES  |     | NULL     |                |
| text     | text             | YES  |     | NULL     |                |
| type     | varchar(16)      | YES  |     | comment  |                |
| status   | varchar(16)      | YES  |     | approved |                |
| parent   | int(10) unsigned | YES  |     | 0        |                |
+----------+------------------+------+-----+----------+----------------+

contents

文章内容数据表单,这里的“text”字段里就放的我们文章的主体;

show columns from typecho_contents;

+--------------+------------------+------+-----+---------+----------------+
| Field        | Type             | Null | Key | Default | Extra          |
+--------------+------------------+------+-----+---------+----------------+
| cid          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| title        | varchar(200)     | YES  |     | NULL    |                |
| slug         | varchar(200)     | YES  | UNI | NULL    |                |
| created      | int(10) unsigned | YES  | MUL | 0       |                |
| modified     | int(10) unsigned | YES  |     | 0       |                |
| text         | longtext         | YES  |     | NULL    |                |
| order        | int(10) unsigned | YES  |     | 0       |                |
| authorId     | int(10) unsigned | YES  |     | 0       |                |
| template     | varchar(32)      | YES  |     | NULL    |                |
| type         | varchar(16)      | YES  |     | post    |                |
| status       | varchar(16)      | YES  |     | publish |                |
| password     | varchar(32)      | YES  |     | NULL    |                |
| commentsNum  | int(10) unsigned | YES  |     | 0       |                |
| allowComment | char(1)          | YES  |     | 0       |                |
| allowPing    | char(1)          | YES  |     | 0       |                |
| allowFeed    | char(1)          | YES  |     | 0       |                |
| parent       | int(10) unsigned | YES  |     | 0       |                |
| views        | int(10)          | YES  |     | 0       |                |
+--------------+------------------+------+-----+---------+----------------+

fields

看了一下表里的内容,似乎是博主的一些自定义字段;

show columns from typecho_fields;

+-------------+------------------+------+-----+---------+-------+
| Field       | Type             | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+-------+
| cid         | int(10) unsigned | NO   | PRI | NULL    |       |
| name        | varchar(200)     | NO   | PRI | NULL    |       |
| type        | varchar(8)       | YES  |     | str     |       |
| str_value   | text             | YES  |     | NULL    |       |
| int_value   | int(10)          | YES  | MUL | 0       |       |
| float_value | float            | YES  | MUL | 0       |       |
+-------------+------------------+------+-----+---------+-------+

metas

里面是一些分类以及标签的描述信息;

show columns from typecho_metas;

+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| mid         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name        | varchar(200)     | YES  |     | NULL    |                |
| slug        | varchar(200)     | YES  | MUL | NULL    |                |
| type        | varchar(32)      | NO   |     | NULL    |                |
| description | varchar(200)     | YES  |     | NULL    |                |
| count       | int(10) unsigned | YES  |     | 0       |                |
| order       | int(10) unsigned | YES  |     | 0       |                |
| parent      | int(10) unsigned | YES  |     | 0       |                |
+-------------+------------------+------+-----+---------+----------------+

options

这里面保存了用户的一些设置信息,不如主题用什么,有没有开启markdown;

show columns from typecho_options;

+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| name  | varchar(32)      | NO   | PRI | NULL    |       |
| user  | int(10) unsigned | NO   | PRI | 0       |       |
| value | text             | YES  |     | NULL    |       |
+-------+------------------+------+-----+---------+-------+

relationships

仅两个字段的关系表,cid指的是文章的id,mid应该是目录或标签的id,这张表里存储了他们之间的从属关系;

show columns from typecho_relationships;

+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| cid   | int(10) unsigned | NO   | PRI | NULL    |       |
| mid   | int(10) unsigned | NO   | PRI | NULL    |       |
+-------+------------------+------+-----+---------+-------+

users

用户表,这个就很容易理解了,typecho虽然小巧,也是可以支持多用户的;

show columns from typecho_users;

+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| uid        | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name       | varchar(32)      | YES  | UNI | NULL    |                |
| password   | varchar(64)      | YES  |     | NULL    |                |
| mail       | varchar(200)     | YES  | UNI | NULL    |                |
| url        | varchar(200)     | YES  |     | NULL    |                |
| screenName | varchar(32)      | YES  |     | NULL    |                |
| created    | int(10) unsigned | YES  |     | 0       |                |
| activated  | int(10) unsigned | YES  |     | 0       |                |
| logged     | int(10) unsigned | YES  |     | 0       |                |
| group      | varchar(16)      | YES  |     | visitor |                |
| authCode   | varchar(64)      | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+

显示所有内容

表里数据较多的时候慎用该命令,可能输出一堆数据,找到所需数据的机会如同大海捞针,找张数据较少的表演示一下吧;

select * from typecho_fields;

+-----+--------------+------+-----------+-----------+-------------+
| cid | name         | type | str_value | int_value | float_value |
+-----+--------------+------+-----------+-----------+-------------+
|  14 | augPostThumb | str  |           |         0 |           0 |
|  15 | augPostThumb | str  |           |         0 |           0 |
|  16 | augPostThumb | str  |           |         0 |           0 |
+-----+--------------+------+-----------+-----------+-------------+

限制输出的行数LIMIT

//显示前两行
select * from typecho_relationships limit 2;

+-----+-----+
| cid | mid |
+-----+-----+
|   1 |   1 |
|   1 |  13 |
+-----+-----+
//从第2行开始,显示4行(初始行号为0)
select * from typecho_relationships limit 2,4;

+-----+-----+
| cid | mid |
+-----+-----+
|  12 |   8 |
|  13 |   9 |
|  14 |   1 |
|  15 |   1 |
+-----+-----+

限定条件的查找WHERE

select * from typecho_metas where mid>18;
+-----+--------+---------+----------+-------------+-------+-------+--------+
| mid | name   | slug    | type     | description | count | order | parent |
+-----+--------+---------+----------+-------------+-------+-------+--------+
|  19 | gggg   | gggg    | tag      | bbbbbbbbbb  |     0 |     0 |      0 |
|  20 | 短篇   | short   | category | NULL        |     0 |     1 |      3 |
|  21 | 节选   | excerpt | category | NULL        |     0 |     2 |      3 |
|  22 | 诗歌   | poem    | category | NULL        |     0 |     3 |      3 |
+-----+--------+---------+----------+-------------+-------+-------+--------+

退出数据库

exit

结语

以上命令能够对一个数据库进行简单的查询,以确认数据字段与储存的状态,至于其他更深入的用法,这里就不展开了,毕竟mysql操作内容是可以塞满整整一本书的。:)

标签: mysql

移动端可扫我直达哦~

推荐阅读

mysql 2022-10-10

mysqldump免密码备份数据库

linux下的mysqldump是一个用来备份mysql数据库的命令,数据库是一个站点的根基,时常备份一下数据与资源文件,在遇到突发情况时也可以从容应对。手动备份数据库,我们可以直接在命令行输入如下指令:mysqldump -u ro...

建站相关 mysql

mysql 2022-09-27

如何手动配置MySQL数据库

MySQL是最流行的关系型数据库管理系统之一,与其他大型数据库例如Oracle、DB2、SQL Server等相比,MySQL自然有它的不足之处,但是这丝毫也没有减少它受欢迎的程度。对于一般的个人使用者和中小型企业来说,MySQL提供...

建站相关 mysql