当前位置:网站首页> MySQL约束之默认约束default与零填充约束zerofill
MySQL约束之默认约束default与零填充约束zerofill
2022-07-07 18:38:00 【1024问】
默认约束
添加默认约束
删除默认约束
零填充约束
总结
默认约束MySQL 默认值约束用来指定某列的默认值。
添加默认约束方式1: <字段名> <数据类型> default <默认值>;
方式2: alter table 表名 modify 列名 类型 default 默认值;
create table t_user10 (id int ,name varchar(20) ,address varchar(20) default ‘北京' -- 指定默认约束);-- alter table 表名 modify 列名 类型 default 默认值;create table t_user11 (id int ,name varchar(20) ,address varchar(20));alter table t_user11 modify address varchar(20) default ‘北京';删除默认约束-- alter table <表名> modify column <字段名> <类型> default null;alter table t_user11 modify column address varchar(20) default null;零填充约束1、插入数据时,当该字段的值的长度小于定义的长度时,会在该值的前面补上相应的0
2、zerofill默认为int(10)
3、当使用zerofill 时,默认会自动加unsigned(无符号)属性,使用unsigned属性后,数值范围是原值的2倍,例如,有符号为-128~+127,无符号为0~256。
比如:现在设置某个字段的长度为5,那么真实数据是1,那么显示在你的数据库的是00001;
create table t_user12 (id int zerofill , -- 零填充约束name varchar(20));alter table t_user12 modify id int;-- 1. 添加约束create table t_user12 (id int zerofill , -- 零填充约束name varchar(20));insert into t_user12 values(123, '张三');insert into t_user12 values(1, '李四');insert into t_user12 values(2, '王五');
前面我们学习了很多的约束,这里这一个大致的总结,我们需要根据自己的应用场景去设置这些约束,最终才可以达到我们的日常需求:
-- 总结-- 1:通过修改表结构添加主键约束create table emp4(eid int primary key,name varchar(20),deptId int,salary double);-- 2:添加自增正约束create table t_user1 (id int primary key auto_increment,name varchar(20));-- 3:创建非空约束create table mydb1.t_user6 (id int ,name varchar(20) not null, -- 指定非空约束address varchar(20) not null -- 指定非空约束);-- 4:创建唯一约束create table t_user8 (id int ,name varchar(20) ,phone_number varchar(20) unique -- 指定唯一约束);-- 5:创建默认约束create table t_user10 (id int ,name varchar(20) ,address varchar(20) default '北京' -- 指定默认约束);到此这篇关于MySQL约束之默认约束default与零填充约束zerofill的文章就介绍到这了,更多相关MySQL约束 内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- Precautions for cjson memory leakage
- Nebula importer data import practice
- How to choose financial products? Novice doesn't know anything
- Intelligent software analysis platform embold
- OneSpin 360 DV新版发布,刷新FPGA形式化验证功能体验
- 华为CE交换机下载文件FTP步骤
- Implement secondary index with Gaussian redis
- ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
- Micro service remote debug, nocalhost + rainbow micro service development second bullet
- Klocwork code static analysis tool
猜你喜欢

神兵利器——敏感文件发现工具

让这个CRMEB单商户微信商城系统火起来,太好用了!

VMWare中虚拟机网络配置

Cantata9.0 | new features

不落人后!简单好用的低代码开发,快速搭建智慧管理信息系统

C语言 整型 和 浮点型 数据在内存中存储详解(内含原码反码补码,大小端存储等详解)

How to meet the dual needs of security and confidentiality of medical devices?

Opencv learning notes high dynamic range (HDR) imaging
![Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]](/img/af/61b384b1b6ba46aa1a6011f8a30085.png)
Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]

Apifox 接口一体化管理新神器
随机推荐
H3C S7000/S7500E/10500系列堆叠后BFD检测配置方法
Referrer和Referrer-Policy简介
阿里云有奖体验:如何通过ECS挂载NAS文件系统
写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!
Read PG in data warehouse in one article_ stat
Implement secondary index with Gaussian redis
写一下跳表
凌云出海记 | 赛盒&华为云:共助跨境电商行业可持续发展
开发一个小程序商城需要多少钱?
[award publicity] issue 22 publicity of the award list in June 2022: Community star selection | Newcomer Award | blog synchronization | recommendation Award
Implement secondary index with Gaussian redis
Mrs offline data analysis: process OBS data through Flink job
开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
Opencv learning notes high dynamic range (HDR) imaging
使用camunda做工作流设计,驳回操作
POJ 1742 coins (monotone queue solution) [suggestions collection]
字符串中数据排序
Apifox interface integrated management new artifact
使用高斯Redis实现二级索引