MySQL(第二篇)—數據表的基本操作

數據表的基本操作

1、創建數據表

在創建數據表之前,我們需要通過 use 數據庫名稱; 切換到要操作的數據庫

語法格式

create table 表名(
        字段名1  數據類型
        字段名2  數據類型
        ......
        字段名n  數據類型
        );

示例

mysql> create table student(id int,name varchar(50),age int);
Query OK, 0 rows affected (0.03 sec)

或者,另一種書寫方式,看起來更加規矩

mysql> create table stu(
    -> id int,
    -> name varchar(50),
    -> age int
    -> );
Query OK, 0 rows affected (0.02 sec)

創建成功了,如果想要驗證,可以通過下面查看全部

2、查看數據表

(1)查看當前數據庫內全部數據表

語法格式

 show tables;

示例

mysql> show tables;
+------------------+
| Tables_in_record |
+------------------+
| stu              |
| student          |
+------------------+
2 rows in set (0.00 sec)

(2)查看創建的數據表

第一種方法

語法格式

 show create table 表名;

示例

mysql> show create table student;
+---------+---------------------------------------------------------+
| Table   | Create Table                                           |
+---------+---------------------------------------------------------+
| student | CREATE TABLE `student` (
  `id` int DEFAULT NULL,
  `name` varchar(50) DEFAULT NULL,
  `age` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------+---------------------------------------------------------+
1 row in set (0.01 sec)

第二種方法,在第一種方法表名後加“ \G”

語法格式

 show create table 表名\G;

示例

mysql> show create table student\G;
*************************** 1. row ***************************
       Table: student
Create Table: CREATE TABLE `student` (
  `id` int DEFAULT NULL,
  `name` varchar(50) DEFAULT NULL,
  `age` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)

ERROR:
No query specified

顯示的格式比第一種稍微整齊了一點,但是在加入“\G”的代碼中出現了一個錯誤,如下:

ERROR:
No query specified

這是因爲“\G”和“;”一樣具有結束語句的意義。
所以說在語句結束時可以直接以“\G”作爲結尾

show create table student\G

如果仔細觀察的話可以發現“\G”具有一項特殊的功能,將表格的輸出方式由橫向輸出改爲縱向輸出。

在這兒要提一下“\g”,與“\G”不同的是,"\g"就等同於“;”

 show create table 表名\g
mysql> show create table student\g
+---------+---------------------------------------------------------+
| Table   | Create Table                                           |
+---------+---------------------------------------------------------+
| student | CREATE TABLE `student` (
  `id` int DEFAULT NULL,
  `name` varchar(50) DEFAULT NULL,
  `age` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------+---------------------------------------------------------+
1 row in set (0.00 sec)

第三種方法,describe語句
如果你只想查看錶中列的信息,那麼使用describe語句是很好的選擇

describe 表名;

或者是describe的簡寫方式

desc 表名;

兩種寫法結果是相同的,都可以輸出下面的代碼

mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | YES  |     | NULL    |       |
| name  | varchar(50) | YES  |     | NULL    |       |
| age   | int         | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.05 sec)

3、修改數據表

(1)修改表名

方式一

語法格式

alter table 原表名 rename 新表名;
mysql> alter table student rename  class;
Query OK, 0 rows affected (0.05 sec)

方式二

語法格式

rename table 原表名 to 新表名;
mysql> rename table class to student;
Query OK, 0 rows affected (0.02 sec)

(2)添加字段

語法格式

alter table 表名 add 新字段名 數據類型;
mysql> alter table student add sex varchar(10);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

添加成功,查看一下

mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | YES  |     | NULL    |       |
| name  | varchar(50) | YES  |     | NULL    |       |
| age   | int         | YES  |     | NULL    |       |
| sex   | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

sex字段成功添加

(3)修改字段

語法格式

alter table 表名 change 原字段名 新字段名 新數據類型;
mysql> alter table student change age birthday date;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

修改成功,查看一下

mysql> desc student;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int         | YES  |     | NULL    |       |
| name     | varchar(50) | YES  |     | NULL    |       |
| birthday | date        | YES  |     | NULL    |       |
| sex      | varchar(10) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

(4)修改字段的數據類型

語法格式

alter table 表名 modify 字段名 數據類型;
mysql> alter table student modify sex char;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

修改成功,查看一下

mysql> desc student;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int         | YES  |     | NULL    |       |
| name     | varchar(50) | YES  |     | NULL    |       |
| birthday | date        | YES  |     | NULL    |       |
| sex      | char(1)     | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

(5)修改字段的排列位置

  • 將字段1修改爲表的第一個字段

語法格式

alter table 表名 modify 字段1 數據類型 first

示例

mysql> alter table student modify sex char first;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| sex      | char(1)     | YES  |     | NULL    |       |
| id       | int         | YES  |     | NULL    |       |
| name     | varchar(50) | YES  |     | NULL    |       |
| birthday | date        | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
  • 將字段1插入到字段2的後面

語法格式

alter table 表名 modify 字段1 數據類型 after 字段2

示例

mysql> alter table student modify sex char after birthday;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int         | YES  |     | NULL    |       |
| name     | varchar(50) | YES  |     | NULL    |       |
| birthday | date        | YES  |     | NULL    |       |
| sex      | char(1)     | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

(6)刪除字段

語法格式

alter table 表名 drop 字段名;

示例

mysql> alter table student drop birthday;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | YES  |     | NULL    |       |
| name  | varchar(50) | YES  |     | NULL    |       |
| sex   | char(1)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

4、刪除數據表

刪除數據表是從數據庫中將數據表刪除,同時刪除表中儲存的數據

語法格式

drop table 表名;

示例

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

未刪除表student前

mysql> show tables;
+------------------+
| Tables_in_record |
+------------------+
| stu              |
| student          |
+------------------+
2 rows in set (0.01 sec)

刪除了表student後

mysql> show tables;
+------------------+
| Tables_in_record |
+------------------+
| stu              |
+------------------+
1 row in set (0.00 sec)

刪除student表成功了

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