MySql教程

MySQL数据表基本操作

本文主要是介绍MySQL数据表基本操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

MySQL数据表

1.创建表

mysql> create table share(name char (5),gender char (10),hobby char (15));
Query OK, 0 rows affected (0.13 sec)

2.查看表结构

mysql> desc share;
+--------+----------+------+-----+---------+-------+
| Field  | Type     | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| name   | char(5)  | YES  |     | NULL    |       |
| gender | char(10) | YES  |     | NULL    |       |
| hobby  | char(15) | YES  |     | NULL    |       |
+--------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

3.查看支持的引擎类型

mysql> show engines
    -> ;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

4.查看数据库中表

mysql> show tables;         ###查看数据库中所有表
+------------------+
| Tables_in_class1 |
+------------------+
| share            |
+------------------+
1 row in set (0.00 sec)
​
mysql> show tables from mysql;    ###查看某个数据库中的表
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
31 rows in set (0.00 sec)

5.查看表状态

mysql> show table status like 'share'\G;
*************************** 1. row ***************************
           Name: share     ###名称:
         Engine: InnoDB    ###引擎:
        Version: 10        ###版本
     Row_format: Dynamic   ###行格式:
           Rows: 0         ###行数:
 Avg_row_length: 0         ###平均行长度:
    Data_length: 16384     ###数据长度:
Max_data_length: 0         ###最大数据长度:
   Index_length: 0         ###索引长度:
      Data_free: 0         ###无数据:
 Auto_increment: NULL      ###自动增量:
    Create_time: 2021-08-11 18:41:05        ###创建时间:
    Update_time: NULL                       ###更新时间:
     Check_time: NULL                       ###检查时间:
      Collation: utf8_general_ci            ###版本:
       Checksum: NULL                       ###校验和:
 Create_options:                            ###创建_选项:
        Comment:                            ###注释
1 row in set (0.00 sec)

6.查看某个表的创建命令

mysql> show create table share;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
| share | CREATE TABLE `share` (
  `name` char(5) DEFAULT NULL,
  `gender` char(10) DEFAULT NULL,
  `hobby` char(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
​

7.删除某个表的命令

mysql> drop table share;
Query OK, 0 rows affected (0.01 sec)

8.重命名某个表名

mysql> rename table share to share2;
Query OK, 0 rows affected (0.00 sec)
这篇关于MySQL数据表基本操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!