3:表的基本操作-MySQL

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: 3:表的基本操作-MySQL

3.1 提出问题,引入“表“的概念与思维模式 table

表的概念

数据库类似于厂库,而表呢就是对数据进行抽象分类的货架

注意:在创建数据库的时候一定要记得设置字符编码

3.2 引用数据库和查看数据库中的表

1. 引用数据库

mysql> use student;
Database changed

2. 查看数据库中的表

mysql> show tables;
Empty set (0.00 sec)

3.3 创建表

创建表

mysql> create table student(
    -> id int,
    -> name varchar(30),
    -> age int,
    -> salary int
    -> );
Query OK, 0 rows affected (0.04 sec)
mysql> show tables
    -> ;
+-----------------------+
| Tables_in_student |
+-----------------------+
| empolyee              |
+-----------------------+

注意:在MySQL中字符串的语句是不使用String的,而是使用varchar,需要表明长度,同时一级一级的写语句能更加整洁明了

3.4 创建表(企业用,有B格)

mysql> create table if not exists teacher(
    -> id int auto_increment primary key comment'主键id',
    -> name varchar(30) not null comment '老师的名字',
    -> phone varchar(20) comment '电话号码',
    -> address varchar(100) default '暂时未知' comment '住址'
    -> )engine=innodb;
Query OK, 0 rows affected (0.03 sec)

注意:在创建表的时候,在default单引号中间的内容要和所定义的数据类型一样,最后一个属性写完不要用“,

关键词解释

  • auto_increment: 对字段进行自动增长
  • primary key: 主键,是关系型数据库连接桥梁,必须填写不能空着
  • comment: 注释
  • not null: 不能为空,必须填写
  • default: 如果为空那么表中这个数据默认为’ '中的内容,默认值
  • engine = innodb: 表示数据库引擎

两种方式创建的有什么区别呢?

简单的

mysql> create table empolyee(
    -> id int,
    -> name varchar(30),
    -> age int,
    -> salary int
    -> );
Query OK, 0 rows affected (0.04 sec)
mysql> show create table empolyee;
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                |
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| empolyee | CREATE TABLE `empolyee` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(30) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `salary` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

复杂的

mysql> create table staff(
    -> id int auto_increment primary key comment'主键id',
    -> name varchar(30) not null,
    -> age int comment'年龄',
    -> salary int default '0' comment'薪水'
    ->  )engine=innodb;
Query OK, 0 rows affected (0.03 sec)
mysql> show create table staff;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                        |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| staff | CREATE TABLE `staff` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键id',
  `name` varchar(30) NOT NULL,
  `age` int(11) DEFAULT NULL COMMENT '年龄',
  `salary` int(11) DEFAULT '0' COMMENT '薪水',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

即使没有写default,但还是会默认default

3.5 查看表结构

1. 显示出表的sql语句

show create table teacher;
mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| teacher           |
+-------------------+
1 row in set (0.00 sec)
mysql> show create table teacher;
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table
                                              |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| teacher | CREATE TABLE `teacher` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键id',
  `name` varchar(30) NOT NULL COMMENT '老师的名字',
  `phone` varchar(20) DEFAULT NULL COMMENT '电话号码',
  `address` varchar(100) DEFAULT '暂时未知' COMMENT '住址',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

2. 查看表的结构

desc teacher;
mysql> desc teacher;
+---------+--------------+------+-----+----------+----------------+
| Field   | Type         | Null | Key | Default  | Extra          |
+---------+--------------+------+-----+----------+----------------+
| id      | int(11)      | NO   | PRI | NULL     | auto_increment |
| name    | varchar(30)  | NO   |     | NULL     |                |
| phone   | varchar(20)  | YES  |     | NULL     |                |
| address | varchar(100) | YES  |     | 暂时未知 |                |
+---------+--------------+------+-----+----------+----------------+
4 rows in set (0.01 sec)
  • Null :代表的是否能为空
  • Key PRI :代表是唯一值
  • Default :说明是否设立default
  • Extra :代表的是规则,上述文件是自动增加

3.6 删除表

1. 删除单个表

drop table if exists staff ;

2. 删除多个表

drop table if exists staff,empolyee;

