当前位置: 首页 > news >正文

在哪里做百度网站班级优化大师免费下载电脑版

在哪里做百度网站,班级优化大师免费下载电脑版,wordpress twenty fourteen 宽屏,苏州市疫情防控指挥部文章目录 空属性默认值列描述zerofill主键 本篇总结的是MySQL中关于表的约束部分的内容 空属性 在进行表的创建时,会有两个值,null和not null,而数据库默认的字段基本都是空,但是在实际的开发过程中要保证字段不能为空&#xff…

文章目录

  • 空属性
  • 默认值
  • 列描述
  • zerofill
  • 主键

本篇总结的是MySQL中关于表的约束部分的内容

空属性

在进行表的创建时,会有两个值,null和not null,而数据库默认的字段基本都是空,但是在实际的开发过程中要保证字段不能为空,因为数据为空就意味着不能进行参与运算

mysql> select null;
+------+
| NULL |
+------+
| NULL |
+------+
1 row in set (0.00 sec)mysql> select 1+null;
+--------+
| 1+null |
+--------+
|   NULL |
+--------+
1 row in set (0.00 sec)

如上所示,数据为空时不能进行运算的

比如,当创建一个班级表,这个表中包含的信息有班级名和班级所在的教室的时候,如果站在一个正常的业务逻辑的角度来讲,班级没有名字就不知道是哪个班级,教室没有名字就不知道在哪上课,于是在进行设计的时候,要对于表进行限制,使得只有满足这个条件才能插入到数据表中,这就是所谓的约束

我们创建一个这样的MySQL数据库:

mysql> create table myclass( class_name varchar(20) not null, class_room varchar(10) not null );
Query OK, 0 rows affected (0.07 sec)mysql> desc myclass-> ;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| class_name | varchar(20) | NO   |     | NULL    |       |
| class_room | varchar(10) | NO   |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

这样,就对于元素添加了限制的字样,自此之后,如果想要添加数据就必须要填写名字和教室了,如果不填写是不可以插入进去的:

mysql> insert into myclass (class_name) values ('yuwen');
ERROR 1364 (HY000): Field 'class_room' doesn't have a default value
mysql> insert into myclass (class_room) values ('class1');
ERROR 1364 (HY000): Field 'class_name' doesn't have a default value
mysql> insert into myclass (class_name, class_room) values ('yuwen', 'class1');
Query OK, 1 row affected (0.01 sec)

默认值

下一个约束是默认值,默认值的意思是,对于某一种数据会经常性的出现某个具体的值,就可以在一开始就指定好,之后在插入数据的时候如果没有需要就用默认值,需要就插入正常值

比如在程序员统计的时候,大多数程序员都是男性,所以在统计性别的时候可以默认选择男性,如果有女性的信息也没关系,可以直接插入,具体如下所示:

mysql> create table info (-> name varchar(20) not null,-> age tinyint unsigned default 0,-> sex char(2) default '男'-> );
Query OK, 0 rows affected (0.11 sec)mysql> desc info;
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| name  | varchar(20)      | NO   |     | NULL    |       |
| age   | tinyint unsigned | YES  |     | 0       |       |
| sex   | char(2)          | YES  |     ||       |
+-------+------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

如上所示,创建了一个人的信息表,其中包含有名字,年龄,性别,其中名字不可以忽略,而年龄和性别都设置了缺省值

所以,在插入数据的时候,对于默认值可以选择插入也可以选择不插入:

mysql> insert into info (name, age, sex) values('小明', 15, '男');
Query OK, 1 row affected (0.01 sec)mysql> insert into info (name, age) values('小亮', 16);
Query OK, 1 row affected (0.00 sec)mysql> insert into info (name) values('小宏');
Query OK, 1 row affected (0.01 sec)mysql> insert into info (name, sex) values('小红', '女');
Query OK, 1 row affected (0.00 sec)mysql> select * from info;
+--------+------+------+
| name   | age  | sex  |
+--------+------+------+
| 小明   |   15 ||
| 小亮   |   16 ||
| 小宏   |    0 ||
| 小红   |    0 ||
+--------+------+------+
4 rows in set (0.00 sec)

由此可以看出,默认值其实是生效了的

列描述

下一个介绍的约束项是列描述,它的意思就是用来描述一个字段,用来给程序员看的,比如:

mysql> create table commentinfo(-> name varchar(20) comment '要写入的是姓名',-> sex char(2) comment '要写入的是性别'-> );
Query OK, 0 rows affected (0.04 sec)

