MySQL基础之表的管理
添加和删除字段操作
添加字段
alter table tbl_name add 字段名称 字段属性 [完整性约束条件] [first|after 字段名称之后];
删除字段
alter table tbl_name drop 字段名称;
测试
create table if not exists user1(
id int unsigned auto_increment key
);
--添加用户名字段 username varchar(20)
alter table user1 add username varchar(20);
--添加密码字段 password varchar(20) 到id字段之后
alter table user1 add password varchar(20) not null default '123456' after id;
--删除密码字段
alter table user1 drop password;
--多次添加或删除用逗号分隔
create table if not exists test(
id int unsigned auto_increment key,
price float(8,2) unsigned not null default 0
);
alter table test
add num int unsigned not null default 100,
add test varchar(50) not null first,
add test1 char(23) not null after price,
drop price;
添加和删除默认值操作
添加默认值
alter table tbl_name alter 字段名称 set default 默认值;
删除默认值
alter table tbl_name alter 字段名称 drop default;
测试
create table user2(
id int unsigned auto_increment key,
username varchar(20) not null,
age tinyint unsigned not null default 18,
email varchar(50) not null
);
--给email字段添加默认值 huowuyan@163.com
alter table user2 alter email set default 'huowuyan@163.com';
--给age删除默认值
alter table user2 alter age drop default;
modify和change关键字的使用
修改字段类型、字段属性
alter table tbl_name modify 字段名称 字段类型 [字段属性] [first|after 字段名称];
修改字段名称、字段类型、字段属性
alter table tbl_name change 原字段名称 新字段名称 字段类型 [字段属性] [first|after 字段名称];
测试
create table user3(
id int unsigned auto_increment key,
username varchar(5) not null unique,
password char(32) not null,
email varchar(10) not null
);
--将用户名字段的类型改为20
alter table user3 modify username varchar(20) not null unique;
--将username 名称改为user
alter table user3 change username user varchar(20) not null;
主键和唯一索引操作
添加主键和删除主键
alter table tbl_name add primary key(字段名称);
alter table tbl_name drop primary key;
添加唯一和删除唯一
alter table tbl_name add unique key|index [index_name] (字段名称);
alter table tbl_name drop index index_name;
--当表中没有主键时,unique就相当于主键
测试
create table user4(
id int unsigned,
username varchar(20) not null,
email varchar(30) not null,
phone varchar(20) not null unique
);
alter table user4 add primary key(id);
alter table user4 drop primary key;
alter table user4 add unique key uni_email (email);
alter table user4 drop index uni_email;
重命名表的操作
alter table tbl_name rename [to|as] new_tbl_name; --第一种方式
rename table tbl_name to new_tbl_name; --第二种方式
MySQL基础之表的管理的更多相关文章
- mysql基础-数据库表的管理-记录(四)
0x01 MySQL中字符大小写 1.SQL关键字及函数不区分大小写 2.数据库.表及视图名称的大小写区分与否取决于底层OS及FS 3.存储过程.存储函数及事件调度器的名字不区分大小写,但触发器区分大 ...
- mysql基础篇--表的管理
表的创建 常见的数据类型 数值型: 整型 tinyint.smallint.mediumint.int/integer.bigint 特点: 1.如果不设置无符号还是有符号,默认是有符号,如果想设置无 ...
- MySQL基础知识:启动管理和账号管理
整理.记录常用的MySQL基础知识:时间久了,很多就忘记了. 操作系统环境为MacOS Catalina, MySQL版本为: 8.0.13 MySQL Community Server - GPL. ...
- MySQL数据库以及表的管理
MySQL数据库以及表的管理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 今天我们探讨的话题就是如何使用MySQL做开发,我们运维的主要工作不是去开发SQL的,但尽管如此,我们有 ...
- MySQL库和表的管理
MySQL数据库服务配置好后,系统会有4个默认的数据库. information_schema:虚拟对象,其对象都保存在内存中performance_schema:服务器性能指标库mysql:记录用户 ...
- MySQL基础篇——安装、管理
MySQL 安装 所有平台的 MySQL 下载地址为https://dev.mysql.com/downloads/mysql/ .挑选你需要的 MySQL Community Server 版本及对 ...
- MySql库、表权限管理
#授权表user #该表放行的权限,针对:所有数据,所有库下所有表,以及表下的所有字段db #该表放行的权限,针对:某一数据库,该数据库下的所有表,以及表下的所有字段tables_priv #该表放行 ...
- mysql基础-数据库表简单查询-记录(五)
0x01 MySQL的查询操作 单表查询:简单查询 多表查询:连续查询 联合查询 选择和投影 投影:挑选要符合的字段 select ...
- mysql基础,数据表的类型
随机推荐
- Ajax跨域请求,设置content
在使用Ajax跨域请求时,如果设置Header的ContentType为application/json,会分两次发送请求.第 一次先发送Method为OPTIONS的请求到服务器,这个请求会询问服务 ...
- JS基础语法---创建对象---三种方式创建对象:调用系统的构造函数;自定义构造函数;字面量的方式
创建对象三种方式: 调用系统的构造函数创建对象 自定义构造函数创建对象(结合第一种和需求通过工厂模式创建对象) 字面量的方式创建对象 第一种:调用系统的构造函数创建对象 //小苏举例子: //实例化对 ...
- 【转载】从使用到原理学习Java线程池
线程池的技术背景 在面向对象编程中,创建和销毁对象是很费时间的,因为创建一个对象要获取内存资源或者其它更多资源.在Java中更是如此,虚拟机将试图跟踪每一个对象,以便能够在对象销毁后进行垃圾回收. 所 ...
- 【React Native】在原生和React Native间通信(RN调用原生)
一.从React Native中调用原生方法(原生模块) 原生模块是JS中也可以使用的Objective-C类.一般来说这样的每一个模块的实例都是在每一次通过JS bridge通信时创建的.他们可以导 ...
- 默认VS 下machine.config的位置
- Linux数据库的创建 导入导出 以及一些基本指令
首先linux 下查看mysql相关目录 查看 mysql 的安装路径 执行查询 SQL mysql>show variables like '%dir%'; datadir 就是数据路径 确定 ...
- [Linux] Nginx服务下统计网站的QPS
单位时间的请求数就是QPS,那么在nginx服务的网站下,如果要统计QPS并且按从高到低排列,需要使用awk配合sort进行处理awk做的主要工作是把access每行日志按分隔符分开,然后循环每一行, ...
- WPF 快捷键
原文:WPF 快捷键 <p><pre name="code" class="csharp"> 前台 <Window.Resourc ...
- CF1248E Queue in the Train
题目链接 problem 火车上的一列人要去排队接水.每个人都会在某个特定的时刻口渴.口渴之后他要去排队接水,如果他前面的座位有人已经在排队或者正在接水,那么他就不会去排队.否则他就会去排队.每个人接 ...
- HTML连载42-清空默认边距、文字行高
一. webstorm取色技巧:webstorm内置了颜色取色器,我们对某种颜色未知的时候,可以利用下图中的取色器,进行颜色识别. 二.系统会默认给body添加外边距,因此我们对 ...