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. Docker 指定数据储存目录

    Docker 指定储存目录(原参数 --graph 已经失效 .) 方法一 一.在 Docker 配置文件中配置 /etc/docker/daemon.json 下面的命令是在 daemon.json ...

  2. Review: Basic Knowledge about WebForm

    Asp.net shanzm

  3. 本机与虚拟机Ping不通

    关闭防火墙,设置虚拟机和本机在同一网段,还是ping不同 解决方法:在VMware中点击 编辑---->虚拟网络编辑器----->更改设置 ------->还原默认设置 然后重新配置 ...

  4. tkinter中的message

    from tkinter import * root =Tk() root.title("message练习") myText = "2019年12月13日,下午一个人, ...

  5. Innodb整体架构

    如下图展示了Innodb内存中和磁盘的结构: 内存中结构主要有如下几种: buffer pool change buffer adaptive hash index (自适应的hash索引) Log ...

  6. SUSE12-SP2安装教程(虚拟机)

    创建虚拟机,安装系统,安装系统后的系统设置 创建虚拟机 将SUSE12-SP2镜像(大于3G)上传到虚拟机主机存储. 创建虚拟机创建虚拟机,CPU>=8核,内存>=16G(注:我这里仅演示 ...

  7. vnc服务器和windows2012密钥

    [root@localhost ~]# vncserver #启动服务器 windows 2012 64位-server版本的密钥 Windows Server 2012 Standard 密钥:NB ...

  8. [MySQL] 为什么要给表加上主键

    1.一个没加主键的表,它的数据无序的放置在磁盘存储器上,一行一行的排列的很整齐. 2.一个加了主键的表,并不能被称之为「表」.如果给表上了主键,那么表在磁盘上的存储结构就由整齐排列的结构转变成了树状结 ...

  9. Arbitrage POJ - 2240

    题目链接:https://vjudge.net/problem/POJ-2240 思路:判正环,Bellman-ford和SPFA,floyd都可以,有正环就可以套利. 这里用SPFA,就是个板子题吧 ...

  10. 阿里云Web应用防火墙采用规则引擎、语义分析和深度学习引擎相结合的方式防护Web攻击

    深度学习引擎最佳实践 {#concept_1113021 .concept} 阿里云Web应用防火墙采用多种Web攻击检测引擎组合的方式为您的网站提供全面防护.Web应用防火墙采用规则引擎.语义分析和 ...