mysql常用表管理語句

#查看庫下面有多少張表.
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| boy            |
| boy2           |
| boy3           |
| course         |
| pj             |
| sc             |
| sc2            |
| stage          |
| student        |
+----------------+
9 rows in set (0.00 sec)

#刪除視圖
mysql> drop view pj;
Query OK, 0 rows affected (0.00 sec)

#刪除表
mysql> drop tables sc;
Query OK, 0 rows affected (0.02 sec)

#查看錶結構信息
mysql> desc boy;
+--------+--------------+------+-----+------------+-------+
| Field  | Type         | Null | Key | Default    | Extra |
+--------+--------------+------+-----+------------+-------+
| hid    | int(11)      | NO   |     | NULL       |       |
| bname  | varchar(100) | NO   |     | NULL       |       |
| birth  | date         | NO   |     | 0000-00-00 |       |
| weight | smallint(6)  | YES  |     | NULL       |       |
+--------+--------------+------+-----+------------+-------+
4 rows in set (0.00 sec)

#查看建表的過程、語句.
mysql> show create table boy;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                           |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| boy   | CREATE TABLE `boy` (
  `hid` int(11) NOT NULL,
  `bname` varchar(100) NOT NULL,
  `birth` date NOT NULL DEFAULT '0000-00-00',
  `weight` smallint(6) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

#查看視圖建表過程(table/view都可以)
mysql> show create table stage;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| View  | Create View                                                                                                                                                            | character_set_client | collation_connection |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| stage | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `stage` AS select `student`.`SNAME` AS `sname`,`student`.`AGE` AS `age` from `student` | utf8                 | utf8_general_ci      |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
1 row in set (0.00 sec)

mysql> show create view stage;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| View  | Create View                                                                                                                                                            | character_set_client | collation_connection |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| stage | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `stage` AS select `student`.`SNAME` AS `sname`,`student`.`AGE` AS `age` from `student` | utf8                 | utf8_general_ci      |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
1 row in set (0.00 sec)

#查看錶的信息和過程(所有表).
mysql> show table  status \G;
*************************** 1. row ***************************
           Name: boy
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 4
Avg_row_length: 4096
    Data_length: 16384
Max_data_length: 0
   Index_length: 0
      Data_free: 0
Auto_increment: NULL
    Create_time: 2019-01-29 00:33:46
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
Create_options:
        Comment:

#單獨的查看某張表的狀態、信息.
mysql> show table  status where name="student" \G;
*************************** 1. row ***************************
           Name: student
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 4
Avg_row_length: 4096
    Data_length: 16384
Max_data_length: 0
   Index_length: 0
      Data_free: 0
Auto_increment: NULL
    Create_time: 2018-09-21 00:55:30
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
Create_options:
        Comment:
1 row in set (0.00 sec)
ERROR:
No query specified


清空表數據truncate和delete的區別?
使用delete對錶中的數據進行操作:

#創建t2表
mysql> create table t2 ( id int primary key auto_increment );
Query OK, 0 rows affected (0.02 sec)
mysql> select * from t2;
Empty set (0.00 sec)

#插入兩行數據.
mysql> insert into t2 values (null);
Query OK, 1 row affected (0.01 sec)
mysql> insert into t2 values (null);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t2;
+----+
| id |
+----+
|  1 |
|  2 |
+----+
2 rows in set (0.00 sec)

#delete其中的一條數據.
mysql> delete from t2 where id="2";
Query OK, 1 row affected (0.00 sec)

#再次插入數據.
mysql> insert into t2 values (null);
Query OK, 1 row affected (0.01 sec)

#發現插入的id從3開始,繼續原數據的id增長.
mysql> select * from t2;
+----+
| id |
+----+
|  1 |
|  3 |
+----+
2 rows in set (0.00 sec)


使用truncate對錶中的數據進行操作:

#表中的數據清空.
mysql> truncate table t2;
Query OK, 0 rows affected (0.02 sec)

#插入數據
mysql> insert into t2 values (null);
Query OK, 1 row affected (0.01 sec)

#發現表的id從1開始.
mysql> select * from t2;
+----+
| id |
+----+
|  1 |
+----+
1 row in set (0.00 sec)
注意:truncate操作其實相當與對錶進行了drop tables---create table操作.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章