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 基本操作 三的更多相关文章

  1. 数据库相关 Mysql基本操作

    数据库相关 设计三范式: 第一范式: 主要强调原子性 即表的每一列(字段)包含的内容,不能再拆分.如果,某张表的列,还可以细分,则违背了数据库设计的第一范式. 第二范式: 主要强调主键,即:数据库中的 ...

  2. MySQL必知必会笔记-Mysql基本操作

    Mysql基本操作 mysql的基本操作包括增.删.改.查,本书中前三章简单的介绍MySQL为何物,查是mysql中非常重要的功能,4-6章展示了mysql的查(查询--select)的简单实现,my ...

  3. day02 MySQL基本操作

    day02 MySQL基本操作 昨日内容回顾 数据库演变史 1.纯文件阶段 2.目录规范 3.单机游戏 4.联网游戏 # 数据库就是一款帮助我们管理数据的程序 软件开发架构及数据库本质 cs架构与bs ...

  4. mysql 基本操作语句

    mysql 基本操作笔记: 创建表demo:CREATE TABLE `role` ( `role_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMME ...

  5. PHP的学习--连接MySQL的三种方式

    记录一下PHP连接MySQL的三种方式. 先mock一下数据,可以执行一下sql. /*创建数据库*/ CREATE DATABASE IF NOT EXISTS `test`; /*选择数据库*/ ...

  6. css属性编写顺序+mysql基本操作+html细节(个人笔记)

    css属性编写顺序: 影响文档流的属性(比如:display, position, float, clear, visibility, table-layout等) 自身盒模型的属性(比如:width ...

  7. (转载)MySQL默认INFORMATION_SCHEMA,MySQL,TEST三个数据库用途

    (转载)http://www.45it.com/database/201204/29390.htm 本文简要说明了MySQL数据库安装好后自带的INFORMATION_SCHEMA,MySQL,TES ...

  8. MySQL默认INFORMATION_SCHEMA,MySQL,TEST三个数据库用途

    本文简要说明了MySQL数据库安装好后自带的INFORMATION_SCHEMA,MySQL,TEST三个数据库的用途. 第一个数据库INFORMATION_SCHEMA:提供了访问数据库元数据的方式 ...

  9. MySQL优化三(InnoDB优化)

    body { font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 1.6; padding-top: 10 ...

随机推荐

  1. 高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP)

    高性能TcpServer(C#) - 1.网络通信协议 高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP) 高性能TcpS ...

  2. 3-Consul 使用手册

    原文:http://www.liangxiansen.cn/2017/04/06/consul/ Consul包含多个组件,但是作为一个整体,为你的基础设施提供服务发现和服务配置的工具.他提供以下关键 ...

  3. Lucene为什么要加Segment概念

    目前我感觉加了Segment有两个好处: 1. 简化了写文档的逻辑,解耦了写文档和读文档.如果没有Segment在写文档的时候势必要修改整个索引,所以会影响到文档的读 2. 提升了写文档的速度,由于只 ...

  4. 4. 移动安全渗透测试-(Android逆向基础)

    4.1 smali 基础 1.注释 smali中使用#来代表注释一行例如:# const-string v0, "aaa" #这句不会被执行 2.数据类型 V void,只能用于返 ...

  5. MES系统如何帮助烟草行业管理生产流程

    与很多其他行业一样,烟草MES系统可以帮助卷烟企业实现智能生产.精益制造.快速实现烟草企业数字化车间的创建,助力企业实现改造升级,从而提升企业生产效率,降低生产成产.烟草行业得MES者得天下. 烟草行 ...

  6. Django 使用 cookie 实现简单的用户管理

    Cookie: 1.保存在用户浏览器 2.可以主动清除 3.可以被伪造 4.跨域名 Cookie 不共享 创建一个项目:user_manager 和应用: app01 创建数据库,添加 models. ...

  7. 什么是证书透明度(Certificate Transparency)?

    SSL基础概念 什么是加密? 加密是一种新型的电子信息保护方式,就像过去使用保险箱和密码锁保护纸上信息一样.加密是密码学的一种技术实现方式:信息被转换为难以理解的形式(即编码),以便只有使用密钥才能将 ...

  8. odoo10学习笔记九:Odoo10 API

    转载请转载原文地址:https://www.cnblogs.com/ygj0930/p/11189315.html 一:纪录集API model中的数据是以集合的形式使用的,因此可以使用集合运算来操作 ...

  9. Rust中的哈希Map

    严谨! fn main() { use std::collections::HashMap; let mut scores = HashMap::new(); scores.insert(String ...

  10. 【比赛游记】CSP2019游记

    往期回顾:[比赛游记]NOIP2018游记 提高 D1: 密码 Ren2Zhen1Si0Kao9?. A B C 00:04 00:32 -5 \(100 + 100 + 0 = 200\) 因为提前 ...