3.7 修改表

1. 添加一个新的字段

alter table 表名 add 字段名 字段类型
mysql> alter table student add name varchar(30);
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(4)      | YES  |     | NULL    |       |
| name  | varchar(30) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

2. 指定位置添加一个新的字段

在某字段之后

alter table 表名 add 字段名 字段类型 after 字段名
mysql> alter table student add age int after id;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(4)      | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
| name  | varchar(30) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

在第一行添加

alter table 表名 add 字段 字段类型 first
mysql> alter table student add phone int(20) first;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| phone | int(20)     | YES  |     | NULL    |       |
| id    | int(4)      | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
| name  | varchar(30) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

3. 删除字段

alter table 表名 drop 字段
mysql> alter table student drop phone;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(4)      | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
| name  | varchar(30) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

4. 修改字段(名、类型)

修改名和类型(完全修改)

alter table 表名 change 字段名 新的字段名 字段类型
mysql> alter table student change age phone varchar(20);
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(4)      | YES  |     | NULL    |       |
| phone | varchar(20) | YES  |     | NULL    |       |
| name  | varchar(30) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

修改类型

alter table 表名 modify 字段名 要改成的字段类型
mysql> alter table student modify phone int(20);
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(4)      | YES  |     | NULL    |       |
| phone | int(20)     | YES  |     | NULL    |       |
| name  | varchar(30) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

5. 修改表名

alter table 表名 rename to 新表名
mysql> alter table student rename to empolyee;
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+-----------------------+
| Tables_in_qiu_company |
+-----------------------+
| empolyee              |
+-----------------------+
1 row in set (0.00 sec)
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
Oracle 关系型数据库 MySQL
【mysql】—— 表的内连和外连
【mysql】—— 表的内连和外连
|
1月前
|
存储 关系型数据库 MySQL
【mysql】—— 表的增删改查
【mysql】—— 表的增删改查
|
1月前
|
分布式计算 DataWorks 关系型数据库
DataWorks支持将ODPS表拆分并回流到MySQL的多个库和表中
【2月更文挑战第14天】DataWorks支持将ODPS表拆分并回流到MySQL的多个库和表中
59 8
|
1月前
|
SQL 关系型数据库 MySQL
Mysql基础第二十四天,创建表和操纵表
Mysql基础第二十四天,创建表和操纵表
31 0
Mysql基础第二十四天,创建表和操纵表
|
2月前
|
存储 JSON 关系型数据库
一文搞懂MySQL表字段类型长度的含义
一文搞懂MySQL表字段类型长度的含义
38 0
|
28天前
|
存储 SQL 关系型数据库
【MySQL】4. 表的操作
【MySQL】4. 表的操作
21 0
|
1月前
|
SQL 关系型数据库 MySQL
【MySQL】— —熟练掌握用SQL语句实现数据库和基本表的创建。熟练掌握MySQL的安装、客户端登录方法;熟练掌握MySQL的编码、数据类型等基础知识;掌握实体完整性的定义和维护方法、掌握参照完整性
【MySQL】— —熟练掌握用SQL语句实现数据库和基本表的创建。熟练掌握MySQL的安装、客户端登录方法;熟练掌握MySQL的编码、数据类型等基础知识;掌握实体完整性的定义和维护方法、掌握参照完整性
105 1
|
27天前
|
存储 关系型数据库 MySQL
MySQL数据库性能大揭秘:表设计优化的高效策略(优化数据类型、增加冗余字段、拆分表以及使用非空约束)
MySQL数据库性能大揭秘:表设计优化的高效策略(优化数据类型、增加冗余字段、拆分表以及使用非空约束)
|
27天前
|
缓存 关系型数据库 MySQL
MySQL 查询优化:提速查询效率的13大秘籍(索引设计、查询优化、缓存策略、子查询优化以及定期表分析和优化)(中)
MySQL 查询优化:提速查询效率的13大秘籍(索引设计、查询优化、缓存策略、子查询优化以及定期表分析和优化)(中)
|
2天前
|
关系型数据库 MySQL 数据库
【MySQL探索之旅】数据库的基本操作
【MySQL探索之旅】数据库的基本操作