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表成功了

原创文章 5 获赞 10 访问量 977

猜你喜欢

转载自blog.csdn.net/weixin_45746601/article/details/106173844