免费建设门户网站,做网站需要买域名吗,seo技术入口,故事性营销软文文章目录 为什么要有约束一. 空属性二. 默认值三. 列描述四. zerofill结束语 为什么要有约束
数据库是用来存放数据的#xff0c;所以其需要保证数据的完整性和可靠性 数据类型也算是一种约束#xff0c;比如#xff0c;整型的数据无法插入字符型。 通过约束#xff0c;让… 文章目录 为什么要有约束一. 空属性二. 默认值三. 列描述四. zerofill结束语 为什么要有约束
数据库是用来存放数据的所以其需要保证数据的完整性和可靠性 数据类型也算是一种约束比如整型的数据无法插入字符型。 通过约束让插入数据库的数据都是符合预期的倒逼程序员插入正确的数据而通过约束在MySQL视角中只要是插入成功的数据都是符合预期的
一. 空属性 NULL空 NOT NULL不为空 空串 空串和空是不一样的比如没有建行银行卡是空空串是有建行银行卡但是没有钱 NULL不参与任何计算 mysql select 1NULL;
--------
| 1NULL |
--------
| NULL |
--------我们在建表时属性如果没有说明not null默认可以为NULL
mysql create table student(- id varchar(6),- name varchar(3)- );mysql desc student;
----------------------------------------------
| Field | Type | Null | Key | Default | Extra |
----------------------------------------------
| id | varchar(6) | YES | | NULL | |
| name | varchar(3) | YES | | NULL | |
----------------------------------------------
//插入数据
mysql insert into student values (1,NULL);
Query OK, 1 row affected (0.01 sec)mysql select * from student;
------------
| id | name |
------------
| 1 | NULL |
------------NULL一列为YES允许为空。 但是这并不合理既然要插入数据学生的姓名和学号都不应该为空。
mysql create table student(- id varchar(6) not null,- name varchar(3) not null- );mysql insert into student values (1,NULL);
ERROR 1048 (23000): Column name cannot be null
mysql insert into student values (1,张三);
Query OK, 1 row affected (0.00 sec)mysql select * from student;
------------
| id | name |
------------
| 1 | 张三 |
------------mysql show create table student \G;
*************************** 1. row ***************************Table: student
Create Table: CREATE TABLE student (id varchar(6) NOT NULL,name varchar(3) NOT NULL
) ENGINEInnoDB DEFAULT CHARSETutf8建表属性也会显示NOT NULL
二. 默认值 Default 默认值 默认值的使用是在该属性没有显示插入数据时使用 在创建表时如果没有指定默认值那么默认值为NULL mysql create table t1(- id int- );mysql desc t1; //默认为空
-------------------------------------------
| Field | Type | Null | Key | Default | Extra |
-------------------------------------------
| id | int(11) | YES | | NULL | |
-------------------------------------------
1 row in set (0.00 sec)mysql show create table t1 \G;
*************************** 1. row ***************************Table: t1
Create Table: CREATE TABLE t1 (id int(11) DEFAULT NULL//默认为空
) ENGINEInnoDB DEFAULT CHARSETutf8
1 row in set (0.00 sec)在建表时指定默认值那么在插入数据时若没显示插入该属性数据则使用默认值
mysql create table person(-name varchar(4) not null,-age tinyint unsigned default 18
);mysql insert into person (name) values (张三);
Query OK, 1 row affected (0.00 sec)mysql select * from person;
--------------
| name | age |
--------------
| 张三 | 18 |
--------------PS:如果设定属性为NOT NULL则没有默认值 因为在建表属性中没有default null
mysql create table t1(- id int not null- );mysql show create table t1 \G;
*************************** 1. row ***************************Table: t1
Create Table: CREATE TABLE t1 (id int(11) NOT NULL
) ENGINEInnoDB DEFAULT CHARSETutf8三. 列描述 列描述comment 建表时使用相当于注释专门用来描述字段会根据表创建语句保存用来给程序员或DBA来进行了解。 mysql create table person(- name varchar(4) not null comment 姓名,- age tinyint unsigned comment 年龄,- gender varchar(1) comment 性别- );mysql show create table person \G;
*************************** 1. row ***************************Table: person
Create Table: CREATE TABLE person (name varchar(4) NOT NULL COMMENT 姓名,age tinyint(3) unsigned DEFAULT NULL COMMENT 年龄,gender varchar(1) DEFAULT NULL COMMENT 性别
) ENGINEInnoDB DEFAULT CHARSETutf8四. zerofill
创建int类型属性时显示的是int(10)这个10是什么意思呢 其实是最大能显示的位数个数使用zerofill验证
mysql create table t1( -id1 int,-id2 int zerofill-);mysql desc t1;
-------------------------------------------------------------
| Field | Type | Null | Key | Default | Extra |
-------------------------------------------------------------
| id1 | int(11) | YES | | NULL | |
| id2 | int(10) unsigned zerofill | YES | | NULL | |
-------------------------------------------------------------mysql insert into t1 values (200,200);mysql select * from t1;
------------------
| id1 | id2 |
------------------
| 200 | 0000000200 |
------------------使用zerofill会将没有显示的位数用0填充 因为int为4个字节有符号的int最大表示21亿10位数足够表示而无符号的使用11位表示足够
结束语
感谢你的阅读
如果觉得本篇文章对你有所帮助的话不妨点个赞支持一下博主拜托啦这对我真的很重要。