当前位置:网站首页> 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约束 内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- 神兵利器——敏感文件发现工具
- With st7008, the Bluetooth test is completely grasped
- How does codesonar help UAVs find software defects?
- Numerical method for solving optimal control problem (0) -- Definition
- Cantata9.0 | 全 新 功 能
- Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city
- [philosophy and practice] the way of program design
- Data sorting in string
- Introduction to referer and referer policy
- 有用的win11小技巧
猜你喜欢

Implement secondary index with Gaussian redis

【OpenCV 例程200篇】223. 特征提取之多边形拟合(cv.approxPolyDP)

软件缺陷静态分析 CodeSonar 5.2 新版发布

机械臂速成小指南(十一):坐标系的标准命名

复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算

Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)

写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!

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

如何满足医疗设备对安全性和保密性的双重需求?

Cantata9.0 | new features
随机推荐
How to choose fund products? What fund is suitable to buy in July 2022?
如何满足医疗设备对安全性和保密性的双重需求?
Spark 判断DF为空
When easygbs cascades, how to solve the streaming failure and screen jam caused by the restart of the superior platform?
[solution] package 'XXXX' is not in goroot
一. 基础概念
Make this crmeb single merchant wechat mall system popular, so easy to use!
开发一个小程序商城需要多少钱?
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
Cantata9.0 | 全 新 功 能
Introduction to referer and referer policy
Try the tuiroom of Tencent cloud (there is an appointment in the evening, which will be continued...)
测量楼的高度
目标:不排斥 yaml 语法。争取快速上手
阿里云有奖体验:如何通过ECS挂载NAS文件系统
Klocwork 代码静态分析工具
Tensorflow2.x下如何运行1.x的代码
Flask1.1.4 Werkzeug1.0.1 源码分析:路由
Lingyun going to sea | yidiantianxia & Huawei cloud: promoting the globalization of Chinese e-commerce enterprise brands
ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your