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 ... 
随机推荐
- C# 去除数字中多于的0
			decimal i = decimal.Parse(Console.ReadLine()); Console.WriteLine((i).ToString(")); Console.Writ ... 
- python爬取豆瓣视频信息代码
			目录 一:代码 二:结果如下(部分例子) 这里是爬取豆瓣视频信息,用pyquery库(jquery的python库). 一:代码 from urllib.request import quote ... 
- HTML5中localStorage的使用
			为什么要存在localStorage 在HTML5中,新加入了一个localStorage特性,这个特性主要是用来作为本地存储来使用的,解决了cookie存储空间不足的问题(cookie中每条cook ... 
- js javascript map函数去重功能的使用实例
			js javascript map函数去重功能的使用实例 先上一个实战例子代码 var map = new Map(); for(var i=0; i<=9; i++){ map.set(i,i ... 
- 【原】Spring测试类代码
			package test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.bea ... 
- c++和c动态申请二维数组
			这是我面试中遇到的一道题,用c和c++分别申请一个二维数组,int **res,要求申请后的可以使用res[3][4]这一类防存方式. 这个是没有错误检查的版本. 答案: c++语言的版本 int * ... 
- WIP表解析
			1,WIP的作用 负责纪录生产相关信息,生产什莫--工单的制定,下达,生产步鄹--工序及其移动,投入什莫--组件需求和投料,资源投入入和费用吸收,负责纪录生产成本的归集和差异分析,投入多少组件,资 ... 
- K8s容器编排
			K8s容器编排 Kubernetes(k8s)具有完备的集群管理能力: 包括多层次的安全防护和准入机制 多租户应用支撑能力 透明的服务注册和服务发现机制 内建智能负载均衡器 强大的故障发现和自我修复能 ... 
- openstack Train 版本dashaboard 404问题
			版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明本文链接:https://blog.csdn.net/weixin_28738845/articl ... 
- JS高阶---定时器相关
			首先看几个问题: [主体] (1)定时器真的时定时执行的吗? 顺序验证: 测试结果: 接下来对上述代码做下修改,增加一个长时间工作的消耗,此时再来验证下定时器运行的精准度 结果如下: (2)定时器回调 ... 
