mysql 基本操作 三
1.alter
创建测试表
MariaDB [jason]> create table testalter_tbl(i int,c char());
Query OK, rows affected (0.08 sec) MariaDB [jason]> show columns from testalter_tbl;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| i | int(11) | YES | | NULL | |
| c | char(1) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.02 sec)
删除 i 字段
MariaDB [jason]> alter table testalter_tbl drop i;
Query OK, rows affected (0.06 sec)
Records: Duplicates: Warnings: MariaDB [jason]> show columns from testalter_tbl;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| c | char() | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
row in set (0.01 sec)
添加字段
MariaDB [jason]> alter table testalter_tbl add i int;
Query OK, rows affected (0.02 sec)
Records: Duplicates: Warnings: MariaDB [jason]> show columns from testalter_tbl;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| c | char() | YES | | NULL | |
| i | int() | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
将字段添加在指定位置
Query OK, rows affected (0.02 sec)
Records: Duplicates: Warnings: MariaDB [jason]> alter table testalter_tbl add i int first
-> ;
Query OK, rows affected (0.03 sec)
Records: Duplicates: Warnings: MariaDB [jason]> show columns from testalter_tbl;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| i | int() | YES | | NULL | |
| c | char() | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
rows in set (0.00 sec) MariaDB [jason]> alter table testalter_tbl drop i;
Query OK, rows affected (0.02 sec)
Records: Duplicates: Warnings: MariaDB [jason]> alter table testalter_tbl add i int after c;
Query OK, rows affected (0.03 sec)
Records: Duplicates: Warnings: MariaDB [jason]> show columns from testalter_tbl;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| c | char() | YES | | NULL | |
| i | int() | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
rows in set (0.00 sec)
修改字段类型及名称
MariaDB [jason]> alter table testalter_tbl modify c char();
Query OK, rows affected (0.02 sec)
Records: Duplicates: Warnings: MariaDB [jason]> show columns from testalter_tbl;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| c | char() | YES | | NULL | |
| i | int() | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
rows in set (0.00 sec)
用change 修改 change 旧名字 新名字 字段类型
MariaDB [jason]> alter table testalter_tbl change i j bigint;
Query OK, rows affected (0.01 sec)
Records: Duplicates: Warnings: MariaDB [jason]> show columns from testalter_tbl;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| c | char() | YES | | NULL | |
| j | bigint() | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
rows in set (0.00 sec) MariaDB [jason]> alter table testalter_tbl change j j int;
Query OK, rows affected (0.02 sec)
Records: Duplicates: Warnings: MariaDB [jason]> show columns from testalter_tbl;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| c | char() | YES | | NULL | |
| j | int() | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
rows in set (0.00 sec)
alter 对null 和默认值的影响
Query OK, rows affected (0.02 sec)
Records: Duplicates: Warnings: MariaDB [jason]> show columns from testalter_tbl;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| c | char() | YES | | NULL | |
| j | bigint() | NO | | | |
+-------+------------+------+-----+---------+-------+
修改字段默认值
MariaDB [jason]> alter table testalter_tbl alter j set default ;
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: MariaDB [jason]> show columns from testalter_tbl;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| c | char() | YES | | NULL | |
| j | bigint() | NO | | | |
+-------+------------+------+-----+---------+-------+
rows in set (0.00 sec)
删除默认值
MariaDB [jason]> alter table testalter_tbl alter j drop default;
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: MariaDB [jason]> show columns from testalter_tbl;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| c | char() | YES | | NULL | |
| j | bigint() | NO | | NULL | |
+-------+------------+------+-----+---------+-------+
rows in set (0.01 sec)
修改表的引擎
MariaDB [jason]> SHOW TABLE STATUS LIKE 'testalter_tbl' \G;
*************************** . row ***************************
Name: testalter_tbl
Engine: InnoDB
Version:
Row_format: Dynamic
Rows:
Avg_row_length:
Data_length:
Max_data_length:
Index_length:
Data_free:
Auto_increment: NULL
Create_time: -- ::
Update_time: NULL
Check_time: NULL
Collation: utf8mb4_general_ci
Checksum: NULL
Create_options:
Comment:
row in set (0.00 sec) ERROR: No query specified MariaDB [jason]> alter table testalter_tbl engine=myisam;
Query OK, rows affected (0.03 sec)
Records: Duplicates: Warnings: MariaDB [jason]> SHOW TABLE STATUS LIKE 'testalter_tbl' \G;
*************************** . row ***************************
Name: testalter_tbl
Engine: MyISAM
Version:
Row_format: Fixed
Rows:
Avg_row_length:
Data_length:
Max_data_length:
Index_length:
Data_free:
Auto_increment: NULL
Create_time: -- ::
Update_time: -- ::
Check_time: NULL
Collation: utf8mb4_general_ci
Checksum: NULL
Create_options:
Comment:
row in set (0.00 sec) ERROR: No query specified
修改表名
MariaDB [jason]> alter table testalter_tbl rename to testalter;
2.索引
添加,删除索引
MariaDB [jason]> alter table testalter add index index_cj(c,j); //index_cj 是索引名字,一个索引中可以包含>=1 个列
Query OK, rows affected (0.01 sec)
Records: Duplicates: Warnings: MariaDB [jason]> show index from testalter;
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| testalter | | index_cj | | c | A | NULL | NULL | NULL | YES | BTREE | | |
| testalter | | index_cj | | j | A | NULL | NULL | NULL | | BTREE | | |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
rows in set (0.01 sec) MariaDB [jason]> drop index index_cj on testalter;
Query OK, rows affected (0.01 sec)
Records: Duplicates: Warnings:
添加,删除 unique 索引
unique 列中 值的组合必须唯一
MariaDB [jason]> alter table testalter add unique uq_c(c);
Query OK, rows affected (0.01 sec)
Records: Duplicates: Warnings: MariaDB [jason]> show index from testalter;
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| testalter | | uq_c | | c | A | NULL | NULL | NULL | YES | BTREE | | |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
row in set (0.00 sec) MariaDB [jason]> drop index uq_c on testalter;
Query OK, rows affected (0.01 sec)
Records: Duplicates: Warnings:
添加,删除主键
MariaDB [jason]> alter table testalter add primary key(c,j);
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: MariaDB [jason]> show index from testalter;
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| testalter | | PRIMARY | | c | A | NULL | NULL | NULL | | BTREE | | |
| testalter | | PRIMARY | | j | A | | NULL | NULL | | BTREE | | |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
rows in set (0.00 sec) MariaDB [jason]> alter table testalter drop primary key;
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: MariaDB [jason]> show index from testalter;
Empty set (0.00 sec)
mysql 基本操作 三的更多相关文章
- 数据库相关 Mysql基本操作
数据库相关 设计三范式: 第一范式: 主要强调原子性 即表的每一列(字段)包含的内容,不能再拆分.如果,某张表的列,还可以细分,则违背了数据库设计的第一范式. 第二范式: 主要强调主键,即:数据库中的 ...
- MySQL必知必会笔记-Mysql基本操作
Mysql基本操作 mysql的基本操作包括增.删.改.查,本书中前三章简单的介绍MySQL为何物,查是mysql中非常重要的功能,4-6章展示了mysql的查(查询--select)的简单实现,my ...
- day02 MySQL基本操作
day02 MySQL基本操作 昨日内容回顾 数据库演变史 1.纯文件阶段 2.目录规范 3.单机游戏 4.联网游戏 # 数据库就是一款帮助我们管理数据的程序 软件开发架构及数据库本质 cs架构与bs ...
- mysql 基本操作语句
mysql 基本操作笔记: 创建表demo:CREATE TABLE `role` ( `role_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMME ...
- PHP的学习--连接MySQL的三种方式
记录一下PHP连接MySQL的三种方式. 先mock一下数据,可以执行一下sql. /*创建数据库*/ CREATE DATABASE IF NOT EXISTS `test`; /*选择数据库*/ ...
- css属性编写顺序+mysql基本操作+html细节(个人笔记)
css属性编写顺序: 影响文档流的属性(比如:display, position, float, clear, visibility, table-layout等) 自身盒模型的属性(比如:width ...
- (转载)MySQL默认INFORMATION_SCHEMA,MySQL,TEST三个数据库用途
(转载)http://www.45it.com/database/201204/29390.htm 本文简要说明了MySQL数据库安装好后自带的INFORMATION_SCHEMA,MySQL,TES ...
- MySQL默认INFORMATION_SCHEMA,MySQL,TEST三个数据库用途
本文简要说明了MySQL数据库安装好后自带的INFORMATION_SCHEMA,MySQL,TEST三个数据库的用途. 第一个数据库INFORMATION_SCHEMA:提供了访问数据库元数据的方式 ...
- MySQL优化三(InnoDB优化)
body { font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 1.6; padding-top: 10 ...
随机推荐
- GO学习笔记 - 模版渲染及多种输出
本文主题:基于内置的text/template实现Golang模版渲染,并将结果写入文件.屏幕.变量. 小慢哥的原创文章,欢迎转载 目录 ▪ 定义结构体 ▪ 定义模版文本 ▪ 模版渲染及输出方式 ▪ ...
- JavaScript的闭包特性如何给循环中的对象添加事件(一)
初学者经常碰到的,即获取HTML元素集合,循环给元素添加事件.在事件响应函数中(event handler)获取对应的索引.但每次获取的都是最后一次循环的索引.原因是初学者并未理解JavaScript ...
- PMP备考-第一章-引论
项目 项目是为创造独特的产品,服务和成果而进行的临时性工作.在规定的范围,进度,成本,和质量要求之下完成项目可交付成果. 项目与运用 项目 :临时性,独特性,渐进明细 运营 :持续的,相似性,标准化 ...
- 章节十一、6-操作集合里面的Web元素
以下演示操作以该网站为例:https://learn.letskodeit.com/p/practice 一.如何操作多个元素(把多个元素放到集合容器中然后操作它们) 列如我们需要操作这些单选框:: ...
- X264-视频帧的存取
X264的编码器结构体x264_t中的子结构体字段frames包含了4个临时视频帧序列空间:current.next.unused和reference,分别保存当前编码帧.将编码帧序列.未处理原始视频 ...
- python中lambda
lambda_expr ::= "lambda" [parameter_list]: expression python中lambda可以理解为一个匿名函数,它的要求是函数的运算部 ...
- vlan技术简单了解
VLAN(虚拟局域网)是对连接到的第二层交换机端口的网络用户的逻辑分段,不受网络用户的物理位置限制而根据用户需求进行网络分段.一个VLAN可以在一个交换机或者跨交换机实现.VLAN可以根据网络用户的位 ...
- MySQL Execution Plan--COUNT相关测试
COUNT全表记录 在MySQL中,相同的SQL不同的存储引擎执行计划不同: 对于MyISAM引擎,由于使用表锁进行并发控制,同一时间点多个并发线程执行相同查询获得的结果相同,且MyISAM存储引擎专 ...
- VUE 直接通过JS 修改html对象的值导致没有更新到数据中去
业务场景 我们在使用vue 编写 代码时,我们有一个 多行文本框控件,希望在页面点击一个按钮 在 文本框焦点位置插入一个 {pk}的数据. 发现插入 这个数据后,这个数据并没有同步到 数据中,但是直接 ...
- [PHP] 项目实践中使用的IOC容器思想
1.容器的意思就是一个全局变量,里面存了很多对象,如果要用到某个对象就从里面取,前提就是要先把对象放进去2.控制反转就是把自己的控制权交给别人3.这两个结合就是,把自己的控制权交给别人并且创建的对象放 ...