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 ...
随机推荐
- Python-绘制3D柱形图
Python-绘制3D柱形图 本文主要讲解如何使用python绘制三维的柱形图,可以得到图1所示的效果. 图1 源代码如下: import numpy as np import matplotlib. ...
- OpenGL入门1.3:着色器 GLSL
前言 经过之前一段时间的学习(渲染管线简介)我们已经知道了着色器(Shader)是运行在GPU上的程序,这些小程序为图形渲染管线的某个特定部分而运行,着色器只是一种把输入转化为输出的程序,着色器也是一 ...
- 基于Task定时检测网络本地网络状况
首先我们需要使用winInet.dll中的InternetGetConnectedState方法来检测本地是否连接网络,然后再通过ping的方式来获取网络状况. 然后我们采用Task来开辟一个线程来定 ...
- PIE属性表多字段的文本绘制
最近研究了PIE SDK文本元素的绘制相关内容,因为在我们的开发中,希望可以做到在打开一个Shp文件后,读取到属性表的所有字段,然后可以选择一些需要的字段,将这些字段的所有要素值的文本,绘制到shp图 ...
- JZOJ 2158. 蚂蚁
这个是今天早上比赛的内容,比较水给大伙们讲一下(我只会这一个) 题目大意: n只蚂蚁以每秒1cm的速度在长为L cm(厘米,不是lcm)的竿子上爬行.当蚂蚁爬到竿子的端点时就会掉落.由于竿子太细,两 ...
- Java中级—转发和重定向的区别
在设计Web应用程序的时候,经常需要把一个系统进行结构化设计,即按照模块进行划分,让不同的Servlet来实现不同的功能,例如可以让其中一个Servlet接收用户的请求,另外一个Servlet来处理用 ...
- maven 学习---部署基于Maven的war文件到Tomcat
在本教程中,我们将学习如何使用Maven的Tomcat插件打包并部署一个WAR文件到Tomcat(Tomcat的6和7. 要用到工具: Maven 3 Tomcat 6.0.37 Tomcat 7.0 ...
- Google Analytics 学习笔记二 —— GA部署
一.直接部署 直接复制GA跟踪代码 放到所有页面 跟踪代码放到 "head"前面 二.GTM部署方法一 三.GTM部署方法二 Tacking ID 四.测试.参数配置与调优
- Django 练习班级管理系统四 -- 编辑班级
修改 classes.html {% extends "layout.html" %} {% block css %} {% endblock %} {% block conten ...
- 设置VMware中Kali 共享文件夹
(软件环境: Vmware Workstation 15.5 Pro , Kali Linux2019.3) 1. VMware设置共享目录 2. 安装VMware-Tools 命令: apt-get ...