文章目录

  • 1.0 MySQL表操作详解
    • 1.1 MySQL创建表
    • 1.2 MySQL查看表结构
    • 1.3 MySQL修改表

1.0 MySQL表操作详解

1.1 MySQL创建表

指令:create table table_name(field1 datatype, field2 datatype) character + collate + engine;
后面三个参数可以省略,MySQL会自动调用默认配置

MariaDB [clx_database]> create table if not exists `stu_list`(    //添加表-> id int,-> name varchar(20) comment '姓名',-> password char(30) comment '校园卡密码',-> birthday date comment '生日'-> );
Query OK, 0 rows affected (0.01 sec)MariaDB [clx_database]> show tables;
+------------------------+
| Tables_in_clx_database |
+------------------------+
| stu_list               |
+------------------------+
1 row in set (0.00 sec)
MariaDB [clx_database]> desc stu_list;                            //打印表结构
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| name     | varchar(20) | YES  |     | NULL    |       |
| password | char(30)    | YES  |     | NULL    |       |
| birthday | date        | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)MariaDB [clx_database]> show create table stu_list \G;          //打印表的构建信息
*************************** 1. row ***************************Table: stu_list
Create Table: CREATE TABLE `stu_list` (`id` int(11) DEFAULT NULL,`name` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '姓名',`password` char(30) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '校园卡密码',`birthday` date DEFAULT NULL COMMENT '生日'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci    //此处为MySQL配置的表的默认属性
1 row in set (0.00 sec)

可以看到stu_list 默认使用的存储引擎是 InnoDB,接下来我们使用MyISAM存储引擎再创建一个表

MariaDB [clx_database]> create table if not exists `stu_myisam`(-> id int,-> name varchar(20),-> password char(30),-> birthday date-> ) engine MyISAM;                  //显式设置使用MyISAM存储引擎
Query OK, 0 rows affected (0.01 sec)
MariaDB [clx_database]>  show tables;
+------------------------+
| Tables_in_clx_database |
+------------------------+
| stu_list               |
| stu_myisam             |
+------------------------+
2 rows in set (0.00 sec)


我们发现,不同的存储引擎,创建表的文件是不一样的。
stu_myisam 表的存储引擎是MyISAM,再数据存储中有三个不同的文件,分别存储不同的信息

stu_myisam.frm  表结构
stu_myisam.MYD  表数据
stu_myisam.MYI  表索引

1.2 MySQL查看表结构

指令:desc + table_name

MariaDB [clx_database]> desc stu_list;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| name     | varchar(20) | YES  |     | NULL    |       |
| password | char(30)    | YES  |     | NULL    |       |
| birthday | date        | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

1.3 MySQL修改表

1.3.1 表的插入
指令:insert into table_name (field1, field2, field3 …) values (variable1, variable2, variable3 …);

MariaDB [clx_database]> system clear
MariaDB [clx_database]> insert into stu_list (id, name, password, birthday) values (1, 'a', 'b', '2000-0-0'), (2, 'c','d','2020-0-0');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0
MariaDB [clx_database]> select * from stu_list;
+------+------+----------+------------+
| id   | name | password | birthday   |
+------+------+----------+------------+
|    1 | a    | b        | 2000-00-00 |
|    2 | c    | d        | 2020-00-00 |
+------+------+----------+------------+
2 rows in set (0.00 sec)

在项目实际开发中,可能想要修改某个表的结构,比如字段名字,字段大小,字段类型,表的字符集类型,表的存储引擎等等。我们还有需求,添加字段,删除字段等等。这时我们就需要修改表。

1.3.2 表添加一个字段
指令: alter table table_name add field datatype + after field(没有默认添加在队尾);

MariaDB [clx_database]> select * from stu_list;
+------+------+----------+------------+
| id   | name | password | birthday   |
+------+------+----------+------------+
|    1 | a    | b        | 2000-00-00 |
|    2 | c    | d        | 2020-00-00 |
+------+------+----------+------------+
2 rows in set (0.00 sec)MariaDB [clx_database]> alter table stu_list add path varchar(100);    //在队尾添加一个path 字段
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0
MariaDB [clx_database]> select * from stu_list;
+------+------+----------+------------+------+
| id   | name | password | birthday   | path |
+------+------+----------+------------+------+
|    1 | a    | b        | 2000-00-00 | NULL |
|    2 | c    | d        | 2020-00-00 | NULL |
+------+------+----------+------------+------+
2 rows in set (0.00 sec)
MariaDB [clx_database]> alter table stu_list add sex char(1) after name;  //在name字段后面添加 sex字段
Query OK, 2 rows affected (0.07 sec)
Records: 2  Duplicates: 0  Warnings: 0MariaDB [clx_database]> select * from stu_list;
+------+------+------+----------+------------+------+
| id   | name | sex  | password | birthday   | path |
+------+------+------+----------+------------+------+
|    1 | a    | NULL | b        | 2000-00-00 | NULL |
|    2 | c    | NULL | d        | 2020-00-00 | NULL |
+------+------+------+----------+------------+------+
2 rows in set (0.00 sec)

可以看到表添加新的字段后,内部原有数据的新建字段都被设置成NULL ,如果因为表的约束内部成员变量不可以为空,则想要插入新字段则必须将原有数据全部删除,或者新建表将原有数据重新编辑然后导入,非常麻烦。就算要插入一定要放在最后,因为上层的业务逻辑一般都是从前向后插入,若你在中间插入一个字段很可能就会将其他字段的数据抢走引起连锁反应,导致后面各个字段拿到的数据与自身类型不匹配的现象

1.3.3 表删除一个字段
指令: alter table table_name drop filed;

MariaDB [clx_database]> alter table stu_list drop sex;  //删除性别这个字段
Query OK, 2 rows affected (0.03 sec)
Records: 2  Duplicates: 0  Warnings: 0MariaDB [clx_database]> select * from stu_list;
+------+------+----------+------------+------+
| id   | name | password | birthday   | path |
+------+------+----------+------------+------+
|    1 | a    | b        | 2000-00-00 | NULL |
|    2 | c    | d        | 2020-00-00 | NULL |
+------+------+----------+------------+------+
2 rows in set (0.00 sec)

1.3.4 表名修改
指令: alter table table_name rename to new_name

MariaDB [clx_database]> alter table stu_list rename to student_list;
Query OK, 0 rows affected (0.01 sec)MariaDB [clx_database]> show tables;
+------------------------+
| Tables_in_clx_database |
+------------------------+
| stu_myisam             |
| student_list           |
+------------------------+
2 rows in set (0.00 sec)

1.3.5 表字段名修改
指令:alter table table_name change name newname newtype;

MariaDB [clx_database]> alter table student_list change name xingming char(60);  //修改name字段为xingming 字段
Query OK, 2 rows affected (0.02 sec)                                             //类型: char(60)
Records: 2  Duplicates: 0  Warnings: 0MariaDB [clx_database]> desc student_list;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int(11)      | YES  |     | NULL    |       |
| xingming | char(60)     | YES  |     | NULL    |       |
| password | char(30)     | YES  |     | NULL    |       |
| birthday | date         | YES  |     | NULL    |       |
| path     | varchar(100) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

1.3.6 表字段类型更改
指令:alter table table_name modify filed datatype;

MariaDB [clx_database]> desc student_list;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int(11)      | YES  |     | NULL    |       |
| xingming | char(60)     | YES  |     | NULL    |       |
| password | char(30)     | YES  |     | NULL    |       |
| birthday | date         | YES  |     | NULL    |       |
| path     | varchar(100) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
MariaDB [clx_database]> alter table student_list modify xingming varchar(20);  //将姓名字段的类型更改为 varchar(20)
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0MariaDB [clx_database]> desc student_list;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int(11)      | YES  |     | NULL    |       |
| xingming | varchar(20)  | YES  |     | NULL    |       |
| password | char(30)     | YES  |     | NULL    |       |
| birthday | date         | YES  |     | NULL    |       |
| path     | varchar(100) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

1.3.7 表的删除
指令: drop table table_name;

MariaDB [clx_database]> drop table stu_myisam;  //删除表stu_myisam
Query OK, 0 rows affected (0.00 sec)MariaDB [clx_database]> show tables;
+------------------------+
| Tables_in_clx_database |
+------------------------+
| student_list           |
+------------------------+
1 row in set (0.00 sec)

【MySQL】3.MySQL表操作相关推荐

  1. mysql配置——库表操作、用户操作

    mysql----库表操作 ## 列出所有的库 mysql> show databases; +--------------------+ | Database           | +--- ...

  2. 95.第十九章 MySQL数据库 -- 单表操作(五)

    3.7 DQL语句 3.7.1 单表操作 官方帮助:https://dev.mysql.com/doc/refman/8.0/en/select.html 语法: SELECT[ALL | DISTI ...

  3. mysql的数据表操作

    1.创建数据表 在创建数据表之前,要用use <数据库名>指定在哪个数据库中操作. 1.1创建表的语法格式 创建数据表语句为,其中[]中的表示可选 create table <表名& ...

  4. MySql随笔part3 表操作

    一:什么是表 表(table): 表似一种结构化的文件,可用来存储某种特定类型的数据.表中的一条记录有对应的标题,标题称之为表的字段 二:创建表 1 create table table_name( ...

  5. mysql的多表操作_Mysql-多表连接的操作和用法

    一.介绍 本节主题 多表连接查询 复合条件连接查询 子查询 准备表 #建表 create table dep( id int, name varchar(20) ); create table emp ...

  6. mysql宠物种类表,mysql中的表操作

    ------------恢复内容开始------------ 创建数据库 create database 数据库名 切换数据库 use 数据库名 建表: create table 表名 ( 字段名1, ...

  7. mysql三:表操作

    阅读目录 一 存储引擎介绍 二 表介绍 三 创建表 四 查看表结构 五 数据类型 六 表完整性约束 七 修改表ALTER TABLE 八 复制表 九 删除表 一 存储引擎介绍 存储引擎即表类型,mys ...

  8. mysql 数据库之表操作

    一.表与表之间建关系 (1) 将所有的数据放在一张表内的弊端 表的组织结构不清晰 浪费存储时间 可扩展性极差 ---> 类似于将所有的代码写入到一个py文件中 -->解耦部分 (2) 如何 ...

  9. mysql datatable_MySQL-数据表操作

    1.打开数据库 语法:USE 数据库名称: SELECT DATABASE();---查询当前使用的数据库 2.创建数据表 语法:CREATE TABLE [IF NOT EXITS] table_n ...

  10. 【MySQL】数据库表操作

    文章目录 1.创建和管理数据库 1.1 创建数据库 1.2 查看数据库 1.3 修改数据库 1.4 删除数据库 2. 创建表 2.1 创建表CREATE 2.2 创建表AS 2.3 查看表结构 3.修 ...

最新文章

  1. 陈彦铭_盆栽(陈彦铭)
  2. 关于亿级账户数据迁移,你应该试试这种方法...
  3. 图片进行base64编解码方法
  4. 多线程学习-基础(四)常用函数说明:sleep-join-yield
  5. 从零开始入门 K8s | Kubernetes 存储架构及插件使用
  6. 2017 ICPC沈阳区域赛
  7. 企业邮箱及邮件服务器架设
  8. 在freemarker中使用jsp标签 Using FreeMarker with servlets
  9. 盘点过去10年美国规模最大科技公司IPO:阿里一直是纪录保持者
  10. Linux拉取代码启动镜像,基于Linux源代码及Busybox源代码制作精简可启动内核镜像技术实验方法...
  11. cc2530设计性实验代码五
  12. Win10 打开MSDTC
  13. 汽车电子ISO7637-2 5A/5B抛负载测试详解
  14. 沟通的艺术I:什么是沟通
  15. 电子设计教程4:稳压管稳压电路
  16. php 签名 验签 pkcs8,PHP和Java的RSA签名和验签
  17. Proteus8.9 VSM Studio WINAVR编译器仿真ATmega16系列a09_扩展内存
  18. HTML嵌入三维仿真 .ezo文件的方法
  19. 编程小白的计算机毕业设计指导开发教程-javaweb i18n国际化的使用
  20. Python保存TXT文件

热门文章

  1. 消息称 Apple TV 国行已确定正式过审是真的吗?
  2. 论文信息系统项目的进度管理
  3. 系统集成项目管理师 高项论文 项目整体管理
  4. 我最喜欢的油猴脚本——可以追跑某盘SVIP
  5. fix协议的服务器,Fix8
  6. mysql odbc_MySQL数据库之mysql odbc 配置详解、解决方案
  7. sql 抛出异常raiserror()
  8. 职场人生(九):2012 回想这半年走过来的路
  9. html百度收录缩略图,百度搜索结果中的缩略图如何替换?
  10. seo网站整体关键词的布局