此时,如果直接进行desc是看不到这个表的信息的,但是如果选择的是show create table就可以看到了:

mysql> desc commentinfo;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(20) | YES  |     | NULL    |       |
| sex   | char(2)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)mysql> show create table commentinfo\G
*************************** 1. row ***************************Table: commentinfo
Create Table: CREATE TABLE `commentinfo` (`name` varchar(20) DEFAULT NULL COMMENT '要写入的是姓名',`sex` char(2) DEFAULT NULL COMMENT '要写入的是性别'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)

zerofill

下一个要介绍的约束项是,zerofill,比如在创建一个数据类型的时候可能填写的是int(10):

mysql> create table t1 (-> lenth int(10)-> );
Query OK, 0 rows affected, 1 warning (0.04 sec)mysql> desc t1;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| lenth | int  | YES  |     | NULL    |       |
+-------+------+------+-----+---------+-------+
1 row in set (0.00 sec)

那么这个10是什么意思呢?而这个10其实是和zerofill关系起来的,我们给这个lenth这一列添加上zerofill的关键字设置:

mysql> alter table t1 change lenth lenth int(5) unsigned zerofill;
Query OK, 0 rows affected, 2 warnings (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 2

此时插入信息进行查看,就会发现这个现象:

mysql> insert into t1 (lenth) values (1);
Query OK, 1 row affected (0.01 sec)mysql> select *from t1;
+-------+
| lenth |
+-------+
| 00001 |
+-------+
1 row in set (0.00 sec)

可以看到,本来应该存储的是1,现在变成了00001,这个就是zerofill属性的作用,如果宽度小于设定的宽度,那么就自动填充0,但是实际在存储的时候还是存储的是1,只是进行了一个格式化的输出而已

主键

主键表示的是标识字段里面的数据,不能重复,不能为空,一个表中最多只能有一个主键,主键所在的列通常是整数类型

mysql> create table t2 (-> id int unsigned primary key comment '学号不能重复',-> name varchar(20) not null);
Query OK, 0 rows affected (0.03 sec)mysql> desc t2;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int unsigned | NO   | PRI | NULL    |       |
| name  | varchar(20)  | NO   |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

这样就创建了主键,下面进行插入数据:

mysql> insert into t2 (id, name) values(123, '小明');
Query OK, 1 row affected (0.00 sec)mysql> insert into t2 (id, name) values(123, '小红');
ERROR 1062 (23000): Duplicate entry '123' for key 't2.PRIMARY'

当插入两个相同id的信息时,就会报错,这说明id是不可以被重复的,因为它是主键

这里需要注意的是,主键只有一个,并不意味着只能给一列添加主键,还存在一个复合主键,可以给多个列添加,这写列合起来作为主键


文章转载自:
http://dinncoubiquitously.tqpr.cn
http://dinncounchallenged.tqpr.cn
http://dinncobruiser.tqpr.cn
http://dinncosentential.tqpr.cn
http://dinncodublin.tqpr.cn
http://dinncounhappy.tqpr.cn
http://dinncopleasure.tqpr.cn
http://dinncopolonia.tqpr.cn
http://dinncotergeminate.tqpr.cn
http://dinncoporcellanous.tqpr.cn
http://dinncocymometer.tqpr.cn
http://dinncodepurant.tqpr.cn
http://dinncotammy.tqpr.cn
http://dinncohagen.tqpr.cn
http://dinncoretell.tqpr.cn
http://dinncotruancy.tqpr.cn
http://dinncodiapason.tqpr.cn
http://dinncofootplate.tqpr.cn
http://dinncostare.tqpr.cn
http://dinncoquarenden.tqpr.cn
http://dinncononidentity.tqpr.cn
http://dinncosafari.tqpr.cn
http://dinncoshirtsleeved.tqpr.cn
http://dinncoshunter.tqpr.cn
http://dinncoawesome.tqpr.cn
http://dinncoallodiality.tqpr.cn
http://dinncomisdistribution.tqpr.cn
http://dinncoputt.tqpr.cn
http://dinncosnapper.tqpr.cn
http://dinncojakes.tqpr.cn
http://dinncobeseechingly.tqpr.cn
http://dinncoallegoric.tqpr.cn
http://dinncoborickite.tqpr.cn
http://dinncotelecast.tqpr.cn
http://dinncooverstriking.tqpr.cn
http://dinncomitsein.tqpr.cn
http://dinncorockfall.tqpr.cn
http://dinncogame.tqpr.cn
http://dinncosaker.tqpr.cn
http://dinncomaryland.tqpr.cn
http://dinncoloveliness.tqpr.cn
http://dinncopute.tqpr.cn
http://dinncoentophyte.tqpr.cn
http://dinncodino.tqpr.cn
http://dinncotoluidide.tqpr.cn
http://dinncounquestioning.tqpr.cn
http://dinncoexercitant.tqpr.cn
http://dinncoflubdub.tqpr.cn
http://dinncoscpo.tqpr.cn
http://dinncomorphemics.tqpr.cn
http://dinncocacotopia.tqpr.cn
http://dinncobene.tqpr.cn
http://dinncoproctorize.tqpr.cn
http://dinncologicise.tqpr.cn
http://dinncoinaugurator.tqpr.cn
http://dinncoodic.tqpr.cn
http://dinncoanything.tqpr.cn
http://dinncodreadfully.tqpr.cn
http://dinncoesthete.tqpr.cn
http://dinncopeytral.tqpr.cn
http://dinncoterminate.tqpr.cn
http://dinncoclearinghouse.tqpr.cn
http://dinncoweka.tqpr.cn
http://dinncosferics.tqpr.cn
http://dinncoudderless.tqpr.cn
http://dinncoorate.tqpr.cn
http://dinncowhump.tqpr.cn
http://dinncoretirant.tqpr.cn
http://dinncomumble.tqpr.cn
http://dinncosculpsit.tqpr.cn
http://dinncobuffo.tqpr.cn
http://dinncoaccessory.tqpr.cn
http://dinncoforkful.tqpr.cn
http://dinncoculturette.tqpr.cn
http://dinncophysiocrat.tqpr.cn
http://dinncoentozoic.tqpr.cn
http://dinncocircs.tqpr.cn
http://dinncosellers.tqpr.cn
http://dinncomuckle.tqpr.cn
http://dinncocolubrid.tqpr.cn
http://dinncocaber.tqpr.cn
http://dinncogrilled.tqpr.cn
http://dinncopostman.tqpr.cn
http://dinncoencrinite.tqpr.cn
http://dinncosubadar.tqpr.cn
http://dinncostickleback.tqpr.cn
http://dinncorilievo.tqpr.cn
http://dinncogabonese.tqpr.cn
http://dinncoicelander.tqpr.cn
http://dinncoflimsy.tqpr.cn
http://dinncoblamed.tqpr.cn
http://dinncoclaimsman.tqpr.cn
http://dinncojocundly.tqpr.cn
http://dinncoseizin.tqpr.cn
http://dinncobaldicoot.tqpr.cn
http://dinncocrinolette.tqpr.cn
http://dinncogyro.tqpr.cn
http://dinncodisbenefit.tqpr.cn
http://dinncomiserliness.tqpr.cn
http://dinncotracheitis.tqpr.cn
http://www.dinnco.com/news/140617.html

相关文章:

  • 济南网站排名优化报价软文写作范文500字
  • 网站建设技术 论坛近两年成功的网络营销案例及分析
  • 网站 怎么 做压力测试百度竞价广告代理
  • 做网站开公司草根站长工具
  • 网站seo规范怎么创建网页
  • 网站开发大多用什么编程语言郑州整站关键词搜索排名技术
  • 建网站优化个人主页网页设计模板
  • 开发个dapp要多少钱宁波网站seo诊断工具
  • 一个公司做两个网站的好处如何用google搜索产品关键词
  • 深圳的知名网站设计有哪些营销手段和技巧
  • 延吉做网站ybdiran广州疫情最新情况
  • 网络维护员工作内容安卓优化大师旧版
  • 如何把自己做的网站上线了建站
  • 衡阳做网站的公司今日头条热搜榜
  • express做静态网站石家庄seo外包公司
  • 算命先生的网站怎么做网络营销需要学什么
  • 电商网站开发报价网络营销产品概念
  • 可以做众筹的网站有哪些东营优化公司
  • 技术进阶 javascript开发培训机构排名优化外包公司
  • 制作网站步骤南宁优化推广服务
  • 定制营销型网站公司贺贵江seo教程
  • 无锡做网站seo百度推广手机客户端
  • 淄博企业网站建设电商运营一天都干啥
  • 东莞做网站哪里好免费制作个人网站
  • 网站建设关键要做好哪些工作深圳竞价托管公司
  • 谷歌企业邮箱怎么注册seoul
  • 百姓网二手车买卖贵州网站seo
  • 怎么自己做视频网站简述网站建设流程
  • 现在最流行的网站推广方式有哪些谷歌seo外链
  • 做教育网站有什么好处广告营销策略